blob: ad71d3abf09fc0d27fc196c34aed9f4f3aa84445 [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()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100880 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500881 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
Calin Juravle27df7582015-04-17 19:12:31 +01001123void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1124 memory_barrier->SetLocations(nullptr);
1125}
1126
1127void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1128 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1129}
1130
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001132 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133}
1134
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001136 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001138}
1139
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001141 LocationSummary* locations =
1142 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 switch (ret->InputAt(0)->GetType()) {
1144 case Primitive::kPrimBoolean:
1145 case Primitive::kPrimByte:
1146 case Primitive::kPrimChar:
1147 case Primitive::kPrimShort:
1148 case Primitive::kPrimInt:
1149 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 break;
1152
1153 case Primitive::kPrimLong:
1154 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 break;
1157
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 case Primitive::kPrimFloat:
1159 case Primitive::kPrimDouble:
1160 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001162 break;
1163
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167}
1168
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001169void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 if (kIsDebugBuild) {
1171 switch (ret->InputAt(0)->GetType()) {
1172 case Primitive::kPrimBoolean:
1173 case Primitive::kPrimByte:
1174 case Primitive::kPrimChar:
1175 case Primitive::kPrimShort:
1176 case Primitive::kPrimInt:
1177 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001178 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 break;
1180
1181 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1183 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 break;
1185
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 case Primitive::kPrimFloat:
1187 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 break;
1190
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 }
1194 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001199 // Explicit clinit checks triggered by static invokes must have been
1200 // pruned by art::PrepareForRegisterAllocation.
1201 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1202
Mark Mendellfb8d2792015-03-31 22:16:59 -04001203 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001204 if (intrinsic.TryDispatch(invoke)) {
1205 return;
1206 }
1207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001208 HandleInvoke(invoke);
1209}
1210
Mark Mendell09ed1a32015-03-25 08:30:06 -04001211static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1212 if (invoke->GetLocations()->Intrinsified()) {
1213 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1214 intrinsic.Dispatch(invoke);
1215 return true;
1216 }
1217 return false;
1218}
1219
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001220void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001221 // Explicit clinit checks triggered by static invokes must have been
1222 // pruned by art::PrepareForRegisterAllocation.
1223 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1224
Mark Mendell09ed1a32015-03-25 08:30:06 -04001225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1226 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001227 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001228
Mark Mendell09ed1a32015-03-25 08:30:06 -04001229 codegen_->GenerateStaticOrDirectCall(
1230 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001232}
1233
1234void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1235 HandleInvoke(invoke);
1236}
1237
1238void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001239 LocationSummary* locations =
1240 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001241 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001242
1243 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001245 HInstruction* input = invoke->InputAt(i);
1246 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1247 }
1248
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 switch (invoke->GetType()) {
1250 case Primitive::kPrimBoolean:
1251 case Primitive::kPrimByte:
1252 case Primitive::kPrimChar:
1253 case Primitive::kPrimShort:
1254 case Primitive::kPrimInt:
1255 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001256 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 break;
1258
1259 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001260 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001261 break;
1262
1263 case Primitive::kPrimVoid:
1264 break;
1265
1266 case Primitive::kPrimDouble:
1267 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001268 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 break;
1270 }
1271
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001272 invoke->SetLocations(locations);
1273}
1274
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001275void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001276 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1278 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1279 LocationSummary* locations = invoke->GetLocations();
1280 Location receiver = locations->InAt(0);
1281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1282 // temp = object->GetClass();
1283 if (receiver.IsStackSlot()) {
1284 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1285 __ movl(temp, Address(temp, class_offset));
1286 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001287 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001289 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 // temp = temp->GetMethodAt(method_offset);
1291 __ movl(temp, Address(temp, method_offset));
1292 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001293 __ call(Address(
1294 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001295
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001296 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001297 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001298}
1299
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1301 HandleInvoke(invoke);
1302 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001303 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001304}
1305
1306void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1307 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001309 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1310 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1311 LocationSummary* locations = invoke->GetLocations();
1312 Location receiver = locations->InAt(0);
1313 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1314
1315 // Set the hidden argument.
1316 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318
1319 // temp = object->GetClass();
1320 if (receiver.IsStackSlot()) {
1321 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1322 __ movl(temp, Address(temp, class_offset));
1323 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001325 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001326 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 // temp = temp->GetImtEntryAt(method_offset);
1328 __ movl(temp, Address(temp, method_offset));
1329 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001330 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001331 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001332
1333 DCHECK(!codegen_->IsLeafMethod());
1334 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1335}
1336
Roland Levillain88cb1752014-10-20 16:36:47 +01001337void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1338 LocationSummary* locations =
1339 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1340 switch (neg->GetResultType()) {
1341 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001342 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001343 locations->SetInAt(0, Location::RequiresRegister());
1344 locations->SetOut(Location::SameAsFirstInput());
1345 break;
1346
Roland Levillain88cb1752014-10-20 16:36:47 +01001347 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001348 locations->SetInAt(0, Location::RequiresFpuRegister());
1349 locations->SetOut(Location::SameAsFirstInput());
1350 locations->AddTemp(Location::RequiresRegister());
1351 locations->AddTemp(Location::RequiresFpuRegister());
1352 break;
1353
Roland Levillain88cb1752014-10-20 16:36:47 +01001354 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001355 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001356 locations->SetOut(Location::SameAsFirstInput());
1357 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001358 break;
1359
1360 default:
1361 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1362 }
1363}
1364
1365void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1366 LocationSummary* locations = neg->GetLocations();
1367 Location out = locations->Out();
1368 Location in = locations->InAt(0);
1369 switch (neg->GetResultType()) {
1370 case Primitive::kPrimInt:
1371 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001372 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001373 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001374 break;
1375
1376 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001377 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001378 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001379 __ negl(out.AsRegisterPairLow<Register>());
1380 // Negation is similar to subtraction from zero. The least
1381 // significant byte triggers a borrow when it is different from
1382 // zero; to take it into account, add 1 to the most significant
1383 // byte if the carry flag (CF) is set to 1 after the first NEGL
1384 // operation.
1385 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1386 __ negl(out.AsRegisterPairHigh<Register>());
1387 break;
1388
Roland Levillain5368c212014-11-27 15:03:41 +00001389 case Primitive::kPrimFloat: {
1390 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001391 Register constant = locations->GetTemp(0).AsRegister<Register>();
1392 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001393 // Implement float negation with an exclusive or with value
1394 // 0x80000000 (mask for bit 31, representing the sign of a
1395 // single-precision floating-point number).
1396 __ movl(constant, Immediate(INT32_C(0x80000000)));
1397 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001398 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001399 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001400 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001401
Roland Levillain5368c212014-11-27 15:03:41 +00001402 case Primitive::kPrimDouble: {
1403 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001404 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001405 // Implement double negation with an exclusive or with value
1406 // 0x8000000000000000 (mask for bit 63, representing the sign of
1407 // a double-precision floating-point number).
1408 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001409 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001410 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001411 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001412
1413 default:
1414 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1415 }
1416}
1417
Roland Levillaindff1f282014-11-05 14:15:05 +00001418void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001419 Primitive::Type result_type = conversion->GetResultType();
1420 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001421 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001422
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001423 // The float-to-long and double-to-long type conversions rely on a
1424 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001425 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001426 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1427 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001428 ? LocationSummary::kCall
1429 : LocationSummary::kNoCall;
1430 LocationSummary* locations =
1431 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1432
David Brazdilb2bd1c52015-03-25 11:17:37 +00001433 // The Java language does not allow treating boolean as an integral type but
1434 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001435
Roland Levillaindff1f282014-11-05 14:15:05 +00001436 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001437 case Primitive::kPrimByte:
1438 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001439 case Primitive::kPrimBoolean:
1440 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001441 case Primitive::kPrimShort:
1442 case Primitive::kPrimInt:
1443 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001444 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001445 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1446 // Make the output overlap to please the register allocator. This greatly simplifies
1447 // the validation of the linear scan implementation
1448 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001449 break;
1450
1451 default:
1452 LOG(FATAL) << "Unexpected type conversion from " << input_type
1453 << " to " << result_type;
1454 }
1455 break;
1456
Roland Levillain01a8d712014-11-14 16:27:39 +00001457 case Primitive::kPrimShort:
1458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001459 case Primitive::kPrimBoolean:
1460 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001461 case Primitive::kPrimByte:
1462 case Primitive::kPrimInt:
1463 case Primitive::kPrimChar:
1464 // Processing a Dex `int-to-short' instruction.
1465 locations->SetInAt(0, Location::Any());
1466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1467 break;
1468
1469 default:
1470 LOG(FATAL) << "Unexpected type conversion from " << input_type
1471 << " to " << result_type;
1472 }
1473 break;
1474
Roland Levillain946e1432014-11-11 17:35:19 +00001475 case Primitive::kPrimInt:
1476 switch (input_type) {
1477 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001478 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001479 locations->SetInAt(0, Location::Any());
1480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1481 break;
1482
1483 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001484 // Processing a Dex `float-to-int' instruction.
1485 locations->SetInAt(0, Location::RequiresFpuRegister());
1486 locations->SetOut(Location::RequiresRegister());
1487 locations->AddTemp(Location::RequiresFpuRegister());
1488 break;
1489
Roland Levillain946e1432014-11-11 17:35:19 +00001490 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001491 // Processing a Dex `double-to-int' instruction.
1492 locations->SetInAt(0, Location::RequiresFpuRegister());
1493 locations->SetOut(Location::RequiresRegister());
1494 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001495 break;
1496
1497 default:
1498 LOG(FATAL) << "Unexpected type conversion from " << input_type
1499 << " to " << result_type;
1500 }
1501 break;
1502
Roland Levillaindff1f282014-11-05 14:15:05 +00001503 case Primitive::kPrimLong:
1504 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001505 case Primitive::kPrimBoolean:
1506 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001507 case Primitive::kPrimByte:
1508 case Primitive::kPrimShort:
1509 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001510 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001511 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001512 locations->SetInAt(0, Location::RegisterLocation(EAX));
1513 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1514 break;
1515
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001516 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001517 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001519 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1521 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1522
Vladimir Marko949c91f2015-01-27 10:48:44 +00001523 // The runtime helper puts the result in EAX, EDX.
1524 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001526 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001527
1528 default:
1529 LOG(FATAL) << "Unexpected type conversion from " << input_type
1530 << " to " << result_type;
1531 }
1532 break;
1533
Roland Levillain981e4542014-11-14 11:47:14 +00001534 case Primitive::kPrimChar:
1535 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001536 case Primitive::kPrimBoolean:
1537 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001538 case Primitive::kPrimByte:
1539 case Primitive::kPrimShort:
1540 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001541 // Processing a Dex `int-to-char' instruction.
1542 locations->SetInAt(0, Location::Any());
1543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1544 break;
1545
1546 default:
1547 LOG(FATAL) << "Unexpected type conversion from " << input_type
1548 << " to " << result_type;
1549 }
1550 break;
1551
Roland Levillaindff1f282014-11-05 14:15:05 +00001552 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001553 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001554 case Primitive::kPrimBoolean:
1555 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001556 case Primitive::kPrimByte:
1557 case Primitive::kPrimShort:
1558 case Primitive::kPrimInt:
1559 case Primitive::kPrimChar:
1560 // Processing a Dex `int-to-float' instruction.
1561 locations->SetInAt(0, Location::RequiresRegister());
1562 locations->SetOut(Location::RequiresFpuRegister());
1563 break;
1564
1565 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001566 // Processing a Dex `long-to-float' instruction.
1567 locations->SetInAt(0, Location::RequiresRegister());
1568 locations->SetOut(Location::RequiresFpuRegister());
1569 locations->AddTemp(Location::RequiresFpuRegister());
1570 locations->AddTemp(Location::RequiresFpuRegister());
1571 break;
1572
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001574 // Processing a Dex `double-to-float' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 };
1583 break;
1584
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001587 case Primitive::kPrimBoolean:
1588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimByte:
1590 case Primitive::kPrimShort:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-double' instruction.
1594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetOut(Location::RequiresFpuRegister());
1596 break;
1597
1598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 // Processing a Dex `long-to-double' instruction.
1600 locations->SetInAt(0, Location::RequiresRegister());
1601 locations->SetOut(Location::RequiresFpuRegister());
1602 locations->AddTemp(Location::RequiresFpuRegister());
1603 locations->AddTemp(Location::RequiresFpuRegister());
1604 break;
1605
Roland Levillaincff13742014-11-17 14:32:17 +00001606 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001607 // Processing a Dex `float-to-double' instruction.
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
1609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001610 break;
1611
1612 default:
1613 LOG(FATAL) << "Unexpected type conversion from " << input_type
1614 << " to " << result_type;
1615 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001616 break;
1617
1618 default:
1619 LOG(FATAL) << "Unexpected type conversion from " << input_type
1620 << " to " << result_type;
1621 }
1622}
1623
1624void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1625 LocationSummary* locations = conversion->GetLocations();
1626 Location out = locations->Out();
1627 Location in = locations->InAt(0);
1628 Primitive::Type result_type = conversion->GetResultType();
1629 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001630 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001631 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001632 case Primitive::kPrimByte:
1633 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001634 case Primitive::kPrimBoolean:
1635 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001636 case Primitive::kPrimShort:
1637 case Primitive::kPrimInt:
1638 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001639 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 if (in.IsRegister()) {
1641 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001642 } else {
1643 DCHECK(in.GetConstant()->IsIntConstant());
1644 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1645 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1646 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001647 break;
1648
1649 default:
1650 LOG(FATAL) << "Unexpected type conversion from " << input_type
1651 << " to " << result_type;
1652 }
1653 break;
1654
Roland Levillain01a8d712014-11-14 16:27:39 +00001655 case Primitive::kPrimShort:
1656 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001657 case Primitive::kPrimBoolean:
1658 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 case Primitive::kPrimByte:
1660 case Primitive::kPrimInt:
1661 case Primitive::kPrimChar:
1662 // Processing a Dex `int-to-short' instruction.
1663 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001667 } else {
1668 DCHECK(in.GetConstant()->IsIntConstant());
1669 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001671 }
1672 break;
1673
1674 default:
1675 LOG(FATAL) << "Unexpected type conversion from " << input_type
1676 << " to " << result_type;
1677 }
1678 break;
1679
Roland Levillain946e1432014-11-11 17:35:19 +00001680 case Primitive::kPrimInt:
1681 switch (input_type) {
1682 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001683 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001684 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001688 } else {
1689 DCHECK(in.IsConstant());
1690 DCHECK(in.GetConstant()->IsLongConstant());
1691 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001692 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001693 }
1694 break;
1695
Roland Levillain3f8f9362014-12-02 17:45:01 +00001696 case Primitive::kPrimFloat: {
1697 // Processing a Dex `float-to-int' instruction.
1698 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1699 Register output = out.AsRegister<Register>();
1700 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1701 Label done, nan;
1702
1703 __ movl(output, Immediate(kPrimIntMax));
1704 // temp = int-to-float(output)
1705 __ cvtsi2ss(temp, output);
1706 // if input >= temp goto done
1707 __ comiss(input, temp);
1708 __ j(kAboveEqual, &done);
1709 // if input == NaN goto nan
1710 __ j(kUnordered, &nan);
1711 // output = float-to-int-truncate(input)
1712 __ cvttss2si(output, input);
1713 __ jmp(&done);
1714 __ Bind(&nan);
1715 // output = 0
1716 __ xorl(output, output);
1717 __ Bind(&done);
1718 break;
1719 }
1720
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001721 case Primitive::kPrimDouble: {
1722 // Processing a Dex `double-to-int' instruction.
1723 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1724 Register output = out.AsRegister<Register>();
1725 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1726 Label done, nan;
1727
1728 __ movl(output, Immediate(kPrimIntMax));
1729 // temp = int-to-double(output)
1730 __ cvtsi2sd(temp, output);
1731 // if input >= temp goto done
1732 __ comisd(input, temp);
1733 __ j(kAboveEqual, &done);
1734 // if input == NaN goto nan
1735 __ j(kUnordered, &nan);
1736 // output = double-to-int-truncate(input)
1737 __ cvttsd2si(output, input);
1738 __ jmp(&done);
1739 __ Bind(&nan);
1740 // output = 0
1741 __ xorl(output, output);
1742 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001743 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001744 }
Roland Levillain946e1432014-11-11 17:35:19 +00001745
1746 default:
1747 LOG(FATAL) << "Unexpected type conversion from " << input_type
1748 << " to " << result_type;
1749 }
1750 break;
1751
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 case Primitive::kPrimLong:
1753 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001754 case Primitive::kPrimBoolean:
1755 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 case Primitive::kPrimByte:
1757 case Primitive::kPrimShort:
1758 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001759 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001760 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1762 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001763 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001764 __ cdq();
1765 break;
1766
1767 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001768 // Processing a Dex `float-to-long' instruction.
1769 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001770 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1771 break;
1772
Roland Levillaindff1f282014-11-05 14:15:05 +00001773 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001774 // Processing a Dex `double-to-long' instruction.
1775 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1776 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 break;
1778
1779 default:
1780 LOG(FATAL) << "Unexpected type conversion from " << input_type
1781 << " to " << result_type;
1782 }
1783 break;
1784
Roland Levillain981e4542014-11-14 11:47:14 +00001785 case Primitive::kPrimChar:
1786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001787 case Primitive::kPrimBoolean:
1788 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001792 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1793 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001796 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001797 } else {
1798 DCHECK(in.GetConstant()->IsIntConstant());
1799 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001801 }
1802 break;
1803
1804 default:
1805 LOG(FATAL) << "Unexpected type conversion from " << input_type
1806 << " to " << result_type;
1807 }
1808 break;
1809
Roland Levillaindff1f282014-11-05 14:15:05 +00001810 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001811 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001812 case Primitive::kPrimBoolean:
1813 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001814 case Primitive::kPrimByte:
1815 case Primitive::kPrimShort:
1816 case Primitive::kPrimInt:
1817 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001818 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001819 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001820 break;
1821
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 case Primitive::kPrimLong: {
1823 // Processing a Dex `long-to-float' instruction.
1824 Register low = in.AsRegisterPairLow<Register>();
1825 Register high = in.AsRegisterPairHigh<Register>();
1826 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1827 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1828 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1829
1830 // Operations use doubles for precision reasons (each 32-bit
1831 // half of a long fits in the 53-bit mantissa of a double,
1832 // but not in the 24-bit mantissa of a float). This is
1833 // especially important for the low bits. The result is
1834 // eventually converted to float.
1835
1836 // low = low - 2^31 (to prevent bit 31 of `low` to be
1837 // interpreted as a sign bit)
1838 __ subl(low, Immediate(0x80000000));
1839 // temp = int-to-double(high)
1840 __ cvtsi2sd(temp, high);
1841 // temp = temp * 2^32
1842 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1843 __ mulsd(temp, constant);
1844 // result = int-to-double(low)
1845 __ cvtsi2sd(result, low);
1846 // result = result + 2^31 (restore the original value of `low`)
1847 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1848 __ addsd(result, constant);
1849 // result = result + temp
1850 __ addsd(result, temp);
1851 // result = double-to-float(result)
1852 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001853 // Restore low.
1854 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001855 break;
1856 }
1857
Roland Levillaincff13742014-11-17 14:32:17 +00001858 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001859 // Processing a Dex `double-to-float' instruction.
1860 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001861 break;
1862
1863 default:
1864 LOG(FATAL) << "Unexpected type conversion from " << input_type
1865 << " to " << result_type;
1866 };
1867 break;
1868
Roland Levillaindff1f282014-11-05 14:15:05 +00001869 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001870 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001871 case Primitive::kPrimBoolean:
1872 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001873 case Primitive::kPrimByte:
1874 case Primitive::kPrimShort:
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001877 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001878 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001879 break;
1880
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 case Primitive::kPrimLong: {
1882 // Processing a Dex `long-to-double' instruction.
1883 Register low = in.AsRegisterPairLow<Register>();
1884 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1886 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1887 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001888
Roland Levillain647b9ed2014-11-27 12:06:00 +00001889 // low = low - 2^31 (to prevent bit 31 of `low` to be
1890 // interpreted as a sign bit)
1891 __ subl(low, Immediate(0x80000000));
1892 // temp = int-to-double(high)
1893 __ cvtsi2sd(temp, high);
1894 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001895 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001896 __ mulsd(temp, constant);
1897 // result = int-to-double(low)
1898 __ cvtsi2sd(result, low);
1899 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001900 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001901 __ addsd(result, constant);
1902 // result = result + temp
1903 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001904 // Restore low.
1905 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001906 break;
1907 }
1908
Roland Levillaincff13742014-11-17 14:32:17 +00001909 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001910 // Processing a Dex `float-to-double' instruction.
1911 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001918 break;
1919
1920 default:
1921 LOG(FATAL) << "Unexpected type conversion from " << input_type
1922 << " to " << result_type;
1923 }
1924}
1925
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001926void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001927 LocationSummary* locations =
1928 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001929 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001930 case Primitive::kPrimInt: {
1931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1934 break;
1935 }
1936
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001938 locations->SetInAt(0, Location::RequiresRegister());
1939 locations->SetInAt(1, Location::Any());
1940 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
1942 }
1943
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 case Primitive::kPrimFloat:
1945 case Primitive::kPrimDouble: {
1946 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001947 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1954 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001956}
1957
1958void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1959 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 Location first = locations->InAt(0);
1961 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001962 Location out = locations->Out();
1963
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001964 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1968 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1969 } else {
1970 __ leal(out.AsRegister<Register>(), Address(
1971 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1972 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001973 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001974 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1975 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1976 __ addl(out.AsRegister<Register>(), Immediate(value));
1977 } else {
1978 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1979 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001980 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001981 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001983 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001984 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001985 }
1986
1987 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001988 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001989 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1990 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001991 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001992 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1993 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001995 } else {
1996 DCHECK(second.IsConstant()) << second;
1997 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1998 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1999 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001 break;
2002 }
2003
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002004 case Primitive::kPrimFloat: {
2005 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002006 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 }
2010
2011 case Primitive::kPrimDouble: {
2012 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014 }
2015 break;
2016 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002017
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002018 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002019 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002020 }
2021}
2022
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002023void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002024 LocationSummary* locations =
2025 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002026 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002027 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002028 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002029 locations->SetInAt(0, Location::RequiresRegister());
2030 locations->SetInAt(1, Location::Any());
2031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032 break;
2033 }
Calin Juravle11351682014-10-23 15:38:15 +01002034 case Primitive::kPrimFloat:
2035 case Primitive::kPrimDouble: {
2036 locations->SetInAt(0, Location::RequiresFpuRegister());
2037 locations->SetInAt(1, Location::RequiresFpuRegister());
2038 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039 break;
Calin Juravle11351682014-10-23 15:38:15 +01002040 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 default:
Calin Juravle11351682014-10-23 15:38:15 +01002043 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002044 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045}
2046
2047void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2048 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 Location first = locations->InAt(0);
2050 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002051 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002052 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002053 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002054 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002057 __ subl(first.AsRegister<Register>(),
2058 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002059 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002061 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002062 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 }
2064
2065 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002066 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002067 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2068 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002069 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002070 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 __ sbbl(first.AsRegisterPairHigh<Register>(),
2072 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002073 } else {
2074 DCHECK(second.IsConstant()) << second;
2075 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2076 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2077 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002079 break;
2080 }
2081
Calin Juravle11351682014-10-23 15:38:15 +01002082 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084 break;
Calin Juravle11351682014-10-23 15:38:15 +01002085 }
2086
2087 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002089 break;
2090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002092 default:
Calin Juravle11351682014-10-23 15:38:15 +01002093 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002094 }
2095}
2096
Calin Juravle34bacdf2014-10-07 20:23:36 +01002097void LocationsBuilderX86::VisitMul(HMul* mul) {
2098 LocationSummary* locations =
2099 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2100 switch (mul->GetResultType()) {
2101 case Primitive::kPrimInt:
2102 locations->SetInAt(0, Location::RequiresRegister());
2103 locations->SetInAt(1, Location::Any());
2104 locations->SetOut(Location::SameAsFirstInput());
2105 break;
2106 case Primitive::kPrimLong: {
2107 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002108 locations->SetInAt(1, Location::Any());
2109 locations->SetOut(Location::SameAsFirstInput());
2110 // Needed for imul on 32bits with 64bits output.
2111 locations->AddTemp(Location::RegisterLocation(EAX));
2112 locations->AddTemp(Location::RegisterLocation(EDX));
2113 break;
2114 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 case Primitive::kPrimFloat:
2116 case Primitive::kPrimDouble: {
2117 locations->SetInAt(0, Location::RequiresFpuRegister());
2118 locations->SetInAt(1, Location::RequiresFpuRegister());
2119 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002120 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002121 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002122
2123 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002124 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002125 }
2126}
2127
2128void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2129 LocationSummary* locations = mul->GetLocations();
2130 Location first = locations->InAt(0);
2131 Location second = locations->InAt(1);
2132 DCHECK(first.Equals(locations->Out()));
2133
2134 switch (mul->GetResultType()) {
2135 case Primitive::kPrimInt: {
2136 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 } else if (second.IsConstant()) {
2139 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002141 } else {
2142 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144 }
2145 break;
2146 }
2147
2148 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002149 Register in1_hi = first.AsRegisterPairHigh<Register>();
2150 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 Register eax = locations->GetTemp(0).AsRegister<Register>();
2152 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002153
2154 DCHECK_EQ(EAX, eax);
2155 DCHECK_EQ(EDX, edx);
2156
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002157 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002158 // output: in1
2159 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2160 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2161 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002162 if (second.IsConstant()) {
2163 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002164
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002165 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2166 int32_t low_value = Low32Bits(value);
2167 int32_t high_value = High32Bits(value);
2168 Immediate low(low_value);
2169 Immediate high(high_value);
2170
2171 __ movl(eax, high);
2172 // eax <- in1.lo * in2.hi
2173 __ imull(eax, in1_lo);
2174 // in1.hi <- in1.hi * in2.lo
2175 __ imull(in1_hi, low);
2176 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2177 __ addl(in1_hi, eax);
2178 // move in2_lo to eax to prepare for double precision
2179 __ movl(eax, low);
2180 // edx:eax <- in1.lo * in2.lo
2181 __ mull(in1_lo);
2182 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2183 __ addl(in1_hi, edx);
2184 // in1.lo <- (in1.lo * in2.lo)[31:0];
2185 __ movl(in1_lo, eax);
2186 } else if (second.IsRegisterPair()) {
2187 Register in2_hi = second.AsRegisterPairHigh<Register>();
2188 Register in2_lo = second.AsRegisterPairLow<Register>();
2189
2190 __ movl(eax, in2_hi);
2191 // eax <- in1.lo * in2.hi
2192 __ imull(eax, in1_lo);
2193 // in1.hi <- in1.hi * in2.lo
2194 __ imull(in1_hi, in2_lo);
2195 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2196 __ addl(in1_hi, eax);
2197 // move in1_lo to eax to prepare for double precision
2198 __ movl(eax, in1_lo);
2199 // edx:eax <- in1.lo * in2.lo
2200 __ mull(in2_lo);
2201 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2202 __ addl(in1_hi, edx);
2203 // in1.lo <- (in1.lo * in2.lo)[31:0];
2204 __ movl(in1_lo, eax);
2205 } else {
2206 DCHECK(second.IsDoubleStackSlot()) << second;
2207 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2208 Address in2_lo(ESP, second.GetStackIndex());
2209
2210 __ movl(eax, in2_hi);
2211 // eax <- in1.lo * in2.hi
2212 __ imull(eax, in1_lo);
2213 // in1.hi <- in1.hi * in2.lo
2214 __ imull(in1_hi, in2_lo);
2215 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2216 __ addl(in1_hi, eax);
2217 // move in1_lo to eax to prepare for double precision
2218 __ movl(eax, in1_lo);
2219 // edx:eax <- in1.lo * in2.lo
2220 __ mull(in2_lo);
2221 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2222 __ addl(in1_hi, edx);
2223 // in1.lo <- (in1.lo * in2.lo)[31:0];
2224 __ movl(in1_lo, eax);
2225 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002226
2227 break;
2228 }
2229
Calin Juravleb5bfa962014-10-21 18:02:24 +01002230 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002232 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002233 }
2234
2235 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002237 break;
2238 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002239
2240 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002241 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002242 }
2243}
2244
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002245void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2246 uint32_t stack_adjustment, bool is_float) {
2247 if (source.IsStackSlot()) {
2248 DCHECK(is_float);
2249 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2250 } else if (source.IsDoubleStackSlot()) {
2251 DCHECK(!is_float);
2252 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2253 } else {
2254 // Write the value to the temporary location on the stack and load to FP stack.
2255 if (is_float) {
2256 Location stack_temp = Location::StackSlot(temp_offset);
2257 codegen_->Move32(stack_temp, source);
2258 __ flds(Address(ESP, temp_offset));
2259 } else {
2260 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2261 codegen_->Move64(stack_temp, source);
2262 __ fldl(Address(ESP, temp_offset));
2263 }
2264 }
2265}
2266
2267void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2268 Primitive::Type type = rem->GetResultType();
2269 bool is_float = type == Primitive::kPrimFloat;
2270 size_t elem_size = Primitive::ComponentSize(type);
2271 LocationSummary* locations = rem->GetLocations();
2272 Location first = locations->InAt(0);
2273 Location second = locations->InAt(1);
2274 Location out = locations->Out();
2275
2276 // Create stack space for 2 elements.
2277 // TODO: enhance register allocator to ask for stack temporaries.
2278 __ subl(ESP, Immediate(2 * elem_size));
2279
2280 // Load the values to the FP stack in reverse order, using temporaries if needed.
2281 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2282 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2283
2284 // Loop doing FPREM until we stabilize.
2285 Label retry;
2286 __ Bind(&retry);
2287 __ fprem();
2288
2289 // Move FP status to AX.
2290 __ fstsw();
2291
2292 // And see if the argument reduction is complete. This is signaled by the
2293 // C2 FPU flag bit set to 0.
2294 __ andl(EAX, Immediate(kC2ConditionMask));
2295 __ j(kNotEqual, &retry);
2296
2297 // We have settled on the final value. Retrieve it into an XMM register.
2298 // Store FP top of stack to real stack.
2299 if (is_float) {
2300 __ fsts(Address(ESP, 0));
2301 } else {
2302 __ fstl(Address(ESP, 0));
2303 }
2304
2305 // Pop the 2 items from the FP stack.
2306 __ fucompp();
2307
2308 // Load the value from the stack into an XMM register.
2309 DCHECK(out.IsFpuRegister()) << out;
2310 if (is_float) {
2311 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2312 } else {
2313 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2314 }
2315
2316 // And remove the temporary stack space we allocated.
2317 __ addl(ESP, Immediate(2 * elem_size));
2318}
2319
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002320
2321void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2322 DCHECK(instruction->IsDiv() || instruction->IsRem());
2323
2324 LocationSummary* locations = instruction->GetLocations();
2325 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002326 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002327
2328 Register out_register = locations->Out().AsRegister<Register>();
2329 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002330 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002331
2332 DCHECK(imm == 1 || imm == -1);
2333
2334 if (instruction->IsRem()) {
2335 __ xorl(out_register, out_register);
2336 } else {
2337 __ movl(out_register, input_register);
2338 if (imm == -1) {
2339 __ negl(out_register);
2340 }
2341 }
2342}
2343
2344
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002345void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002346 LocationSummary* locations = instruction->GetLocations();
2347
2348 Register out_register = locations->Out().AsRegister<Register>();
2349 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002350 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002351
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002352 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002353 Register num = locations->GetTemp(0).AsRegister<Register>();
2354
2355 __ leal(num, Address(input_register, std::abs(imm) - 1));
2356 __ testl(input_register, input_register);
2357 __ cmovl(kGreaterEqual, num, input_register);
2358 int shift = CTZ(imm);
2359 __ sarl(num, Immediate(shift));
2360
2361 if (imm < 0) {
2362 __ negl(num);
2363 }
2364
2365 __ movl(out_register, num);
2366}
2367
2368void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2369 DCHECK(instruction->IsDiv() || instruction->IsRem());
2370
2371 LocationSummary* locations = instruction->GetLocations();
2372 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2373
2374 Register eax = locations->InAt(0).AsRegister<Register>();
2375 Register out = locations->Out().AsRegister<Register>();
2376 Register num;
2377 Register edx;
2378
2379 if (instruction->IsDiv()) {
2380 edx = locations->GetTemp(0).AsRegister<Register>();
2381 num = locations->GetTemp(1).AsRegister<Register>();
2382 } else {
2383 edx = locations->Out().AsRegister<Register>();
2384 num = locations->GetTemp(0).AsRegister<Register>();
2385 }
2386
2387 DCHECK_EQ(EAX, eax);
2388 DCHECK_EQ(EDX, edx);
2389 if (instruction->IsDiv()) {
2390 DCHECK_EQ(EAX, out);
2391 } else {
2392 DCHECK_EQ(EDX, out);
2393 }
2394
2395 int64_t magic;
2396 int shift;
2397 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2398
2399 Label ndiv;
2400 Label end;
2401 // If numerator is 0, the result is 0, no computation needed.
2402 __ testl(eax, eax);
2403 __ j(kNotEqual, &ndiv);
2404
2405 __ xorl(out, out);
2406 __ jmp(&end);
2407
2408 __ Bind(&ndiv);
2409
2410 // Save the numerator.
2411 __ movl(num, eax);
2412
2413 // EAX = magic
2414 __ movl(eax, Immediate(magic));
2415
2416 // EDX:EAX = magic * numerator
2417 __ imull(num);
2418
2419 if (imm > 0 && magic < 0) {
2420 // EDX += num
2421 __ addl(edx, num);
2422 } else if (imm < 0 && magic > 0) {
2423 __ subl(edx, num);
2424 }
2425
2426 // Shift if needed.
2427 if (shift != 0) {
2428 __ sarl(edx, Immediate(shift));
2429 }
2430
2431 // EDX += 1 if EDX < 0
2432 __ movl(eax, edx);
2433 __ shrl(edx, Immediate(31));
2434 __ addl(edx, eax);
2435
2436 if (instruction->IsRem()) {
2437 __ movl(eax, num);
2438 __ imull(edx, Immediate(imm));
2439 __ subl(eax, edx);
2440 __ movl(edx, eax);
2441 } else {
2442 __ movl(eax, edx);
2443 }
2444 __ Bind(&end);
2445}
2446
Calin Juravlebacfec32014-11-14 15:54:36 +00002447void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2448 DCHECK(instruction->IsDiv() || instruction->IsRem());
2449
2450 LocationSummary* locations = instruction->GetLocations();
2451 Location out = locations->Out();
2452 Location first = locations->InAt(0);
2453 Location second = locations->InAt(1);
2454 bool is_div = instruction->IsDiv();
2455
2456 switch (instruction->GetResultType()) {
2457 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 DCHECK_EQ(EAX, first.AsRegister<Register>());
2459 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002460
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002461 if (instruction->InputAt(1)->IsIntConstant()) {
2462 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002463
2464 if (imm == 0) {
2465 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2466 } else if (imm == 1 || imm == -1) {
2467 DivRemOneOrMinusOne(instruction);
2468 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002469 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002470 } else {
2471 DCHECK(imm <= -2 || imm >= 2);
2472 GenerateDivRemWithAnyConstant(instruction);
2473 }
2474 } else {
2475 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002476 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477 is_div);
2478 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002479
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002480 Register second_reg = second.AsRegister<Register>();
2481 // 0x80000000/-1 triggers an arithmetic exception!
2482 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2483 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002484
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002485 __ cmpl(second_reg, Immediate(-1));
2486 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002487
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002488 // edx:eax <- sign-extended of eax
2489 __ cdq();
2490 // eax = quotient, edx = remainder
2491 __ idivl(second_reg);
2492 __ Bind(slow_path->GetExitLabel());
2493 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002494 break;
2495 }
2496
2497 case Primitive::kPrimLong: {
2498 InvokeRuntimeCallingConvention calling_convention;
2499 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2500 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2501 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2502 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2503 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2504 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2505
2506 if (is_div) {
2507 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2508 } else {
2509 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2510 }
2511 uint32_t dex_pc = is_div
2512 ? instruction->AsDiv()->GetDexPc()
2513 : instruction->AsRem()->GetDexPc();
2514 codegen_->RecordPcInfo(instruction, dex_pc);
2515
2516 break;
2517 }
2518
2519 default:
2520 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2521 }
2522}
2523
Calin Juravle7c4954d2014-10-28 16:57:40 +00002524void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002525 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002526 ? LocationSummary::kCall
2527 : LocationSummary::kNoCall;
2528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2529
Calin Juravle7c4954d2014-10-28 16:57:40 +00002530 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002531 case Primitive::kPrimInt: {
2532 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002533 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002534 locations->SetOut(Location::SameAsFirstInput());
2535 // Intel uses edx:eax as the dividend.
2536 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002537 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2538 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2539 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002540 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002541 locations->AddTemp(Location::RequiresRegister());
2542 }
Calin Juravled0d48522014-11-04 16:40:20 +00002543 break;
2544 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002545 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002546 InvokeRuntimeCallingConvention calling_convention;
2547 locations->SetInAt(0, Location::RegisterPairLocation(
2548 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2549 locations->SetInAt(1, Location::RegisterPairLocation(
2550 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2551 // Runtime helper puts the result in EAX, EDX.
2552 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002553 break;
2554 }
2555 case Primitive::kPrimFloat:
2556 case Primitive::kPrimDouble: {
2557 locations->SetInAt(0, Location::RequiresFpuRegister());
2558 locations->SetInAt(1, Location::RequiresFpuRegister());
2559 locations->SetOut(Location::SameAsFirstInput());
2560 break;
2561 }
2562
2563 default:
2564 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2565 }
2566}
2567
2568void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2569 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002570 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002571 Location first = locations->InAt(0);
2572 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002573
2574 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002575 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002576 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002577 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002578 break;
2579 }
2580
2581 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002582 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 break;
2585 }
2586
2587 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002588 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 break;
2591 }
2592
2593 default:
2594 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2595 }
2596}
2597
Calin Juravlebacfec32014-11-14 15:54:36 +00002598void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002599 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002600
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002601 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2602 ? LocationSummary::kCall
2603 : LocationSummary::kNoCall;
2604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002605
Calin Juravled2ec87d2014-12-08 14:24:46 +00002606 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002607 case Primitive::kPrimInt: {
2608 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002609 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002610 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002611 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2612 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2613 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002614 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002615 locations->AddTemp(Location::RequiresRegister());
2616 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002617 break;
2618 }
2619 case Primitive::kPrimLong: {
2620 InvokeRuntimeCallingConvention calling_convention;
2621 locations->SetInAt(0, Location::RegisterPairLocation(
2622 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2623 locations->SetInAt(1, Location::RegisterPairLocation(
2624 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2625 // Runtime helper puts the result in EAX, EDX.
2626 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2627 break;
2628 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002629 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002630 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002631 locations->SetInAt(0, Location::Any());
2632 locations->SetInAt(1, Location::Any());
2633 locations->SetOut(Location::RequiresFpuRegister());
2634 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002635 break;
2636 }
2637
2638 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002639 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002640 }
2641}
2642
2643void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2644 Primitive::Type type = rem->GetResultType();
2645 switch (type) {
2646 case Primitive::kPrimInt:
2647 case Primitive::kPrimLong: {
2648 GenerateDivRemIntegral(rem);
2649 break;
2650 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002651 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002652 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002653 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002654 break;
2655 }
2656 default:
2657 LOG(FATAL) << "Unexpected rem type " << type;
2658 }
2659}
2660
Calin Juravled0d48522014-11-04 16:40:20 +00002661void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2662 LocationSummary* locations =
2663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002664 switch (instruction->GetType()) {
2665 case Primitive::kPrimInt: {
2666 locations->SetInAt(0, Location::Any());
2667 break;
2668 }
2669 case Primitive::kPrimLong: {
2670 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2671 if (!instruction->IsConstant()) {
2672 locations->AddTemp(Location::RequiresRegister());
2673 }
2674 break;
2675 }
2676 default:
2677 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2678 }
Calin Juravled0d48522014-11-04 16:40:20 +00002679 if (instruction->HasUses()) {
2680 locations->SetOut(Location::SameAsFirstInput());
2681 }
2682}
2683
2684void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2685 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2686 codegen_->AddSlowPath(slow_path);
2687
2688 LocationSummary* locations = instruction->GetLocations();
2689 Location value = locations->InAt(0);
2690
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002691 switch (instruction->GetType()) {
2692 case Primitive::kPrimInt: {
2693 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002695 __ j(kEqual, slow_path->GetEntryLabel());
2696 } else if (value.IsStackSlot()) {
2697 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2698 __ j(kEqual, slow_path->GetEntryLabel());
2699 } else {
2700 DCHECK(value.IsConstant()) << value;
2701 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2702 __ jmp(slow_path->GetEntryLabel());
2703 }
2704 }
2705 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002706 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002707 case Primitive::kPrimLong: {
2708 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002710 __ movl(temp, value.AsRegisterPairLow<Register>());
2711 __ orl(temp, value.AsRegisterPairHigh<Register>());
2712 __ j(kEqual, slow_path->GetEntryLabel());
2713 } else {
2714 DCHECK(value.IsConstant()) << value;
2715 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2716 __ jmp(slow_path->GetEntryLabel());
2717 }
2718 }
2719 break;
2720 }
2721 default:
2722 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002723 }
Calin Juravled0d48522014-11-04 16:40:20 +00002724}
2725
Calin Juravle9aec02f2014-11-18 23:06:35 +00002726void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2727 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2728
2729 LocationSummary* locations =
2730 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2731
2732 switch (op->GetResultType()) {
2733 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002734 locations->SetInAt(0, Location::RequiresRegister());
2735 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002736 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2737 locations->SetOut(Location::SameAsFirstInput());
2738 break;
2739 }
2740 case Primitive::kPrimLong: {
2741 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002742 // The shift count needs to be in CL.
2743 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002744 locations->SetOut(Location::SameAsFirstInput());
2745 break;
2746 }
2747 default:
2748 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2749 }
2750}
2751
2752void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2753 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2754
2755 LocationSummary* locations = op->GetLocations();
2756 Location first = locations->InAt(0);
2757 Location second = locations->InAt(1);
2758 DCHECK(first.Equals(locations->Out()));
2759
2760 switch (op->GetResultType()) {
2761 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002762 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002763 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002764 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002765 DCHECK_EQ(ECX, second_reg);
2766 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002767 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002768 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002769 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002770 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002771 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 }
2773 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002774 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2775 if (op->IsShl()) {
2776 __ shll(first_reg, imm);
2777 } else if (op->IsShr()) {
2778 __ sarl(first_reg, imm);
2779 } else {
2780 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002781 }
2782 }
2783 break;
2784 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 case Primitive::kPrimLong: {
2786 Register second_reg = second.AsRegister<Register>();
2787 DCHECK_EQ(ECX, second_reg);
2788 if (op->IsShl()) {
2789 GenerateShlLong(first, second_reg);
2790 } else if (op->IsShr()) {
2791 GenerateShrLong(first, second_reg);
2792 } else {
2793 GenerateUShrLong(first, second_reg);
2794 }
2795 break;
2796 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002797 default:
2798 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2799 }
2800}
2801
2802void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2803 Label done;
2804 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2805 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2806 __ testl(shifter, Immediate(32));
2807 __ j(kEqual, &done);
2808 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2809 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2810 __ Bind(&done);
2811}
2812
2813void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2814 Label done;
2815 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2816 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2817 __ testl(shifter, Immediate(32));
2818 __ j(kEqual, &done);
2819 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2820 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2821 __ Bind(&done);
2822}
2823
2824void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2825 Label done;
2826 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2827 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2828 __ testl(shifter, Immediate(32));
2829 __ j(kEqual, &done);
2830 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2831 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2832 __ Bind(&done);
2833}
2834
2835void LocationsBuilderX86::VisitShl(HShl* shl) {
2836 HandleShift(shl);
2837}
2838
2839void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2840 HandleShift(shl);
2841}
2842
2843void LocationsBuilderX86::VisitShr(HShr* shr) {
2844 HandleShift(shr);
2845}
2846
2847void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2848 HandleShift(shr);
2849}
2850
2851void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2852 HandleShift(ushr);
2853}
2854
2855void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2856 HandleShift(ushr);
2857}
2858
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002859void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002860 LocationSummary* locations =
2861 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002862 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002863 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002864 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2865 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002866}
2867
2868void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2869 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002870 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002871 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002872
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002873 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002874
Nicolas Geoffray39468442014-09-02 15:17:15 +01002875 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002876 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002877}
2878
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002879void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2882 locations->SetOut(Location::RegisterLocation(EAX));
2883 InvokeRuntimeCallingConvention calling_convention;
2884 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002885 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2886 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002887}
2888
2889void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2890 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002891 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002892 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2893
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002894 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002895
2896 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2897 DCHECK(!codegen_->IsLeafMethod());
2898}
2899
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002900void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002901 LocationSummary* locations =
2902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002903 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2904 if (location.IsStackSlot()) {
2905 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2906 } else if (location.IsDoubleStackSlot()) {
2907 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002908 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002909 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002910}
2911
2912void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002913 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914}
2915
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002916void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002917 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002918 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002921}
2922
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002923void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2924 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002925 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002926 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002927 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002928 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002929 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002931 break;
2932
2933 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002934 __ notl(out.AsRegisterPairLow<Register>());
2935 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002936 break;
2937
2938 default:
2939 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2940 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002941}
2942
David Brazdil66d126e2015-04-03 16:02:44 +01002943void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2944 LocationSummary* locations =
2945 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2946 locations->SetInAt(0, Location::RequiresRegister());
2947 locations->SetOut(Location::SameAsFirstInput());
2948}
2949
2950void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002951 LocationSummary* locations = bool_not->GetLocations();
2952 Location in = locations->InAt(0);
2953 Location out = locations->Out();
2954 DCHECK(in.Equals(out));
2955 __ xorl(out.AsRegister<Register>(), Immediate(1));
2956}
2957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002958void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002961 switch (compare->InputAt(0)->GetType()) {
2962 case Primitive::kPrimLong: {
2963 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002964 locations->SetInAt(1, Location::Any());
2965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2966 break;
2967 }
2968 case Primitive::kPrimFloat:
2969 case Primitive::kPrimDouble: {
2970 locations->SetInAt(0, Location::RequiresFpuRegister());
2971 locations->SetInAt(1, Location::RequiresFpuRegister());
2972 locations->SetOut(Location::RequiresRegister());
2973 break;
2974 }
2975 default:
2976 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2977 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002978}
2979
2980void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002981 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002983 Location left = locations->InAt(0);
2984 Location right = locations->InAt(1);
2985
2986 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002987 switch (compare->InputAt(0)->GetType()) {
2988 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002989 Register left_low = left.AsRegisterPairLow<Register>();
2990 Register left_high = left.AsRegisterPairHigh<Register>();
2991 int32_t val_low = 0;
2992 int32_t val_high = 0;
2993 bool right_is_const = false;
2994
2995 if (right.IsConstant()) {
2996 DCHECK(right.GetConstant()->IsLongConstant());
2997 right_is_const = true;
2998 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2999 val_low = Low32Bits(val);
3000 val_high = High32Bits(val);
3001 }
3002
Calin Juravleddb7df22014-11-25 20:56:51 +00003003 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003004 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003005 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003006 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003007 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003008 DCHECK(right_is_const) << right;
3009 if (val_high == 0) {
3010 __ testl(left_high, left_high);
3011 } else {
3012 __ cmpl(left_high, Immediate(val_high));
3013 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003014 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003015 __ j(kLess, &less); // Signed compare.
3016 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003017 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003018 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003020 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003022 DCHECK(right_is_const) << right;
3023 if (val_low == 0) {
3024 __ testl(left_low, left_low);
3025 } else {
3026 __ cmpl(left_low, Immediate(val_low));
3027 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003029 break;
3030 }
3031 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003033 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3034 break;
3035 }
3036 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003038 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003039 break;
3040 }
3041 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003042 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003043 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003044 __ movl(out, Immediate(0));
3045 __ j(kEqual, &done);
3046 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3047
3048 __ Bind(&greater);
3049 __ movl(out, Immediate(1));
3050 __ jmp(&done);
3051
3052 __ Bind(&less);
3053 __ movl(out, Immediate(-1));
3054
3055 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003056}
3057
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003058void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003061 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3062 locations->SetInAt(i, Location::Any());
3063 }
3064 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003065}
3066
3067void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003068 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003069 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003070}
3071
Calin Juravle52c48962014-12-16 17:02:57 +00003072void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3073 /*
3074 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3075 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3076 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3077 */
3078 switch (kind) {
3079 case MemBarrierKind::kAnyAny: {
3080 __ mfence();
3081 break;
3082 }
3083 case MemBarrierKind::kAnyStore:
3084 case MemBarrierKind::kLoadAny:
3085 case MemBarrierKind::kStoreStore: {
3086 // nop
3087 break;
3088 }
3089 default:
3090 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003091 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003092}
3093
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003094
Mark Mendell09ed1a32015-03-25 08:30:06 -04003095void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3096 Register temp) {
3097 // TODO: Implement all kinds of calls:
3098 // 1) boot -> boot
3099 // 2) app -> boot
3100 // 3) app -> app
3101 //
3102 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3103 // temp = method;
3104 LoadCurrentMethod(temp);
3105 if (!invoke->IsRecursive()) {
3106 // temp = temp->dex_cache_resolved_methods_;
3107 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3108 // temp = temp[index_in_cache]
3109 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3110 // (temp + offset_of_quick_compiled_code)()
3111 __ call(Address(
3112 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3113 } else {
3114 __ call(GetFrameEntryLabel());
3115 }
3116
3117 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003118}
3119
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003120void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 Label is_null;
3122 __ testl(value, value);
3123 __ j(kEqual, &is_null);
3124 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3125 __ movl(temp, object);
3126 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003127 __ movb(Address(temp, card, TIMES_1, 0),
3128 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 __ Bind(&is_null);
3130}
3131
Calin Juravle52c48962014-12-16 17:02:57 +00003132void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3133 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003134 LocationSummary* locations =
3135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003136 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003138 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3139 locations->SetOut(Location::RequiresFpuRegister());
3140 } else {
3141 // The output overlaps in case of long: we don't want the low move to overwrite
3142 // the object's location.
3143 locations->SetOut(Location::RequiresRegister(),
3144 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3145 : Location::kNoOutputOverlap);
3146 }
Calin Juravle52c48962014-12-16 17:02:57 +00003147
3148 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3149 // Long values can be loaded atomically into an XMM using movsd.
3150 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3151 // and then copy the XMM into the output 32bits at a time).
3152 locations->AddTemp(Location::RequiresFpuRegister());
3153 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003154}
3155
Calin Juravle52c48962014-12-16 17:02:57 +00003156void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3157 const FieldInfo& field_info) {
3158 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003159
Calin Juravle52c48962014-12-16 17:02:57 +00003160 LocationSummary* locations = instruction->GetLocations();
3161 Register base = locations->InAt(0).AsRegister<Register>();
3162 Location out = locations->Out();
3163 bool is_volatile = field_info.IsVolatile();
3164 Primitive::Type field_type = field_info.GetFieldType();
3165 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3166
3167 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003168 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003169 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003170 break;
3171 }
3172
3173 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003174 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003175 break;
3176 }
3177
3178 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003179 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003180 break;
3181 }
3182
3183 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003184 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003185 break;
3186 }
3187
3188 case Primitive::kPrimInt:
3189 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003190 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003191 break;
3192 }
3193
3194 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003195 if (is_volatile) {
3196 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3197 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003199 __ movd(out.AsRegisterPairLow<Register>(), temp);
3200 __ psrlq(temp, Immediate(32));
3201 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3202 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003203 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003204 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003205 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003206 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3207 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003208 break;
3209 }
3210
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003211 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003212 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003213 break;
3214 }
3215
3216 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003217 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003218 break;
3219 }
3220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003221 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003222 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003223 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003224 }
Calin Juravle52c48962014-12-16 17:02:57 +00003225
Calin Juravle77520bc2015-01-12 18:45:46 +00003226 // Longs are handled in the switch.
3227 if (field_type != Primitive::kPrimLong) {
3228 codegen_->MaybeRecordImplicitNullCheck(instruction);
3229 }
3230
Calin Juravle52c48962014-12-16 17:02:57 +00003231 if (is_volatile) {
3232 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3233 }
3234}
3235
3236void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3237 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3238
3239 LocationSummary* locations =
3240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3241 locations->SetInAt(0, Location::RequiresRegister());
3242 bool is_volatile = field_info.IsVolatile();
3243 Primitive::Type field_type = field_info.GetFieldType();
3244 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3245 || (field_type == Primitive::kPrimByte);
3246
3247 // The register allocator does not support multiple
3248 // inputs that die at entry with one in a specific register.
3249 if (is_byte_type) {
3250 // Ensure the value is in a byte register.
3251 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003252 } else if (Primitive::IsFloatingPointType(field_type)) {
3253 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003254 } else {
3255 locations->SetInAt(1, Location::RequiresRegister());
3256 }
3257 // Temporary registers for the write barrier.
3258 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3259 locations->AddTemp(Location::RequiresRegister());
3260 // Ensure the card is in a byte register.
3261 locations->AddTemp(Location::RegisterLocation(ECX));
3262 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3263 // 64bits value can be atomically written to an address with movsd and an XMM register.
3264 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3265 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3266 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3267 // isolated cases when we need this it isn't worth adding the extra complexity.
3268 locations->AddTemp(Location::RequiresFpuRegister());
3269 locations->AddTemp(Location::RequiresFpuRegister());
3270 }
3271}
3272
3273void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3274 const FieldInfo& field_info) {
3275 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3276
3277 LocationSummary* locations = instruction->GetLocations();
3278 Register base = locations->InAt(0).AsRegister<Register>();
3279 Location value = locations->InAt(1);
3280 bool is_volatile = field_info.IsVolatile();
3281 Primitive::Type field_type = field_info.GetFieldType();
3282 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3283
3284 if (is_volatile) {
3285 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3286 }
3287
3288 switch (field_type) {
3289 case Primitive::kPrimBoolean:
3290 case Primitive::kPrimByte: {
3291 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3292 break;
3293 }
3294
3295 case Primitive::kPrimShort:
3296 case Primitive::kPrimChar: {
3297 __ movw(Address(base, offset), value.AsRegister<Register>());
3298 break;
3299 }
3300
3301 case Primitive::kPrimInt:
3302 case Primitive::kPrimNot: {
3303 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003304 break;
3305 }
3306
3307 case Primitive::kPrimLong: {
3308 if (is_volatile) {
3309 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3310 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3311 __ movd(temp1, value.AsRegisterPairLow<Register>());
3312 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3313 __ punpckldq(temp1, temp2);
3314 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003315 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003316 } else {
3317 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003319 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3320 }
3321 break;
3322 }
3323
3324 case Primitive::kPrimFloat: {
3325 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3326 break;
3327 }
3328
3329 case Primitive::kPrimDouble: {
3330 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3331 break;
3332 }
3333
3334 case Primitive::kPrimVoid:
3335 LOG(FATAL) << "Unreachable type " << field_type;
3336 UNREACHABLE();
3337 }
3338
Calin Juravle77520bc2015-01-12 18:45:46 +00003339 // Longs are handled in the switch.
3340 if (field_type != Primitive::kPrimLong) {
3341 codegen_->MaybeRecordImplicitNullCheck(instruction);
3342 }
3343
3344 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3345 Register temp = locations->GetTemp(0).AsRegister<Register>();
3346 Register card = locations->GetTemp(1).AsRegister<Register>();
3347 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3348 }
3349
Calin Juravle52c48962014-12-16 17:02:57 +00003350 if (is_volatile) {
3351 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3352 }
3353}
3354
3355void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3356 HandleFieldGet(instruction, instruction->GetFieldInfo());
3357}
3358
3359void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3360 HandleFieldGet(instruction, instruction->GetFieldInfo());
3361}
3362
3363void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3364 HandleFieldSet(instruction, instruction->GetFieldInfo());
3365}
3366
3367void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3368 HandleFieldSet(instruction, instruction->GetFieldInfo());
3369}
3370
3371void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3372 HandleFieldSet(instruction, instruction->GetFieldInfo());
3373}
3374
3375void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3376 HandleFieldSet(instruction, instruction->GetFieldInfo());
3377}
3378
3379void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3380 HandleFieldGet(instruction, instruction->GetFieldInfo());
3381}
3382
3383void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3384 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385}
3386
3387void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003390 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3391 ? Location::RequiresRegister()
3392 : Location::Any();
3393 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003394 if (instruction->HasUses()) {
3395 locations->SetOut(Location::SameAsFirstInput());
3396 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003397}
3398
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003399void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003400 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3401 return;
3402 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003403 LocationSummary* locations = instruction->GetLocations();
3404 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003405
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003406 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3407 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3408}
3409
3410void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003411 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003412 codegen_->AddSlowPath(slow_path);
3413
3414 LocationSummary* locations = instruction->GetLocations();
3415 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416
3417 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003418 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003419 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003421 } else {
3422 DCHECK(obj.IsConstant()) << obj;
3423 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3424 __ jmp(slow_path->GetEntryLabel());
3425 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 }
3427 __ j(kEqual, slow_path->GetEntryLabel());
3428}
3429
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003430void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3431 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3432 GenerateImplicitNullCheck(instruction);
3433 } else {
3434 GenerateExplicitNullCheck(instruction);
3435 }
3436}
3437
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003438void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003439 LocationSummary* locations =
3440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003441 locations->SetInAt(0, Location::RequiresRegister());
3442 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003443 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3445 } else {
3446 // The output overlaps in case of long: we don't want the low move to overwrite
3447 // the array's location.
3448 locations->SetOut(Location::RequiresRegister(),
3449 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3450 : Location::kNoOutputOverlap);
3451 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003452}
3453
3454void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3455 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003457 Location index = locations->InAt(1);
3458
Calin Juravle77520bc2015-01-12 18:45:46 +00003459 Primitive::Type type = instruction->GetType();
3460 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461 case Primitive::kPrimBoolean: {
3462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 if (index.IsConstant()) {
3465 __ movzxb(out, Address(obj,
3466 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3467 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003468 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003469 }
3470 break;
3471 }
3472
3473 case Primitive::kPrimByte: {
3474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003476 if (index.IsConstant()) {
3477 __ movsxb(out, Address(obj,
3478 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3479 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003480 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003481 }
3482 break;
3483 }
3484
3485 case Primitive::kPrimShort: {
3486 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 if (index.IsConstant()) {
3489 __ movsxw(out, Address(obj,
3490 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3491 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003493 }
3494 break;
3495 }
3496
3497 case Primitive::kPrimChar: {
3498 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 if (index.IsConstant()) {
3501 __ movzxw(out, Address(obj,
3502 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3503 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003504 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003505 }
3506 break;
3507 }
3508
3509 case Primitive::kPrimInt:
3510 case Primitive::kPrimNot: {
3511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003512 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 if (index.IsConstant()) {
3514 __ movl(out, Address(obj,
3515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3516 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003518 }
3519 break;
3520 }
3521
3522 case Primitive::kPrimLong: {
3523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003524 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003525 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003526 if (index.IsConstant()) {
3527 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003528 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003530 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003531 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003532 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003535 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003537 }
3538 break;
3539 }
3540
Mark Mendell7c8d0092015-01-26 11:21:33 -05003541 case Primitive::kPrimFloat: {
3542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3543 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3544 if (index.IsConstant()) {
3545 __ movss(out, Address(obj,
3546 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3547 } else {
3548 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3549 }
3550 break;
3551 }
3552
3553 case Primitive::kPrimDouble: {
3554 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3555 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3556 if (index.IsConstant()) {
3557 __ movsd(out, Address(obj,
3558 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3559 } else {
3560 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3561 }
3562 break;
3563 }
3564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003566 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003567 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003568 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003569
3570 if (type != Primitive::kPrimLong) {
3571 codegen_->MaybeRecordImplicitNullCheck(instruction);
3572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573}
3574
3575void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003576 // This location builder might end up asking to up to four registers, which is
3577 // not currently possible for baseline. The situation in which we need four
3578 // registers cannot be met by baseline though, because it has not run any
3579 // optimization.
3580
Nicolas Geoffray39468442014-09-02 15:17:15 +01003581 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003582 bool needs_write_barrier =
3583 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3584
Mark Mendell5f874182015-03-04 15:42:45 -05003585 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003586
Nicolas Geoffray39468442014-09-02 15:17:15 +01003587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3588 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003589 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003590
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003591 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3594 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3595 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003597 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3598 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003599 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003600 // In case of a byte operation, the register allocator does not support multiple
3601 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003602 locations->SetInAt(0, Location::RequiresRegister());
3603 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003604 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003605 // Ensure the value is in a byte register.
3606 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003607 } else if (Primitive::IsFloatingPointType(value_type)) {
3608 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003610 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 // Temporary registers for the write barrier.
3613 if (needs_write_barrier) {
3614 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003615 // Ensure the card is in a byte register.
3616 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619}
3620
3621void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3622 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003625 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003626 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003627 bool needs_runtime_call = locations->WillCall();
3628 bool needs_write_barrier =
3629 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003630
3631 switch (value_type) {
3632 case Primitive::kPrimBoolean:
3633 case Primitive::kPrimByte: {
3634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 if (index.IsConstant()) {
3636 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003637 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003638 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003639 } else {
3640 __ movb(Address(obj, offset),
3641 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003644 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003646 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003647 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003649 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3650 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003651 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 break;
3654 }
3655
3656 case Primitive::kPrimShort:
3657 case Primitive::kPrimChar: {
3658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 if (index.IsConstant()) {
3660 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003661 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003663 } else {
3664 __ movw(Address(obj, offset),
3665 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003668 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003669 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3670 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003671 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003673 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3674 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003675 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003677 break;
3678 }
3679
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003680 case Primitive::kPrimInt:
3681 case Primitive::kPrimNot: {
3682 if (!needs_runtime_call) {
3683 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3684 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003685 size_t offset =
3686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003687 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003688 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003689 } else {
3690 DCHECK(value.IsConstant()) << value;
3691 __ movl(Address(obj, offset),
3692 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3693 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003694 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003695 DCHECK(index.IsRegister()) << index;
3696 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003697 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3698 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 } else {
3700 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003702 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3703 }
3704 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003705 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003706
3707 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 Register temp = locations->GetTemp(0).AsRegister<Register>();
3709 Register card = locations->GetTemp(1).AsRegister<Register>();
3710 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003711 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003713 DCHECK_EQ(value_type, Primitive::kPrimNot);
3714 DCHECK(!codegen_->IsLeafMethod());
3715 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3716 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 }
3718 break;
3719 }
3720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 case Primitive::kPrimLong: {
3722 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003723 if (index.IsConstant()) {
3724 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003725 if (value.IsRegisterPair()) {
3726 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003727 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003728 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003729 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003730 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003731 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3732 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003734 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3735 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003737 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003739 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003742 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003743 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003744 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003745 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003747 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003748 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003749 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003750 Immediate(High32Bits(val)));
3751 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752 }
3753 break;
3754 }
3755
Mark Mendell7c8d0092015-01-26 11:21:33 -05003756 case Primitive::kPrimFloat: {
3757 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3758 DCHECK(value.IsFpuRegister());
3759 if (index.IsConstant()) {
3760 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3761 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3762 } else {
3763 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3764 value.AsFpuRegister<XmmRegister>());
3765 }
3766 break;
3767 }
3768
3769 case Primitive::kPrimDouble: {
3770 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3771 DCHECK(value.IsFpuRegister());
3772 if (index.IsConstant()) {
3773 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3774 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3775 } else {
3776 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3777 value.AsFpuRegister<XmmRegister>());
3778 }
3779 break;
3780 }
3781
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 case Primitive::kPrimVoid:
3783 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003784 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 }
3786}
3787
3788void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3789 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003790 locations->SetInAt(0, Location::RequiresRegister());
3791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 instruction->SetLocations(locations);
3793}
3794
3795void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3796 LocationSummary* locations = instruction->GetLocations();
3797 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 Register obj = locations->InAt(0).AsRegister<Register>();
3799 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003800 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003801 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802}
3803
3804void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003805 LocationSummary* locations =
3806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003807 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003809 if (instruction->HasUses()) {
3810 locations->SetOut(Location::SameAsFirstInput());
3811 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812}
3813
3814void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3815 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003816 Location index_loc = locations->InAt(0);
3817 Location length_loc = locations->InAt(1);
3818 SlowPathCodeX86* slow_path =
3819 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 codegen_->AddSlowPath(slow_path);
3821
Mark Mendellf60c90b2015-03-04 15:12:59 -05003822 Register length = length_loc.AsRegister<Register>();
3823 if (index_loc.IsConstant()) {
3824 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3825 __ cmpl(length, Immediate(value));
3826 } else {
3827 __ cmpl(length, index_loc.AsRegister<Register>());
3828 }
3829 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830}
3831
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003832void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3833 temp->SetLocations(nullptr);
3834}
3835
3836void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3837 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003838 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839}
3840
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003841void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003842 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003843 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003844}
3845
3846void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003847 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3848}
3849
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003850void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3851 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3852}
3853
3854void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003855 HBasicBlock* block = instruction->GetBlock();
3856 if (block->GetLoopInformation() != nullptr) {
3857 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3858 // The back edge will generate the suspend check.
3859 return;
3860 }
3861 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3862 // The goto will generate the suspend check.
3863 return;
3864 }
3865 GenerateSuspendCheck(instruction, nullptr);
3866}
3867
3868void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3869 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003870 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003871 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003872 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003873 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003874 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003875 if (successor == nullptr) {
3876 __ j(kNotEqual, slow_path->GetEntryLabel());
3877 __ Bind(slow_path->GetReturnLabel());
3878 } else {
3879 __ j(kEqual, codegen_->GetLabelOf(successor));
3880 __ jmp(slow_path->GetEntryLabel());
3881 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003882}
3883
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003884X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3885 return codegen_->GetAssembler();
3886}
3887
Mark Mendell7c8d0092015-01-26 11:21:33 -05003888void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003889 ScratchRegisterScope ensure_scratch(
3890 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3891 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3892 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3893 __ movl(temp_reg, Address(ESP, src + stack_offset));
3894 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003895}
3896
3897void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003898 ScratchRegisterScope ensure_scratch(
3899 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3900 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3901 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3902 __ movl(temp_reg, Address(ESP, src + stack_offset));
3903 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3904 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3905 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003906}
3907
3908void ParallelMoveResolverX86::EmitMove(size_t index) {
3909 MoveOperands* move = moves_.Get(index);
3910 Location source = move->GetSource();
3911 Location destination = move->GetDestination();
3912
3913 if (source.IsRegister()) {
3914 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003915 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003916 } else {
3917 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003919 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003920 } else if (source.IsFpuRegister()) {
3921 if (destination.IsFpuRegister()) {
3922 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3923 } else if (destination.IsStackSlot()) {
3924 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3925 } else {
3926 DCHECK(destination.IsDoubleStackSlot());
3927 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3928 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003929 } else if (source.IsStackSlot()) {
3930 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003932 } else if (destination.IsFpuRegister()) {
3933 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003934 } else {
3935 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003936 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3937 }
3938 } else if (source.IsDoubleStackSlot()) {
3939 if (destination.IsFpuRegister()) {
3940 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3941 } else {
3942 DCHECK(destination.IsDoubleStackSlot()) << destination;
3943 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003944 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003945 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003946 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003947 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003948 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003949 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003950 if (value == 0) {
3951 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3952 } else {
3953 __ movl(destination.AsRegister<Register>(), Immediate(value));
3954 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003955 } else {
3956 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003957 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003958 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003959 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003960 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003961 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003962 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003963 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003964 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3965 if (value == 0) {
3966 // Easy handling of 0.0.
3967 __ xorps(dest, dest);
3968 } else {
3969 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003970 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3971 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3972 __ movl(temp, Immediate(value));
3973 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003974 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003975 } else {
3976 DCHECK(destination.IsStackSlot()) << destination;
3977 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3978 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003979 } else if (constant->IsLongConstant()) {
3980 int64_t value = constant->AsLongConstant()->GetValue();
3981 int32_t low_value = Low32Bits(value);
3982 int32_t high_value = High32Bits(value);
3983 Immediate low(low_value);
3984 Immediate high(high_value);
3985 if (destination.IsDoubleStackSlot()) {
3986 __ movl(Address(ESP, destination.GetStackIndex()), low);
3987 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3988 } else {
3989 __ movl(destination.AsRegisterPairLow<Register>(), low);
3990 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3991 }
3992 } else {
3993 DCHECK(constant->IsDoubleConstant());
3994 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003995 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003996 int32_t low_value = Low32Bits(value);
3997 int32_t high_value = High32Bits(value);
3998 Immediate low(low_value);
3999 Immediate high(high_value);
4000 if (destination.IsFpuRegister()) {
4001 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4002 if (value == 0) {
4003 // Easy handling of 0.0.
4004 __ xorpd(dest, dest);
4005 } else {
4006 __ pushl(high);
4007 __ pushl(low);
4008 __ movsd(dest, Address(ESP, 0));
4009 __ addl(ESP, Immediate(8));
4010 }
4011 } else {
4012 DCHECK(destination.IsDoubleStackSlot()) << destination;
4013 __ movl(Address(ESP, destination.GetStackIndex()), low);
4014 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4015 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004016 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004017 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004018 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004019 }
4020}
4021
Mark Mendella5c19ce2015-04-01 12:51:05 -04004022void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004023 Register suggested_scratch = reg == EAX ? EBX : EAX;
4024 ScratchRegisterScope ensure_scratch(
4025 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4026
4027 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4028 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4029 __ movl(Address(ESP, mem + stack_offset), reg);
4030 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004031}
4032
Mark Mendell7c8d0092015-01-26 11:21:33 -05004033void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004034 ScratchRegisterScope ensure_scratch(
4035 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4036
4037 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4038 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4039 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4040 __ movss(Address(ESP, mem + stack_offset), reg);
4041 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004042}
4043
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004044void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004045 ScratchRegisterScope ensure_scratch1(
4046 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004047
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004048 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4049 ScratchRegisterScope ensure_scratch2(
4050 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004051
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004052 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4053 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4054 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4055 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4056 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4057 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004058}
4059
4060void ParallelMoveResolverX86::EmitSwap(size_t index) {
4061 MoveOperands* move = moves_.Get(index);
4062 Location source = move->GetSource();
4063 Location destination = move->GetDestination();
4064
4065 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004066 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004067 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004069 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004071 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4072 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004073 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4074 // Use XOR Swap algorithm to avoid a temporary.
4075 DCHECK_NE(source.reg(), destination.reg());
4076 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4077 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4078 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4079 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4080 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4081 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4082 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004083 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4084 // Take advantage of the 16 bytes in the XMM register.
4085 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4086 Address stack(ESP, destination.GetStackIndex());
4087 // Load the double into the high doubleword.
4088 __ movhpd(reg, stack);
4089
4090 // Store the low double into the destination.
4091 __ movsd(stack, reg);
4092
4093 // Move the high double to the low double.
4094 __ psrldq(reg, Immediate(8));
4095 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4096 // Take advantage of the 16 bytes in the XMM register.
4097 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4098 Address stack(ESP, source.GetStackIndex());
4099 // Load the double into the high doubleword.
4100 __ movhpd(reg, stack);
4101
4102 // Store the low double into the destination.
4103 __ movsd(stack, reg);
4104
4105 // Move the high double to the low double.
4106 __ psrldq(reg, Immediate(8));
4107 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4108 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4109 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004110 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004111 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004112 }
4113}
4114
4115void ParallelMoveResolverX86::SpillScratch(int reg) {
4116 __ pushl(static_cast<Register>(reg));
4117}
4118
4119void ParallelMoveResolverX86::RestoreScratch(int reg) {
4120 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004121}
4122
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004123void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004124 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4125 ? LocationSummary::kCallOnSlowPath
4126 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004127 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004128 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004129 locations->SetOut(Location::RequiresRegister());
4130}
4131
4132void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004134 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004135 DCHECK(!cls->CanCallRuntime());
4136 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004137 codegen_->LoadCurrentMethod(out);
4138 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4139 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004140 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 codegen_->LoadCurrentMethod(out);
4142 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4143 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004144
4145 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4146 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4147 codegen_->AddSlowPath(slow_path);
4148 __ testl(out, out);
4149 __ j(kEqual, slow_path->GetEntryLabel());
4150 if (cls->MustGenerateClinitCheck()) {
4151 GenerateClassInitializationCheck(slow_path, out);
4152 } else {
4153 __ Bind(slow_path->GetExitLabel());
4154 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004155 }
4156}
4157
4158void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4159 LocationSummary* locations =
4160 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4161 locations->SetInAt(0, Location::RequiresRegister());
4162 if (check->HasUses()) {
4163 locations->SetOut(Location::SameAsFirstInput());
4164 }
4165}
4166
4167void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004168 // We assume the class to not be null.
4169 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4170 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004171 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004172 GenerateClassInitializationCheck(slow_path,
4173 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004174}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004175
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004176void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4177 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004178 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4179 Immediate(mirror::Class::kStatusInitialized));
4180 __ j(kLess, slow_path->GetEntryLabel());
4181 __ Bind(slow_path->GetExitLabel());
4182 // No need for memory fence, thanks to the X86 memory model.
4183}
4184
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004185void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4186 LocationSummary* locations =
4187 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4188 locations->SetOut(Location::RequiresRegister());
4189}
4190
4191void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4192 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4193 codegen_->AddSlowPath(slow_path);
4194
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004196 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004197 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4198 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004199 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4200 __ testl(out, out);
4201 __ j(kEqual, slow_path->GetEntryLabel());
4202 __ Bind(slow_path->GetExitLabel());
4203}
4204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004205void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4206 LocationSummary* locations =
4207 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4208 locations->SetOut(Location::RequiresRegister());
4209}
4210
4211void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4212 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004213 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004214 __ fs()->movl(address, Immediate(0));
4215}
4216
4217void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4218 LocationSummary* locations =
4219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4220 InvokeRuntimeCallingConvention calling_convention;
4221 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4222}
4223
4224void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4225 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4226 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4227}
4228
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004229void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004230 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4231 ? LocationSummary::kNoCall
4232 : LocationSummary::kCallOnSlowPath;
4233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetInAt(1, Location::Any());
4236 locations->SetOut(Location::RequiresRegister());
4237}
4238
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004239void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004240 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004241 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004242 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004244 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4245 Label done, zero;
4246 SlowPathCodeX86* slow_path = nullptr;
4247
4248 // Return 0 if `obj` is null.
4249 // TODO: avoid this check if we know obj is not null.
4250 __ testl(obj, obj);
4251 __ j(kEqual, &zero);
4252 __ movl(out, Address(obj, class_offset));
4253 // Compare the class of `obj` with `cls`.
4254 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004255 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004256 } else {
4257 DCHECK(cls.IsStackSlot()) << cls;
4258 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4259 }
4260
4261 if (instruction->IsClassFinal()) {
4262 // Classes must be equal for the instanceof to succeed.
4263 __ j(kNotEqual, &zero);
4264 __ movl(out, Immediate(1));
4265 __ jmp(&done);
4266 } else {
4267 // If the classes are not equal, we go into a slow path.
4268 DCHECK(locations->OnlyCallsOnSlowPath());
4269 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004270 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004271 codegen_->AddSlowPath(slow_path);
4272 __ j(kNotEqual, slow_path->GetEntryLabel());
4273 __ movl(out, Immediate(1));
4274 __ jmp(&done);
4275 }
4276 __ Bind(&zero);
4277 __ movl(out, Immediate(0));
4278 if (slow_path != nullptr) {
4279 __ Bind(slow_path->GetExitLabel());
4280 }
4281 __ Bind(&done);
4282}
4283
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004284void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4286 instruction, LocationSummary::kCallOnSlowPath);
4287 locations->SetInAt(0, Location::RequiresRegister());
4288 locations->SetInAt(1, Location::Any());
4289 locations->AddTemp(Location::RequiresRegister());
4290}
4291
4292void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4293 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004294 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004295 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004296 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004297 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4298 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4299 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4300 codegen_->AddSlowPath(slow_path);
4301
4302 // TODO: avoid this check if we know obj is not null.
4303 __ testl(obj, obj);
4304 __ j(kEqual, slow_path->GetExitLabel());
4305 __ movl(temp, Address(obj, class_offset));
4306
4307 // Compare the class of `obj` with `cls`.
4308 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004309 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004310 } else {
4311 DCHECK(cls.IsStackSlot()) << cls;
4312 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4313 }
4314
4315 __ j(kNotEqual, slow_path->GetEntryLabel());
4316 __ Bind(slow_path->GetExitLabel());
4317}
4318
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004319void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4320 LocationSummary* locations =
4321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4322 InvokeRuntimeCallingConvention calling_convention;
4323 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4324}
4325
4326void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4327 __ fs()->call(Address::Absolute(instruction->IsEnter()
4328 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4329 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4330 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4331}
4332
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004333void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4334void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4335void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4336
4337void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4338 LocationSummary* locations =
4339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4340 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4341 || instruction->GetResultType() == Primitive::kPrimLong);
4342 locations->SetInAt(0, Location::RequiresRegister());
4343 locations->SetInAt(1, Location::Any());
4344 locations->SetOut(Location::SameAsFirstInput());
4345}
4346
4347void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4348 HandleBitwiseOperation(instruction);
4349}
4350
4351void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4352 HandleBitwiseOperation(instruction);
4353}
4354
4355void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4356 HandleBitwiseOperation(instruction);
4357}
4358
4359void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4360 LocationSummary* locations = instruction->GetLocations();
4361 Location first = locations->InAt(0);
4362 Location second = locations->InAt(1);
4363 DCHECK(first.Equals(locations->Out()));
4364
4365 if (instruction->GetResultType() == Primitive::kPrimInt) {
4366 if (second.IsRegister()) {
4367 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004369 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004371 } else {
4372 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004373 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004374 }
4375 } else if (second.IsConstant()) {
4376 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004377 __ andl(first.AsRegister<Register>(),
4378 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004379 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004380 __ orl(first.AsRegister<Register>(),
4381 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004382 } else {
4383 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004384 __ xorl(first.AsRegister<Register>(),
4385 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004386 }
4387 } else {
4388 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004389 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004390 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004391 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004392 } else {
4393 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004395 }
4396 }
4397 } else {
4398 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4399 if (second.IsRegisterPair()) {
4400 if (instruction->IsAnd()) {
4401 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4402 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4403 } else if (instruction->IsOr()) {
4404 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4405 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4406 } else {
4407 DCHECK(instruction->IsXor());
4408 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4409 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4410 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004411 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004412 if (instruction->IsAnd()) {
4413 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4414 __ andl(first.AsRegisterPairHigh<Register>(),
4415 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4416 } else if (instruction->IsOr()) {
4417 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4418 __ orl(first.AsRegisterPairHigh<Register>(),
4419 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4420 } else {
4421 DCHECK(instruction->IsXor());
4422 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4423 __ xorl(first.AsRegisterPairHigh<Register>(),
4424 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4425 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004426 } else {
4427 DCHECK(second.IsConstant()) << second;
4428 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004429 int32_t low_value = Low32Bits(value);
4430 int32_t high_value = High32Bits(value);
4431 Immediate low(low_value);
4432 Immediate high(high_value);
4433 Register first_low = first.AsRegisterPairLow<Register>();
4434 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004435 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004436 if (low_value == 0) {
4437 __ xorl(first_low, first_low);
4438 } else if (low_value != -1) {
4439 __ andl(first_low, low);
4440 }
4441 if (high_value == 0) {
4442 __ xorl(first_high, first_high);
4443 } else if (high_value != -1) {
4444 __ andl(first_high, high);
4445 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004446 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004447 if (low_value != 0) {
4448 __ orl(first_low, low);
4449 }
4450 if (high_value != 0) {
4451 __ orl(first_high, high);
4452 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004453 } else {
4454 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004455 if (low_value != 0) {
4456 __ xorl(first_low, low);
4457 }
4458 if (high_value != 0) {
4459 __ xorl(first_high, high);
4460 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004461 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004462 }
4463 }
4464}
4465
Calin Juravleb1498f62015-02-16 13:13:29 +00004466void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4467 // Nothing to do, this should be removed during prepare for register allocator.
4468 UNUSED(instruction);
4469 LOG(FATAL) << "Unreachable";
4470}
4471
4472void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4473 // Nothing to do, this should be removed during prepare for register allocator.
4474 UNUSED(instruction);
4475 LOG(FATAL) << "Unreachable";
4476}
4477
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004478} // namespace x86
4479} // namespace art