blob: 0f1175563e6eccd90588225d6347c2901a6665e3 [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
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.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;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
156 private:
157 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 Label return_label_;
160
161 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
162};
163
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000164class LoadStringSlowPathX86 : public SlowPathCodeX86 {
165 public:
166 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
167
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169 LocationSummary* locations = instruction_->GetLocations();
170 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
171
172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000175
176 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800177 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
178 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000181 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183
184 __ jmp(GetExitLabel());
185 }
186
187 private:
188 HLoadString* const instruction_;
189
190 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
191};
192
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193class LoadClassSlowPathX86 : public SlowPathCodeX86 {
194 public:
195 LoadClassSlowPathX86(HLoadClass* cls,
196 HInstruction* at,
197 uint32_t dex_pc,
198 bool do_clinit)
199 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
200 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201 }
202
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LocationSummary* locations = at_->GetLocations();
205 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
206 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208
209 InvokeRuntimeCallingConvention calling_convention;
210 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
211 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
212 __ fs()->call(Address::Absolute(do_clinit_
213 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
214 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216
217 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000218 Location out = locations->Out();
219 if (out.IsValid()) {
220 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
221 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000223
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 // The class this slow path will load.
230 HLoadClass* const cls_;
231
232 // The instruction where this slow path is happening.
233 // (Might be the load class or an initialization check).
234 HInstruction* const at_;
235
236 // The dex PC of `at_`.
237 const uint32_t dex_pc_;
238
239 // Whether to initialize the class.
240 const bool do_clinit_;
241
242 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
243};
244
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
246 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000247 TypeCheckSlowPathX86(HInstruction* instruction,
248 Location class_to_check,
249 Location object_class,
250 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000252 class_to_check_(class_to_check),
253 object_class_(object_class),
254 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000258 DCHECK(instruction_->IsCheckCast()
259 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260
261 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000268 x86_codegen->EmitParallelMoves(
269 class_to_check_,
270 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100271 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000272 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100273 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
274 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000276 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000277 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
278 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 } else {
280 DCHECK(instruction_->IsCheckCast());
281 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 if (instruction_->IsInstanceOf()) {
286 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
287 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 __ jmp(GetExitLabel());
291 }
292
293 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 HInstruction* const instruction_;
295 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
300};
301
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700302class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
303 public:
304 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
305 : instruction_(instruction) {}
306
307 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
308 __ Bind(GetEntryLabel());
309 SaveLiveRegisters(codegen, instruction_->GetLocations());
310 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
311 // No need to restore live registers.
312 DCHECK(instruction_->IsDeoptimize());
313 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
314 uint32_t dex_pc = deoptimize->GetDexPc();
315 codegen->RecordPcInfo(instruction_, dex_pc, this);
316 }
317
318 private:
319 HInstruction* const instruction_;
320 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
321};
322
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100323#undef __
324#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
325
Dave Allison20dfc792014-06-16 20:44:29 -0700326inline Condition X86Condition(IfCondition cond) {
327 switch (cond) {
328 case kCondEQ: return kEqual;
329 case kCondNE: return kNotEqual;
330 case kCondLT: return kLess;
331 case kCondLE: return kLessEqual;
332 case kCondGT: return kGreater;
333 case kCondGE: return kGreaterEqual;
334 default:
335 LOG(FATAL) << "Unknown if condition";
336 }
337 return kEqual;
338}
339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
341 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
342}
343
344void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
346}
347
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
349 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
350 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Mark Mendell7c8d0092015-01-26 11:21:33 -0500358size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
359 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
360 return GetFloatingPointSpillSlotSize();
361}
362
363size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
365 return GetFloatingPointSpillSlotSize();
366}
367
Mark Mendellfb8d2792015-03-31 22:16:59 -0400368CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
369 const X86InstructionSetFeatures& isa_features,
370 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500371 : CodeGenerator(graph,
372 kNumberOfCpuRegisters,
373 kNumberOfXmmRegisters,
374 kNumberOfRegisterPairs,
375 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
376 arraysize(kCoreCalleeSaves))
377 | (1 << kFakeReturnRegister),
378 0,
379 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400383 move_resolver_(graph->GetArena(), this),
384 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 // Use a fake return address register to mimic Quick.
386 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100387}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 switch (type) {
391 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 X86ManagedRegister pair =
394 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100395 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100397 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
398 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100399 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100400 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401 }
402
403 case Primitive::kPrimByte:
404 case Primitive::kPrimBoolean:
405 case Primitive::kPrimChar:
406 case Primitive::kPrimShort:
407 case Primitive::kPrimInt:
408 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100410 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
413 X86ManagedRegister current =
414 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
415 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420 }
421
422 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 return Location::FpuRegisterLocation(
425 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427
428 case Primitive::kPrimVoid:
429 LOG(FATAL) << "Unreachable type " << type;
430 }
431
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433}
434
Mark Mendell5f874182015-03-04 15:42:45 -0500435void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438
439 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
Mark Mendell5f874182015-03-04 15:42:45 -0500442 if (is_baseline) {
443 blocked_core_registers_[EBP] = true;
444 blocked_core_registers_[ESI] = true;
445 blocked_core_registers_[EDI] = true;
446 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100447
448 UpdateBlockedPairRegisters();
449}
450
451void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
452 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
453 X86ManagedRegister current =
454 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
455 if (blocked_core_registers_[current.AsRegisterPairLow()]
456 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
457 blocked_register_pairs_[i] = true;
458 }
459 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100460}
461
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
463 : HGraphVisitor(graph),
464 assembler_(codegen->GetAssembler()),
465 codegen_(codegen) {}
466
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100468 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100469}
470
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000471void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000473 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000474 bool skip_overflow_check =
475 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000477
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100479 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100480 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100481 }
482
Mark Mendell5f874182015-03-04 15:42:45 -0500483 if (HasEmptyFrame()) {
484 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000485 }
Mark Mendell5f874182015-03-04 15:42:45 -0500486
487 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
488 Register reg = kCoreCalleeSaves[i];
489 if (allocated_registers_.ContainsCoreRegister(reg)) {
490 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 __ cfi().AdjustCFAOffset(kX86WordSize);
492 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500493 }
494 }
495
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496 int adjust = GetFrameSize() - FrameEntrySpillSize();
497 __ subl(ESP, Immediate(adjust));
498 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500499 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500}
501
502void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100503 __ cfi().RememberState();
504 if (!HasEmptyFrame()) {
505 int adjust = GetFrameSize() - FrameEntrySpillSize();
506 __ addl(ESP, Immediate(adjust));
507 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500508
David Srbeckyc34dc932015-04-12 09:27:43 +0100509 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
510 Register reg = kCoreCalleeSaves[i];
511 if (allocated_registers_.ContainsCoreRegister(reg)) {
512 __ popl(reg);
513 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
514 __ cfi().Restore(DWARFReg(reg));
515 }
Mark Mendell5f874182015-03-04 15:42:45 -0500516 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000517 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100518 __ ret();
519 __ cfi().RestoreState();
520 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100523void CodeGeneratorX86::Bind(HBasicBlock* block) {
524 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525}
526
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100527void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000528 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100529 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000530}
531
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
533 switch (load->GetType()) {
534 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537
538 case Primitive::kPrimInt:
539 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542
543 case Primitive::kPrimBoolean:
544 case Primitive::kPrimByte:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimVoid:
548 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700549 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550 }
551
552 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700553 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100554}
555
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
557 switch (type) {
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimByte:
560 case Primitive::kPrimChar:
561 case Primitive::kPrimShort:
562 case Primitive::kPrimInt:
563 case Primitive::kPrimNot: {
564 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000565 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100567 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000569 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100570 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000573 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574 uint32_t index = gp_index_;
575 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100578 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
579 calling_convention.GetRegisterPairAt(index));
580 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100581 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000582 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
583 }
584 }
585
586 case Primitive::kPrimFloat: {
587 uint32_t index = fp_index_++;
588 stack_index_++;
589 if (index < calling_convention.GetNumberOfFpuRegisters()) {
590 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
591 } else {
592 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
593 }
594 }
595
596 case Primitive::kPrimDouble: {
597 uint32_t index = fp_index_++;
598 stack_index_ += 2;
599 if (index < calling_convention.GetNumberOfFpuRegisters()) {
600 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
601 } else {
602 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 }
604 }
605
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 case Primitive::kPrimVoid:
607 LOG(FATAL) << "Unexpected parameter type " << type;
608 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100609 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100610 return Location();
611}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100612
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613void CodeGeneratorX86::Move32(Location destination, Location source) {
614 if (source.Equals(destination)) {
615 return;
616 }
617 if (destination.IsRegister()) {
618 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 } else {
623 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (destination.IsFpuRegister()) {
627 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else {
632 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000636 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500641 } else if (source.IsConstant()) {
642 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000643 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500644 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 } else {
646 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100647 __ pushl(Address(ESP, source.GetStackIndex()));
648 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 }
650 }
651}
652
653void CodeGeneratorX86::Move64(Location destination, Location source) {
654 if (source.Equals(destination)) {
655 return;
656 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 if (destination.IsRegisterPair()) {
658 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 EmitParallelMoves(
660 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
661 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100662 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100664 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
665 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (source.IsFpuRegister()) {
667 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000669 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
672 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
674 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500676 if (source.IsFpuRegister()) {
677 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
678 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000679 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100680 } else {
681 LOG(FATAL) << "Unimplemented";
682 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000684 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000686 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000692 } else if (source.IsConstant()) {
693 HConstant* constant = source.GetConstant();
694 int64_t value;
695 if (constant->IsLongConstant()) {
696 value = constant->AsLongConstant()->GetValue();
697 } else {
698 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000699 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000700 }
701 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
702 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000704 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000705 EmitParallelMoves(
706 Location::StackSlot(source.GetStackIndex()),
707 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000709 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100710 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
711 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 }
713 }
714}
715
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100716void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000717 LocationSummary* locations = instruction->GetLocations();
718 if (locations != nullptr && locations->Out().Equals(location)) {
719 return;
720 }
721
722 if (locations != nullptr && locations->Out().IsConstant()) {
723 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000724 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
725 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000728 } else if (location.IsStackSlot()) {
729 __ movl(Address(ESP, location.GetStackIndex()), imm);
730 } else {
731 DCHECK(location.IsConstant());
732 DCHECK_EQ(location.GetConstant(), const_to_move);
733 }
734 } else if (const_to_move->IsLongConstant()) {
735 int64_t value = const_to_move->AsLongConstant()->GetValue();
736 if (location.IsRegisterPair()) {
737 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
738 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
739 } else if (location.IsDoubleStackSlot()) {
740 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000741 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
742 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 } else {
744 DCHECK(location.IsConstant());
745 DCHECK_EQ(location.GetConstant(), instruction);
746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000748 } else if (instruction->IsTemporary()) {
749 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000750 if (temp_location.IsStackSlot()) {
751 Move32(location, temp_location);
752 } else {
753 DCHECK(temp_location.IsDoubleStackSlot());
754 Move64(location, temp_location);
755 }
Roland Levillain476df552014-10-09 17:51:36 +0100756 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100758 switch (instruction->GetType()) {
759 case Primitive::kPrimBoolean:
760 case Primitive::kPrimByte:
761 case Primitive::kPrimChar:
762 case Primitive::kPrimShort:
763 case Primitive::kPrimInt:
764 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 case Primitive::kPrimFloat:
766 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 break;
768
769 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 case Primitive::kPrimDouble:
771 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 break;
773
774 default:
775 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
776 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100778 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 switch (instruction->GetType()) {
780 case Primitive::kPrimBoolean:
781 case Primitive::kPrimByte:
782 case Primitive::kPrimChar:
783 case Primitive::kPrimShort:
784 case Primitive::kPrimInt:
785 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 break;
789
790 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000792 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794
795 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798 }
799}
800
801void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803}
804
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 DCHECK(!successor->IsExitBlock());
808
809 HBasicBlock* block = got->GetBlock();
810 HInstruction* previous = got->GetPrevious();
811
812 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000813 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
815 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
816 return;
817 }
818
819 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
820 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
821 }
822 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000823 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000824 }
825}
826
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700832 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000833}
834
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700835void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
836 Label* true_target,
837 Label* false_target,
838 Label* always_true_target) {
839 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (cond->IsIntConstant()) {
841 // Constant condition, statically compared against 1.
842 int32_t cond_value = cond->AsIntConstant()->GetValue();
843 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844 if (always_true_target != nullptr) {
845 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100846 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100848 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 DCHECK_EQ(cond_value, 0);
850 }
851 } else {
852 bool materialized =
853 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
854 // Moves do not affect the eflags register, so if the condition is
855 // evaluated just before the if, we don't need to evaluate it
856 // again.
857 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700858 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100859 if (materialized) {
860 if (!eflags_set) {
861 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500864 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 } else {
866 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
867 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700870 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 }
872 } else {
873 Location lhs = cond->GetLocations()->InAt(0);
874 Location rhs = cond->GetLocations()->InAt(1);
875 // LHS is guaranteed to be in a register (see
876 // LocationsBuilderX86::VisitCondition).
877 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500880 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
881 if (constant == 0) {
882 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
883 } else {
884 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
885 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000887 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700889 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 if (false_target != nullptr) {
893 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894 }
895}
896
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700897void LocationsBuilderX86::VisitIf(HIf* if_instr) {
898 LocationSummary* locations =
899 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
900 HInstruction* cond = if_instr->InputAt(0);
901 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
902 locations->SetInAt(0, Location::Any());
903 }
904}
905
906void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
907 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
908 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
909 Label* always_true_target = true_target;
910 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
911 if_instr->IfTrueSuccessor())) {
912 always_true_target = nullptr;
913 }
914 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
915 if_instr->IfFalseSuccessor())) {
916 false_target = nullptr;
917 }
918 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
919}
920
921void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
922 LocationSummary* locations = new (GetGraph()->GetArena())
923 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
924 HInstruction* cond = deoptimize->InputAt(0);
925 DCHECK(cond->IsCondition());
926 if (cond->AsCondition()->NeedsMaterialization()) {
927 locations->SetInAt(0, Location::Any());
928 }
929}
930
931void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
932 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
933 DeoptimizationSlowPathX86(deoptimize);
934 codegen_->AddSlowPath(slow_path);
935 Label* slow_path_entry = slow_path->GetEntryLabel();
936 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
937}
938
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941}
942
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000943void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
944 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000945}
946
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100948 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949}
950
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000951void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100952 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700953 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000954}
955
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 switch (store->InputAt(1)->GetType()) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
968 break;
969
970 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
973 break;
974
975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 }
978 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983}
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100988 locations->SetInAt(0, Location::RequiresRegister());
989 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100990 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500991 // We need a byte register.
992 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100993 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000994}
995
Dave Allison20dfc792014-06-16 20:44:29 -0700996void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
997 if (comp->NeedsMaterialization()) {
998 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001000 // Clear register: setcc only sets the low byte.
1001 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001002 Location lhs = locations->InAt(0);
1003 Location rhs = locations->InAt(1);
1004 if (rhs.IsRegister()) {
1005 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1006 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001007 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001008 if (constant == 0) {
1009 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1010 } else {
1011 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1012 }
Dave Allison20dfc792014-06-16 20:44:29 -07001013 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001014 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001015 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001016 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001017 }
Dave Allison20dfc792014-06-16 20:44:29 -07001018}
1019
1020void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1065 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
1068void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001069 LocationSummary* locations =
1070 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001071 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072}
1073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001076 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077}
1078
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001079void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1080 LocationSummary* locations =
1081 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1082 locations->SetOut(Location::ConstantLocation(constant));
1083}
1084
1085void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1086 // Will be generated at use site.
1087 UNUSED(constant);
1088}
1089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 LocationSummary* locations =
1092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001093 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094}
1095
1096void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1097 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001098 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099}
1100
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001101void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1102 LocationSummary* locations =
1103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1104 locations->SetOut(Location::ConstantLocation(constant));
1105}
1106
1107void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001110}
1111
1112void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1113 LocationSummary* locations =
1114 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1115 locations->SetOut(Location::ConstantLocation(constant));
1116}
1117
1118void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1119 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001121}
1122
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001123void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001124 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001125}
1126
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001127void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001128 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 LocationSummary* locations =
1134 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 switch (ret->InputAt(0)->GetType()) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001142 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 break;
1144
1145 case Primitive::kPrimLong:
1146 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 break;
1149
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 case Primitive::kPrimFloat:
1151 case Primitive::kPrimDouble:
1152 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001154 break;
1155
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001159}
1160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 if (kIsDebugBuild) {
1163 switch (ret->InputAt(0)->GetType()) {
1164 case Primitive::kPrimBoolean:
1165 case Primitive::kPrimByte:
1166 case Primitive::kPrimChar:
1167 case Primitive::kPrimShort:
1168 case Primitive::kPrimInt:
1169 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001170 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 break;
1172
1173 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1175 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 break;
1177
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 case Primitive::kPrimFloat:
1179 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001180 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 break;
1182
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 }
1186 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001187 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188}
1189
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001190void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001191 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001192 if (intrinsic.TryDispatch(invoke)) {
1193 return;
1194 }
1195
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001196 HandleInvoke(invoke);
1197}
1198
Mark Mendell09ed1a32015-03-25 08:30:06 -04001199static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1200 if (invoke->GetLocations()->Intrinsified()) {
1201 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1202 intrinsic.Dispatch(invoke);
1203 return true;
1204 }
1205 return false;
1206}
1207
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001208void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001209 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1210 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001211 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001212
Mark Mendell09ed1a32015-03-25 08:30:06 -04001213 codegen_->GenerateStaticOrDirectCall(
1214 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001215}
1216
1217void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1218 HandleInvoke(invoke);
1219}
1220
1221void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001224 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001225
1226 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001228 HInstruction* input = invoke->InputAt(i);
1229 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1230 }
1231
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 switch (invoke->GetType()) {
1233 case Primitive::kPrimBoolean:
1234 case Primitive::kPrimByte:
1235 case Primitive::kPrimChar:
1236 case Primitive::kPrimShort:
1237 case Primitive::kPrimInt:
1238 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001239 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 break;
1241
1242 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001243 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 break;
1245
1246 case Primitive::kPrimVoid:
1247 break;
1248
1249 case Primitive::kPrimDouble:
1250 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001251 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 break;
1253 }
1254
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001255 invoke->SetLocations(locations);
1256}
1257
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001258void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001260 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1261 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1262 LocationSummary* locations = invoke->GetLocations();
1263 Location receiver = locations->InAt(0);
1264 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1265 // temp = object->GetClass();
1266 if (receiver.IsStackSlot()) {
1267 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1268 __ movl(temp, Address(temp, class_offset));
1269 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001270 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001271 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001272 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001273 // temp = temp->GetMethodAt(method_offset);
1274 __ movl(temp, Address(temp, method_offset));
1275 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001276 __ call(Address(
1277 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001278
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001279 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001280 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001281}
1282
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001283void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1284 HandleInvoke(invoke);
1285 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001286 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001287}
1288
1289void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1290 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001291 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001292 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1293 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1294 LocationSummary* locations = invoke->GetLocations();
1295 Location receiver = locations->InAt(0);
1296 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1297
1298 // Set the hidden argument.
1299 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301
1302 // temp = object->GetClass();
1303 if (receiver.IsStackSlot()) {
1304 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1305 __ movl(temp, Address(temp, class_offset));
1306 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001309 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310 // temp = temp->GetImtEntryAt(method_offset);
1311 __ movl(temp, Address(temp, method_offset));
1312 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001313 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001314 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001315
1316 DCHECK(!codegen_->IsLeafMethod());
1317 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1318}
1319
Roland Levillain88cb1752014-10-20 16:36:47 +01001320void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1321 LocationSummary* locations =
1322 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1323 switch (neg->GetResultType()) {
1324 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001325 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001326 locations->SetInAt(0, Location::RequiresRegister());
1327 locations->SetOut(Location::SameAsFirstInput());
1328 break;
1329
Roland Levillain88cb1752014-10-20 16:36:47 +01001330 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001331 locations->SetInAt(0, Location::RequiresFpuRegister());
1332 locations->SetOut(Location::SameAsFirstInput());
1333 locations->AddTemp(Location::RequiresRegister());
1334 locations->AddTemp(Location::RequiresFpuRegister());
1335 break;
1336
Roland Levillain88cb1752014-10-20 16:36:47 +01001337 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001338 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001339 locations->SetOut(Location::SameAsFirstInput());
1340 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001341 break;
1342
1343 default:
1344 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1345 }
1346}
1347
1348void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1349 LocationSummary* locations = neg->GetLocations();
1350 Location out = locations->Out();
1351 Location in = locations->InAt(0);
1352 switch (neg->GetResultType()) {
1353 case Primitive::kPrimInt:
1354 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001355 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001356 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001357 break;
1358
1359 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001360 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001361 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001362 __ negl(out.AsRegisterPairLow<Register>());
1363 // Negation is similar to subtraction from zero. The least
1364 // significant byte triggers a borrow when it is different from
1365 // zero; to take it into account, add 1 to the most significant
1366 // byte if the carry flag (CF) is set to 1 after the first NEGL
1367 // operation.
1368 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1369 __ negl(out.AsRegisterPairHigh<Register>());
1370 break;
1371
Roland Levillain5368c212014-11-27 15:03:41 +00001372 case Primitive::kPrimFloat: {
1373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 Register constant = locations->GetTemp(0).AsRegister<Register>();
1375 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001376 // Implement float negation with an exclusive or with value
1377 // 0x80000000 (mask for bit 31, representing the sign of a
1378 // single-precision floating-point number).
1379 __ movl(constant, Immediate(INT32_C(0x80000000)));
1380 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001382 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001383 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001384
Roland Levillain5368c212014-11-27 15:03:41 +00001385 case Primitive::kPrimDouble: {
1386 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001387 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001388 // Implement double negation with an exclusive or with value
1389 // 0x8000000000000000 (mask for bit 63, representing the sign of
1390 // a double-precision floating-point number).
1391 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001392 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001393 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001394 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001395
1396 default:
1397 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1398 }
1399}
1400
Roland Levillaindff1f282014-11-05 14:15:05 +00001401void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001402 Primitive::Type result_type = conversion->GetResultType();
1403 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001404 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001405
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001406 // The float-to-long and double-to-long type conversions rely on a
1407 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001408 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001409 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1410 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001411 ? LocationSummary::kCall
1412 : LocationSummary::kNoCall;
1413 LocationSummary* locations =
1414 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1415
David Brazdilb2bd1c52015-03-25 11:17:37 +00001416 // The Java language does not allow treating boolean as an integral type but
1417 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001418
Roland Levillaindff1f282014-11-05 14:15:05 +00001419 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001420 case Primitive::kPrimByte:
1421 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001422 case Primitive::kPrimBoolean:
1423 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001424 case Primitive::kPrimShort:
1425 case Primitive::kPrimInt:
1426 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001427 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001428 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1429 // Make the output overlap to please the register allocator. This greatly simplifies
1430 // the validation of the linear scan implementation
1431 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected type conversion from " << input_type
1436 << " to " << result_type;
1437 }
1438 break;
1439
Roland Levillain01a8d712014-11-14 16:27:39 +00001440 case Primitive::kPrimShort:
1441 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001442 case Primitive::kPrimBoolean:
1443 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001444 case Primitive::kPrimByte:
1445 case Primitive::kPrimInt:
1446 case Primitive::kPrimChar:
1447 // Processing a Dex `int-to-short' instruction.
1448 locations->SetInAt(0, Location::Any());
1449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1450 break;
1451
1452 default:
1453 LOG(FATAL) << "Unexpected type conversion from " << input_type
1454 << " to " << result_type;
1455 }
1456 break;
1457
Roland Levillain946e1432014-11-11 17:35:19 +00001458 case Primitive::kPrimInt:
1459 switch (input_type) {
1460 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001461 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001462 locations->SetInAt(0, Location::Any());
1463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1464 break;
1465
1466 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001467 // Processing a Dex `float-to-int' instruction.
1468 locations->SetInAt(0, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::RequiresRegister());
1470 locations->AddTemp(Location::RequiresFpuRegister());
1471 break;
1472
Roland Levillain946e1432014-11-11 17:35:19 +00001473 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001474 // Processing a Dex `double-to-int' instruction.
1475 locations->SetInAt(0, Location::RequiresFpuRegister());
1476 locations->SetOut(Location::RequiresRegister());
1477 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001478 break;
1479
1480 default:
1481 LOG(FATAL) << "Unexpected type conversion from " << input_type
1482 << " to " << result_type;
1483 }
1484 break;
1485
Roland Levillaindff1f282014-11-05 14:15:05 +00001486 case Primitive::kPrimLong:
1487 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001488 case Primitive::kPrimBoolean:
1489 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001490 case Primitive::kPrimByte:
1491 case Primitive::kPrimShort:
1492 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001493 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001494 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001495 locations->SetInAt(0, Location::RegisterLocation(EAX));
1496 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1497 break;
1498
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001499 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001500 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001501 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001502 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001503 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1504 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1505
Vladimir Marko949c91f2015-01-27 10:48:44 +00001506 // The runtime helper puts the result in EAX, EDX.
1507 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001508 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001509 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001510
1511 default:
1512 LOG(FATAL) << "Unexpected type conversion from " << input_type
1513 << " to " << result_type;
1514 }
1515 break;
1516
Roland Levillain981e4542014-11-14 11:47:14 +00001517 case Primitive::kPrimChar:
1518 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001519 case Primitive::kPrimBoolean:
1520 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001521 case Primitive::kPrimByte:
1522 case Primitive::kPrimShort:
1523 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001524 // Processing a Dex `int-to-char' instruction.
1525 locations->SetInAt(0, Location::Any());
1526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1527 break;
1528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
1533 break;
1534
Roland Levillaindff1f282014-11-05 14:15:05 +00001535 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001537 case Primitive::kPrimBoolean:
1538 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001539 case Primitive::kPrimByte:
1540 case Primitive::kPrimShort:
1541 case Primitive::kPrimInt:
1542 case Primitive::kPrimChar:
1543 // Processing a Dex `int-to-float' instruction.
1544 locations->SetInAt(0, Location::RequiresRegister());
1545 locations->SetOut(Location::RequiresFpuRegister());
1546 break;
1547
1548 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001549 // Processing a Dex `long-to-float' instruction.
1550 locations->SetInAt(0, Location::RequiresRegister());
1551 locations->SetOut(Location::RequiresFpuRegister());
1552 locations->AddTemp(Location::RequiresFpuRegister());
1553 locations->AddTemp(Location::RequiresFpuRegister());
1554 break;
1555
Roland Levillaincff13742014-11-17 14:32:17 +00001556 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001557 // Processing a Dex `double-to-float' instruction.
1558 locations->SetInAt(0, Location::RequiresFpuRegister());
1559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001560 break;
1561
1562 default:
1563 LOG(FATAL) << "Unexpected type conversion from " << input_type
1564 << " to " << result_type;
1565 };
1566 break;
1567
Roland Levillaindff1f282014-11-05 14:15:05 +00001568 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001569 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001570 case Primitive::kPrimBoolean:
1571 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001572 case Primitive::kPrimByte:
1573 case Primitive::kPrimShort:
1574 case Primitive::kPrimInt:
1575 case Primitive::kPrimChar:
1576 // Processing a Dex `int-to-double' instruction.
1577 locations->SetInAt(0, Location::RequiresRegister());
1578 locations->SetOut(Location::RequiresFpuRegister());
1579 break;
1580
1581 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001582 // Processing a Dex `long-to-double' instruction.
1583 locations->SetInAt(0, Location::RequiresRegister());
1584 locations->SetOut(Location::RequiresFpuRegister());
1585 locations->AddTemp(Location::RequiresFpuRegister());
1586 locations->AddTemp(Location::RequiresFpuRegister());
1587 break;
1588
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001590 // Processing a Dex `float-to-double' instruction.
1591 locations->SetInAt(0, Location::RequiresFpuRegister());
1592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001593 break;
1594
1595 default:
1596 LOG(FATAL) << "Unexpected type conversion from " << input_type
1597 << " to " << result_type;
1598 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001599 break;
1600
1601 default:
1602 LOG(FATAL) << "Unexpected type conversion from " << input_type
1603 << " to " << result_type;
1604 }
1605}
1606
1607void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1608 LocationSummary* locations = conversion->GetLocations();
1609 Location out = locations->Out();
1610 Location in = locations->InAt(0);
1611 Primitive::Type result_type = conversion->GetResultType();
1612 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001613 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001614 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001615 case Primitive::kPrimByte:
1616 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001617 case Primitive::kPrimBoolean:
1618 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001619 case Primitive::kPrimShort:
1620 case Primitive::kPrimInt:
1621 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001622 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001623 if (in.IsRegister()) {
1624 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001625 } else {
1626 DCHECK(in.GetConstant()->IsIntConstant());
1627 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1628 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1629 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 break;
1631
1632 default:
1633 LOG(FATAL) << "Unexpected type conversion from " << input_type
1634 << " to " << result_type;
1635 }
1636 break;
1637
Roland Levillain01a8d712014-11-14 16:27:39 +00001638 case Primitive::kPrimShort:
1639 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001640 case Primitive::kPrimBoolean:
1641 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001642 case Primitive::kPrimByte:
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimChar:
1645 // Processing a Dex `int-to-short' instruction.
1646 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001648 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001649 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001650 } else {
1651 DCHECK(in.GetConstant()->IsIntConstant());
1652 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001654 }
1655 break;
1656
1657 default:
1658 LOG(FATAL) << "Unexpected type conversion from " << input_type
1659 << " to " << result_type;
1660 }
1661 break;
1662
Roland Levillain946e1432014-11-11 17:35:19 +00001663 case Primitive::kPrimInt:
1664 switch (input_type) {
1665 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001666 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001667 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001669 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001671 } else {
1672 DCHECK(in.IsConstant());
1673 DCHECK(in.GetConstant()->IsLongConstant());
1674 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001676 }
1677 break;
1678
Roland Levillain3f8f9362014-12-02 17:45:01 +00001679 case Primitive::kPrimFloat: {
1680 // Processing a Dex `float-to-int' instruction.
1681 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1682 Register output = out.AsRegister<Register>();
1683 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1684 Label done, nan;
1685
1686 __ movl(output, Immediate(kPrimIntMax));
1687 // temp = int-to-float(output)
1688 __ cvtsi2ss(temp, output);
1689 // if input >= temp goto done
1690 __ comiss(input, temp);
1691 __ j(kAboveEqual, &done);
1692 // if input == NaN goto nan
1693 __ j(kUnordered, &nan);
1694 // output = float-to-int-truncate(input)
1695 __ cvttss2si(output, input);
1696 __ jmp(&done);
1697 __ Bind(&nan);
1698 // output = 0
1699 __ xorl(output, output);
1700 __ Bind(&done);
1701 break;
1702 }
1703
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001704 case Primitive::kPrimDouble: {
1705 // Processing a Dex `double-to-int' instruction.
1706 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1707 Register output = out.AsRegister<Register>();
1708 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1709 Label done, nan;
1710
1711 __ movl(output, Immediate(kPrimIntMax));
1712 // temp = int-to-double(output)
1713 __ cvtsi2sd(temp, output);
1714 // if input >= temp goto done
1715 __ comisd(input, temp);
1716 __ j(kAboveEqual, &done);
1717 // if input == NaN goto nan
1718 __ j(kUnordered, &nan);
1719 // output = double-to-int-truncate(input)
1720 __ cvttsd2si(output, input);
1721 __ jmp(&done);
1722 __ Bind(&nan);
1723 // output = 0
1724 __ xorl(output, output);
1725 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001726 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001727 }
Roland Levillain946e1432014-11-11 17:35:19 +00001728
1729 default:
1730 LOG(FATAL) << "Unexpected type conversion from " << input_type
1731 << " to " << result_type;
1732 }
1733 break;
1734
Roland Levillaindff1f282014-11-05 14:15:05 +00001735 case Primitive::kPrimLong:
1736 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001737 case Primitive::kPrimBoolean:
1738 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001739 case Primitive::kPrimByte:
1740 case Primitive::kPrimShort:
1741 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001742 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001743 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1745 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001746 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001747 __ cdq();
1748 break;
1749
1750 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001751 // Processing a Dex `float-to-long' instruction.
1752 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001753 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1754 break;
1755
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001757 // Processing a Dex `double-to-long' instruction.
1758 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1759 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 break;
1761
1762 default:
1763 LOG(FATAL) << "Unexpected type conversion from " << input_type
1764 << " to " << result_type;
1765 }
1766 break;
1767
Roland Levillain981e4542014-11-14 11:47:14 +00001768 case Primitive::kPrimChar:
1769 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001770 case Primitive::kPrimBoolean:
1771 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001772 case Primitive::kPrimByte:
1773 case Primitive::kPrimShort:
1774 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001775 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1776 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001778 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001779 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001780 } else {
1781 DCHECK(in.GetConstant()->IsIntConstant());
1782 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001784 }
1785 break;
1786
1787 default:
1788 LOG(FATAL) << "Unexpected type conversion from " << input_type
1789 << " to " << result_type;
1790 }
1791 break;
1792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001794 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001795 case Primitive::kPrimBoolean:
1796 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001797 case Primitive::kPrimByte:
1798 case Primitive::kPrimShort:
1799 case Primitive::kPrimInt:
1800 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001801 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001802 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001803 break;
1804
Roland Levillain6d0e4832014-11-27 18:31:21 +00001805 case Primitive::kPrimLong: {
1806 // Processing a Dex `long-to-float' instruction.
1807 Register low = in.AsRegisterPairLow<Register>();
1808 Register high = in.AsRegisterPairHigh<Register>();
1809 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1810 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1811 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1812
1813 // Operations use doubles for precision reasons (each 32-bit
1814 // half of a long fits in the 53-bit mantissa of a double,
1815 // but not in the 24-bit mantissa of a float). This is
1816 // especially important for the low bits. The result is
1817 // eventually converted to float.
1818
1819 // low = low - 2^31 (to prevent bit 31 of `low` to be
1820 // interpreted as a sign bit)
1821 __ subl(low, Immediate(0x80000000));
1822 // temp = int-to-double(high)
1823 __ cvtsi2sd(temp, high);
1824 // temp = temp * 2^32
1825 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1826 __ mulsd(temp, constant);
1827 // result = int-to-double(low)
1828 __ cvtsi2sd(result, low);
1829 // result = result + 2^31 (restore the original value of `low`)
1830 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1831 __ addsd(result, constant);
1832 // result = result + temp
1833 __ addsd(result, temp);
1834 // result = double-to-float(result)
1835 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001836 // Restore low.
1837 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001838 break;
1839 }
1840
Roland Levillaincff13742014-11-17 14:32:17 +00001841 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001842 // Processing a Dex `double-to-float' instruction.
1843 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001844 break;
1845
1846 default:
1847 LOG(FATAL) << "Unexpected type conversion from " << input_type
1848 << " to " << result_type;
1849 };
1850 break;
1851
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001853 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001854 case Primitive::kPrimBoolean:
1855 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001856 case Primitive::kPrimByte:
1857 case Primitive::kPrimShort:
1858 case Primitive::kPrimInt:
1859 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001860 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001862 break;
1863
Roland Levillain647b9ed2014-11-27 12:06:00 +00001864 case Primitive::kPrimLong: {
1865 // Processing a Dex `long-to-double' instruction.
1866 Register low = in.AsRegisterPairLow<Register>();
1867 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1869 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1870 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001871
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872 // low = low - 2^31 (to prevent bit 31 of `low` to be
1873 // interpreted as a sign bit)
1874 __ subl(low, Immediate(0x80000000));
1875 // temp = int-to-double(high)
1876 __ cvtsi2sd(temp, high);
1877 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001878 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001879 __ mulsd(temp, constant);
1880 // result = int-to-double(low)
1881 __ cvtsi2sd(result, low);
1882 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001883 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001884 __ addsd(result, constant);
1885 // result = result + temp
1886 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001887 // Restore low.
1888 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001889 break;
1890 }
1891
Roland Levillaincff13742014-11-17 14:32:17 +00001892 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001893 // Processing a Dex `float-to-double' instruction.
1894 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001895 break;
1896
1897 default:
1898 LOG(FATAL) << "Unexpected type conversion from " << input_type
1899 << " to " << result_type;
1900 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 break;
1902
1903 default:
1904 LOG(FATAL) << "Unexpected type conversion from " << input_type
1905 << " to " << result_type;
1906 }
1907}
1908
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001909void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001912 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001913 case Primitive::kPrimInt: {
1914 locations->SetInAt(0, Location::RequiresRegister());
1915 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1917 break;
1918 }
1919
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001921 locations->SetInAt(0, Location::RequiresRegister());
1922 locations->SetInAt(1, Location::Any());
1923 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
1925 }
1926
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 case Primitive::kPrimFloat:
1928 case Primitive::kPrimDouble: {
1929 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001930 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001935 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1937 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001938 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939}
1940
1941void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1942 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 Location first = locations->InAt(0);
1944 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001945 Location out = locations->Out();
1946
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001947 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001949 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001950 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1951 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1952 } else {
1953 __ leal(out.AsRegister<Register>(), Address(
1954 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1955 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001957 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1958 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1959 __ addl(out.AsRegister<Register>(), Immediate(value));
1960 } else {
1961 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1962 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001963 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001964 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001965 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001966 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001967 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968 }
1969
1970 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001971 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001972 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1973 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001974 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1976 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001977 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001978 } else {
1979 DCHECK(second.IsConstant()) << second;
1980 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1981 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1982 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001983 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001984 break;
1985 }
1986
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 case Primitive::kPrimFloat: {
1988 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001992 }
1993
1994 case Primitive::kPrimDouble: {
1995 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 }
1998 break;
1999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002001 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002003 }
2004}
2005
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002006void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002009 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002010 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002012 locations->SetInAt(0, Location::RequiresRegister());
2013 locations->SetInAt(1, Location::Any());
2014 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002015 break;
2016 }
Calin Juravle11351682014-10-23 15:38:15 +01002017 case Primitive::kPrimFloat:
2018 case Primitive::kPrimDouble: {
2019 locations->SetInAt(0, Location::RequiresFpuRegister());
2020 locations->SetInAt(1, Location::RequiresFpuRegister());
2021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 break;
Calin Juravle11351682014-10-23 15:38:15 +01002023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002025 default:
Calin Juravle11351682014-10-23 15:38:15 +01002026 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002027 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002028}
2029
2030void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2031 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002032 Location first = locations->InAt(0);
2033 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002034 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002036 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002037 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002038 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002040 __ subl(first.AsRegister<Register>(),
2041 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002042 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002043 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002044 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046 }
2047
2048 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002049 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002050 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2051 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002052 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002053 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002054 __ sbbl(first.AsRegisterPairHigh<Register>(),
2055 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002056 } else {
2057 DCHECK(second.IsConstant()) << second;
2058 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2059 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2060 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002061 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 break;
2063 }
2064
Calin Juravle11351682014-10-23 15:38:15 +01002065 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002066 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002067 break;
Calin Juravle11351682014-10-23 15:38:15 +01002068 }
2069
2070 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002071 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002072 break;
2073 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002075 default:
Calin Juravle11351682014-10-23 15:38:15 +01002076 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002077 }
2078}
2079
Calin Juravle34bacdf2014-10-07 20:23:36 +01002080void LocationsBuilderX86::VisitMul(HMul* mul) {
2081 LocationSummary* locations =
2082 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2083 switch (mul->GetResultType()) {
2084 case Primitive::kPrimInt:
2085 locations->SetInAt(0, Location::RequiresRegister());
2086 locations->SetInAt(1, Location::Any());
2087 locations->SetOut(Location::SameAsFirstInput());
2088 break;
2089 case Primitive::kPrimLong: {
2090 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002091 locations->SetInAt(1, Location::Any());
2092 locations->SetOut(Location::SameAsFirstInput());
2093 // Needed for imul on 32bits with 64bits output.
2094 locations->AddTemp(Location::RegisterLocation(EAX));
2095 locations->AddTemp(Location::RegisterLocation(EDX));
2096 break;
2097 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002098 case Primitive::kPrimFloat:
2099 case Primitive::kPrimDouble: {
2100 locations->SetInAt(0, Location::RequiresFpuRegister());
2101 locations->SetInAt(1, Location::RequiresFpuRegister());
2102 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002103 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002104 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002105
2106 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002107 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002108 }
2109}
2110
2111void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2112 LocationSummary* locations = mul->GetLocations();
2113 Location first = locations->InAt(0);
2114 Location second = locations->InAt(1);
2115 DCHECK(first.Equals(locations->Out()));
2116
2117 switch (mul->GetResultType()) {
2118 case Primitive::kPrimInt: {
2119 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002121 } else if (second.IsConstant()) {
2122 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002124 } else {
2125 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002126 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002127 }
2128 break;
2129 }
2130
2131 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 Register in1_hi = first.AsRegisterPairHigh<Register>();
2133 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 Register eax = locations->GetTemp(0).AsRegister<Register>();
2135 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136
2137 DCHECK_EQ(EAX, eax);
2138 DCHECK_EQ(EDX, edx);
2139
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002140 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002141 // output: in1
2142 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2143 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2144 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002145 if (second.IsConstant()) {
2146 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002148 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2149 int32_t low_value = Low32Bits(value);
2150 int32_t high_value = High32Bits(value);
2151 Immediate low(low_value);
2152 Immediate high(high_value);
2153
2154 __ movl(eax, high);
2155 // eax <- in1.lo * in2.hi
2156 __ imull(eax, in1_lo);
2157 // in1.hi <- in1.hi * in2.lo
2158 __ imull(in1_hi, low);
2159 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2160 __ addl(in1_hi, eax);
2161 // move in2_lo to eax to prepare for double precision
2162 __ movl(eax, low);
2163 // edx:eax <- in1.lo * in2.lo
2164 __ mull(in1_lo);
2165 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2166 __ addl(in1_hi, edx);
2167 // in1.lo <- (in1.lo * in2.lo)[31:0];
2168 __ movl(in1_lo, eax);
2169 } else if (second.IsRegisterPair()) {
2170 Register in2_hi = second.AsRegisterPairHigh<Register>();
2171 Register in2_lo = second.AsRegisterPairLow<Register>();
2172
2173 __ movl(eax, in2_hi);
2174 // eax <- in1.lo * in2.hi
2175 __ imull(eax, in1_lo);
2176 // in1.hi <- in1.hi * in2.lo
2177 __ imull(in1_hi, in2_lo);
2178 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2179 __ addl(in1_hi, eax);
2180 // move in1_lo to eax to prepare for double precision
2181 __ movl(eax, in1_lo);
2182 // edx:eax <- in1.lo * in2.lo
2183 __ mull(in2_lo);
2184 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2185 __ addl(in1_hi, edx);
2186 // in1.lo <- (in1.lo * in2.lo)[31:0];
2187 __ movl(in1_lo, eax);
2188 } else {
2189 DCHECK(second.IsDoubleStackSlot()) << second;
2190 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2191 Address in2_lo(ESP, second.GetStackIndex());
2192
2193 __ movl(eax, in2_hi);
2194 // eax <- in1.lo * in2.hi
2195 __ imull(eax, in1_lo);
2196 // in1.hi <- in1.hi * in2.lo
2197 __ imull(in1_hi, in2_lo);
2198 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2199 __ addl(in1_hi, eax);
2200 // move in1_lo to eax to prepare for double precision
2201 __ movl(eax, in1_lo);
2202 // edx:eax <- in1.lo * in2.lo
2203 __ mull(in2_lo);
2204 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2205 __ addl(in1_hi, edx);
2206 // in1.lo <- (in1.lo * in2.lo)[31:0];
2207 __ movl(in1_lo, eax);
2208 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002209
2210 break;
2211 }
2212
Calin Juravleb5bfa962014-10-21 18:02:24 +01002213 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002215 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 }
2217
2218 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002219 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002220 break;
2221 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222
2223 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002225 }
2226}
2227
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002228void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2229 uint32_t stack_adjustment, bool is_float) {
2230 if (source.IsStackSlot()) {
2231 DCHECK(is_float);
2232 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2233 } else if (source.IsDoubleStackSlot()) {
2234 DCHECK(!is_float);
2235 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2236 } else {
2237 // Write the value to the temporary location on the stack and load to FP stack.
2238 if (is_float) {
2239 Location stack_temp = Location::StackSlot(temp_offset);
2240 codegen_->Move32(stack_temp, source);
2241 __ flds(Address(ESP, temp_offset));
2242 } else {
2243 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2244 codegen_->Move64(stack_temp, source);
2245 __ fldl(Address(ESP, temp_offset));
2246 }
2247 }
2248}
2249
2250void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2251 Primitive::Type type = rem->GetResultType();
2252 bool is_float = type == Primitive::kPrimFloat;
2253 size_t elem_size = Primitive::ComponentSize(type);
2254 LocationSummary* locations = rem->GetLocations();
2255 Location first = locations->InAt(0);
2256 Location second = locations->InAt(1);
2257 Location out = locations->Out();
2258
2259 // Create stack space for 2 elements.
2260 // TODO: enhance register allocator to ask for stack temporaries.
2261 __ subl(ESP, Immediate(2 * elem_size));
2262
2263 // Load the values to the FP stack in reverse order, using temporaries if needed.
2264 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2265 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2266
2267 // Loop doing FPREM until we stabilize.
2268 Label retry;
2269 __ Bind(&retry);
2270 __ fprem();
2271
2272 // Move FP status to AX.
2273 __ fstsw();
2274
2275 // And see if the argument reduction is complete. This is signaled by the
2276 // C2 FPU flag bit set to 0.
2277 __ andl(EAX, Immediate(kC2ConditionMask));
2278 __ j(kNotEqual, &retry);
2279
2280 // We have settled on the final value. Retrieve it into an XMM register.
2281 // Store FP top of stack to real stack.
2282 if (is_float) {
2283 __ fsts(Address(ESP, 0));
2284 } else {
2285 __ fstl(Address(ESP, 0));
2286 }
2287
2288 // Pop the 2 items from the FP stack.
2289 __ fucompp();
2290
2291 // Load the value from the stack into an XMM register.
2292 DCHECK(out.IsFpuRegister()) << out;
2293 if (is_float) {
2294 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2295 } else {
2296 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2297 }
2298
2299 // And remove the temporary stack space we allocated.
2300 __ addl(ESP, Immediate(2 * elem_size));
2301}
2302
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002303
2304void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2305 DCHECK(instruction->IsDiv() || instruction->IsRem());
2306
2307 LocationSummary* locations = instruction->GetLocations();
2308 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002309 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002310
2311 Register out_register = locations->Out().AsRegister<Register>();
2312 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002313 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002314
2315 DCHECK(imm == 1 || imm == -1);
2316
2317 if (instruction->IsRem()) {
2318 __ xorl(out_register, out_register);
2319 } else {
2320 __ movl(out_register, input_register);
2321 if (imm == -1) {
2322 __ negl(out_register);
2323 }
2324 }
2325}
2326
2327
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002328void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002329 LocationSummary* locations = instruction->GetLocations();
2330
2331 Register out_register = locations->Out().AsRegister<Register>();
2332 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002333 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002334
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002335 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002336 Register num = locations->GetTemp(0).AsRegister<Register>();
2337
2338 __ leal(num, Address(input_register, std::abs(imm) - 1));
2339 __ testl(input_register, input_register);
2340 __ cmovl(kGreaterEqual, num, input_register);
2341 int shift = CTZ(imm);
2342 __ sarl(num, Immediate(shift));
2343
2344 if (imm < 0) {
2345 __ negl(num);
2346 }
2347
2348 __ movl(out_register, num);
2349}
2350
2351void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2352 DCHECK(instruction->IsDiv() || instruction->IsRem());
2353
2354 LocationSummary* locations = instruction->GetLocations();
2355 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2356
2357 Register eax = locations->InAt(0).AsRegister<Register>();
2358 Register out = locations->Out().AsRegister<Register>();
2359 Register num;
2360 Register edx;
2361
2362 if (instruction->IsDiv()) {
2363 edx = locations->GetTemp(0).AsRegister<Register>();
2364 num = locations->GetTemp(1).AsRegister<Register>();
2365 } else {
2366 edx = locations->Out().AsRegister<Register>();
2367 num = locations->GetTemp(0).AsRegister<Register>();
2368 }
2369
2370 DCHECK_EQ(EAX, eax);
2371 DCHECK_EQ(EDX, edx);
2372 if (instruction->IsDiv()) {
2373 DCHECK_EQ(EAX, out);
2374 } else {
2375 DCHECK_EQ(EDX, out);
2376 }
2377
2378 int64_t magic;
2379 int shift;
2380 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2381
2382 Label ndiv;
2383 Label end;
2384 // If numerator is 0, the result is 0, no computation needed.
2385 __ testl(eax, eax);
2386 __ j(kNotEqual, &ndiv);
2387
2388 __ xorl(out, out);
2389 __ jmp(&end);
2390
2391 __ Bind(&ndiv);
2392
2393 // Save the numerator.
2394 __ movl(num, eax);
2395
2396 // EAX = magic
2397 __ movl(eax, Immediate(magic));
2398
2399 // EDX:EAX = magic * numerator
2400 __ imull(num);
2401
2402 if (imm > 0 && magic < 0) {
2403 // EDX += num
2404 __ addl(edx, num);
2405 } else if (imm < 0 && magic > 0) {
2406 __ subl(edx, num);
2407 }
2408
2409 // Shift if needed.
2410 if (shift != 0) {
2411 __ sarl(edx, Immediate(shift));
2412 }
2413
2414 // EDX += 1 if EDX < 0
2415 __ movl(eax, edx);
2416 __ shrl(edx, Immediate(31));
2417 __ addl(edx, eax);
2418
2419 if (instruction->IsRem()) {
2420 __ movl(eax, num);
2421 __ imull(edx, Immediate(imm));
2422 __ subl(eax, edx);
2423 __ movl(edx, eax);
2424 } else {
2425 __ movl(eax, edx);
2426 }
2427 __ Bind(&end);
2428}
2429
Calin Juravlebacfec32014-11-14 15:54:36 +00002430void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2431 DCHECK(instruction->IsDiv() || instruction->IsRem());
2432
2433 LocationSummary* locations = instruction->GetLocations();
2434 Location out = locations->Out();
2435 Location first = locations->InAt(0);
2436 Location second = locations->InAt(1);
2437 bool is_div = instruction->IsDiv();
2438
2439 switch (instruction->GetResultType()) {
2440 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 DCHECK_EQ(EAX, first.AsRegister<Register>());
2442 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002443
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002444 if (instruction->InputAt(1)->IsIntConstant()) {
2445 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002446
2447 if (imm == 0) {
2448 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2449 } else if (imm == 1 || imm == -1) {
2450 DivRemOneOrMinusOne(instruction);
2451 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002452 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002453 } else {
2454 DCHECK(imm <= -2 || imm >= 2);
2455 GenerateDivRemWithAnyConstant(instruction);
2456 }
2457 } else {
2458 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002459 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002460 is_div);
2461 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002462
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002463 Register second_reg = second.AsRegister<Register>();
2464 // 0x80000000/-1 triggers an arithmetic exception!
2465 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2466 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002467
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002468 __ cmpl(second_reg, Immediate(-1));
2469 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002470
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002471 // edx:eax <- sign-extended of eax
2472 __ cdq();
2473 // eax = quotient, edx = remainder
2474 __ idivl(second_reg);
2475 __ Bind(slow_path->GetExitLabel());
2476 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002477 break;
2478 }
2479
2480 case Primitive::kPrimLong: {
2481 InvokeRuntimeCallingConvention calling_convention;
2482 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2483 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2484 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2485 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2486 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2487 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2488
2489 if (is_div) {
2490 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2491 } else {
2492 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2493 }
2494 uint32_t dex_pc = is_div
2495 ? instruction->AsDiv()->GetDexPc()
2496 : instruction->AsRem()->GetDexPc();
2497 codegen_->RecordPcInfo(instruction, dex_pc);
2498
2499 break;
2500 }
2501
2502 default:
2503 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2504 }
2505}
2506
Calin Juravle7c4954d2014-10-28 16:57:40 +00002507void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002508 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002509 ? LocationSummary::kCall
2510 : LocationSummary::kNoCall;
2511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2512
Calin Juravle7c4954d2014-10-28 16:57:40 +00002513 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002514 case Primitive::kPrimInt: {
2515 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002516 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002517 locations->SetOut(Location::SameAsFirstInput());
2518 // Intel uses edx:eax as the dividend.
2519 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002520 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2521 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2522 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002523 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002524 locations->AddTemp(Location::RequiresRegister());
2525 }
Calin Juravled0d48522014-11-04 16:40:20 +00002526 break;
2527 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002528 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002529 InvokeRuntimeCallingConvention calling_convention;
2530 locations->SetInAt(0, Location::RegisterPairLocation(
2531 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2532 locations->SetInAt(1, Location::RegisterPairLocation(
2533 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2534 // Runtime helper puts the result in EAX, EDX.
2535 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002536 break;
2537 }
2538 case Primitive::kPrimFloat:
2539 case Primitive::kPrimDouble: {
2540 locations->SetInAt(0, Location::RequiresFpuRegister());
2541 locations->SetInAt(1, Location::RequiresFpuRegister());
2542 locations->SetOut(Location::SameAsFirstInput());
2543 break;
2544 }
2545
2546 default:
2547 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2548 }
2549}
2550
2551void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2552 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002553 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002554 Location first = locations->InAt(0);
2555 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002556
2557 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002558 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002560 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002561 break;
2562 }
2563
2564 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002565 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 break;
2568 }
2569
2570 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002571 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002572 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002573 break;
2574 }
2575
2576 default:
2577 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2578 }
2579}
2580
Calin Juravlebacfec32014-11-14 15:54:36 +00002581void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002582 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002583
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002584 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2585 ? LocationSummary::kCall
2586 : LocationSummary::kNoCall;
2587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002588
Calin Juravled2ec87d2014-12-08 14:24:46 +00002589 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002590 case Primitive::kPrimInt: {
2591 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002592 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002593 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002594 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2595 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2596 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002597 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002598 locations->AddTemp(Location::RequiresRegister());
2599 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002600 break;
2601 }
2602 case Primitive::kPrimLong: {
2603 InvokeRuntimeCallingConvention calling_convention;
2604 locations->SetInAt(0, Location::RegisterPairLocation(
2605 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2606 locations->SetInAt(1, Location::RegisterPairLocation(
2607 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2608 // Runtime helper puts the result in EAX, EDX.
2609 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2610 break;
2611 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002612 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002613 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002614 locations->SetInAt(0, Location::Any());
2615 locations->SetInAt(1, Location::Any());
2616 locations->SetOut(Location::RequiresFpuRegister());
2617 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002618 break;
2619 }
2620
2621 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002622 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 }
2624}
2625
2626void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2627 Primitive::Type type = rem->GetResultType();
2628 switch (type) {
2629 case Primitive::kPrimInt:
2630 case Primitive::kPrimLong: {
2631 GenerateDivRemIntegral(rem);
2632 break;
2633 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002634 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002635 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002636 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002637 break;
2638 }
2639 default:
2640 LOG(FATAL) << "Unexpected rem type " << type;
2641 }
2642}
2643
Calin Juravled0d48522014-11-04 16:40:20 +00002644void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2645 LocationSummary* locations =
2646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002647 switch (instruction->GetType()) {
2648 case Primitive::kPrimInt: {
2649 locations->SetInAt(0, Location::Any());
2650 break;
2651 }
2652 case Primitive::kPrimLong: {
2653 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2654 if (!instruction->IsConstant()) {
2655 locations->AddTemp(Location::RequiresRegister());
2656 }
2657 break;
2658 }
2659 default:
2660 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2661 }
Calin Juravled0d48522014-11-04 16:40:20 +00002662 if (instruction->HasUses()) {
2663 locations->SetOut(Location::SameAsFirstInput());
2664 }
2665}
2666
2667void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2668 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2669 codegen_->AddSlowPath(slow_path);
2670
2671 LocationSummary* locations = instruction->GetLocations();
2672 Location value = locations->InAt(0);
2673
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002674 switch (instruction->GetType()) {
2675 case Primitive::kPrimInt: {
2676 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002677 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002678 __ j(kEqual, slow_path->GetEntryLabel());
2679 } else if (value.IsStackSlot()) {
2680 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2681 __ j(kEqual, slow_path->GetEntryLabel());
2682 } else {
2683 DCHECK(value.IsConstant()) << value;
2684 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2685 __ jmp(slow_path->GetEntryLabel());
2686 }
2687 }
2688 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002689 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002690 case Primitive::kPrimLong: {
2691 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002693 __ movl(temp, value.AsRegisterPairLow<Register>());
2694 __ orl(temp, value.AsRegisterPairHigh<Register>());
2695 __ j(kEqual, slow_path->GetEntryLabel());
2696 } else {
2697 DCHECK(value.IsConstant()) << value;
2698 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2699 __ jmp(slow_path->GetEntryLabel());
2700 }
2701 }
2702 break;
2703 }
2704 default:
2705 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002706 }
Calin Juravled0d48522014-11-04 16:40:20 +00002707}
2708
Calin Juravle9aec02f2014-11-18 23:06:35 +00002709void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2710 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2711
2712 LocationSummary* locations =
2713 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2714
2715 switch (op->GetResultType()) {
2716 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002717 locations->SetInAt(0, Location::RequiresRegister());
2718 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002719 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2720 locations->SetOut(Location::SameAsFirstInput());
2721 break;
2722 }
2723 case Primitive::kPrimLong: {
2724 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002725 // The shift count needs to be in CL.
2726 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002727 locations->SetOut(Location::SameAsFirstInput());
2728 break;
2729 }
2730 default:
2731 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2732 }
2733}
2734
2735void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2736 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2737
2738 LocationSummary* locations = op->GetLocations();
2739 Location first = locations->InAt(0);
2740 Location second = locations->InAt(1);
2741 DCHECK(first.Equals(locations->Out()));
2742
2743 switch (op->GetResultType()) {
2744 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002745 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002746 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002748 DCHECK_EQ(ECX, second_reg);
2749 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002750 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002751 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002752 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002753 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002754 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002755 }
2756 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002757 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2758 if (op->IsShl()) {
2759 __ shll(first_reg, imm);
2760 } else if (op->IsShr()) {
2761 __ sarl(first_reg, imm);
2762 } else {
2763 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002764 }
2765 }
2766 break;
2767 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002768 case Primitive::kPrimLong: {
2769 Register second_reg = second.AsRegister<Register>();
2770 DCHECK_EQ(ECX, second_reg);
2771 if (op->IsShl()) {
2772 GenerateShlLong(first, second_reg);
2773 } else if (op->IsShr()) {
2774 GenerateShrLong(first, second_reg);
2775 } else {
2776 GenerateUShrLong(first, second_reg);
2777 }
2778 break;
2779 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002780 default:
2781 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2782 }
2783}
2784
2785void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2786 Label done;
2787 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2788 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2789 __ testl(shifter, Immediate(32));
2790 __ j(kEqual, &done);
2791 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2792 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2793 __ Bind(&done);
2794}
2795
2796void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2797 Label done;
2798 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2799 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2800 __ testl(shifter, Immediate(32));
2801 __ j(kEqual, &done);
2802 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2803 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2804 __ Bind(&done);
2805}
2806
2807void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2808 Label done;
2809 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2810 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2811 __ testl(shifter, Immediate(32));
2812 __ j(kEqual, &done);
2813 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2814 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2815 __ Bind(&done);
2816}
2817
2818void LocationsBuilderX86::VisitShl(HShl* shl) {
2819 HandleShift(shl);
2820}
2821
2822void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2823 HandleShift(shl);
2824}
2825
2826void LocationsBuilderX86::VisitShr(HShr* shr) {
2827 HandleShift(shr);
2828}
2829
2830void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2831 HandleShift(shr);
2832}
2833
2834void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2835 HandleShift(ushr);
2836}
2837
2838void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2839 HandleShift(ushr);
2840}
2841
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002842void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002843 LocationSummary* locations =
2844 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002845 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002846 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2848 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002849}
2850
2851void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2852 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002853 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002855
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002856 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002857
Nicolas Geoffray39468442014-09-02 15:17:15 +01002858 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002859 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002860}
2861
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002862void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2865 locations->SetOut(Location::RegisterLocation(EAX));
2866 InvokeRuntimeCallingConvention calling_convention;
2867 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002868 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2869 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002870}
2871
2872void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2873 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002874 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002875 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2876
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002877 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002878
2879 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2880 DCHECK(!codegen_->IsLeafMethod());
2881}
2882
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002883void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002884 LocationSummary* locations =
2885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002886 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2887 if (location.IsStackSlot()) {
2888 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2889 } else if (location.IsDoubleStackSlot()) {
2890 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002891 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002892 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002893}
2894
2895void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002896 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002897}
2898
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002899void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002900 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002901 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002904}
2905
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002906void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2907 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002908 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002909 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002910 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002911 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002912 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002913 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002914 break;
2915
2916 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002917 __ notl(out.AsRegisterPairLow<Register>());
2918 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002919 break;
2920
2921 default:
2922 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2923 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002924}
2925
David Brazdil66d126e2015-04-03 16:02:44 +01002926void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2927 LocationSummary* locations =
2928 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetOut(Location::SameAsFirstInput());
2931}
2932
2933void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002934 LocationSummary* locations = bool_not->GetLocations();
2935 Location in = locations->InAt(0);
2936 Location out = locations->Out();
2937 DCHECK(in.Equals(out));
2938 __ xorl(out.AsRegister<Register>(), Immediate(1));
2939}
2940
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002941void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002942 LocationSummary* locations =
2943 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002944 switch (compare->InputAt(0)->GetType()) {
2945 case Primitive::kPrimLong: {
2946 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002947 locations->SetInAt(1, Location::Any());
2948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2949 break;
2950 }
2951 case Primitive::kPrimFloat:
2952 case Primitive::kPrimDouble: {
2953 locations->SetInAt(0, Location::RequiresFpuRegister());
2954 locations->SetInAt(1, Location::RequiresFpuRegister());
2955 locations->SetOut(Location::RequiresRegister());
2956 break;
2957 }
2958 default:
2959 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2960 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002961}
2962
2963void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002964 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002966 Location left = locations->InAt(0);
2967 Location right = locations->InAt(1);
2968
2969 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002970 switch (compare->InputAt(0)->GetType()) {
2971 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002972 Register left_low = left.AsRegisterPairLow<Register>();
2973 Register left_high = left.AsRegisterPairHigh<Register>();
2974 int32_t val_low = 0;
2975 int32_t val_high = 0;
2976 bool right_is_const = false;
2977
2978 if (right.IsConstant()) {
2979 DCHECK(right.GetConstant()->IsLongConstant());
2980 right_is_const = true;
2981 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2982 val_low = Low32Bits(val);
2983 val_high = High32Bits(val);
2984 }
2985
Calin Juravleddb7df22014-11-25 20:56:51 +00002986 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002987 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002988 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002989 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002990 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002991 DCHECK(right_is_const) << right;
2992 if (val_high == 0) {
2993 __ testl(left_high, left_high);
2994 } else {
2995 __ cmpl(left_high, Immediate(val_high));
2996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002997 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002998 __ j(kLess, &less); // Signed compare.
2999 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003000 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003001 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003002 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003003 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003004 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003005 DCHECK(right_is_const) << right;
3006 if (val_low == 0) {
3007 __ testl(left_low, left_low);
3008 } else {
3009 __ cmpl(left_low, Immediate(val_low));
3010 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003011 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003012 break;
3013 }
3014 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003016 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3017 break;
3018 }
3019 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003021 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003022 break;
3023 }
3024 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003025 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003026 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003027 __ movl(out, Immediate(0));
3028 __ j(kEqual, &done);
3029 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3030
3031 __ Bind(&greater);
3032 __ movl(out, Immediate(1));
3033 __ jmp(&done);
3034
3035 __ Bind(&less);
3036 __ movl(out, Immediate(-1));
3037
3038 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039}
3040
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003041void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003044 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3045 locations->SetInAt(i, Location::Any());
3046 }
3047 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003048}
3049
3050void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003051 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003052 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003053}
3054
Calin Juravle52c48962014-12-16 17:02:57 +00003055void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3056 /*
3057 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3058 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3059 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3060 */
3061 switch (kind) {
3062 case MemBarrierKind::kAnyAny: {
3063 __ mfence();
3064 break;
3065 }
3066 case MemBarrierKind::kAnyStore:
3067 case MemBarrierKind::kLoadAny:
3068 case MemBarrierKind::kStoreStore: {
3069 // nop
3070 break;
3071 }
3072 default:
3073 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003074 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003075}
3076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003077
Mark Mendell09ed1a32015-03-25 08:30:06 -04003078void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3079 Register temp) {
3080 // TODO: Implement all kinds of calls:
3081 // 1) boot -> boot
3082 // 2) app -> boot
3083 // 3) app -> app
3084 //
3085 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3086 // temp = method;
3087 LoadCurrentMethod(temp);
3088 if (!invoke->IsRecursive()) {
3089 // temp = temp->dex_cache_resolved_methods_;
3090 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3091 // temp = temp[index_in_cache]
3092 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3093 // (temp + offset_of_quick_compiled_code)()
3094 __ call(Address(
3095 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3096 } else {
3097 __ call(GetFrameEntryLabel());
3098 }
3099
3100 DCHECK(!IsLeafMethod());
3101 RecordPcInfo(invoke, invoke->GetDexPc());
3102}
3103
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003104void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003105 Label is_null;
3106 __ testl(value, value);
3107 __ j(kEqual, &is_null);
3108 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3109 __ movl(temp, object);
3110 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003111 __ movb(Address(temp, card, TIMES_1, 0),
3112 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 __ Bind(&is_null);
3114}
3115
Calin Juravle52c48962014-12-16 17:02:57 +00003116void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3117 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003118 LocationSummary* locations =
3119 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003120 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003121
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003122 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3123 locations->SetOut(Location::RequiresFpuRegister());
3124 } else {
3125 // The output overlaps in case of long: we don't want the low move to overwrite
3126 // the object's location.
3127 locations->SetOut(Location::RequiresRegister(),
3128 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3129 : Location::kNoOutputOverlap);
3130 }
Calin Juravle52c48962014-12-16 17:02:57 +00003131
3132 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3133 // Long values can be loaded atomically into an XMM using movsd.
3134 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3135 // and then copy the XMM into the output 32bits at a time).
3136 locations->AddTemp(Location::RequiresFpuRegister());
3137 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003138}
3139
Calin Juravle52c48962014-12-16 17:02:57 +00003140void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3141 const FieldInfo& field_info) {
3142 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003143
Calin Juravle52c48962014-12-16 17:02:57 +00003144 LocationSummary* locations = instruction->GetLocations();
3145 Register base = locations->InAt(0).AsRegister<Register>();
3146 Location out = locations->Out();
3147 bool is_volatile = field_info.IsVolatile();
3148 Primitive::Type field_type = field_info.GetFieldType();
3149 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3150
3151 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003152 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003153 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003154 break;
3155 }
3156
3157 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003158 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159 break;
3160 }
3161
3162 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003163 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003164 break;
3165 }
3166
3167 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003168 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003169 break;
3170 }
3171
3172 case Primitive::kPrimInt:
3173 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003174 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003175 break;
3176 }
3177
3178 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003179 if (is_volatile) {
3180 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3181 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003182 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003183 __ movd(out.AsRegisterPairLow<Register>(), temp);
3184 __ psrlq(temp, Immediate(32));
3185 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3186 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003187 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003188 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003189 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003190 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3191 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003192 break;
3193 }
3194
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003195 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003196 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003197 break;
3198 }
3199
3200 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003201 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003202 break;
3203 }
3204
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003206 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003207 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003208 }
Calin Juravle52c48962014-12-16 17:02:57 +00003209
Calin Juravle77520bc2015-01-12 18:45:46 +00003210 // Longs are handled in the switch.
3211 if (field_type != Primitive::kPrimLong) {
3212 codegen_->MaybeRecordImplicitNullCheck(instruction);
3213 }
3214
Calin Juravle52c48962014-12-16 17:02:57 +00003215 if (is_volatile) {
3216 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3217 }
3218}
3219
3220void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3221 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3222
3223 LocationSummary* locations =
3224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3225 locations->SetInAt(0, Location::RequiresRegister());
3226 bool is_volatile = field_info.IsVolatile();
3227 Primitive::Type field_type = field_info.GetFieldType();
3228 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3229 || (field_type == Primitive::kPrimByte);
3230
3231 // The register allocator does not support multiple
3232 // inputs that die at entry with one in a specific register.
3233 if (is_byte_type) {
3234 // Ensure the value is in a byte register.
3235 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003236 } else if (Primitive::IsFloatingPointType(field_type)) {
3237 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003238 } else {
3239 locations->SetInAt(1, Location::RequiresRegister());
3240 }
3241 // Temporary registers for the write barrier.
3242 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3243 locations->AddTemp(Location::RequiresRegister());
3244 // Ensure the card is in a byte register.
3245 locations->AddTemp(Location::RegisterLocation(ECX));
3246 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3247 // 64bits value can be atomically written to an address with movsd and an XMM register.
3248 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3249 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3250 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3251 // isolated cases when we need this it isn't worth adding the extra complexity.
3252 locations->AddTemp(Location::RequiresFpuRegister());
3253 locations->AddTemp(Location::RequiresFpuRegister());
3254 }
3255}
3256
3257void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3258 const FieldInfo& field_info) {
3259 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3260
3261 LocationSummary* locations = instruction->GetLocations();
3262 Register base = locations->InAt(0).AsRegister<Register>();
3263 Location value = locations->InAt(1);
3264 bool is_volatile = field_info.IsVolatile();
3265 Primitive::Type field_type = field_info.GetFieldType();
3266 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3267
3268 if (is_volatile) {
3269 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3270 }
3271
3272 switch (field_type) {
3273 case Primitive::kPrimBoolean:
3274 case Primitive::kPrimByte: {
3275 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3276 break;
3277 }
3278
3279 case Primitive::kPrimShort:
3280 case Primitive::kPrimChar: {
3281 __ movw(Address(base, offset), value.AsRegister<Register>());
3282 break;
3283 }
3284
3285 case Primitive::kPrimInt:
3286 case Primitive::kPrimNot: {
3287 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003288 break;
3289 }
3290
3291 case Primitive::kPrimLong: {
3292 if (is_volatile) {
3293 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3294 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3295 __ movd(temp1, value.AsRegisterPairLow<Register>());
3296 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3297 __ punpckldq(temp1, temp2);
3298 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003299 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003300 } else {
3301 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003302 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003303 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3304 }
3305 break;
3306 }
3307
3308 case Primitive::kPrimFloat: {
3309 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3310 break;
3311 }
3312
3313 case Primitive::kPrimDouble: {
3314 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3315 break;
3316 }
3317
3318 case Primitive::kPrimVoid:
3319 LOG(FATAL) << "Unreachable type " << field_type;
3320 UNREACHABLE();
3321 }
3322
Calin Juravle77520bc2015-01-12 18:45:46 +00003323 // Longs are handled in the switch.
3324 if (field_type != Primitive::kPrimLong) {
3325 codegen_->MaybeRecordImplicitNullCheck(instruction);
3326 }
3327
3328 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3329 Register temp = locations->GetTemp(0).AsRegister<Register>();
3330 Register card = locations->GetTemp(1).AsRegister<Register>();
3331 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3332 }
3333
Calin Juravle52c48962014-12-16 17:02:57 +00003334 if (is_volatile) {
3335 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3336 }
3337}
3338
3339void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3340 HandleFieldGet(instruction, instruction->GetFieldInfo());
3341}
3342
3343void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3344 HandleFieldGet(instruction, instruction->GetFieldInfo());
3345}
3346
3347void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3348 HandleFieldSet(instruction, instruction->GetFieldInfo());
3349}
3350
3351void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3352 HandleFieldSet(instruction, instruction->GetFieldInfo());
3353}
3354
3355void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3356 HandleFieldSet(instruction, instruction->GetFieldInfo());
3357}
3358
3359void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3360 HandleFieldSet(instruction, instruction->GetFieldInfo());
3361}
3362
3363void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3364 HandleFieldGet(instruction, instruction->GetFieldInfo());
3365}
3366
3367void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3368 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003369}
3370
3371void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003374 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3375 ? Location::RequiresRegister()
3376 : Location::Any();
3377 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003378 if (instruction->HasUses()) {
3379 locations->SetOut(Location::SameAsFirstInput());
3380 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381}
3382
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003383void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003384 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3385 return;
3386 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003387 LocationSummary* locations = instruction->GetLocations();
3388 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003389
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003390 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3391 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3392}
3393
3394void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003395 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003396 codegen_->AddSlowPath(slow_path);
3397
3398 LocationSummary* locations = instruction->GetLocations();
3399 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003400
3401 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003402 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003403 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003404 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003405 } else {
3406 DCHECK(obj.IsConstant()) << obj;
3407 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3408 __ jmp(slow_path->GetEntryLabel());
3409 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003410 }
3411 __ j(kEqual, slow_path->GetEntryLabel());
3412}
3413
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003414void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3415 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3416 GenerateImplicitNullCheck(instruction);
3417 } else {
3418 GenerateExplicitNullCheck(instruction);
3419 }
3420}
3421
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003422void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003423 LocationSummary* locations =
3424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003425 locations->SetInAt(0, Location::RequiresRegister());
3426 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003427 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3428 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3429 } else {
3430 // The output overlaps in case of long: we don't want the low move to overwrite
3431 // the array's location.
3432 locations->SetOut(Location::RequiresRegister(),
3433 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3434 : Location::kNoOutputOverlap);
3435 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003436}
3437
3438void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3439 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003440 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003441 Location index = locations->InAt(1);
3442
Calin Juravle77520bc2015-01-12 18:45:46 +00003443 Primitive::Type type = instruction->GetType();
3444 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 case Primitive::kPrimBoolean: {
3446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003447 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448 if (index.IsConstant()) {
3449 __ movzxb(out, Address(obj,
3450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3451 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 }
3454 break;
3455 }
3456
3457 case Primitive::kPrimByte: {
3458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003460 if (index.IsConstant()) {
3461 __ movsxb(out, Address(obj,
3462 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3463 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003464 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003465 }
3466 break;
3467 }
3468
3469 case Primitive::kPrimShort: {
3470 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003471 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003472 if (index.IsConstant()) {
3473 __ movsxw(out, Address(obj,
3474 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3475 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 }
3478 break;
3479 }
3480
3481 case Primitive::kPrimChar: {
3482 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003484 if (index.IsConstant()) {
3485 __ movzxw(out, Address(obj,
3486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3487 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003488 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003489 }
3490 break;
3491 }
3492
3493 case Primitive::kPrimInt:
3494 case Primitive::kPrimNot: {
3495 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003497 if (index.IsConstant()) {
3498 __ movl(out, Address(obj,
3499 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3500 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003502 }
3503 break;
3504 }
3505
3506 case Primitive::kPrimLong: {
3507 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003508 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003509 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510 if (index.IsConstant()) {
3511 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003512 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003514 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003515 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003516 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003519 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 }
3522 break;
3523 }
3524
Mark Mendell7c8d0092015-01-26 11:21:33 -05003525 case Primitive::kPrimFloat: {
3526 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3527 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3528 if (index.IsConstant()) {
3529 __ movss(out, Address(obj,
3530 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3531 } else {
3532 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3533 }
3534 break;
3535 }
3536
3537 case Primitive::kPrimDouble: {
3538 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3539 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3540 if (index.IsConstant()) {
3541 __ movsd(out, Address(obj,
3542 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3543 } else {
3544 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3545 }
3546 break;
3547 }
3548
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003549 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003550 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003551 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003552 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003553
3554 if (type != Primitive::kPrimLong) {
3555 codegen_->MaybeRecordImplicitNullCheck(instruction);
3556 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557}
3558
3559void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003560 // This location builder might end up asking to up to four registers, which is
3561 // not currently possible for baseline. The situation in which we need four
3562 // registers cannot be met by baseline though, because it has not run any
3563 // optimization.
3564
Nicolas Geoffray39468442014-09-02 15:17:15 +01003565 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003566 bool needs_write_barrier =
3567 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3568
Mark Mendell5f874182015-03-04 15:42:45 -05003569 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003570
Nicolas Geoffray39468442014-09-02 15:17:15 +01003571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3572 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003573 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003574
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003575 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003577 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3578 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3579 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003580 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003581 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3582 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003583 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003584 // In case of a byte operation, the register allocator does not support multiple
3585 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003588 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003589 // Ensure the value is in a byte register.
3590 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003591 } else if (Primitive::IsFloatingPointType(value_type)) {
3592 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003594 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003595 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003596 // Temporary registers for the write barrier.
3597 if (needs_write_barrier) {
3598 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003599 // Ensure the card is in a byte register.
3600 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003601 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603}
3604
3605void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3606 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003609 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003610 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003611 bool needs_runtime_call = locations->WillCall();
3612 bool needs_write_barrier =
3613 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614
3615 switch (value_type) {
3616 case Primitive::kPrimBoolean:
3617 case Primitive::kPrimByte: {
3618 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619 if (index.IsConstant()) {
3620 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003621 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003622 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003623 } else {
3624 __ movb(Address(obj, offset),
3625 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3626 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003627 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003628 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003629 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003630 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003631 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003633 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3634 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 break;
3638 }
3639
3640 case Primitive::kPrimShort:
3641 case Primitive::kPrimChar: {
3642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 if (index.IsConstant()) {
3644 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003645 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003647 } else {
3648 __ movw(Address(obj, offset),
3649 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3650 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003651 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003652 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3654 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003655 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003657 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3658 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003660 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 break;
3662 }
3663
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003664 case Primitive::kPrimInt:
3665 case Primitive::kPrimNot: {
3666 if (!needs_runtime_call) {
3667 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3668 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003669 size_t offset =
3670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003671 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003673 } else {
3674 DCHECK(value.IsConstant()) << value;
3675 __ movl(Address(obj, offset),
3676 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3677 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003678 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003679 DCHECK(index.IsRegister()) << index;
3680 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003681 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3682 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003683 } else {
3684 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003686 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3687 }
3688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003689 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003690
3691 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 Register temp = locations->GetTemp(0).AsRegister<Register>();
3693 Register card = locations->GetTemp(1).AsRegister<Register>();
3694 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003696 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003697 DCHECK_EQ(value_type, Primitive::kPrimNot);
3698 DCHECK(!codegen_->IsLeafMethod());
3699 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3700 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003701 }
3702 break;
3703 }
3704
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003705 case Primitive::kPrimLong: {
3706 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 if (index.IsConstant()) {
3708 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003709 if (value.IsRegisterPair()) {
3710 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003712 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003713 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003714 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003715 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3716 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003717 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003718 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3719 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003720 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003721 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003723 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003724 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003725 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003726 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003727 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003728 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003729 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003731 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003732 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003734 Immediate(High32Bits(val)));
3735 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 }
3737 break;
3738 }
3739
Mark Mendell7c8d0092015-01-26 11:21:33 -05003740 case Primitive::kPrimFloat: {
3741 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3742 DCHECK(value.IsFpuRegister());
3743 if (index.IsConstant()) {
3744 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3745 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3746 } else {
3747 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3748 value.AsFpuRegister<XmmRegister>());
3749 }
3750 break;
3751 }
3752
3753 case Primitive::kPrimDouble: {
3754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3755 DCHECK(value.IsFpuRegister());
3756 if (index.IsConstant()) {
3757 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3758 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3759 } else {
3760 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3761 value.AsFpuRegister<XmmRegister>());
3762 }
3763 break;
3764 }
3765
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766 case Primitive::kPrimVoid:
3767 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003768 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769 }
3770}
3771
3772void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003774 locations->SetInAt(0, Location::RequiresRegister());
3775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003776 instruction->SetLocations(locations);
3777}
3778
3779void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3780 LocationSummary* locations = instruction->GetLocations();
3781 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 Register obj = locations->InAt(0).AsRegister<Register>();
3783 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786}
3787
3788void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003789 LocationSummary* locations =
3790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003791 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003793 if (instruction->HasUses()) {
3794 locations->SetOut(Location::SameAsFirstInput());
3795 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796}
3797
3798void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3799 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003800 Location index_loc = locations->InAt(0);
3801 Location length_loc = locations->InAt(1);
3802 SlowPathCodeX86* slow_path =
3803 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 codegen_->AddSlowPath(slow_path);
3805
Mark Mendellf60c90b2015-03-04 15:12:59 -05003806 Register length = length_loc.AsRegister<Register>();
3807 if (index_loc.IsConstant()) {
3808 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3809 __ cmpl(length, Immediate(value));
3810 } else {
3811 __ cmpl(length, index_loc.AsRegister<Register>());
3812 }
3813 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814}
3815
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003816void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3817 temp->SetLocations(nullptr);
3818}
3819
3820void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3821 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003822 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003823}
3824
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003825void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003826 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003827 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003828}
3829
3830void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003831 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3832}
3833
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003834void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3836}
3837
3838void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003839 HBasicBlock* block = instruction->GetBlock();
3840 if (block->GetLoopInformation() != nullptr) {
3841 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3842 // The back edge will generate the suspend check.
3843 return;
3844 }
3845 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3846 // The goto will generate the suspend check.
3847 return;
3848 }
3849 GenerateSuspendCheck(instruction, nullptr);
3850}
3851
3852void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3853 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003854 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003855 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003856 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003857 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003858 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003859 if (successor == nullptr) {
3860 __ j(kNotEqual, slow_path->GetEntryLabel());
3861 __ Bind(slow_path->GetReturnLabel());
3862 } else {
3863 __ j(kEqual, codegen_->GetLabelOf(successor));
3864 __ jmp(slow_path->GetEntryLabel());
3865 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003866}
3867
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003868X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3869 return codegen_->GetAssembler();
3870}
3871
Mark Mendell7c8d0092015-01-26 11:21:33 -05003872void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003873 ScratchRegisterScope ensure_scratch(
3874 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3875 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3876 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3877 __ movl(temp_reg, Address(ESP, src + stack_offset));
3878 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003879}
3880
3881void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003882 ScratchRegisterScope ensure_scratch(
3883 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3884 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3885 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3886 __ movl(temp_reg, Address(ESP, src + stack_offset));
3887 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3888 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3889 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003890}
3891
3892void ParallelMoveResolverX86::EmitMove(size_t index) {
3893 MoveOperands* move = moves_.Get(index);
3894 Location source = move->GetSource();
3895 Location destination = move->GetDestination();
3896
3897 if (source.IsRegister()) {
3898 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003899 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003900 } else {
3901 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003903 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003904 } else if (source.IsFpuRegister()) {
3905 if (destination.IsFpuRegister()) {
3906 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3907 } else if (destination.IsStackSlot()) {
3908 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3909 } else {
3910 DCHECK(destination.IsDoubleStackSlot());
3911 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3912 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003913 } else if (source.IsStackSlot()) {
3914 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003915 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003916 } else if (destination.IsFpuRegister()) {
3917 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003918 } else {
3919 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003920 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3921 }
3922 } else if (source.IsDoubleStackSlot()) {
3923 if (destination.IsFpuRegister()) {
3924 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3925 } else {
3926 DCHECK(destination.IsDoubleStackSlot()) << destination;
3927 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003928 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003929 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003930 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003931 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003932 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003933 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003934 if (value == 0) {
3935 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3936 } else {
3937 __ movl(destination.AsRegister<Register>(), Immediate(value));
3938 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003939 } else {
3940 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003941 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003942 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003943 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003944 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003945 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003946 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003947 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003948 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3949 if (value == 0) {
3950 // Easy handling of 0.0.
3951 __ xorps(dest, dest);
3952 } else {
3953 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003954 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3955 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3956 __ movl(temp, Immediate(value));
3957 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003958 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003959 } else {
3960 DCHECK(destination.IsStackSlot()) << destination;
3961 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3962 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003963 } else if (constant->IsLongConstant()) {
3964 int64_t value = constant->AsLongConstant()->GetValue();
3965 int32_t low_value = Low32Bits(value);
3966 int32_t high_value = High32Bits(value);
3967 Immediate low(low_value);
3968 Immediate high(high_value);
3969 if (destination.IsDoubleStackSlot()) {
3970 __ movl(Address(ESP, destination.GetStackIndex()), low);
3971 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3972 } else {
3973 __ movl(destination.AsRegisterPairLow<Register>(), low);
3974 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3975 }
3976 } else {
3977 DCHECK(constant->IsDoubleConstant());
3978 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003979 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003980 int32_t low_value = Low32Bits(value);
3981 int32_t high_value = High32Bits(value);
3982 Immediate low(low_value);
3983 Immediate high(high_value);
3984 if (destination.IsFpuRegister()) {
3985 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3986 if (value == 0) {
3987 // Easy handling of 0.0.
3988 __ xorpd(dest, dest);
3989 } else {
3990 __ pushl(high);
3991 __ pushl(low);
3992 __ movsd(dest, Address(ESP, 0));
3993 __ addl(ESP, Immediate(8));
3994 }
3995 } else {
3996 DCHECK(destination.IsDoubleStackSlot()) << destination;
3997 __ movl(Address(ESP, destination.GetStackIndex()), low);
3998 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3999 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004000 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004001 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004002 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004003 }
4004}
4005
Mark Mendella5c19ce2015-04-01 12:51:05 -04004006void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004007 Register suggested_scratch = reg == EAX ? EBX : EAX;
4008 ScratchRegisterScope ensure_scratch(
4009 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4010
4011 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4012 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4013 __ movl(Address(ESP, mem + stack_offset), reg);
4014 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004015}
4016
Mark Mendell7c8d0092015-01-26 11:21:33 -05004017void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004018 ScratchRegisterScope ensure_scratch(
4019 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4020
4021 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4022 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4023 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4024 __ movss(Address(ESP, mem + stack_offset), reg);
4025 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004026}
4027
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004028void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004029 ScratchRegisterScope ensure_scratch1(
4030 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004031
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004032 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4033 ScratchRegisterScope ensure_scratch2(
4034 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004035
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004036 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4037 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4038 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4039 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4040 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4041 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004042}
4043
4044void ParallelMoveResolverX86::EmitSwap(size_t index) {
4045 MoveOperands* move = moves_.Get(index);
4046 Location source = move->GetSource();
4047 Location destination = move->GetDestination();
4048
4049 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004050 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004051 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004053 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004054 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004055 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4056 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004057 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4058 // Use XOR Swap algorithm to avoid a temporary.
4059 DCHECK_NE(source.reg(), destination.reg());
4060 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4061 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4062 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4063 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4064 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4065 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4066 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004067 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4068 // Take advantage of the 16 bytes in the XMM register.
4069 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4070 Address stack(ESP, destination.GetStackIndex());
4071 // Load the double into the high doubleword.
4072 __ movhpd(reg, stack);
4073
4074 // Store the low double into the destination.
4075 __ movsd(stack, reg);
4076
4077 // Move the high double to the low double.
4078 __ psrldq(reg, Immediate(8));
4079 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4080 // Take advantage of the 16 bytes in the XMM register.
4081 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4082 Address stack(ESP, source.GetStackIndex());
4083 // Load the double into the high doubleword.
4084 __ movhpd(reg, stack);
4085
4086 // Store the low double into the destination.
4087 __ movsd(stack, reg);
4088
4089 // Move the high double to the low double.
4090 __ psrldq(reg, Immediate(8));
4091 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4092 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4093 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004094 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004095 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004096 }
4097}
4098
4099void ParallelMoveResolverX86::SpillScratch(int reg) {
4100 __ pushl(static_cast<Register>(reg));
4101}
4102
4103void ParallelMoveResolverX86::RestoreScratch(int reg) {
4104 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004105}
4106
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004107void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004108 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4109 ? LocationSummary::kCallOnSlowPath
4110 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004111 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004112 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113 locations->SetOut(Location::RequiresRegister());
4114}
4115
4116void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004117 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004118 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004119 DCHECK(!cls->CanCallRuntime());
4120 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004121 codegen_->LoadCurrentMethod(out);
4122 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4123 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004124 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004125 codegen_->LoadCurrentMethod(out);
4126 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4127 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004128
4129 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4130 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4131 codegen_->AddSlowPath(slow_path);
4132 __ testl(out, out);
4133 __ j(kEqual, slow_path->GetEntryLabel());
4134 if (cls->MustGenerateClinitCheck()) {
4135 GenerateClassInitializationCheck(slow_path, out);
4136 } else {
4137 __ Bind(slow_path->GetExitLabel());
4138 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004139 }
4140}
4141
4142void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4143 LocationSummary* locations =
4144 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4145 locations->SetInAt(0, Location::RequiresRegister());
4146 if (check->HasUses()) {
4147 locations->SetOut(Location::SameAsFirstInput());
4148 }
4149}
4150
4151void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004152 // We assume the class to not be null.
4153 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4154 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004155 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004156 GenerateClassInitializationCheck(slow_path,
4157 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004158}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004159
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004160void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4161 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004162 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4163 Immediate(mirror::Class::kStatusInitialized));
4164 __ j(kLess, slow_path->GetEntryLabel());
4165 __ Bind(slow_path->GetExitLabel());
4166 // No need for memory fence, thanks to the X86 memory model.
4167}
4168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004169void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4170 LocationSummary* locations =
4171 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4172 locations->SetOut(Location::RequiresRegister());
4173}
4174
4175void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4176 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4177 codegen_->AddSlowPath(slow_path);
4178
Roland Levillain271ab9c2014-11-27 15:23:57 +00004179 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004180 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004181 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4182 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004183 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4184 __ testl(out, out);
4185 __ j(kEqual, slow_path->GetEntryLabel());
4186 __ Bind(slow_path->GetExitLabel());
4187}
4188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004189void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4190 LocationSummary* locations =
4191 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4192 locations->SetOut(Location::RequiresRegister());
4193}
4194
4195void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4196 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004197 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004198 __ fs()->movl(address, Immediate(0));
4199}
4200
4201void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4202 LocationSummary* locations =
4203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4204 InvokeRuntimeCallingConvention calling_convention;
4205 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4206}
4207
4208void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4209 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4210 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4211}
4212
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004213void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004214 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4215 ? LocationSummary::kNoCall
4216 : LocationSummary::kCallOnSlowPath;
4217 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4218 locations->SetInAt(0, Location::RequiresRegister());
4219 locations->SetInAt(1, Location::Any());
4220 locations->SetOut(Location::RequiresRegister());
4221}
4222
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004223void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004224 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004225 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004226 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004228 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4229 Label done, zero;
4230 SlowPathCodeX86* slow_path = nullptr;
4231
4232 // Return 0 if `obj` is null.
4233 // TODO: avoid this check if we know obj is not null.
4234 __ testl(obj, obj);
4235 __ j(kEqual, &zero);
4236 __ movl(out, Address(obj, class_offset));
4237 // Compare the class of `obj` with `cls`.
4238 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004239 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004240 } else {
4241 DCHECK(cls.IsStackSlot()) << cls;
4242 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4243 }
4244
4245 if (instruction->IsClassFinal()) {
4246 // Classes must be equal for the instanceof to succeed.
4247 __ j(kNotEqual, &zero);
4248 __ movl(out, Immediate(1));
4249 __ jmp(&done);
4250 } else {
4251 // If the classes are not equal, we go into a slow path.
4252 DCHECK(locations->OnlyCallsOnSlowPath());
4253 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004254 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004255 codegen_->AddSlowPath(slow_path);
4256 __ j(kNotEqual, slow_path->GetEntryLabel());
4257 __ movl(out, Immediate(1));
4258 __ jmp(&done);
4259 }
4260 __ Bind(&zero);
4261 __ movl(out, Immediate(0));
4262 if (slow_path != nullptr) {
4263 __ Bind(slow_path->GetExitLabel());
4264 }
4265 __ Bind(&done);
4266}
4267
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004268void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4270 instruction, LocationSummary::kCallOnSlowPath);
4271 locations->SetInAt(0, Location::RequiresRegister());
4272 locations->SetInAt(1, Location::Any());
4273 locations->AddTemp(Location::RequiresRegister());
4274}
4275
4276void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4277 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004279 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004280 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4282 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4283 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4284 codegen_->AddSlowPath(slow_path);
4285
4286 // TODO: avoid this check if we know obj is not null.
4287 __ testl(obj, obj);
4288 __ j(kEqual, slow_path->GetExitLabel());
4289 __ movl(temp, Address(obj, class_offset));
4290
4291 // Compare the class of `obj` with `cls`.
4292 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004293 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004294 } else {
4295 DCHECK(cls.IsStackSlot()) << cls;
4296 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4297 }
4298
4299 __ j(kNotEqual, slow_path->GetEntryLabel());
4300 __ Bind(slow_path->GetExitLabel());
4301}
4302
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004303void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4304 LocationSummary* locations =
4305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4306 InvokeRuntimeCallingConvention calling_convention;
4307 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4308}
4309
4310void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4311 __ fs()->call(Address::Absolute(instruction->IsEnter()
4312 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4313 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4314 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4315}
4316
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004317void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4318void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4319void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4320
4321void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4322 LocationSummary* locations =
4323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4324 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4325 || instruction->GetResultType() == Primitive::kPrimLong);
4326 locations->SetInAt(0, Location::RequiresRegister());
4327 locations->SetInAt(1, Location::Any());
4328 locations->SetOut(Location::SameAsFirstInput());
4329}
4330
4331void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4332 HandleBitwiseOperation(instruction);
4333}
4334
4335void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4336 HandleBitwiseOperation(instruction);
4337}
4338
4339void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4340 HandleBitwiseOperation(instruction);
4341}
4342
4343void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4344 LocationSummary* locations = instruction->GetLocations();
4345 Location first = locations->InAt(0);
4346 Location second = locations->InAt(1);
4347 DCHECK(first.Equals(locations->Out()));
4348
4349 if (instruction->GetResultType() == Primitive::kPrimInt) {
4350 if (second.IsRegister()) {
4351 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004353 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004355 } else {
4356 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004357 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004358 }
4359 } else if (second.IsConstant()) {
4360 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004361 __ andl(first.AsRegister<Register>(),
4362 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004363 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004364 __ orl(first.AsRegister<Register>(),
4365 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004366 } else {
4367 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004368 __ xorl(first.AsRegister<Register>(),
4369 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004370 }
4371 } else {
4372 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004373 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004374 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004375 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004376 } else {
4377 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004378 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004379 }
4380 }
4381 } else {
4382 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4383 if (second.IsRegisterPair()) {
4384 if (instruction->IsAnd()) {
4385 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4386 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4387 } else if (instruction->IsOr()) {
4388 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4389 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4390 } else {
4391 DCHECK(instruction->IsXor());
4392 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4393 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4394 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004396 if (instruction->IsAnd()) {
4397 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4398 __ andl(first.AsRegisterPairHigh<Register>(),
4399 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4400 } else if (instruction->IsOr()) {
4401 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4402 __ orl(first.AsRegisterPairHigh<Register>(),
4403 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4404 } else {
4405 DCHECK(instruction->IsXor());
4406 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4407 __ xorl(first.AsRegisterPairHigh<Register>(),
4408 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4409 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004410 } else {
4411 DCHECK(second.IsConstant()) << second;
4412 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004413 int32_t low_value = Low32Bits(value);
4414 int32_t high_value = High32Bits(value);
4415 Immediate low(low_value);
4416 Immediate high(high_value);
4417 Register first_low = first.AsRegisterPairLow<Register>();
4418 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004419 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004420 if (low_value == 0) {
4421 __ xorl(first_low, first_low);
4422 } else if (low_value != -1) {
4423 __ andl(first_low, low);
4424 }
4425 if (high_value == 0) {
4426 __ xorl(first_high, first_high);
4427 } else if (high_value != -1) {
4428 __ andl(first_high, high);
4429 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004430 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004431 if (low_value != 0) {
4432 __ orl(first_low, low);
4433 }
4434 if (high_value != 0) {
4435 __ orl(first_high, high);
4436 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004437 } else {
4438 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004439 if (low_value != 0) {
4440 __ xorl(first_low, low);
4441 }
4442 if (high_value != 0) {
4443 __ xorl(first_high, high);
4444 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004445 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004446 }
4447 }
4448}
4449
Calin Juravleb1498f62015-02-16 13:13:29 +00004450void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4451 // Nothing to do, this should be removed during prepare for register allocator.
4452 UNUSED(instruction);
4453 LOG(FATAL) << "Unreachable";
4454}
4455
4456void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4457 // Nothing to do, this should be removed during prepare for register allocator.
4458 UNUSED(instruction);
4459 LOG(FATAL) << "Unreachable";
4460}
4461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004462} // namespace x86
4463} // namespace art