blob: 5ffab33190d56b9a238b02916b9008aebff37f70 [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010048#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x))
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010050class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Alexandre Rames2ed20af2015-03-06 13:55:35 +000054 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010055 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010057 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
58 instruction_,
59 instruction_->GetDexPc(),
60 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 }
62
Alexandre Rames8158f282015-08-07 10:26:17 +010063 bool IsFatal() const OVERRIDE { return true; }
64
Alexandre Rames9931f312015-06-19 14:47:01 +010065 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
66
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010068 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
70};
71
Calin Juravled0d48522014-11-04 16:40:20 +000072class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
73 public:
74 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
75
Alexandre Rames2ed20af2015-03-06 13:55:35 +000076 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010077 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000078 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010079 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
80 instruction_,
81 instruction_->GetDexPc(),
82 this);
Calin Juravled0d48522014-11-04 16:40:20 +000083 }
84
Alexandre Rames8158f282015-08-07 10:26:17 +010085 bool IsFatal() const OVERRIDE { return true; }
86
Alexandre Rames9931f312015-06-19 14:47:01 +010087 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
88
Calin Juravled0d48522014-11-04 16:40:20 +000089 private:
90 HDivZeroCheck* const instruction_;
91 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
92};
93
Calin Juravlebacfec32014-11-14 15:54:36 +000094class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000095 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000096 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000097
Alexandre Rames2ed20af2015-03-06 13:55:35 +000098 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000099 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 if (is_div_) {
101 __ negl(reg_);
102 } else {
103 __ movl(reg_, Immediate(0));
104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 bool is_div_;
113 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000114};
115
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100117 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100118 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
119 Location index_location,
120 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000121 : instruction_(instruction),
122 index_location_(index_location),
123 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100124
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000125 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000131 x86_codegen->EmitParallelMoves(
132 index_location_,
133 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100134 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000135 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100136 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
137 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100138 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
139 instruction_,
140 instruction_->GetDexPc(),
141 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 }
143
Alexandre Rames8158f282015-08-07 10:26:17 +0100144 bool IsFatal() const OVERRIDE { return true; }
145
Alexandre Rames9931f312015-06-19 14:47:01 +0100146 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 const Location index_location_;
151 const Location length_location_;
152
153 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
154};
155
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000158 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000161 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000164 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
166 instruction_,
167 instruction_->GetDexPc(),
168 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000169 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100182 HBasicBlock* GetSuccessor() const {
183 return successor_;
184 }
185
Alexandre Rames9931f312015-06-19 14:47:01 +0100186 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
187
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 private:
189 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191 Label return_label_;
192
193 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
194};
195
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000196class LoadStringSlowPathX86 : public SlowPathCodeX86 {
197 public:
198 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
199
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000200 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000201 LocationSummary* locations = instruction_->GetLocations();
202 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
203
204 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207
208 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800209 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100210 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
211 instruction_,
212 instruction_->GetDexPc(),
213 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000214 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216
217 __ jmp(GetExitLabel());
218 }
219
Alexandre Rames9931f312015-06-19 14:47:01 +0100220 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
221
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222 private:
223 HLoadString* const instruction_;
224
225 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
226};
227
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228class LoadClassSlowPathX86 : public SlowPathCodeX86 {
229 public:
230 LoadClassSlowPathX86(HLoadClass* cls,
231 HInstruction* at,
232 uint32_t dex_pc,
233 bool do_clinit)
234 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
235 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
236 }
237
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000238 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 LocationSummary* locations = at_->GetLocations();
240 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
241 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243
244 InvokeRuntimeCallingConvention calling_convention;
245 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
247 : QUICK_ENTRY_POINT(pInitializeType),
248 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249
250 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 Location out = locations->Out();
252 if (out.IsValid()) {
253 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
254 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000256
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 __ jmp(GetExitLabel());
259 }
260
Alexandre Rames9931f312015-06-19 14:47:01 +0100261 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
262
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 private:
264 // The class this slow path will load.
265 HLoadClass* const cls_;
266
267 // The instruction where this slow path is happening.
268 // (Might be the load class or an initialization check).
269 HInstruction* const at_;
270
271 // The dex PC of `at_`.
272 const uint32_t dex_pc_;
273
274 // Whether to initialize the class.
275 const bool do_clinit_;
276
277 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
278};
279
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
281 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 TypeCheckSlowPathX86(HInstruction* instruction,
283 Location class_to_check,
284 Location object_class,
285 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 class_to_check_(class_to_check),
288 object_class_(object_class),
289 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 DCHECK(instruction_->IsCheckCast()
294 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
296 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
297 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000298 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 x86_codegen->EmitParallelMoves(
304 class_to_check_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100312 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 } else {
317 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100318 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
319 instruction_,
320 instruction_->GetDexPc(),
321 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 }
323
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 if (instruction_->IsInstanceOf()) {
326 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
327 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000328 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 __ jmp(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 HInstruction* const instruction_;
337 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
341 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
342};
343
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700344class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
345 public:
346 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
347 : instruction_(instruction) {}
348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357 // No need to restore live registers.
358 DCHECK(instruction_->IsDeoptimize());
359 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
360 uint32_t dex_pc = deoptimize->GetDexPc();
361 codegen->RecordPcInfo(instruction_, dex_pc, this);
362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
365
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 private:
367 HInstruction* const instruction_;
368 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
369};
370
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100371#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100372#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100373
Roland Levillain4fa13f62015-07-06 18:11:54 +0100374inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700375 switch (cond) {
376 case kCondEQ: return kEqual;
377 case kCondNE: return kNotEqual;
378 case kCondLT: return kLess;
379 case kCondLE: return kLessEqual;
380 case kCondGT: return kGreater;
381 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700382 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100383 LOG(FATAL) << "Unreachable";
384 UNREACHABLE();
385}
386
387inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
388 switch (cond) {
389 case kCondEQ: return kEqual;
390 case kCondNE: return kNotEqual;
391 case kCondLT: return kBelow;
392 case kCondLE: return kBelowEqual;
393 case kCondGT: return kAbove;
394 case kCondGE: return kAboveEqual;
395 }
396 LOG(FATAL) << "Unreachable";
397 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700398}
399
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100400void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100401 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100402}
403
404void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100405 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100406}
407
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
409 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
410 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100411}
412
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100413size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
414 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
415 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100416}
417
Mark Mendell7c8d0092015-01-26 11:21:33 -0500418size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
419 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
420 return GetFloatingPointSpillSlotSize();
421}
422
423size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
424 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
425 return GetFloatingPointSpillSlotSize();
426}
427
Alexandre Rames8158f282015-08-07 10:26:17 +0100428void CodeGeneratorX86::InvokeRuntime(Address entry_point,
429 HInstruction* instruction,
430 uint32_t dex_pc,
431 SlowPathCode* slow_path) {
432 // Ensure that the call kind indication given to the register allocator is
433 // coherent with the runtime call generated.
434 if (slow_path == nullptr) {
435 DCHECK(instruction->GetLocations()->WillCall());
436 } else {
437 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal());
438 }
439
440 __ fs()->call(entry_point);
441 RecordPcInfo(instruction, dex_pc, slow_path);
442 DCHECK(instruction->IsSuspendCheck()
443 || instruction->IsBoundsCheck()
444 || instruction->IsNullCheck()
445 || instruction->IsDivZeroCheck()
446 || !IsLeafMethod());
447}
448
Mark Mendellfb8d2792015-03-31 22:16:59 -0400449CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
450 const X86InstructionSetFeatures& isa_features,
451 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500452 : CodeGenerator(graph,
453 kNumberOfCpuRegisters,
454 kNumberOfXmmRegisters,
455 kNumberOfRegisterPairs,
456 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
457 arraysize(kCoreCalleeSaves))
458 | (1 << kFakeReturnRegister),
459 0,
460 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100461 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100463 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400464 move_resolver_(graph->GetArena(), this),
465 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 // Use a fake return address register to mimic Quick.
467 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100468}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100469
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100470Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100471 switch (type) {
472 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 X86ManagedRegister pair =
475 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100476 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
477 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
479 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100480 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100482 }
483
484 case Primitive::kPrimByte:
485 case Primitive::kPrimBoolean:
486 case Primitive::kPrimChar:
487 case Primitive::kPrimShort:
488 case Primitive::kPrimInt:
489 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100490 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100491 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100492 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100493 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
494 X86ManagedRegister current =
495 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
496 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100497 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100498 }
499 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100500 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501 }
502
503 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100505 return Location::FpuRegisterLocation(
506 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100508
509 case Primitive::kPrimVoid:
510 LOG(FATAL) << "Unreachable type " << type;
511 }
512
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100513 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514}
515
Mark Mendell5f874182015-03-04 15:42:45 -0500516void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100518 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100519
520 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100521 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522
Mark Mendell5f874182015-03-04 15:42:45 -0500523 if (is_baseline) {
524 blocked_core_registers_[EBP] = true;
525 blocked_core_registers_[ESI] = true;
526 blocked_core_registers_[EDI] = true;
527 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100528
529 UpdateBlockedPairRegisters();
530}
531
532void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
533 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
534 X86ManagedRegister current =
535 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
536 if (blocked_core_registers_[current.AsRegisterPairLow()]
537 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
538 blocked_register_pairs_[i] = true;
539 }
540 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100541}
542
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100543InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
544 : HGraphVisitor(graph),
545 assembler_(codegen->GetAssembler()),
546 codegen_(codegen) {}
547
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100548static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100549 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100550}
551
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000552void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100553 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000554 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000555 bool skip_overflow_check =
556 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000557 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000558
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000559 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100560 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100561 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100562 }
563
Mark Mendell5f874182015-03-04 15:42:45 -0500564 if (HasEmptyFrame()) {
565 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000566 }
Mark Mendell5f874182015-03-04 15:42:45 -0500567
568 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
569 Register reg = kCoreCalleeSaves[i];
570 if (allocated_registers_.ContainsCoreRegister(reg)) {
571 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100572 __ cfi().AdjustCFAOffset(kX86WordSize);
573 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500574 }
575 }
576
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100577 int adjust = GetFrameSize() - FrameEntrySpillSize();
578 __ subl(ESP, Immediate(adjust));
579 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100580 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581}
582
583void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100584 __ cfi().RememberState();
585 if (!HasEmptyFrame()) {
586 int adjust = GetFrameSize() - FrameEntrySpillSize();
587 __ addl(ESP, Immediate(adjust));
588 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500589
David Srbeckyc34dc932015-04-12 09:27:43 +0100590 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
591 Register reg = kCoreCalleeSaves[i];
592 if (allocated_registers_.ContainsCoreRegister(reg)) {
593 __ popl(reg);
594 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
595 __ cfi().Restore(DWARFReg(reg));
596 }
Mark Mendell5f874182015-03-04 15:42:45 -0500597 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000598 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100599 __ ret();
600 __ cfi().RestoreState();
601 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000602}
603
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100604void CodeGeneratorX86::Bind(HBasicBlock* block) {
605 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000606}
607
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100608Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
609 switch (load->GetType()) {
610 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100612 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613
614 case Primitive::kPrimInt:
615 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100616 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100618
619 case Primitive::kPrimBoolean:
620 case Primitive::kPrimByte:
621 case Primitive::kPrimChar:
622 case Primitive::kPrimShort:
623 case Primitive::kPrimVoid:
624 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700625 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100626 }
627
628 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700629 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100630}
631
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100632Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
633 switch (type) {
634 case Primitive::kPrimBoolean:
635 case Primitive::kPrimByte:
636 case Primitive::kPrimChar:
637 case Primitive::kPrimShort:
638 case Primitive::kPrimInt:
639 case Primitive::kPrimNot:
640 return Location::RegisterLocation(EAX);
641
642 case Primitive::kPrimLong:
643 return Location::RegisterPairLocation(EAX, EDX);
644
645 case Primitive::kPrimVoid:
646 return Location::NoLocation();
647
648 case Primitive::kPrimDouble:
649 case Primitive::kPrimFloat:
650 return Location::FpuRegisterLocation(XMM0);
651 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100652
653 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100654}
655
656Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
657 return Location::RegisterLocation(kMethodRegisterArgument);
658}
659
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100660Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100661 switch (type) {
662 case Primitive::kPrimBoolean:
663 case Primitive::kPrimByte:
664 case Primitive::kPrimChar:
665 case Primitive::kPrimShort:
666 case Primitive::kPrimInt:
667 case Primitive::kPrimNot: {
668 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000669 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100670 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100672 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000673 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100674 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100675 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100676
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000677 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100678 uint32_t index = gp_index_;
679 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000680 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100681 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100682 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
683 calling_convention.GetRegisterPairAt(index));
684 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100685 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000686 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
687 }
688 }
689
690 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100691 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000692 stack_index_++;
693 if (index < calling_convention.GetNumberOfFpuRegisters()) {
694 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
695 } else {
696 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
697 }
698 }
699
700 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100701 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000702 stack_index_ += 2;
703 if (index < calling_convention.GetNumberOfFpuRegisters()) {
704 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
705 } else {
706 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100707 }
708 }
709
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100710 case Primitive::kPrimVoid:
711 LOG(FATAL) << "Unexpected parameter type " << type;
712 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100713 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100714 return Location();
715}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100716
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717void CodeGeneratorX86::Move32(Location destination, Location source) {
718 if (source.Equals(destination)) {
719 return;
720 }
721 if (destination.IsRegister()) {
722 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000723 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 } else {
727 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000728 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 } else if (destination.IsFpuRegister()) {
731 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000732 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000734 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 } else {
736 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000737 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000740 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000742 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000744 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500745 } else if (source.IsConstant()) {
746 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000747 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500748 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 } else {
750 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100751 __ pushl(Address(ESP, source.GetStackIndex()));
752 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 }
754 }
755}
756
757void CodeGeneratorX86::Move64(Location destination, Location source) {
758 if (source.Equals(destination)) {
759 return;
760 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100761 if (destination.IsRegisterPair()) {
762 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000763 EmitParallelMoves(
764 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
765 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100766 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000767 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100768 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
769 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 } else if (source.IsFpuRegister()) {
771 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000773 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100775 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
776 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
778 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100779 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500780 if (source.IsFpuRegister()) {
781 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
782 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000783 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 } else {
785 LOG(FATAL) << "Unimplemented";
786 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000788 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100789 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000790 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100791 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100792 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100793 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000796 } else if (source.IsConstant()) {
797 HConstant* constant = source.GetConstant();
798 int64_t value;
799 if (constant->IsLongConstant()) {
800 value = constant->AsLongConstant()->GetValue();
801 } else {
802 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000803 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000804 }
805 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
806 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100807 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000808 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000809 EmitParallelMoves(
810 Location::StackSlot(source.GetStackIndex()),
811 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100812 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000813 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100814 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
815 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 }
817 }
818}
819
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100820void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000821 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100822 if (instruction->IsCurrentMethod()) {
823 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
824 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000825 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100826 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000827 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000828 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
829 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000830 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000831 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000832 } else if (location.IsStackSlot()) {
833 __ movl(Address(ESP, location.GetStackIndex()), imm);
834 } else {
835 DCHECK(location.IsConstant());
836 DCHECK_EQ(location.GetConstant(), const_to_move);
837 }
838 } else if (const_to_move->IsLongConstant()) {
839 int64_t value = const_to_move->AsLongConstant()->GetValue();
840 if (location.IsRegisterPair()) {
841 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
842 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
843 } else if (location.IsDoubleStackSlot()) {
844 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000845 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
846 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000847 } else {
848 DCHECK(location.IsConstant());
849 DCHECK_EQ(location.GetConstant(), instruction);
850 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000852 } else if (instruction->IsTemporary()) {
853 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000854 if (temp_location.IsStackSlot()) {
855 Move32(location, temp_location);
856 } else {
857 DCHECK(temp_location.IsDoubleStackSlot());
858 Move64(location, temp_location);
859 }
Roland Levillain476df552014-10-09 17:51:36 +0100860 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100861 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 switch (instruction->GetType()) {
863 case Primitive::kPrimBoolean:
864 case Primitive::kPrimByte:
865 case Primitive::kPrimChar:
866 case Primitive::kPrimShort:
867 case Primitive::kPrimInt:
868 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100869 case Primitive::kPrimFloat:
870 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871 break;
872
873 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 case Primitive::kPrimDouble:
875 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 break;
877
878 default:
879 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
880 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000881 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100882 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 switch (instruction->GetType()) {
884 case Primitive::kPrimBoolean:
885 case Primitive::kPrimByte:
886 case Primitive::kPrimChar:
887 case Primitive::kPrimShort:
888 case Primitive::kPrimInt:
889 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100890 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000891 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 break;
893
894 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100895 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000896 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 break;
898
899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100900 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100901 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000902 }
903}
904
David Brazdilfc6a86a2015-06-26 10:33:45 +0000905void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100906 DCHECK(!successor->IsExitBlock());
907
908 HBasicBlock* block = got->GetBlock();
909 HInstruction* previous = got->GetPrevious();
910
911 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000912 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100913 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
914 return;
915 }
916
917 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
918 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
919 }
920 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000922 }
923}
924
David Brazdilfc6a86a2015-06-26 10:33:45 +0000925void LocationsBuilderX86::VisitGoto(HGoto* got) {
926 got->SetLocations(nullptr);
927}
928
929void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
930 HandleGoto(got, got->GetSuccessor());
931}
932
933void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
934 try_boundary->SetLocations(nullptr);
935}
936
937void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
938 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
939 if (!successor->IsExitBlock()) {
940 HandleGoto(try_boundary, successor);
941 }
942}
943
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000944void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000945 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000946}
947
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000948void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700949 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000950}
951
Mark Mendellc4701932015-04-10 13:18:51 -0400952void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
953 Label* true_label,
954 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 if (cond->IsFPConditionTrueIfNaN()) {
956 __ j(kUnordered, true_label);
957 } else if (cond->IsFPConditionFalseIfNaN()) {
958 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400959 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400961}
962
963void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
964 Label* true_label,
965 Label* false_label) {
966 LocationSummary* locations = cond->GetLocations();
967 Location left = locations->InAt(0);
968 Location right = locations->InAt(1);
969 IfCondition if_cond = cond->GetCondition();
970
Mark Mendellc4701932015-04-10 13:18:51 -0400971 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100972 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400973 IfCondition true_high_cond = if_cond;
974 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100975 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400976
977 // Set the conditions for the test, remembering that == needs to be
978 // decided using the low words.
979 switch (if_cond) {
980 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400981 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100982 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400983 break;
984 case kCondLT:
985 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400986 break;
987 case kCondLE:
988 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400989 break;
990 case kCondGT:
991 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400992 break;
993 case kCondGE:
994 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400995 break;
996 }
997
998 if (right.IsConstant()) {
999 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001000 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001001 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001002
1003 if (val_high == 0) {
1004 __ testl(left_high, left_high);
1005 } else {
1006 __ cmpl(left_high, Immediate(val_high));
1007 }
1008 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001009 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001010 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001011 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001012 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 __ j(X86SignedCondition(true_high_cond), true_label);
1014 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001015 }
1016 // Must be equal high, so compare the lows.
1017 if (val_low == 0) {
1018 __ testl(left_low, left_low);
1019 } else {
1020 __ cmpl(left_low, Immediate(val_low));
1021 }
1022 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001023 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001024 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001025
1026 __ cmpl(left_high, right_high);
1027 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001028 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001029 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001031 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001032 __ j(X86SignedCondition(true_high_cond), true_label);
1033 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001034 }
1035 // Must be equal high, so compare the lows.
1036 __ cmpl(left_low, right_low);
1037 }
1038 // The last comparison might be unsigned.
1039 __ j(final_condition, true_label);
1040}
1041
1042void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1043 HCondition* condition,
1044 Label* true_target,
1045 Label* false_target,
1046 Label* always_true_target) {
1047 LocationSummary* locations = condition->GetLocations();
1048 Location left = locations->InAt(0);
1049 Location right = locations->InAt(1);
1050
1051 // We don't want true_target as a nullptr.
1052 if (true_target == nullptr) {
1053 true_target = always_true_target;
1054 }
1055 bool falls_through = (false_target == nullptr);
1056
1057 // FP compares don't like null false_targets.
1058 if (false_target == nullptr) {
1059 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1060 }
1061
1062 Primitive::Type type = condition->InputAt(0)->GetType();
1063 switch (type) {
1064 case Primitive::kPrimLong:
1065 GenerateLongComparesAndJumps(condition, true_target, false_target);
1066 break;
1067 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001068 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1069 GenerateFPJumps(condition, true_target, false_target);
1070 break;
1071 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001072 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1073 GenerateFPJumps(condition, true_target, false_target);
1074 break;
1075 default:
1076 LOG(FATAL) << "Unexpected compare type " << type;
1077 }
1078
1079 if (!falls_through) {
1080 __ jmp(false_target);
1081 }
1082}
1083
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001084void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1085 Label* true_target,
1086 Label* false_target,
1087 Label* always_true_target) {
1088 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001089 if (cond->IsIntConstant()) {
1090 // Constant condition, statically compared against 1.
1091 int32_t cond_value = cond->AsIntConstant()->GetValue();
1092 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001093 if (always_true_target != nullptr) {
1094 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001095 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001097 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001098 DCHECK_EQ(cond_value, 0);
1099 }
1100 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001101 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001102 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1103 // Moves do not affect the eflags register, so if the condition is
1104 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001105 // again. We can't use the eflags on long/FP conditions if they are
1106 // materialized due to the complex branching.
1107 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001109 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1111 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001112 if (!eflags_set) {
1113 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001114 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001115 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001116 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 } else {
1118 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1119 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001120 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001121 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001122 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001123 }
1124 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001125 // Condition has not been materialized, use its inputs as the
1126 // comparison and its condition as the branch condition.
1127
Mark Mendellc4701932015-04-10 13:18:51 -04001128 // Is this a long or FP comparison that has been folded into the HCondition?
1129 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1130 // Generate the comparison directly.
1131 GenerateCompareTestAndBranch(instruction->AsIf(),
1132 cond->AsCondition(),
1133 true_target,
1134 false_target,
1135 always_true_target);
1136 return;
1137 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001138
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 Location lhs = cond->GetLocations()->InAt(0);
1140 Location rhs = cond->GetLocations()->InAt(1);
1141 // LHS is guaranteed to be in a register (see
1142 // LocationsBuilderX86::VisitCondition).
1143 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001144 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001145 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001146 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001147 if (constant == 0) {
1148 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1149 } else {
1150 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1151 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001152 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001153 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001154 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001155 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001156 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001157 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001158 if (false_target != nullptr) {
1159 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160 }
1161}
1162
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001163void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1164 LocationSummary* locations =
1165 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1166 HInstruction* cond = if_instr->InputAt(0);
1167 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1168 locations->SetInAt(0, Location::Any());
1169 }
1170}
1171
1172void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1173 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1174 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1175 Label* always_true_target = true_target;
1176 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1177 if_instr->IfTrueSuccessor())) {
1178 always_true_target = nullptr;
1179 }
1180 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1181 if_instr->IfFalseSuccessor())) {
1182 false_target = nullptr;
1183 }
1184 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1185}
1186
1187void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1188 LocationSummary* locations = new (GetGraph()->GetArena())
1189 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1190 HInstruction* cond = deoptimize->InputAt(0);
1191 DCHECK(cond->IsCondition());
1192 if (cond->AsCondition()->NeedsMaterialization()) {
1193 locations->SetInAt(0, Location::Any());
1194 }
1195}
1196
1197void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1198 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1199 DeoptimizationSlowPathX86(deoptimize);
1200 codegen_->AddSlowPath(slow_path);
1201 Label* slow_path_entry = slow_path->GetEntryLabel();
1202 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1203}
1204
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001206 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001207}
1208
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001209void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1210 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001211}
1212
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001213void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001214 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001215}
1216
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001217void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001218 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001219 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 LocationSummary* locations =
1224 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 switch (store->InputAt(1)->GetType()) {
1226 case Primitive::kPrimBoolean:
1227 case Primitive::kPrimByte:
1228 case Primitive::kPrimChar:
1229 case Primitive::kPrimShort:
1230 case Primitive::kPrimInt:
1231 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1234 break;
1235
1236 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1239 break;
1240
1241 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001244}
1245
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001246void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001247 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001248}
1249
Roland Levillain0d37cd02015-05-27 16:39:19 +01001250void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001251 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001252 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001253 // Handle the long/FP comparisons made in instruction simplification.
1254 switch (cond->InputAt(0)->GetType()) {
1255 case Primitive::kPrimLong: {
1256 locations->SetInAt(0, Location::RequiresRegister());
1257 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1258 if (cond->NeedsMaterialization()) {
1259 locations->SetOut(Location::RequiresRegister());
1260 }
1261 break;
1262 }
1263 case Primitive::kPrimFloat:
1264 case Primitive::kPrimDouble: {
1265 locations->SetInAt(0, Location::RequiresFpuRegister());
1266 locations->SetInAt(1, Location::RequiresFpuRegister());
1267 if (cond->NeedsMaterialization()) {
1268 locations->SetOut(Location::RequiresRegister());
1269 }
1270 break;
1271 }
1272 default:
1273 locations->SetInAt(0, Location::RequiresRegister());
1274 locations->SetInAt(1, Location::Any());
1275 if (cond->NeedsMaterialization()) {
1276 // We need a byte register.
1277 locations->SetOut(Location::RegisterLocation(ECX));
1278 }
1279 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001280 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281}
1282
Roland Levillain0d37cd02015-05-27 16:39:19 +01001283void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001284 if (!cond->NeedsMaterialization()) {
1285 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001286 }
Mark Mendellc4701932015-04-10 13:18:51 -04001287
1288 LocationSummary* locations = cond->GetLocations();
1289 Location lhs = locations->InAt(0);
1290 Location rhs = locations->InAt(1);
1291 Register reg = locations->Out().AsRegister<Register>();
1292 Label true_label, false_label;
1293
1294 switch (cond->InputAt(0)->GetType()) {
1295 default: {
1296 // Integer case.
1297
1298 // Clear output register: setcc only sets the low byte.
1299 __ xorl(reg, reg);
1300
1301 if (rhs.IsRegister()) {
1302 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1303 } else if (rhs.IsConstant()) {
1304 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1305 if (constant == 0) {
1306 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1307 } else {
1308 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1309 }
1310 } else {
1311 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1312 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001314 return;
1315 }
1316 case Primitive::kPrimLong:
1317 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1318 break;
1319 case Primitive::kPrimFloat:
1320 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1321 GenerateFPJumps(cond, &true_label, &false_label);
1322 break;
1323 case Primitive::kPrimDouble:
1324 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1325 GenerateFPJumps(cond, &true_label, &false_label);
1326 break;
1327 }
1328
1329 // Convert the jumps into the result.
1330 Label done_label;
1331
Roland Levillain4fa13f62015-07-06 18:11:54 +01001332 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001333 __ Bind(&false_label);
1334 __ xorl(reg, reg);
1335 __ jmp(&done_label);
1336
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001338 __ Bind(&true_label);
1339 __ movl(reg, Immediate(1));
1340 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001341}
1342
1343void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1344 VisitCondition(comp);
1345}
1346
1347void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1348 VisitCondition(comp);
1349}
1350
1351void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1352 VisitCondition(comp);
1353}
1354
1355void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1356 VisitCondition(comp);
1357}
1358
1359void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1360 VisitCondition(comp);
1361}
1362
1363void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1364 VisitCondition(comp);
1365}
1366
1367void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1368 VisitCondition(comp);
1369}
1370
1371void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1372 VisitCondition(comp);
1373}
1374
1375void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1376 VisitCondition(comp);
1377}
1378
1379void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1380 VisitCondition(comp);
1381}
1382
1383void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1384 VisitCondition(comp);
1385}
1386
1387void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1388 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389}
1390
1391void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001392 LocationSummary* locations =
1393 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001394 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395}
1396
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001397void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001398 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001399 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001400}
1401
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001402void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1403 LocationSummary* locations =
1404 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1405 locations->SetOut(Location::ConstantLocation(constant));
1406}
1407
1408void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1409 // Will be generated at use site.
1410 UNUSED(constant);
1411}
1412
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001413void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001414 LocationSummary* locations =
1415 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001416 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417}
1418
1419void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1420 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001421 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422}
1423
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001424void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1425 LocationSummary* locations =
1426 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1427 locations->SetOut(Location::ConstantLocation(constant));
1428}
1429
1430void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1431 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001432 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001433}
1434
1435void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1438 locations->SetOut(Location::ConstantLocation(constant));
1439}
1440
1441void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1442 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001443 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001444}
1445
Calin Juravle27df7582015-04-17 19:12:31 +01001446void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1447 memory_barrier->SetLocations(nullptr);
1448}
1449
1450void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1451 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1452}
1453
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001454void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001455 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456}
1457
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001458void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001459 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001460 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001461}
1462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001464 LocationSummary* locations =
1465 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001466 switch (ret->InputAt(0)->GetType()) {
1467 case Primitive::kPrimBoolean:
1468 case Primitive::kPrimByte:
1469 case Primitive::kPrimChar:
1470 case Primitive::kPrimShort:
1471 case Primitive::kPrimInt:
1472 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001473 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 break;
1475
1476 case Primitive::kPrimLong:
1477 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479 break;
1480
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 case Primitive::kPrimFloat:
1482 case Primitive::kPrimDouble:
1483 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001484 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 break;
1486
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001488 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001490}
1491
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001492void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001493 if (kIsDebugBuild) {
1494 switch (ret->InputAt(0)->GetType()) {
1495 case Primitive::kPrimBoolean:
1496 case Primitive::kPrimByte:
1497 case Primitive::kPrimChar:
1498 case Primitive::kPrimShort:
1499 case Primitive::kPrimInt:
1500 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001501 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502 break;
1503
1504 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001505 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1506 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507 break;
1508
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001509 case Primitive::kPrimFloat:
1510 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001511 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001512 break;
1513
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001514 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001515 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001516 }
1517 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001518 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001519}
1520
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001521void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001522 // When we do not run baseline, explicit clinit checks triggered by static
1523 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1524 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001525
Mark Mendellfb8d2792015-03-31 22:16:59 -04001526 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001527 if (intrinsic.TryDispatch(invoke)) {
1528 return;
1529 }
1530
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001531 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001532
1533 if (codegen_->IsBaseline()) {
1534 // Baseline does not have enough registers if the current method also
1535 // needs a register. We therefore do not require a register for it, and let
1536 // the code generation of the invoke handle it.
1537 LocationSummary* locations = invoke->GetLocations();
1538 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1539 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1540 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1541 }
1542 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001543}
1544
Mark Mendell09ed1a32015-03-25 08:30:06 -04001545static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1546 if (invoke->GetLocations()->Intrinsified()) {
1547 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1548 intrinsic.Dispatch(invoke);
1549 return true;
1550 }
1551 return false;
1552}
1553
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001554void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001555 // When we do not run baseline, explicit clinit checks triggered by static
1556 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1557 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001558
Mark Mendell09ed1a32015-03-25 08:30:06 -04001559 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1560 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001561 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001562
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001563 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001564 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001565 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001566 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001567}
1568
1569void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1570 HandleInvoke(invoke);
1571}
1572
1573void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001574 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001575 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001576}
1577
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001578void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001580 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1581 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001582 LocationSummary* locations = invoke->GetLocations();
1583 Location receiver = locations->InAt(0);
1584 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001585 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001586 DCHECK(receiver.IsRegister());
1587 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001588 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001589 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001590 // temp = temp->GetMethodAt(method_offset);
1591 __ movl(temp, Address(temp, method_offset));
1592 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001593 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001594 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001595
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001596 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001597 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001598}
1599
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001600void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1601 HandleInvoke(invoke);
1602 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001603 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001604}
1605
1606void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1607 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001608 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1610 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001611 LocationSummary* locations = invoke->GetLocations();
1612 Location receiver = locations->InAt(0);
1613 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1614
1615 // Set the hidden argument.
1616 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001617 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001618
1619 // temp = object->GetClass();
1620 if (receiver.IsStackSlot()) {
1621 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1622 __ movl(temp, Address(temp, class_offset));
1623 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001624 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001625 }
Roland Levillain4d027112015-07-01 15:41:14 +01001626 codegen_->MaybeRecordImplicitNullCheck(invoke);
1627 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001628 // temp = temp->GetImtEntryAt(method_offset);
1629 __ movl(temp, Address(temp, method_offset));
1630 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001631 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001632 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001633
1634 DCHECK(!codegen_->IsLeafMethod());
1635 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1636}
1637
Roland Levillain88cb1752014-10-20 16:36:47 +01001638void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1639 LocationSummary* locations =
1640 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1641 switch (neg->GetResultType()) {
1642 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001643 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001644 locations->SetInAt(0, Location::RequiresRegister());
1645 locations->SetOut(Location::SameAsFirstInput());
1646 break;
1647
Roland Levillain88cb1752014-10-20 16:36:47 +01001648 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001649 locations->SetInAt(0, Location::RequiresFpuRegister());
1650 locations->SetOut(Location::SameAsFirstInput());
1651 locations->AddTemp(Location::RequiresRegister());
1652 locations->AddTemp(Location::RequiresFpuRegister());
1653 break;
1654
Roland Levillain88cb1752014-10-20 16:36:47 +01001655 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001656 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001657 locations->SetOut(Location::SameAsFirstInput());
1658 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001659 break;
1660
1661 default:
1662 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1663 }
1664}
1665
1666void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1667 LocationSummary* locations = neg->GetLocations();
1668 Location out = locations->Out();
1669 Location in = locations->InAt(0);
1670 switch (neg->GetResultType()) {
1671 case Primitive::kPrimInt:
1672 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001673 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001674 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001675 break;
1676
1677 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001678 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001679 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001680 __ negl(out.AsRegisterPairLow<Register>());
1681 // Negation is similar to subtraction from zero. The least
1682 // significant byte triggers a borrow when it is different from
1683 // zero; to take it into account, add 1 to the most significant
1684 // byte if the carry flag (CF) is set to 1 after the first NEGL
1685 // operation.
1686 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1687 __ negl(out.AsRegisterPairHigh<Register>());
1688 break;
1689
Roland Levillain5368c212014-11-27 15:03:41 +00001690 case Primitive::kPrimFloat: {
1691 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001692 Register constant = locations->GetTemp(0).AsRegister<Register>();
1693 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001694 // Implement float negation with an exclusive or with value
1695 // 0x80000000 (mask for bit 31, representing the sign of a
1696 // single-precision floating-point number).
1697 __ movl(constant, Immediate(INT32_C(0x80000000)));
1698 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001699 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001700 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001701 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001702
Roland Levillain5368c212014-11-27 15:03:41 +00001703 case Primitive::kPrimDouble: {
1704 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001706 // Implement double negation with an exclusive or with value
1707 // 0x8000000000000000 (mask for bit 63, representing the sign of
1708 // a double-precision floating-point number).
1709 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001710 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001711 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001712 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001713
1714 default:
1715 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1716 }
1717}
1718
Roland Levillaindff1f282014-11-05 14:15:05 +00001719void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001720 Primitive::Type result_type = conversion->GetResultType();
1721 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001722 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001723
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001724 // The float-to-long and double-to-long type conversions rely on a
1725 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001726 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001727 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1728 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001729 ? LocationSummary::kCall
1730 : LocationSummary::kNoCall;
1731 LocationSummary* locations =
1732 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1733
David Brazdilb2bd1c52015-03-25 11:17:37 +00001734 // The Java language does not allow treating boolean as an integral type but
1735 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001736
Roland Levillaindff1f282014-11-05 14:15:05 +00001737 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001738 case Primitive::kPrimByte:
1739 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001740 case Primitive::kPrimBoolean:
1741 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001742 case Primitive::kPrimShort:
1743 case Primitive::kPrimInt:
1744 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001745 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001746 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1747 // Make the output overlap to please the register allocator. This greatly simplifies
1748 // the validation of the linear scan implementation
1749 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001750 break;
1751
1752 default:
1753 LOG(FATAL) << "Unexpected type conversion from " << input_type
1754 << " to " << result_type;
1755 }
1756 break;
1757
Roland Levillain01a8d712014-11-14 16:27:39 +00001758 case Primitive::kPrimShort:
1759 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001760 case Primitive::kPrimBoolean:
1761 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001762 case Primitive::kPrimByte:
1763 case Primitive::kPrimInt:
1764 case Primitive::kPrimChar:
1765 // Processing a Dex `int-to-short' instruction.
1766 locations->SetInAt(0, Location::Any());
1767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1768 break;
1769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillain946e1432014-11-11 17:35:19 +00001776 case Primitive::kPrimInt:
1777 switch (input_type) {
1778 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001779 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001780 locations->SetInAt(0, Location::Any());
1781 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1782 break;
1783
1784 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001785 // Processing a Dex `float-to-int' instruction.
1786 locations->SetInAt(0, Location::RequiresFpuRegister());
1787 locations->SetOut(Location::RequiresRegister());
1788 locations->AddTemp(Location::RequiresFpuRegister());
1789 break;
1790
Roland Levillain946e1432014-11-11 17:35:19 +00001791 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001792 // Processing a Dex `double-to-int' instruction.
1793 locations->SetInAt(0, Location::RequiresFpuRegister());
1794 locations->SetOut(Location::RequiresRegister());
1795 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001796 break;
1797
1798 default:
1799 LOG(FATAL) << "Unexpected type conversion from " << input_type
1800 << " to " << result_type;
1801 }
1802 break;
1803
Roland Levillaindff1f282014-11-05 14:15:05 +00001804 case Primitive::kPrimLong:
1805 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001806 case Primitive::kPrimBoolean:
1807 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimByte:
1809 case Primitive::kPrimShort:
1810 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001811 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001812 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 locations->SetInAt(0, Location::RegisterLocation(EAX));
1814 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1815 break;
1816
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001817 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001818 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001819 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001820 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001821 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1822 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1823
Vladimir Marko949c91f2015-01-27 10:48:44 +00001824 // The runtime helper puts the result in EAX, EDX.
1825 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001826 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001827 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001828
1829 default:
1830 LOG(FATAL) << "Unexpected type conversion from " << input_type
1831 << " to " << result_type;
1832 }
1833 break;
1834
Roland Levillain981e4542014-11-14 11:47:14 +00001835 case Primitive::kPrimChar:
1836 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001837 case Primitive::kPrimBoolean:
1838 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001839 case Primitive::kPrimByte:
1840 case Primitive::kPrimShort:
1841 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001842 // Processing a Dex `int-to-char' instruction.
1843 locations->SetInAt(0, Location::Any());
1844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1845 break;
1846
1847 default:
1848 LOG(FATAL) << "Unexpected type conversion from " << input_type
1849 << " to " << result_type;
1850 }
1851 break;
1852
Roland Levillaindff1f282014-11-05 14:15:05 +00001853 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001854 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001855 case Primitive::kPrimBoolean:
1856 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001857 case Primitive::kPrimByte:
1858 case Primitive::kPrimShort:
1859 case Primitive::kPrimInt:
1860 case Primitive::kPrimChar:
1861 // Processing a Dex `int-to-float' instruction.
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 locations->SetOut(Location::RequiresFpuRegister());
1864 break;
1865
1866 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001867 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001868 locations->SetInAt(0, Location::Any());
1869 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001870 break;
1871
Roland Levillaincff13742014-11-17 14:32:17 +00001872 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001873 // Processing a Dex `double-to-float' instruction.
1874 locations->SetInAt(0, Location::RequiresFpuRegister());
1875 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001876 break;
1877
1878 default:
1879 LOG(FATAL) << "Unexpected type conversion from " << input_type
1880 << " to " << result_type;
1881 };
1882 break;
1883
Roland Levillaindff1f282014-11-05 14:15:05 +00001884 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001885 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001886 case Primitive::kPrimBoolean:
1887 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001888 case Primitive::kPrimByte:
1889 case Primitive::kPrimShort:
1890 case Primitive::kPrimInt:
1891 case Primitive::kPrimChar:
1892 // Processing a Dex `int-to-double' instruction.
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetOut(Location::RequiresFpuRegister());
1895 break;
1896
1897 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001898 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001899 locations->SetInAt(0, Location::Any());
1900 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001901 break;
1902
Roland Levillaincff13742014-11-17 14:32:17 +00001903 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001904 // Processing a Dex `float-to-double' instruction.
1905 locations->SetInAt(0, Location::RequiresFpuRegister());
1906 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001907 break;
1908
1909 default:
1910 LOG(FATAL) << "Unexpected type conversion from " << input_type
1911 << " to " << result_type;
1912 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001913 break;
1914
1915 default:
1916 LOG(FATAL) << "Unexpected type conversion from " << input_type
1917 << " to " << result_type;
1918 }
1919}
1920
1921void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1922 LocationSummary* locations = conversion->GetLocations();
1923 Location out = locations->Out();
1924 Location in = locations->InAt(0);
1925 Primitive::Type result_type = conversion->GetResultType();
1926 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001927 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001928 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001929 case Primitive::kPrimByte:
1930 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001931 case Primitive::kPrimBoolean:
1932 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001933 case Primitive::kPrimShort:
1934 case Primitive::kPrimInt:
1935 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001936 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001937 if (in.IsRegister()) {
1938 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001939 } else {
1940 DCHECK(in.GetConstant()->IsIntConstant());
1941 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1942 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1943 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001944 break;
1945
1946 default:
1947 LOG(FATAL) << "Unexpected type conversion from " << input_type
1948 << " to " << result_type;
1949 }
1950 break;
1951
Roland Levillain01a8d712014-11-14 16:27:39 +00001952 case Primitive::kPrimShort:
1953 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001954 case Primitive::kPrimBoolean:
1955 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001956 case Primitive::kPrimByte:
1957 case Primitive::kPrimInt:
1958 case Primitive::kPrimChar:
1959 // Processing a Dex `int-to-short' instruction.
1960 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001962 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001964 } else {
1965 DCHECK(in.GetConstant()->IsIntConstant());
1966 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001968 }
1969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected type conversion from " << input_type
1973 << " to " << result_type;
1974 }
1975 break;
1976
Roland Levillain946e1432014-11-11 17:35:19 +00001977 case Primitive::kPrimInt:
1978 switch (input_type) {
1979 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001980 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001981 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001983 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001985 } else {
1986 DCHECK(in.IsConstant());
1987 DCHECK(in.GetConstant()->IsLongConstant());
1988 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001990 }
1991 break;
1992
Roland Levillain3f8f9362014-12-02 17:45:01 +00001993 case Primitive::kPrimFloat: {
1994 // Processing a Dex `float-to-int' instruction.
1995 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1996 Register output = out.AsRegister<Register>();
1997 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1998 Label done, nan;
1999
2000 __ movl(output, Immediate(kPrimIntMax));
2001 // temp = int-to-float(output)
2002 __ cvtsi2ss(temp, output);
2003 // if input >= temp goto done
2004 __ comiss(input, temp);
2005 __ j(kAboveEqual, &done);
2006 // if input == NaN goto nan
2007 __ j(kUnordered, &nan);
2008 // output = float-to-int-truncate(input)
2009 __ cvttss2si(output, input);
2010 __ jmp(&done);
2011 __ Bind(&nan);
2012 // output = 0
2013 __ xorl(output, output);
2014 __ Bind(&done);
2015 break;
2016 }
2017
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002018 case Primitive::kPrimDouble: {
2019 // Processing a Dex `double-to-int' instruction.
2020 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2021 Register output = out.AsRegister<Register>();
2022 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2023 Label done, nan;
2024
2025 __ movl(output, Immediate(kPrimIntMax));
2026 // temp = int-to-double(output)
2027 __ cvtsi2sd(temp, output);
2028 // if input >= temp goto done
2029 __ comisd(input, temp);
2030 __ j(kAboveEqual, &done);
2031 // if input == NaN goto nan
2032 __ j(kUnordered, &nan);
2033 // output = double-to-int-truncate(input)
2034 __ cvttsd2si(output, input);
2035 __ jmp(&done);
2036 __ Bind(&nan);
2037 // output = 0
2038 __ xorl(output, output);
2039 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002040 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002041 }
Roland Levillain946e1432014-11-11 17:35:19 +00002042
2043 default:
2044 LOG(FATAL) << "Unexpected type conversion from " << input_type
2045 << " to " << result_type;
2046 }
2047 break;
2048
Roland Levillaindff1f282014-11-05 14:15:05 +00002049 case Primitive::kPrimLong:
2050 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002051 case Primitive::kPrimBoolean:
2052 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 case Primitive::kPrimByte:
2054 case Primitive::kPrimShort:
2055 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002056 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002057 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2059 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002061 __ cdq();
2062 break;
2063
2064 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002065 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002066 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2067 conversion,
2068 conversion->GetDexPc(),
2069 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002070 break;
2071
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002073 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002074 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2075 conversion,
2076 conversion->GetDexPc(),
2077 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected type conversion from " << input_type
2082 << " to " << result_type;
2083 }
2084 break;
2085
Roland Levillain981e4542014-11-14 11:47:14 +00002086 case Primitive::kPrimChar:
2087 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002088 case Primitive::kPrimBoolean:
2089 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002090 case Primitive::kPrimByte:
2091 case Primitive::kPrimShort:
2092 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002093 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2094 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002096 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002097 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002098 } else {
2099 DCHECK(in.GetConstant()->IsIntConstant());
2100 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002102 }
2103 break;
2104
2105 default:
2106 LOG(FATAL) << "Unexpected type conversion from " << input_type
2107 << " to " << result_type;
2108 }
2109 break;
2110
Roland Levillaindff1f282014-11-05 14:15:05 +00002111 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002112 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002113 case Primitive::kPrimBoolean:
2114 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002115 case Primitive::kPrimByte:
2116 case Primitive::kPrimShort:
2117 case Primitive::kPrimInt:
2118 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002119 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002121 break;
2122
Roland Levillain6d0e4832014-11-27 18:31:21 +00002123 case Primitive::kPrimLong: {
2124 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002125 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002126
Roland Levillain232ade02015-04-20 15:14:36 +01002127 // Create stack space for the call to
2128 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2129 // TODO: enhance register allocator to ask for stack temporaries.
2130 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2131 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2132 __ subl(ESP, Immediate(adjustment));
2133 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002134
Roland Levillain232ade02015-04-20 15:14:36 +01002135 // Load the value to the FP stack, using temporaries if needed.
2136 PushOntoFPStack(in, 0, adjustment, false, true);
2137
2138 if (out.IsStackSlot()) {
2139 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2140 } else {
2141 __ fstps(Address(ESP, 0));
2142 Location stack_temp = Location::StackSlot(0);
2143 codegen_->Move32(out, stack_temp);
2144 }
2145
2146 // Remove the temporary stack space we allocated.
2147 if (adjustment != 0) {
2148 __ addl(ESP, Immediate(adjustment));
2149 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002150 break;
2151 }
2152
Roland Levillaincff13742014-11-17 14:32:17 +00002153 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002154 // Processing a Dex `double-to-float' instruction.
2155 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002156 break;
2157
2158 default:
2159 LOG(FATAL) << "Unexpected type conversion from " << input_type
2160 << " to " << result_type;
2161 };
2162 break;
2163
Roland Levillaindff1f282014-11-05 14:15:05 +00002164 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002165 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002166 case Primitive::kPrimBoolean:
2167 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002168 case Primitive::kPrimByte:
2169 case Primitive::kPrimShort:
2170 case Primitive::kPrimInt:
2171 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002172 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002173 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002174 break;
2175
Roland Levillain647b9ed2014-11-27 12:06:00 +00002176 case Primitive::kPrimLong: {
2177 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002178 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002179
Roland Levillain232ade02015-04-20 15:14:36 +01002180 // Create stack space for the call to
2181 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2182 // TODO: enhance register allocator to ask for stack temporaries.
2183 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2184 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2185 __ subl(ESP, Immediate(adjustment));
2186 }
2187
2188 // Load the value to the FP stack, using temporaries if needed.
2189 PushOntoFPStack(in, 0, adjustment, false, true);
2190
2191 if (out.IsDoubleStackSlot()) {
2192 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2193 } else {
2194 __ fstpl(Address(ESP, 0));
2195 Location stack_temp = Location::DoubleStackSlot(0);
2196 codegen_->Move64(out, stack_temp);
2197 }
2198
2199 // Remove the temporary stack space we allocated.
2200 if (adjustment != 0) {
2201 __ addl(ESP, Immediate(adjustment));
2202 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002203 break;
2204 }
2205
Roland Levillaincff13742014-11-17 14:32:17 +00002206 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002207 // Processing a Dex `float-to-double' instruction.
2208 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002209 break;
2210
2211 default:
2212 LOG(FATAL) << "Unexpected type conversion from " << input_type
2213 << " to " << result_type;
2214 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002215 break;
2216
2217 default:
2218 LOG(FATAL) << "Unexpected type conversion from " << input_type
2219 << " to " << result_type;
2220 }
2221}
2222
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002223void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002226 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002227 case Primitive::kPrimInt: {
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2231 break;
2232 }
2233
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002235 locations->SetInAt(0, Location::RequiresRegister());
2236 locations->SetInAt(1, Location::Any());
2237 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002238 break;
2239 }
2240
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 case Primitive::kPrimFloat:
2242 case Primitive::kPrimDouble: {
2243 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002244 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002246 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002247 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002248
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002249 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002250 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2251 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002252 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002253}
2254
2255void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2256 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002257 Location first = locations->InAt(0);
2258 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002259 Location out = locations->Out();
2260
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002261 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002262 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002263 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002264 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2265 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002266 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2267 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002268 } else {
2269 __ leal(out.AsRegister<Register>(), Address(
2270 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2271 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002272 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002273 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2274 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2275 __ addl(out.AsRegister<Register>(), Immediate(value));
2276 } else {
2277 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2278 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002279 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002280 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002281 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002282 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002283 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002284 }
2285
2286 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002287 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002288 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2289 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002290 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002291 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2292 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002293 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002294 } else {
2295 DCHECK(second.IsConstant()) << second;
2296 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2297 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2298 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002299 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002300 break;
2301 }
2302
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002303 case Primitive::kPrimFloat: {
2304 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002305 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002307 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002308 }
2309
2310 case Primitive::kPrimDouble: {
2311 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 }
2314 break;
2315 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002316
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002317 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002318 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002319 }
2320}
2321
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002322void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002323 LocationSummary* locations =
2324 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002325 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002326 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002327 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002328 locations->SetInAt(0, Location::RequiresRegister());
2329 locations->SetInAt(1, Location::Any());
2330 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 break;
2332 }
Calin Juravle11351682014-10-23 15:38:15 +01002333 case Primitive::kPrimFloat:
2334 case Primitive::kPrimDouble: {
2335 locations->SetInAt(0, Location::RequiresFpuRegister());
2336 locations->SetInAt(1, Location::RequiresFpuRegister());
2337 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002338 break;
Calin Juravle11351682014-10-23 15:38:15 +01002339 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002340
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002341 default:
Calin Juravle11351682014-10-23 15:38:15 +01002342 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002343 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002344}
2345
2346void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2347 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 Location first = locations->InAt(0);
2349 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002350 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002351 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002353 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002355 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002356 __ subl(first.AsRegister<Register>(),
2357 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002358 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002359 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002360 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002361 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002362 }
2363
2364 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002365 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002366 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2367 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002368 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002369 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002370 __ sbbl(first.AsRegisterPairHigh<Register>(),
2371 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002372 } else {
2373 DCHECK(second.IsConstant()) << second;
2374 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2375 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2376 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002377 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378 break;
2379 }
2380
Calin Juravle11351682014-10-23 15:38:15 +01002381 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002383 break;
Calin Juravle11351682014-10-23 15:38:15 +01002384 }
2385
2386 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002387 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002388 break;
2389 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002390
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002391 default:
Calin Juravle11351682014-10-23 15:38:15 +01002392 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002393 }
2394}
2395
Calin Juravle34bacdf2014-10-07 20:23:36 +01002396void LocationsBuilderX86::VisitMul(HMul* mul) {
2397 LocationSummary* locations =
2398 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2399 switch (mul->GetResultType()) {
2400 case Primitive::kPrimInt:
2401 locations->SetInAt(0, Location::RequiresRegister());
2402 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002403 if (mul->InputAt(1)->IsIntConstant()) {
2404 // Can use 3 operand multiply.
2405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2406 } else {
2407 locations->SetOut(Location::SameAsFirstInput());
2408 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002409 break;
2410 case Primitive::kPrimLong: {
2411 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002412 locations->SetInAt(1, Location::Any());
2413 locations->SetOut(Location::SameAsFirstInput());
2414 // Needed for imul on 32bits with 64bits output.
2415 locations->AddTemp(Location::RegisterLocation(EAX));
2416 locations->AddTemp(Location::RegisterLocation(EDX));
2417 break;
2418 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002419 case Primitive::kPrimFloat:
2420 case Primitive::kPrimDouble: {
2421 locations->SetInAt(0, Location::RequiresFpuRegister());
2422 locations->SetInAt(1, Location::RequiresFpuRegister());
2423 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002424 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002425 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002426
2427 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002428 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002429 }
2430}
2431
2432void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2433 LocationSummary* locations = mul->GetLocations();
2434 Location first = locations->InAt(0);
2435 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002436 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002437
2438 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002439 case Primitive::kPrimInt:
2440 // The constant may have ended up in a register, so test explicitly to avoid
2441 // problems where the output may not be the same as the first operand.
2442 if (mul->InputAt(1)->IsIntConstant()) {
2443 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2444 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2445 } else if (second.IsRegister()) {
2446 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002448 } else {
2449 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002450 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002451 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002452 }
2453 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454
2455 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002456 Register in1_hi = first.AsRegisterPairHigh<Register>();
2457 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 Register eax = locations->GetTemp(0).AsRegister<Register>();
2459 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002460
2461 DCHECK_EQ(EAX, eax);
2462 DCHECK_EQ(EDX, edx);
2463
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002464 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002465 // output: in1
2466 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2467 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2468 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002469 if (second.IsConstant()) {
2470 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002472 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2473 int32_t low_value = Low32Bits(value);
2474 int32_t high_value = High32Bits(value);
2475 Immediate low(low_value);
2476 Immediate high(high_value);
2477
2478 __ movl(eax, high);
2479 // eax <- in1.lo * in2.hi
2480 __ imull(eax, in1_lo);
2481 // in1.hi <- in1.hi * in2.lo
2482 __ imull(in1_hi, low);
2483 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2484 __ addl(in1_hi, eax);
2485 // move in2_lo to eax to prepare for double precision
2486 __ movl(eax, low);
2487 // edx:eax <- in1.lo * in2.lo
2488 __ mull(in1_lo);
2489 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2490 __ addl(in1_hi, edx);
2491 // in1.lo <- (in1.lo * in2.lo)[31:0];
2492 __ movl(in1_lo, eax);
2493 } else if (second.IsRegisterPair()) {
2494 Register in2_hi = second.AsRegisterPairHigh<Register>();
2495 Register in2_lo = second.AsRegisterPairLow<Register>();
2496
2497 __ movl(eax, in2_hi);
2498 // eax <- in1.lo * in2.hi
2499 __ imull(eax, in1_lo);
2500 // in1.hi <- in1.hi * in2.lo
2501 __ imull(in1_hi, in2_lo);
2502 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2503 __ addl(in1_hi, eax);
2504 // move in1_lo to eax to prepare for double precision
2505 __ movl(eax, in1_lo);
2506 // edx:eax <- in1.lo * in2.lo
2507 __ mull(in2_lo);
2508 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2509 __ addl(in1_hi, edx);
2510 // in1.lo <- (in1.lo * in2.lo)[31:0];
2511 __ movl(in1_lo, eax);
2512 } else {
2513 DCHECK(second.IsDoubleStackSlot()) << second;
2514 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2515 Address in2_lo(ESP, second.GetStackIndex());
2516
2517 __ movl(eax, in2_hi);
2518 // eax <- in1.lo * in2.hi
2519 __ imull(eax, in1_lo);
2520 // in1.hi <- in1.hi * in2.lo
2521 __ imull(in1_hi, in2_lo);
2522 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2523 __ addl(in1_hi, eax);
2524 // move in1_lo to eax to prepare for double precision
2525 __ movl(eax, in1_lo);
2526 // edx:eax <- in1.lo * in2.lo
2527 __ mull(in2_lo);
2528 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2529 __ addl(in1_hi, edx);
2530 // in1.lo <- (in1.lo * in2.lo)[31:0];
2531 __ movl(in1_lo, eax);
2532 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002533
2534 break;
2535 }
2536
Calin Juravleb5bfa962014-10-21 18:02:24 +01002537 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002538 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002539 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002540 }
2541
2542 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002544 break;
2545 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002546
2547 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002548 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002549 }
2550}
2551
Roland Levillain232ade02015-04-20 15:14:36 +01002552void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2553 uint32_t temp_offset,
2554 uint32_t stack_adjustment,
2555 bool is_fp,
2556 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002557 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002558 DCHECK(!is_wide);
2559 if (is_fp) {
2560 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2561 } else {
2562 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002564 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002565 DCHECK(is_wide);
2566 if (is_fp) {
2567 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2568 } else {
2569 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2570 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002571 } else {
2572 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002574 Location stack_temp = Location::StackSlot(temp_offset);
2575 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002576 if (is_fp) {
2577 __ flds(Address(ESP, temp_offset));
2578 } else {
2579 __ filds(Address(ESP, temp_offset));
2580 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002581 } else {
2582 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2583 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002584 if (is_fp) {
2585 __ fldl(Address(ESP, temp_offset));
2586 } else {
2587 __ fildl(Address(ESP, temp_offset));
2588 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002589 }
2590 }
2591}
2592
2593void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2594 Primitive::Type type = rem->GetResultType();
2595 bool is_float = type == Primitive::kPrimFloat;
2596 size_t elem_size = Primitive::ComponentSize(type);
2597 LocationSummary* locations = rem->GetLocations();
2598 Location first = locations->InAt(0);
2599 Location second = locations->InAt(1);
2600 Location out = locations->Out();
2601
2602 // Create stack space for 2 elements.
2603 // TODO: enhance register allocator to ask for stack temporaries.
2604 __ subl(ESP, Immediate(2 * elem_size));
2605
2606 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002607 const bool is_wide = !is_float;
2608 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2609 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002610
2611 // Loop doing FPREM until we stabilize.
2612 Label retry;
2613 __ Bind(&retry);
2614 __ fprem();
2615
2616 // Move FP status to AX.
2617 __ fstsw();
2618
2619 // And see if the argument reduction is complete. This is signaled by the
2620 // C2 FPU flag bit set to 0.
2621 __ andl(EAX, Immediate(kC2ConditionMask));
2622 __ j(kNotEqual, &retry);
2623
2624 // We have settled on the final value. Retrieve it into an XMM register.
2625 // Store FP top of stack to real stack.
2626 if (is_float) {
2627 __ fsts(Address(ESP, 0));
2628 } else {
2629 __ fstl(Address(ESP, 0));
2630 }
2631
2632 // Pop the 2 items from the FP stack.
2633 __ fucompp();
2634
2635 // Load the value from the stack into an XMM register.
2636 DCHECK(out.IsFpuRegister()) << out;
2637 if (is_float) {
2638 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2639 } else {
2640 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2641 }
2642
2643 // And remove the temporary stack space we allocated.
2644 __ addl(ESP, Immediate(2 * elem_size));
2645}
2646
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002647
2648void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2649 DCHECK(instruction->IsDiv() || instruction->IsRem());
2650
2651 LocationSummary* locations = instruction->GetLocations();
2652 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002653 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002654
2655 Register out_register = locations->Out().AsRegister<Register>();
2656 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002657 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002658
2659 DCHECK(imm == 1 || imm == -1);
2660
2661 if (instruction->IsRem()) {
2662 __ xorl(out_register, out_register);
2663 } else {
2664 __ movl(out_register, input_register);
2665 if (imm == -1) {
2666 __ negl(out_register);
2667 }
2668 }
2669}
2670
2671
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002672void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002673 LocationSummary* locations = instruction->GetLocations();
2674
2675 Register out_register = locations->Out().AsRegister<Register>();
2676 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002677 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002678
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002679 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002680 Register num = locations->GetTemp(0).AsRegister<Register>();
2681
2682 __ leal(num, Address(input_register, std::abs(imm) - 1));
2683 __ testl(input_register, input_register);
2684 __ cmovl(kGreaterEqual, num, input_register);
2685 int shift = CTZ(imm);
2686 __ sarl(num, Immediate(shift));
2687
2688 if (imm < 0) {
2689 __ negl(num);
2690 }
2691
2692 __ movl(out_register, num);
2693}
2694
2695void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2696 DCHECK(instruction->IsDiv() || instruction->IsRem());
2697
2698 LocationSummary* locations = instruction->GetLocations();
2699 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2700
2701 Register eax = locations->InAt(0).AsRegister<Register>();
2702 Register out = locations->Out().AsRegister<Register>();
2703 Register num;
2704 Register edx;
2705
2706 if (instruction->IsDiv()) {
2707 edx = locations->GetTemp(0).AsRegister<Register>();
2708 num = locations->GetTemp(1).AsRegister<Register>();
2709 } else {
2710 edx = locations->Out().AsRegister<Register>();
2711 num = locations->GetTemp(0).AsRegister<Register>();
2712 }
2713
2714 DCHECK_EQ(EAX, eax);
2715 DCHECK_EQ(EDX, edx);
2716 if (instruction->IsDiv()) {
2717 DCHECK_EQ(EAX, out);
2718 } else {
2719 DCHECK_EQ(EDX, out);
2720 }
2721
2722 int64_t magic;
2723 int shift;
2724 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2725
2726 Label ndiv;
2727 Label end;
2728 // If numerator is 0, the result is 0, no computation needed.
2729 __ testl(eax, eax);
2730 __ j(kNotEqual, &ndiv);
2731
2732 __ xorl(out, out);
2733 __ jmp(&end);
2734
2735 __ Bind(&ndiv);
2736
2737 // Save the numerator.
2738 __ movl(num, eax);
2739
2740 // EAX = magic
2741 __ movl(eax, Immediate(magic));
2742
2743 // EDX:EAX = magic * numerator
2744 __ imull(num);
2745
2746 if (imm > 0 && magic < 0) {
2747 // EDX += num
2748 __ addl(edx, num);
2749 } else if (imm < 0 && magic > 0) {
2750 __ subl(edx, num);
2751 }
2752
2753 // Shift if needed.
2754 if (shift != 0) {
2755 __ sarl(edx, Immediate(shift));
2756 }
2757
2758 // EDX += 1 if EDX < 0
2759 __ movl(eax, edx);
2760 __ shrl(edx, Immediate(31));
2761 __ addl(edx, eax);
2762
2763 if (instruction->IsRem()) {
2764 __ movl(eax, num);
2765 __ imull(edx, Immediate(imm));
2766 __ subl(eax, edx);
2767 __ movl(edx, eax);
2768 } else {
2769 __ movl(eax, edx);
2770 }
2771 __ Bind(&end);
2772}
2773
Calin Juravlebacfec32014-11-14 15:54:36 +00002774void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2775 DCHECK(instruction->IsDiv() || instruction->IsRem());
2776
2777 LocationSummary* locations = instruction->GetLocations();
2778 Location out = locations->Out();
2779 Location first = locations->InAt(0);
2780 Location second = locations->InAt(1);
2781 bool is_div = instruction->IsDiv();
2782
2783 switch (instruction->GetResultType()) {
2784 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 DCHECK_EQ(EAX, first.AsRegister<Register>());
2786 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002787
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002788 if (instruction->InputAt(1)->IsIntConstant()) {
2789 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002790
2791 if (imm == 0) {
2792 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2793 } else if (imm == 1 || imm == -1) {
2794 DivRemOneOrMinusOne(instruction);
2795 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002796 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002797 } else {
2798 DCHECK(imm <= -2 || imm >= 2);
2799 GenerateDivRemWithAnyConstant(instruction);
2800 }
2801 } else {
2802 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002803 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002804 is_div);
2805 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002806
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002807 Register second_reg = second.AsRegister<Register>();
2808 // 0x80000000/-1 triggers an arithmetic exception!
2809 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2810 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002811
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002812 __ cmpl(second_reg, Immediate(-1));
2813 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002814
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002815 // edx:eax <- sign-extended of eax
2816 __ cdq();
2817 // eax = quotient, edx = remainder
2818 __ idivl(second_reg);
2819 __ Bind(slow_path->GetExitLabel());
2820 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002821 break;
2822 }
2823
2824 case Primitive::kPrimLong: {
2825 InvokeRuntimeCallingConvention calling_convention;
2826 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2827 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2828 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2829 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2830 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2831 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2832
2833 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002834 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2835 instruction,
2836 instruction->GetDexPc(),
2837 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002838 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002839 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2840 instruction,
2841 instruction->GetDexPc(),
2842 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002843 }
2844 uint32_t dex_pc = is_div
2845 ? instruction->AsDiv()->GetDexPc()
2846 : instruction->AsRem()->GetDexPc();
2847 codegen_->RecordPcInfo(instruction, dex_pc);
2848
2849 break;
2850 }
2851
2852 default:
2853 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2854 }
2855}
2856
Calin Juravle7c4954d2014-10-28 16:57:40 +00002857void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002858 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002859 ? LocationSummary::kCall
2860 : LocationSummary::kNoCall;
2861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2862
Calin Juravle7c4954d2014-10-28 16:57:40 +00002863 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002864 case Primitive::kPrimInt: {
2865 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002866 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002867 locations->SetOut(Location::SameAsFirstInput());
2868 // Intel uses edx:eax as the dividend.
2869 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002870 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2871 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2872 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002873 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002874 locations->AddTemp(Location::RequiresRegister());
2875 }
Calin Juravled0d48522014-11-04 16:40:20 +00002876 break;
2877 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002878 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002879 InvokeRuntimeCallingConvention calling_convention;
2880 locations->SetInAt(0, Location::RegisterPairLocation(
2881 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2882 locations->SetInAt(1, Location::RegisterPairLocation(
2883 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2884 // Runtime helper puts the result in EAX, EDX.
2885 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002886 break;
2887 }
2888 case Primitive::kPrimFloat:
2889 case Primitive::kPrimDouble: {
2890 locations->SetInAt(0, Location::RequiresFpuRegister());
2891 locations->SetInAt(1, Location::RequiresFpuRegister());
2892 locations->SetOut(Location::SameAsFirstInput());
2893 break;
2894 }
2895
2896 default:
2897 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2898 }
2899}
2900
2901void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2902 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002903 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002904 Location first = locations->InAt(0);
2905 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002906
2907 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002908 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002909 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002910 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002911 break;
2912 }
2913
2914 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002915 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002916 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002917 break;
2918 }
2919
2920 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002921 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002922 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002923 break;
2924 }
2925
2926 default:
2927 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2928 }
2929}
2930
Calin Juravlebacfec32014-11-14 15:54:36 +00002931void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002932 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002933
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002934 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2935 ? LocationSummary::kCall
2936 : LocationSummary::kNoCall;
2937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002938
Calin Juravled2ec87d2014-12-08 14:24:46 +00002939 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002940 case Primitive::kPrimInt: {
2941 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002942 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002943 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002944 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2945 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2946 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002947 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002948 locations->AddTemp(Location::RequiresRegister());
2949 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002950 break;
2951 }
2952 case Primitive::kPrimLong: {
2953 InvokeRuntimeCallingConvention calling_convention;
2954 locations->SetInAt(0, Location::RegisterPairLocation(
2955 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2956 locations->SetInAt(1, Location::RegisterPairLocation(
2957 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2958 // Runtime helper puts the result in EAX, EDX.
2959 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2960 break;
2961 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002962 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002963 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002964 locations->SetInAt(0, Location::Any());
2965 locations->SetInAt(1, Location::Any());
2966 locations->SetOut(Location::RequiresFpuRegister());
2967 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002968 break;
2969 }
2970
2971 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002972 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002973 }
2974}
2975
2976void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2977 Primitive::Type type = rem->GetResultType();
2978 switch (type) {
2979 case Primitive::kPrimInt:
2980 case Primitive::kPrimLong: {
2981 GenerateDivRemIntegral(rem);
2982 break;
2983 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002984 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002985 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002986 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002987 break;
2988 }
2989 default:
2990 LOG(FATAL) << "Unexpected rem type " << type;
2991 }
2992}
2993
Calin Juravled0d48522014-11-04 16:40:20 +00002994void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2995 LocationSummary* locations =
2996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002997 switch (instruction->GetType()) {
2998 case Primitive::kPrimInt: {
2999 locations->SetInAt(0, Location::Any());
3000 break;
3001 }
3002 case Primitive::kPrimLong: {
3003 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3004 if (!instruction->IsConstant()) {
3005 locations->AddTemp(Location::RequiresRegister());
3006 }
3007 break;
3008 }
3009 default:
3010 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3011 }
Calin Juravled0d48522014-11-04 16:40:20 +00003012 if (instruction->HasUses()) {
3013 locations->SetOut(Location::SameAsFirstInput());
3014 }
3015}
3016
3017void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3018 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3019 codegen_->AddSlowPath(slow_path);
3020
3021 LocationSummary* locations = instruction->GetLocations();
3022 Location value = locations->InAt(0);
3023
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003024 switch (instruction->GetType()) {
3025 case Primitive::kPrimInt: {
3026 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003027 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003028 __ j(kEqual, slow_path->GetEntryLabel());
3029 } else if (value.IsStackSlot()) {
3030 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3031 __ j(kEqual, slow_path->GetEntryLabel());
3032 } else {
3033 DCHECK(value.IsConstant()) << value;
3034 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3035 __ jmp(slow_path->GetEntryLabel());
3036 }
3037 }
3038 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003039 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003040 case Primitive::kPrimLong: {
3041 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003043 __ movl(temp, value.AsRegisterPairLow<Register>());
3044 __ orl(temp, value.AsRegisterPairHigh<Register>());
3045 __ j(kEqual, slow_path->GetEntryLabel());
3046 } else {
3047 DCHECK(value.IsConstant()) << value;
3048 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3049 __ jmp(slow_path->GetEntryLabel());
3050 }
3051 }
3052 break;
3053 }
3054 default:
3055 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003056 }
Calin Juravled0d48522014-11-04 16:40:20 +00003057}
3058
Calin Juravle9aec02f2014-11-18 23:06:35 +00003059void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3060 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3061
3062 LocationSummary* locations =
3063 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3064
3065 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003066 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003067 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003068 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003069 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003070 // The shift count needs to be in CL or a constant.
3071 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003072 locations->SetOut(Location::SameAsFirstInput());
3073 break;
3074 }
3075 default:
3076 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3077 }
3078}
3079
3080void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3081 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3082
3083 LocationSummary* locations = op->GetLocations();
3084 Location first = locations->InAt(0);
3085 Location second = locations->InAt(1);
3086 DCHECK(first.Equals(locations->Out()));
3087
3088 switch (op->GetResultType()) {
3089 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003090 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003091 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003092 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003094 DCHECK_EQ(ECX, second_reg);
3095 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003096 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003097 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003098 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003099 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003100 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003101 }
3102 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003103 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3104 if (shift == 0) {
3105 return;
3106 }
3107 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003108 if (op->IsShl()) {
3109 __ shll(first_reg, imm);
3110 } else if (op->IsShr()) {
3111 __ sarl(first_reg, imm);
3112 } else {
3113 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003114 }
3115 }
3116 break;
3117 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003118 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003119 if (second.IsRegister()) {
3120 Register second_reg = second.AsRegister<Register>();
3121 DCHECK_EQ(ECX, second_reg);
3122 if (op->IsShl()) {
3123 GenerateShlLong(first, second_reg);
3124 } else if (op->IsShr()) {
3125 GenerateShrLong(first, second_reg);
3126 } else {
3127 GenerateUShrLong(first, second_reg);
3128 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003129 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003130 // Shift by a constant.
3131 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3132 // Nothing to do if the shift is 0, as the input is already the output.
3133 if (shift != 0) {
3134 if (op->IsShl()) {
3135 GenerateShlLong(first, shift);
3136 } else if (op->IsShr()) {
3137 GenerateShrLong(first, shift);
3138 } else {
3139 GenerateUShrLong(first, shift);
3140 }
3141 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003142 }
3143 break;
3144 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003145 default:
3146 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3147 }
3148}
3149
Mark P Mendell73945692015-04-29 14:56:17 +00003150void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3151 Register low = loc.AsRegisterPairLow<Register>();
3152 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003153 if (shift == 1) {
3154 // This is just an addition.
3155 __ addl(low, low);
3156 __ adcl(high, high);
3157 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003158 // Shift by 32 is easy. High gets low, and low gets 0.
3159 codegen_->EmitParallelMoves(
3160 loc.ToLow(),
3161 loc.ToHigh(),
3162 Primitive::kPrimInt,
3163 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3164 loc.ToLow(),
3165 Primitive::kPrimInt);
3166 } else if (shift > 32) {
3167 // Low part becomes 0. High part is low part << (shift-32).
3168 __ movl(high, low);
3169 __ shll(high, Immediate(shift - 32));
3170 __ xorl(low, low);
3171 } else {
3172 // Between 1 and 31.
3173 __ shld(high, low, Immediate(shift));
3174 __ shll(low, Immediate(shift));
3175 }
3176}
3177
Calin Juravle9aec02f2014-11-18 23:06:35 +00003178void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3179 Label done;
3180 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3181 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3182 __ testl(shifter, Immediate(32));
3183 __ j(kEqual, &done);
3184 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3185 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3186 __ Bind(&done);
3187}
3188
Mark P Mendell73945692015-04-29 14:56:17 +00003189void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3190 Register low = loc.AsRegisterPairLow<Register>();
3191 Register high = loc.AsRegisterPairHigh<Register>();
3192 if (shift == 32) {
3193 // Need to copy the sign.
3194 DCHECK_NE(low, high);
3195 __ movl(low, high);
3196 __ sarl(high, Immediate(31));
3197 } else if (shift > 32) {
3198 DCHECK_NE(low, high);
3199 // High part becomes sign. Low part is shifted by shift - 32.
3200 __ movl(low, high);
3201 __ sarl(high, Immediate(31));
3202 __ sarl(low, Immediate(shift - 32));
3203 } else {
3204 // Between 1 and 31.
3205 __ shrd(low, high, Immediate(shift));
3206 __ sarl(high, Immediate(shift));
3207 }
3208}
3209
Calin Juravle9aec02f2014-11-18 23:06:35 +00003210void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3211 Label done;
3212 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3213 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3214 __ testl(shifter, Immediate(32));
3215 __ j(kEqual, &done);
3216 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3217 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3218 __ Bind(&done);
3219}
3220
Mark P Mendell73945692015-04-29 14:56:17 +00003221void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3222 Register low = loc.AsRegisterPairLow<Register>();
3223 Register high = loc.AsRegisterPairHigh<Register>();
3224 if (shift == 32) {
3225 // Shift by 32 is easy. Low gets high, and high gets 0.
3226 codegen_->EmitParallelMoves(
3227 loc.ToHigh(),
3228 loc.ToLow(),
3229 Primitive::kPrimInt,
3230 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3231 loc.ToHigh(),
3232 Primitive::kPrimInt);
3233 } else if (shift > 32) {
3234 // Low part is high >> (shift - 32). High part becomes 0.
3235 __ movl(low, high);
3236 __ shrl(low, Immediate(shift - 32));
3237 __ xorl(high, high);
3238 } else {
3239 // Between 1 and 31.
3240 __ shrd(low, high, Immediate(shift));
3241 __ shrl(high, Immediate(shift));
3242 }
3243}
3244
Calin Juravle9aec02f2014-11-18 23:06:35 +00003245void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3246 Label done;
3247 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3248 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3249 __ testl(shifter, Immediate(32));
3250 __ j(kEqual, &done);
3251 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3252 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3253 __ Bind(&done);
3254}
3255
3256void LocationsBuilderX86::VisitShl(HShl* shl) {
3257 HandleShift(shl);
3258}
3259
3260void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3261 HandleShift(shl);
3262}
3263
3264void LocationsBuilderX86::VisitShr(HShr* shr) {
3265 HandleShift(shr);
3266}
3267
3268void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3269 HandleShift(shr);
3270}
3271
3272void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3273 HandleShift(ushr);
3274}
3275
3276void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3277 HandleShift(ushr);
3278}
3279
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003280void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003281 LocationSummary* locations =
3282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003283 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003285 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003286 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003287}
3288
3289void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3290 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003291 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003292 // Note: if heap poisoning is enabled, the entry point takes cares
3293 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003294 codegen_->InvokeRuntime(
3295 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3296 instruction,
3297 instruction->GetDexPc(),
3298 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003299 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003300}
3301
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003302void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3305 locations->SetOut(Location::RegisterLocation(EAX));
3306 InvokeRuntimeCallingConvention calling_convention;
3307 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003308 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003309 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003310}
3311
3312void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3313 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003314 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3315
Roland Levillain4d027112015-07-01 15:41:14 +01003316 // Note: if heap poisoning is enabled, the entry point takes cares
3317 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003318 codegen_->InvokeRuntime(
3319 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3320 instruction,
3321 instruction->GetDexPc(),
3322 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003323 DCHECK(!codegen_->IsLeafMethod());
3324}
3325
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003326void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003327 LocationSummary* locations =
3328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003329 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3330 if (location.IsStackSlot()) {
3331 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3332 } else if (location.IsDoubleStackSlot()) {
3333 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003334 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003335 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003336}
3337
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003338void InstructionCodeGeneratorX86::VisitParameterValue(
3339 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3340}
3341
3342void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3343 LocationSummary* locations =
3344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3345 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3346}
3347
3348void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003349}
3350
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003351void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003352 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003353 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003354 locations->SetInAt(0, Location::RequiresRegister());
3355 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003356}
3357
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003358void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3359 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003360 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003361 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003362 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003363 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003364 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003365 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003366 break;
3367
3368 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003369 __ notl(out.AsRegisterPairLow<Register>());
3370 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003371 break;
3372
3373 default:
3374 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3375 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003376}
3377
David Brazdil66d126e2015-04-03 16:02:44 +01003378void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3379 LocationSummary* locations =
3380 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3381 locations->SetInAt(0, Location::RequiresRegister());
3382 locations->SetOut(Location::SameAsFirstInput());
3383}
3384
3385void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003386 LocationSummary* locations = bool_not->GetLocations();
3387 Location in = locations->InAt(0);
3388 Location out = locations->Out();
3389 DCHECK(in.Equals(out));
3390 __ xorl(out.AsRegister<Register>(), Immediate(1));
3391}
3392
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003393void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003396 switch (compare->InputAt(0)->GetType()) {
3397 case Primitive::kPrimLong: {
3398 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003399 locations->SetInAt(1, Location::Any());
3400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3401 break;
3402 }
3403 case Primitive::kPrimFloat:
3404 case Primitive::kPrimDouble: {
3405 locations->SetInAt(0, Location::RequiresFpuRegister());
3406 locations->SetInAt(1, Location::RequiresFpuRegister());
3407 locations->SetOut(Location::RequiresRegister());
3408 break;
3409 }
3410 default:
3411 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3412 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003413}
3414
3415void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003416 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003418 Location left = locations->InAt(0);
3419 Location right = locations->InAt(1);
3420
3421 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003422 switch (compare->InputAt(0)->GetType()) {
3423 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003424 Register left_low = left.AsRegisterPairLow<Register>();
3425 Register left_high = left.AsRegisterPairHigh<Register>();
3426 int32_t val_low = 0;
3427 int32_t val_high = 0;
3428 bool right_is_const = false;
3429
3430 if (right.IsConstant()) {
3431 DCHECK(right.GetConstant()->IsLongConstant());
3432 right_is_const = true;
3433 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3434 val_low = Low32Bits(val);
3435 val_high = High32Bits(val);
3436 }
3437
Calin Juravleddb7df22014-11-25 20:56:51 +00003438 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003439 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003440 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003441 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003442 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003443 DCHECK(right_is_const) << right;
3444 if (val_high == 0) {
3445 __ testl(left_high, left_high);
3446 } else {
3447 __ cmpl(left_high, Immediate(val_high));
3448 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003449 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003450 __ j(kLess, &less); // Signed compare.
3451 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003452 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003453 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003454 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003455 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003456 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003457 DCHECK(right_is_const) << right;
3458 if (val_low == 0) {
3459 __ testl(left_low, left_low);
3460 } else {
3461 __ cmpl(left_low, Immediate(val_low));
3462 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003463 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003464 break;
3465 }
3466 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003468 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3469 break;
3470 }
3471 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003473 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003474 break;
3475 }
3476 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003477 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003478 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003479 __ movl(out, Immediate(0));
3480 __ j(kEqual, &done);
3481 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3482
3483 __ Bind(&greater);
3484 __ movl(out, Immediate(1));
3485 __ jmp(&done);
3486
3487 __ Bind(&less);
3488 __ movl(out, Immediate(-1));
3489
3490 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003491}
3492
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003493void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003494 LocationSummary* locations =
3495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003496 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3497 locations->SetInAt(i, Location::Any());
3498 }
3499 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003500}
3501
3502void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003503 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003504 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003505}
3506
Calin Juravle52c48962014-12-16 17:02:57 +00003507void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3508 /*
3509 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3510 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3511 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3512 */
3513 switch (kind) {
3514 case MemBarrierKind::kAnyAny: {
3515 __ mfence();
3516 break;
3517 }
3518 case MemBarrierKind::kAnyStore:
3519 case MemBarrierKind::kLoadAny:
3520 case MemBarrierKind::kStoreStore: {
3521 // nop
3522 break;
3523 }
3524 default:
3525 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003526 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527}
3528
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003529
Mark Mendell09ed1a32015-03-25 08:30:06 -04003530void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003531 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003532 // TODO: Implement all kinds of calls:
3533 // 1) boot -> boot
3534 // 2) app -> boot
3535 // 3) app -> app
3536 //
3537 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003538
3539 if (invoke->IsStringInit()) {
3540 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003541 Register reg = temp.AsRegister<Register>();
3542 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003543 // (temp + offset_of_quick_compiled_code)()
3544 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003545 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3546 } else if (invoke->IsRecursive()) {
3547 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003548 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003549 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3550
3551 Register method_reg;
3552 Register reg = temp.AsRegister<Register>();
3553 if (current_method.IsRegister()) {
3554 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003555 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003556 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003557 DCHECK(!current_method.IsValid());
3558 method_reg = reg;
3559 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003560 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003561 // temp = temp->dex_cache_resolved_methods_;
3562 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3563 // temp = temp[index_in_cache]
3564 __ movl(reg, Address(reg,
3565 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3566 // (temp + offset_of_quick_compiled_code)()
3567 __ call(Address(reg,
3568 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003569 }
3570
3571 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003572}
3573
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003574void CodeGeneratorX86::MarkGCCard(Register temp,
3575 Register card,
3576 Register object,
3577 Register value,
3578 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003580 if (value_can_be_null) {
3581 __ testl(value, value);
3582 __ j(kEqual, &is_null);
3583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3585 __ movl(temp, object);
3586 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003587 __ movb(Address(temp, card, TIMES_1, 0),
3588 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003589 if (value_can_be_null) {
3590 __ Bind(&is_null);
3591 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592}
3593
Calin Juravle52c48962014-12-16 17:02:57 +00003594void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3595 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003596 LocationSummary* locations =
3597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003598 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003599
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003600 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3601 locations->SetOut(Location::RequiresFpuRegister());
3602 } else {
3603 // The output overlaps in case of long: we don't want the low move to overwrite
3604 // the object's location.
3605 locations->SetOut(Location::RequiresRegister(),
3606 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3607 : Location::kNoOutputOverlap);
3608 }
Calin Juravle52c48962014-12-16 17:02:57 +00003609
3610 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3611 // Long values can be loaded atomically into an XMM using movsd.
3612 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3613 // and then copy the XMM into the output 32bits at a time).
3614 locations->AddTemp(Location::RequiresFpuRegister());
3615 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003616}
3617
Calin Juravle52c48962014-12-16 17:02:57 +00003618void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3619 const FieldInfo& field_info) {
3620 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621
Calin Juravle52c48962014-12-16 17:02:57 +00003622 LocationSummary* locations = instruction->GetLocations();
3623 Register base = locations->InAt(0).AsRegister<Register>();
3624 Location out = locations->Out();
3625 bool is_volatile = field_info.IsVolatile();
3626 Primitive::Type field_type = field_info.GetFieldType();
3627 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3628
3629 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003630 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003631 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003632 break;
3633 }
3634
3635 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003636 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003637 break;
3638 }
3639
3640 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003641 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003642 break;
3643 }
3644
3645 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003646 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647 break;
3648 }
3649
3650 case Primitive::kPrimInt:
3651 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003652 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003653 break;
3654 }
3655
3656 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003657 if (is_volatile) {
3658 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3659 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003660 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003661 __ movd(out.AsRegisterPairLow<Register>(), temp);
3662 __ psrlq(temp, Immediate(32));
3663 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3664 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003665 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003666 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003667 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003668 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3669 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003670 break;
3671 }
3672
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003673 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003674 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003675 break;
3676 }
3677
3678 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003679 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003680 break;
3681 }
3682
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003683 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003684 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003685 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003686 }
Calin Juravle52c48962014-12-16 17:02:57 +00003687
Calin Juravle77520bc2015-01-12 18:45:46 +00003688 // Longs are handled in the switch.
3689 if (field_type != Primitive::kPrimLong) {
3690 codegen_->MaybeRecordImplicitNullCheck(instruction);
3691 }
3692
Calin Juravle52c48962014-12-16 17:02:57 +00003693 if (is_volatile) {
3694 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3695 }
Roland Levillain4d027112015-07-01 15:41:14 +01003696
3697 if (field_type == Primitive::kPrimNot) {
3698 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3699 }
Calin Juravle52c48962014-12-16 17:02:57 +00003700}
3701
3702void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3703 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3704
3705 LocationSummary* locations =
3706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3707 locations->SetInAt(0, Location::RequiresRegister());
3708 bool is_volatile = field_info.IsVolatile();
3709 Primitive::Type field_type = field_info.GetFieldType();
3710 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3711 || (field_type == Primitive::kPrimByte);
3712
3713 // The register allocator does not support multiple
3714 // inputs that die at entry with one in a specific register.
3715 if (is_byte_type) {
3716 // Ensure the value is in a byte register.
3717 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003718 } else if (Primitive::IsFloatingPointType(field_type)) {
3719 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003720 } else {
3721 locations->SetInAt(1, Location::RequiresRegister());
3722 }
Calin Juravle52c48962014-12-16 17:02:57 +00003723 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003724 // Temporary registers for the write barrier.
3725 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003726 // Ensure the card is in a byte register.
3727 locations->AddTemp(Location::RegisterLocation(ECX));
3728 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3729 // 64bits value can be atomically written to an address with movsd and an XMM register.
3730 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3731 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3732 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3733 // isolated cases when we need this it isn't worth adding the extra complexity.
3734 locations->AddTemp(Location::RequiresFpuRegister());
3735 locations->AddTemp(Location::RequiresFpuRegister());
3736 }
3737}
3738
3739void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003740 const FieldInfo& field_info,
3741 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003742 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3743
3744 LocationSummary* locations = instruction->GetLocations();
3745 Register base = locations->InAt(0).AsRegister<Register>();
3746 Location value = locations->InAt(1);
3747 bool is_volatile = field_info.IsVolatile();
3748 Primitive::Type field_type = field_info.GetFieldType();
3749 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003750 bool needs_write_barrier =
3751 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003752
3753 if (is_volatile) {
3754 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3755 }
3756
3757 switch (field_type) {
3758 case Primitive::kPrimBoolean:
3759 case Primitive::kPrimByte: {
3760 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3761 break;
3762 }
3763
3764 case Primitive::kPrimShort:
3765 case Primitive::kPrimChar: {
3766 __ movw(Address(base, offset), value.AsRegister<Register>());
3767 break;
3768 }
3769
3770 case Primitive::kPrimInt:
3771 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003772 if (kPoisonHeapReferences && needs_write_barrier) {
3773 // Note that in the case where `value` is a null reference,
3774 // we do not enter this block, as the reference does not
3775 // need poisoning.
3776 DCHECK_EQ(field_type, Primitive::kPrimNot);
3777 Register temp = locations->GetTemp(0).AsRegister<Register>();
3778 __ movl(temp, value.AsRegister<Register>());
3779 __ PoisonHeapReference(temp);
3780 __ movl(Address(base, offset), temp);
3781 } else {
3782 __ movl(Address(base, offset), value.AsRegister<Register>());
3783 }
Calin Juravle52c48962014-12-16 17:02:57 +00003784 break;
3785 }
3786
3787 case Primitive::kPrimLong: {
3788 if (is_volatile) {
3789 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3790 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3791 __ movd(temp1, value.AsRegisterPairLow<Register>());
3792 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3793 __ punpckldq(temp1, temp2);
3794 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003796 } else {
3797 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003799 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3800 }
3801 break;
3802 }
3803
3804 case Primitive::kPrimFloat: {
3805 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3806 break;
3807 }
3808
3809 case Primitive::kPrimDouble: {
3810 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3811 break;
3812 }
3813
3814 case Primitive::kPrimVoid:
3815 LOG(FATAL) << "Unreachable type " << field_type;
3816 UNREACHABLE();
3817 }
3818
Calin Juravle77520bc2015-01-12 18:45:46 +00003819 // Longs are handled in the switch.
3820 if (field_type != Primitive::kPrimLong) {
3821 codegen_->MaybeRecordImplicitNullCheck(instruction);
3822 }
3823
Roland Levillain4d027112015-07-01 15:41:14 +01003824 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003825 Register temp = locations->GetTemp(0).AsRegister<Register>();
3826 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003827 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003828 }
3829
Calin Juravle52c48962014-12-16 17:02:57 +00003830 if (is_volatile) {
3831 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3832 }
3833}
3834
3835void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3836 HandleFieldGet(instruction, instruction->GetFieldInfo());
3837}
3838
3839void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3840 HandleFieldGet(instruction, instruction->GetFieldInfo());
3841}
3842
3843void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3844 HandleFieldSet(instruction, instruction->GetFieldInfo());
3845}
3846
3847void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003848 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003849}
3850
3851void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3852 HandleFieldSet(instruction, instruction->GetFieldInfo());
3853}
3854
3855void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003856 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003857}
3858
3859void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3860 HandleFieldGet(instruction, instruction->GetFieldInfo());
3861}
3862
3863void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3864 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003865}
3866
3867void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003868 LocationSummary* locations =
3869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003870 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3871 ? Location::RequiresRegister()
3872 : Location::Any();
3873 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003874 if (instruction->HasUses()) {
3875 locations->SetOut(Location::SameAsFirstInput());
3876 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003877}
3878
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003879void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003880 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3881 return;
3882 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003883 LocationSummary* locations = instruction->GetLocations();
3884 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003885
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003886 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3887 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3888}
3889
3890void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003891 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003892 codegen_->AddSlowPath(slow_path);
3893
3894 LocationSummary* locations = instruction->GetLocations();
3895 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003896
3897 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003898 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003899 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003900 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003901 } else {
3902 DCHECK(obj.IsConstant()) << obj;
3903 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3904 __ jmp(slow_path->GetEntryLabel());
3905 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906 }
3907 __ j(kEqual, slow_path->GetEntryLabel());
3908}
3909
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003910void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3911 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3912 GenerateImplicitNullCheck(instruction);
3913 } else {
3914 GenerateExplicitNullCheck(instruction);
3915 }
3916}
3917
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003919 LocationSummary* locations =
3920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003921 locations->SetInAt(0, Location::RequiresRegister());
3922 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003923 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3924 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3925 } else {
3926 // The output overlaps in case of long: we don't want the low move to overwrite
3927 // the array's location.
3928 locations->SetOut(Location::RequiresRegister(),
3929 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3930 : Location::kNoOutputOverlap);
3931 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932}
3933
3934void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3935 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003937 Location index = locations->InAt(1);
3938
Calin Juravle77520bc2015-01-12 18:45:46 +00003939 Primitive::Type type = instruction->GetType();
3940 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003941 case Primitive::kPrimBoolean: {
3942 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003944 if (index.IsConstant()) {
3945 __ movzxb(out, Address(obj,
3946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3947 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003948 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 }
3950 break;
3951 }
3952
3953 case Primitive::kPrimByte: {
3954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003955 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956 if (index.IsConstant()) {
3957 __ movsxb(out, Address(obj,
3958 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3959 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003960 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 }
3962 break;
3963 }
3964
3965 case Primitive::kPrimShort: {
3966 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003967 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003968 if (index.IsConstant()) {
3969 __ movsxw(out, Address(obj,
3970 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3971 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003972 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 }
3974 break;
3975 }
3976
3977 case Primitive::kPrimChar: {
3978 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003979 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980 if (index.IsConstant()) {
3981 __ movzxw(out, Address(obj,
3982 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3983 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003984 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985 }
3986 break;
3987 }
3988
3989 case Primitive::kPrimInt:
3990 case Primitive::kPrimNot: {
3991 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003993 if (index.IsConstant()) {
3994 __ movl(out, Address(obj,
3995 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3996 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 }
3999 break;
4000 }
4001
4002 case Primitive::kPrimLong: {
4003 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004004 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004005 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004006 if (index.IsConstant()) {
4007 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004008 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004010 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004012 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004015 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004017 }
4018 break;
4019 }
4020
Mark Mendell7c8d0092015-01-26 11:21:33 -05004021 case Primitive::kPrimFloat: {
4022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4023 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4024 if (index.IsConstant()) {
4025 __ movss(out, Address(obj,
4026 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4027 } else {
4028 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4029 }
4030 break;
4031 }
4032
4033 case Primitive::kPrimDouble: {
4034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4035 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4036 if (index.IsConstant()) {
4037 __ movsd(out, Address(obj,
4038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4039 } else {
4040 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4041 }
4042 break;
4043 }
4044
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004045 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004046 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004047 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004048 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004049
4050 if (type != Primitive::kPrimLong) {
4051 codegen_->MaybeRecordImplicitNullCheck(instruction);
4052 }
Roland Levillain4d027112015-07-01 15:41:14 +01004053
4054 if (type == Primitive::kPrimNot) {
4055 Register out = locations->Out().AsRegister<Register>();
4056 __ MaybeUnpoisonHeapReference(out);
4057 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004058}
4059
4060void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004061 // This location builder might end up asking to up to four registers, which is
4062 // not currently possible for baseline. The situation in which we need four
4063 // registers cannot be met by baseline though, because it has not run any
4064 // optimization.
4065
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004067 bool needs_write_barrier =
4068 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4069
Mark Mendell5f874182015-03-04 15:42:45 -05004070 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004071
Nicolas Geoffray39468442014-09-02 15:17:15 +01004072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4073 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004074 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004075
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004076 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004078 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4079 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4080 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004081 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004082 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4083 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004084 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004085 // In case of a byte operation, the register allocator does not support multiple
4086 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004089 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004090 // Ensure the value is in a byte register.
4091 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004092 } else if (Primitive::IsFloatingPointType(value_type)) {
4093 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004095 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004097 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004098 // Temporary registers for the write barrier.
4099 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004100 // Ensure the card is in a byte register.
4101 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004102 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104}
4105
4106void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4107 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004108 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004109 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004110 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004111 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004112 bool needs_runtime_call = locations->WillCall();
4113 bool needs_write_barrier =
4114 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115
4116 switch (value_type) {
4117 case Primitive::kPrimBoolean:
4118 case Primitive::kPrimByte: {
4119 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 if (index.IsConstant()) {
4121 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004122 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004123 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004124 } else {
4125 __ movb(Address(obj, offset),
4126 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4127 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004128 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004129 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004130 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004131 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004132 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004134 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004138 break;
4139 }
4140
4141 case Primitive::kPrimShort:
4142 case Primitive::kPrimChar: {
4143 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144 if (index.IsConstant()) {
4145 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004146 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004148 } else {
4149 __ movw(Address(obj, offset),
4150 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4151 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004152 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004153 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4155 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004156 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004158 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004160 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 break;
4163 }
4164
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004165 case Primitive::kPrimInt:
4166 case Primitive::kPrimNot: {
4167 if (!needs_runtime_call) {
4168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4169 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004170 size_t offset =
4171 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004172 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004173 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4174 Register temp = locations->GetTemp(0).AsRegister<Register>();
4175 __ movl(temp, value.AsRegister<Register>());
4176 __ PoisonHeapReference(temp);
4177 __ movl(Address(obj, offset), temp);
4178 } else {
4179 __ movl(Address(obj, offset), value.AsRegister<Register>());
4180 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004181 } else {
4182 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004183 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4184 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4185 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4186 // Note: if heap poisoning is enabled, no need to poison
4187 // (negate) `v` if it is a reference, as it would be null.
4188 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004189 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004190 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004191 DCHECK(index.IsRegister()) << index;
4192 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004193 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4194 Register temp = locations->GetTemp(0).AsRegister<Register>();
4195 __ movl(temp, value.AsRegister<Register>());
4196 __ PoisonHeapReference(temp);
4197 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4198 } else {
4199 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4200 value.AsRegister<Register>());
4201 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004202 } else {
4203 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004204 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4205 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4206 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4207 // Note: if heap poisoning is enabled, no need to poison
4208 // (negate) `v` if it is a reference, as it would be null.
4209 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004210 }
4211 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004212 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213
4214 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 Register temp = locations->GetTemp(0).AsRegister<Register>();
4216 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004217 codegen_->MarkGCCard(
4218 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004219 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004220 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 DCHECK_EQ(value_type, Primitive::kPrimNot);
4222 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004223 // Note: if heap poisoning is enabled, pAputObject takes cares
4224 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004225 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4226 instruction,
4227 instruction->GetDexPc(),
4228 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229 }
4230 break;
4231 }
4232
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 case Primitive::kPrimLong: {
4234 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235 if (index.IsConstant()) {
4236 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004237 if (value.IsRegisterPair()) {
4238 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004239 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004240 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004241 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004242 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004243 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4244 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004245 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004246 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004248 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004249 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004250 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004251 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004252 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004253 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004254 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004255 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004256 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004257 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004258 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004259 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004260 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004261 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004262 Immediate(High32Bits(val)));
4263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 }
4265 break;
4266 }
4267
Mark Mendell7c8d0092015-01-26 11:21:33 -05004268 case Primitive::kPrimFloat: {
4269 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4270 DCHECK(value.IsFpuRegister());
4271 if (index.IsConstant()) {
4272 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4273 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4274 } else {
4275 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4276 value.AsFpuRegister<XmmRegister>());
4277 }
4278 break;
4279 }
4280
4281 case Primitive::kPrimDouble: {
4282 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4283 DCHECK(value.IsFpuRegister());
4284 if (index.IsConstant()) {
4285 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4286 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4287 } else {
4288 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4289 value.AsFpuRegister<XmmRegister>());
4290 }
4291 break;
4292 }
4293
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294 case Primitive::kPrimVoid:
4295 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004296 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 }
4298}
4299
4300void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4301 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004302 locations->SetInAt(0, Location::RequiresRegister());
4303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304}
4305
4306void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4307 LocationSummary* locations = instruction->GetLocations();
4308 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004309 Register obj = locations->InAt(0).AsRegister<Register>();
4310 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004313}
4314
4315void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004316 LocationSummary* locations =
4317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004318 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004319 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004320 if (instruction->HasUses()) {
4321 locations->SetOut(Location::SameAsFirstInput());
4322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323}
4324
4325void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4326 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004327 Location index_loc = locations->InAt(0);
4328 Location length_loc = locations->InAt(1);
4329 SlowPathCodeX86* slow_path =
4330 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331
Mark Mendell99dbd682015-04-22 16:18:52 -04004332 if (length_loc.IsConstant()) {
4333 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4334 if (index_loc.IsConstant()) {
4335 // BCE will remove the bounds check if we are guarenteed to pass.
4336 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4337 if (index < 0 || index >= length) {
4338 codegen_->AddSlowPath(slow_path);
4339 __ jmp(slow_path->GetEntryLabel());
4340 } else {
4341 // Some optimization after BCE may have generated this, and we should not
4342 // generate a bounds check if it is a valid range.
4343 }
4344 return;
4345 }
4346
4347 // We have to reverse the jump condition because the length is the constant.
4348 Register index_reg = index_loc.AsRegister<Register>();
4349 __ cmpl(index_reg, Immediate(length));
4350 codegen_->AddSlowPath(slow_path);
4351 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004352 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004353 Register length = length_loc.AsRegister<Register>();
4354 if (index_loc.IsConstant()) {
4355 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4356 __ cmpl(length, Immediate(value));
4357 } else {
4358 __ cmpl(length, index_loc.AsRegister<Register>());
4359 }
4360 codegen_->AddSlowPath(slow_path);
4361 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004362 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363}
4364
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004365void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4366 temp->SetLocations(nullptr);
4367}
4368
4369void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4370 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004371 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372}
4373
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004374void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004375 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004376 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004377}
4378
4379void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004380 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4381}
4382
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004383void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4385}
4386
4387void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004388 HBasicBlock* block = instruction->GetBlock();
4389 if (block->GetLoopInformation() != nullptr) {
4390 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4391 // The back edge will generate the suspend check.
4392 return;
4393 }
4394 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4395 // The goto will generate the suspend check.
4396 return;
4397 }
4398 GenerateSuspendCheck(instruction, nullptr);
4399}
4400
4401void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4402 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004403 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004404 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4405 if (slow_path == nullptr) {
4406 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4407 instruction->SetSlowPath(slow_path);
4408 codegen_->AddSlowPath(slow_path);
4409 if (successor != nullptr) {
4410 DCHECK(successor->IsLoopHeader());
4411 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4412 }
4413 } else {
4414 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4415 }
4416
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004417 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004418 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004419 if (successor == nullptr) {
4420 __ j(kNotEqual, slow_path->GetEntryLabel());
4421 __ Bind(slow_path->GetReturnLabel());
4422 } else {
4423 __ j(kEqual, codegen_->GetLabelOf(successor));
4424 __ jmp(slow_path->GetEntryLabel());
4425 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004426}
4427
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004428X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4429 return codegen_->GetAssembler();
4430}
4431
Mark Mendell7c8d0092015-01-26 11:21:33 -05004432void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004433 ScratchRegisterScope ensure_scratch(
4434 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4435 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4436 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4437 __ movl(temp_reg, Address(ESP, src + stack_offset));
4438 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004439}
4440
4441void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004442 ScratchRegisterScope ensure_scratch(
4443 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4444 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4445 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4446 __ movl(temp_reg, Address(ESP, src + stack_offset));
4447 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4448 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4449 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004450}
4451
4452void ParallelMoveResolverX86::EmitMove(size_t index) {
4453 MoveOperands* move = moves_.Get(index);
4454 Location source = move->GetSource();
4455 Location destination = move->GetDestination();
4456
4457 if (source.IsRegister()) {
4458 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004459 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004460 } else {
4461 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004462 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004463 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004464 } else if (source.IsFpuRegister()) {
4465 if (destination.IsFpuRegister()) {
4466 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4467 } else if (destination.IsStackSlot()) {
4468 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4469 } else {
4470 DCHECK(destination.IsDoubleStackSlot());
4471 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4472 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004473 } else if (source.IsStackSlot()) {
4474 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004475 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004476 } else if (destination.IsFpuRegister()) {
4477 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004478 } else {
4479 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004480 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4481 }
4482 } else if (source.IsDoubleStackSlot()) {
4483 if (destination.IsFpuRegister()) {
4484 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4485 } else {
4486 DCHECK(destination.IsDoubleStackSlot()) << destination;
4487 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004488 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004489 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004490 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004491 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004492 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004493 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004494 if (value == 0) {
4495 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4496 } else {
4497 __ movl(destination.AsRegister<Register>(), Immediate(value));
4498 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004499 } else {
4500 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004501 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004502 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004503 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004504 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004505 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004506 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004507 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004508 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4509 if (value == 0) {
4510 // Easy handling of 0.0.
4511 __ xorps(dest, dest);
4512 } else {
4513 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004514 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4515 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4516 __ movl(temp, Immediate(value));
4517 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004518 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004519 } else {
4520 DCHECK(destination.IsStackSlot()) << destination;
4521 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4522 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004523 } else if (constant->IsLongConstant()) {
4524 int64_t value = constant->AsLongConstant()->GetValue();
4525 int32_t low_value = Low32Bits(value);
4526 int32_t high_value = High32Bits(value);
4527 Immediate low(low_value);
4528 Immediate high(high_value);
4529 if (destination.IsDoubleStackSlot()) {
4530 __ movl(Address(ESP, destination.GetStackIndex()), low);
4531 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4532 } else {
4533 __ movl(destination.AsRegisterPairLow<Register>(), low);
4534 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4535 }
4536 } else {
4537 DCHECK(constant->IsDoubleConstant());
4538 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004539 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004540 int32_t low_value = Low32Bits(value);
4541 int32_t high_value = High32Bits(value);
4542 Immediate low(low_value);
4543 Immediate high(high_value);
4544 if (destination.IsFpuRegister()) {
4545 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4546 if (value == 0) {
4547 // Easy handling of 0.0.
4548 __ xorpd(dest, dest);
4549 } else {
4550 __ pushl(high);
4551 __ pushl(low);
4552 __ movsd(dest, Address(ESP, 0));
4553 __ addl(ESP, Immediate(8));
4554 }
4555 } else {
4556 DCHECK(destination.IsDoubleStackSlot()) << destination;
4557 __ movl(Address(ESP, destination.GetStackIndex()), low);
4558 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4559 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004560 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004561 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004562 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004563 }
4564}
4565
Mark Mendella5c19ce2015-04-01 12:51:05 -04004566void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004567 Register suggested_scratch = reg == EAX ? EBX : EAX;
4568 ScratchRegisterScope ensure_scratch(
4569 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4570
4571 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4572 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4573 __ movl(Address(ESP, mem + stack_offset), reg);
4574 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004575}
4576
Mark Mendell7c8d0092015-01-26 11:21:33 -05004577void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004578 ScratchRegisterScope ensure_scratch(
4579 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4580
4581 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4582 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4583 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4584 __ movss(Address(ESP, mem + stack_offset), reg);
4585 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004586}
4587
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004588void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004589 ScratchRegisterScope ensure_scratch1(
4590 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004591
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004592 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4593 ScratchRegisterScope ensure_scratch2(
4594 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004595
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004596 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4597 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4598 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4599 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4600 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4601 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004602}
4603
4604void ParallelMoveResolverX86::EmitSwap(size_t index) {
4605 MoveOperands* move = moves_.Get(index);
4606 Location source = move->GetSource();
4607 Location destination = move->GetDestination();
4608
4609 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004610 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4611 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4612 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4613 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4614 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004615 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004616 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004617 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004618 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004619 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4620 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004621 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4622 // Use XOR Swap algorithm to avoid a temporary.
4623 DCHECK_NE(source.reg(), destination.reg());
4624 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4625 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4626 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4627 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4628 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4629 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4630 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004631 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4632 // Take advantage of the 16 bytes in the XMM register.
4633 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4634 Address stack(ESP, destination.GetStackIndex());
4635 // Load the double into the high doubleword.
4636 __ movhpd(reg, stack);
4637
4638 // Store the low double into the destination.
4639 __ movsd(stack, reg);
4640
4641 // Move the high double to the low double.
4642 __ psrldq(reg, Immediate(8));
4643 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4644 // Take advantage of the 16 bytes in the XMM register.
4645 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4646 Address stack(ESP, source.GetStackIndex());
4647 // Load the double into the high doubleword.
4648 __ movhpd(reg, stack);
4649
4650 // Store the low double into the destination.
4651 __ movsd(stack, reg);
4652
4653 // Move the high double to the low double.
4654 __ psrldq(reg, Immediate(8));
4655 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4656 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4657 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004658 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004659 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004660 }
4661}
4662
4663void ParallelMoveResolverX86::SpillScratch(int reg) {
4664 __ pushl(static_cast<Register>(reg));
4665}
4666
4667void ParallelMoveResolverX86::RestoreScratch(int reg) {
4668 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004669}
4670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004671void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004672 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4673 ? LocationSummary::kCallOnSlowPath
4674 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004675 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004676 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004677 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004678 locations->SetOut(Location::RequiresRegister());
4679}
4680
4681void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004682 LocationSummary* locations = cls->GetLocations();
4683 Register out = locations->Out().AsRegister<Register>();
4684 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004685 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004686 DCHECK(!cls->CanCallRuntime());
4687 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004688 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004689 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004690 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004691 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004692 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004693 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004694 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004695
4696 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4697 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4698 codegen_->AddSlowPath(slow_path);
4699 __ testl(out, out);
4700 __ j(kEqual, slow_path->GetEntryLabel());
4701 if (cls->MustGenerateClinitCheck()) {
4702 GenerateClassInitializationCheck(slow_path, out);
4703 } else {
4704 __ Bind(slow_path->GetExitLabel());
4705 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004706 }
4707}
4708
4709void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4710 LocationSummary* locations =
4711 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4712 locations->SetInAt(0, Location::RequiresRegister());
4713 if (check->HasUses()) {
4714 locations->SetOut(Location::SameAsFirstInput());
4715 }
4716}
4717
4718void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004719 // We assume the class to not be null.
4720 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4721 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004722 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004723 GenerateClassInitializationCheck(slow_path,
4724 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004725}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004726
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004727void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4728 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004729 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4730 Immediate(mirror::Class::kStatusInitialized));
4731 __ j(kLess, slow_path->GetEntryLabel());
4732 __ Bind(slow_path->GetExitLabel());
4733 // No need for memory fence, thanks to the X86 memory model.
4734}
4735
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004736void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4737 LocationSummary* locations =
4738 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004739 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004740 locations->SetOut(Location::RequiresRegister());
4741}
4742
4743void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4744 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4745 codegen_->AddSlowPath(slow_path);
4746
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004747 LocationSummary* locations = load->GetLocations();
4748 Register out = locations->Out().AsRegister<Register>();
4749 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004750 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004751 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004752 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004753 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004754 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004755 __ testl(out, out);
4756 __ j(kEqual, slow_path->GetEntryLabel());
4757 __ Bind(slow_path->GetExitLabel());
4758}
4759
David Brazdilcb1c0552015-08-04 16:22:25 +01004760static Address GetExceptionTlsAddress() {
4761 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4762}
4763
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004764void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4765 LocationSummary* locations =
4766 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4767 locations->SetOut(Location::RequiresRegister());
4768}
4769
4770void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004771 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4772}
4773
4774void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4775 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4776}
4777
4778void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4779 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004780}
4781
4782void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4783 LocationSummary* locations =
4784 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4785 InvokeRuntimeCallingConvention calling_convention;
4786 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4787}
4788
4789void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004790 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4791 instruction,
4792 instruction->GetDexPc(),
4793 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004794}
4795
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004796void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004797 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4798 ? LocationSummary::kNoCall
4799 : LocationSummary::kCallOnSlowPath;
4800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4801 locations->SetInAt(0, Location::RequiresRegister());
4802 locations->SetInAt(1, Location::Any());
4803 locations->SetOut(Location::RequiresRegister());
4804}
4805
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004806void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004807 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004808 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004809 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004810 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004811 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4812 Label done, zero;
4813 SlowPathCodeX86* slow_path = nullptr;
4814
4815 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004816 // Avoid null check if we know obj is not null.
4817 if (instruction->MustDoNullCheck()) {
4818 __ testl(obj, obj);
4819 __ j(kEqual, &zero);
4820 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004821 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004822 __ movl(out, Address(obj, class_offset));
4823 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004824 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004825 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004826 } else {
4827 DCHECK(cls.IsStackSlot()) << cls;
4828 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4829 }
4830
4831 if (instruction->IsClassFinal()) {
4832 // Classes must be equal for the instanceof to succeed.
4833 __ j(kNotEqual, &zero);
4834 __ movl(out, Immediate(1));
4835 __ jmp(&done);
4836 } else {
4837 // If the classes are not equal, we go into a slow path.
4838 DCHECK(locations->OnlyCallsOnSlowPath());
4839 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004840 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004841 codegen_->AddSlowPath(slow_path);
4842 __ j(kNotEqual, slow_path->GetEntryLabel());
4843 __ movl(out, Immediate(1));
4844 __ jmp(&done);
4845 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004846
4847 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4848 __ Bind(&zero);
4849 __ movl(out, Immediate(0));
4850 }
4851
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004852 if (slow_path != nullptr) {
4853 __ Bind(slow_path->GetExitLabel());
4854 }
4855 __ Bind(&done);
4856}
4857
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004858void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4859 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4860 instruction, LocationSummary::kCallOnSlowPath);
4861 locations->SetInAt(0, Location::RequiresRegister());
4862 locations->SetInAt(1, Location::Any());
4863 locations->AddTemp(Location::RequiresRegister());
4864}
4865
4866void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4867 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004868 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004869 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004870 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004871 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4872 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4873 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4874 codegen_->AddSlowPath(slow_path);
4875
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004876 // Avoid null check if we know obj is not null.
4877 if (instruction->MustDoNullCheck()) {
4878 __ testl(obj, obj);
4879 __ j(kEqual, slow_path->GetExitLabel());
4880 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004881 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004882 __ movl(temp, Address(obj, class_offset));
4883 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004884 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004885 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004886 } else {
4887 DCHECK(cls.IsStackSlot()) << cls;
4888 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4889 }
Roland Levillain4d027112015-07-01 15:41:14 +01004890 // The checkcast succeeds if the classes are equal (fast path).
4891 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004892 __ j(kNotEqual, slow_path->GetEntryLabel());
4893 __ Bind(slow_path->GetExitLabel());
4894}
4895
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004896void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4897 LocationSummary* locations =
4898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4899 InvokeRuntimeCallingConvention calling_convention;
4900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4901}
4902
4903void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004904 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4905 : QUICK_ENTRY_POINT(pUnlockObject),
4906 instruction,
4907 instruction->GetDexPc(),
4908 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004909}
4910
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004911void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4912void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4913void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4914
4915void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4916 LocationSummary* locations =
4917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4918 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4919 || instruction->GetResultType() == Primitive::kPrimLong);
4920 locations->SetInAt(0, Location::RequiresRegister());
4921 locations->SetInAt(1, Location::Any());
4922 locations->SetOut(Location::SameAsFirstInput());
4923}
4924
4925void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4926 HandleBitwiseOperation(instruction);
4927}
4928
4929void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4930 HandleBitwiseOperation(instruction);
4931}
4932
4933void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4934 HandleBitwiseOperation(instruction);
4935}
4936
4937void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4938 LocationSummary* locations = instruction->GetLocations();
4939 Location first = locations->InAt(0);
4940 Location second = locations->InAt(1);
4941 DCHECK(first.Equals(locations->Out()));
4942
4943 if (instruction->GetResultType() == Primitive::kPrimInt) {
4944 if (second.IsRegister()) {
4945 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004946 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004947 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004948 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004949 } else {
4950 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004951 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952 }
4953 } else if (second.IsConstant()) {
4954 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004955 __ andl(first.AsRegister<Register>(),
4956 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004957 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004958 __ orl(first.AsRegister<Register>(),
4959 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004960 } else {
4961 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004962 __ xorl(first.AsRegister<Register>(),
4963 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004964 }
4965 } else {
4966 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004967 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004968 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004969 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004970 } else {
4971 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004972 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004973 }
4974 }
4975 } else {
4976 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4977 if (second.IsRegisterPair()) {
4978 if (instruction->IsAnd()) {
4979 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4980 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4981 } else if (instruction->IsOr()) {
4982 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4983 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4984 } else {
4985 DCHECK(instruction->IsXor());
4986 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4987 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4988 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004989 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990 if (instruction->IsAnd()) {
4991 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4992 __ andl(first.AsRegisterPairHigh<Register>(),
4993 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4994 } else if (instruction->IsOr()) {
4995 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4996 __ orl(first.AsRegisterPairHigh<Register>(),
4997 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4998 } else {
4999 DCHECK(instruction->IsXor());
5000 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5001 __ xorl(first.AsRegisterPairHigh<Register>(),
5002 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5003 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005004 } else {
5005 DCHECK(second.IsConstant()) << second;
5006 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005007 int32_t low_value = Low32Bits(value);
5008 int32_t high_value = High32Bits(value);
5009 Immediate low(low_value);
5010 Immediate high(high_value);
5011 Register first_low = first.AsRegisterPairLow<Register>();
5012 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005013 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005014 if (low_value == 0) {
5015 __ xorl(first_low, first_low);
5016 } else if (low_value != -1) {
5017 __ andl(first_low, low);
5018 }
5019 if (high_value == 0) {
5020 __ xorl(first_high, first_high);
5021 } else if (high_value != -1) {
5022 __ andl(first_high, high);
5023 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005024 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005025 if (low_value != 0) {
5026 __ orl(first_low, low);
5027 }
5028 if (high_value != 0) {
5029 __ orl(first_high, high);
5030 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005031 } else {
5032 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005033 if (low_value != 0) {
5034 __ xorl(first_low, low);
5035 }
5036 if (high_value != 0) {
5037 __ xorl(first_high, high);
5038 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005039 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005040 }
5041 }
5042}
5043
Calin Juravleb1498f62015-02-16 13:13:29 +00005044void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5045 // Nothing to do, this should be removed during prepare for register allocator.
5046 UNUSED(instruction);
5047 LOG(FATAL) << "Unreachable";
5048}
5049
5050void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5051 // Nothing to do, this should be removed during prepare for register allocator.
5052 UNUSED(instruction);
5053 LOG(FATAL) << "Unreachable";
5054}
5055
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005056void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5057 DCHECK(codegen_->IsBaseline());
5058 LocationSummary* locations =
5059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5060 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5061}
5062
5063void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5064 DCHECK(codegen_->IsBaseline());
5065 // Will be generated at use site.
5066}
5067
Roland Levillain4d027112015-07-01 15:41:14 +01005068#undef __
5069
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005070} // namespace x86
5071} // namespace art