blob: 6ac4ad798c112b7dd2cf17018741ebe98c5f4a89 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
156 private:
157 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 Label return_label_;
160
161 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
162};
163
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000164class LoadStringSlowPathX86 : public SlowPathCodeX86 {
165 public:
166 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
167
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169 LocationSummary* locations = instruction_->GetLocations();
170 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
171
172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000175
176 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800177 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
178 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000181 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183
184 __ jmp(GetExitLabel());
185 }
186
187 private:
188 HLoadString* const instruction_;
189
190 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
191};
192
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193class LoadClassSlowPathX86 : public SlowPathCodeX86 {
194 public:
195 LoadClassSlowPathX86(HLoadClass* cls,
196 HInstruction* at,
197 uint32_t dex_pc,
198 bool do_clinit)
199 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
200 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201 }
202
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LocationSummary* locations = at_->GetLocations();
205 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
206 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208
209 InvokeRuntimeCallingConvention calling_convention;
210 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
211 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
212 __ fs()->call(Address::Absolute(do_clinit_
213 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
214 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216
217 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000218 Location out = locations->Out();
219 if (out.IsValid()) {
220 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
221 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000223
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 // The class this slow path will load.
230 HLoadClass* const cls_;
231
232 // The instruction where this slow path is happening.
233 // (Might be the load class or an initialization check).
234 HInstruction* const at_;
235
236 // The dex PC of `at_`.
237 const uint32_t dex_pc_;
238
239 // Whether to initialize the class.
240 const bool do_clinit_;
241
242 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
243};
244
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
246 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000247 TypeCheckSlowPathX86(HInstruction* instruction,
248 Location class_to_check,
249 Location object_class,
250 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000252 class_to_check_(class_to_check),
253 object_class_(object_class),
254 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000258 DCHECK(instruction_->IsCheckCast()
259 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260
261 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000268 x86_codegen->EmitParallelMoves(
269 class_to_check_,
270 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100271 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000272 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100273 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
274 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000276 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000277 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
278 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 } else {
280 DCHECK(instruction_->IsCheckCast());
281 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 if (instruction_->IsInstanceOf()) {
286 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
287 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 __ jmp(GetExitLabel());
291 }
292
293 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 HInstruction* const instruction_;
295 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
300};
301
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700302class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
303 public:
304 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
305 : instruction_(instruction) {}
306
307 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
308 __ Bind(GetEntryLabel());
309 SaveLiveRegisters(codegen, instruction_->GetLocations());
310 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
311 // No need to restore live registers.
312 DCHECK(instruction_->IsDeoptimize());
313 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
314 uint32_t dex_pc = deoptimize->GetDexPc();
315 codegen->RecordPcInfo(instruction_, dex_pc, this);
316 }
317
318 private:
319 HInstruction* const instruction_;
320 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
321};
322
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100323#undef __
324#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
325
Dave Allison20dfc792014-06-16 20:44:29 -0700326inline Condition X86Condition(IfCondition cond) {
327 switch (cond) {
328 case kCondEQ: return kEqual;
329 case kCondNE: return kNotEqual;
330 case kCondLT: return kLess;
331 case kCondLE: return kLessEqual;
332 case kCondGT: return kGreater;
333 case kCondGE: return kGreaterEqual;
334 default:
335 LOG(FATAL) << "Unknown if condition";
336 }
337 return kEqual;
338}
339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
341 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
342}
343
344void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
346}
347
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
349 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
350 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Mark Mendell7c8d0092015-01-26 11:21:33 -0500358size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
359 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
360 return GetFloatingPointSpillSlotSize();
361}
362
363size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
365 return GetFloatingPointSpillSlotSize();
366}
367
Mark Mendellfb8d2792015-03-31 22:16:59 -0400368CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
369 const X86InstructionSetFeatures& isa_features,
370 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500371 : CodeGenerator(graph,
372 kNumberOfCpuRegisters,
373 kNumberOfXmmRegisters,
374 kNumberOfRegisterPairs,
375 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
376 arraysize(kCoreCalleeSaves))
377 | (1 << kFakeReturnRegister),
378 0,
379 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400383 move_resolver_(graph->GetArena(), this),
384 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 // Use a fake return address register to mimic Quick.
386 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100387}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 switch (type) {
391 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 X86ManagedRegister pair =
394 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100395 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100397 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
398 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100399 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100400 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401 }
402
403 case Primitive::kPrimByte:
404 case Primitive::kPrimBoolean:
405 case Primitive::kPrimChar:
406 case Primitive::kPrimShort:
407 case Primitive::kPrimInt:
408 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100410 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
413 X86ManagedRegister current =
414 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
415 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420 }
421
422 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 return Location::FpuRegisterLocation(
425 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427
428 case Primitive::kPrimVoid:
429 LOG(FATAL) << "Unreachable type " << type;
430 }
431
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433}
434
Mark Mendell5f874182015-03-04 15:42:45 -0500435void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438
439 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
Mark Mendell5f874182015-03-04 15:42:45 -0500442 if (is_baseline) {
443 blocked_core_registers_[EBP] = true;
444 blocked_core_registers_[ESI] = true;
445 blocked_core_registers_[EDI] = true;
446 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100447
448 UpdateBlockedPairRegisters();
449}
450
451void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
452 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
453 X86ManagedRegister current =
454 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
455 if (blocked_core_registers_[current.AsRegisterPairLow()]
456 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
457 blocked_register_pairs_[i] = true;
458 }
459 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100460}
461
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
463 : HGraphVisitor(graph),
464 assembler_(codegen->GetAssembler()),
465 codegen_(codegen) {}
466
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100468 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100469}
470
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000471void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000473 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000474 bool skip_overflow_check =
475 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000477
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100479 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100480 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100481 }
482
Mark Mendell5f874182015-03-04 15:42:45 -0500483 if (HasEmptyFrame()) {
484 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000485 }
Mark Mendell5f874182015-03-04 15:42:45 -0500486
487 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
488 Register reg = kCoreCalleeSaves[i];
489 if (allocated_registers_.ContainsCoreRegister(reg)) {
490 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 __ cfi().AdjustCFAOffset(kX86WordSize);
492 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500493 }
494 }
495
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496 int adjust = GetFrameSize() - FrameEntrySpillSize();
497 __ subl(ESP, Immediate(adjust));
498 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500499 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500}
501
502void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100503 __ cfi().RememberState();
504 if (!HasEmptyFrame()) {
505 int adjust = GetFrameSize() - FrameEntrySpillSize();
506 __ addl(ESP, Immediate(adjust));
507 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500508
David Srbeckyc34dc932015-04-12 09:27:43 +0100509 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
510 Register reg = kCoreCalleeSaves[i];
511 if (allocated_registers_.ContainsCoreRegister(reg)) {
512 __ popl(reg);
513 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
514 __ cfi().Restore(DWARFReg(reg));
515 }
Mark Mendell5f874182015-03-04 15:42:45 -0500516 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000517 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100518 __ ret();
519 __ cfi().RestoreState();
520 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100523void CodeGeneratorX86::Bind(HBasicBlock* block) {
524 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525}
526
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100527void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000528 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100529 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000530}
531
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
533 switch (load->GetType()) {
534 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537
538 case Primitive::kPrimInt:
539 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542
543 case Primitive::kPrimBoolean:
544 case Primitive::kPrimByte:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimVoid:
548 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700549 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550 }
551
552 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700553 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100554}
555
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
557 switch (type) {
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimByte:
560 case Primitive::kPrimChar:
561 case Primitive::kPrimShort:
562 case Primitive::kPrimInt:
563 case Primitive::kPrimNot: {
564 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000565 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100567 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000569 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100570 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000573 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574 uint32_t index = gp_index_;
575 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100578 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
579 calling_convention.GetRegisterPairAt(index));
580 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100581 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000582 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
583 }
584 }
585
586 case Primitive::kPrimFloat: {
587 uint32_t index = fp_index_++;
588 stack_index_++;
589 if (index < calling_convention.GetNumberOfFpuRegisters()) {
590 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
591 } else {
592 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
593 }
594 }
595
596 case Primitive::kPrimDouble: {
597 uint32_t index = fp_index_++;
598 stack_index_ += 2;
599 if (index < calling_convention.GetNumberOfFpuRegisters()) {
600 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
601 } else {
602 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 }
604 }
605
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 case Primitive::kPrimVoid:
607 LOG(FATAL) << "Unexpected parameter type " << type;
608 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100609 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100610 return Location();
611}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100612
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613void CodeGeneratorX86::Move32(Location destination, Location source) {
614 if (source.Equals(destination)) {
615 return;
616 }
617 if (destination.IsRegister()) {
618 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 } else {
623 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (destination.IsFpuRegister()) {
627 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else {
632 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000636 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500641 } else if (source.IsConstant()) {
642 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000643 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500644 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 } else {
646 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100647 __ pushl(Address(ESP, source.GetStackIndex()));
648 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 }
650 }
651}
652
653void CodeGeneratorX86::Move64(Location destination, Location source) {
654 if (source.Equals(destination)) {
655 return;
656 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 if (destination.IsRegisterPair()) {
658 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 EmitParallelMoves(
660 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
661 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100662 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100664 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
665 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (source.IsFpuRegister()) {
667 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000669 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
672 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
674 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500676 if (source.IsFpuRegister()) {
677 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
678 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000679 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100680 } else {
681 LOG(FATAL) << "Unimplemented";
682 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000684 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000686 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000692 } else if (source.IsConstant()) {
693 HConstant* constant = source.GetConstant();
694 int64_t value;
695 if (constant->IsLongConstant()) {
696 value = constant->AsLongConstant()->GetValue();
697 } else {
698 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000699 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000700 }
701 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
702 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000704 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000705 EmitParallelMoves(
706 Location::StackSlot(source.GetStackIndex()),
707 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000709 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100710 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
711 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 }
713 }
714}
715
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100716void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000717 LocationSummary* locations = instruction->GetLocations();
718 if (locations != nullptr && locations->Out().Equals(location)) {
719 return;
720 }
721
722 if (locations != nullptr && locations->Out().IsConstant()) {
723 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000724 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
725 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000728 } else if (location.IsStackSlot()) {
729 __ movl(Address(ESP, location.GetStackIndex()), imm);
730 } else {
731 DCHECK(location.IsConstant());
732 DCHECK_EQ(location.GetConstant(), const_to_move);
733 }
734 } else if (const_to_move->IsLongConstant()) {
735 int64_t value = const_to_move->AsLongConstant()->GetValue();
736 if (location.IsRegisterPair()) {
737 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
738 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
739 } else if (location.IsDoubleStackSlot()) {
740 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000741 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
742 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 } else {
744 DCHECK(location.IsConstant());
745 DCHECK_EQ(location.GetConstant(), instruction);
746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000748 } else if (instruction->IsTemporary()) {
749 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000750 if (temp_location.IsStackSlot()) {
751 Move32(location, temp_location);
752 } else {
753 DCHECK(temp_location.IsDoubleStackSlot());
754 Move64(location, temp_location);
755 }
Roland Levillain476df552014-10-09 17:51:36 +0100756 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100758 switch (instruction->GetType()) {
759 case Primitive::kPrimBoolean:
760 case Primitive::kPrimByte:
761 case Primitive::kPrimChar:
762 case Primitive::kPrimShort:
763 case Primitive::kPrimInt:
764 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 case Primitive::kPrimFloat:
766 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 break;
768
769 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 case Primitive::kPrimDouble:
771 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 break;
773
774 default:
775 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
776 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100778 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 switch (instruction->GetType()) {
780 case Primitive::kPrimBoolean:
781 case Primitive::kPrimByte:
782 case Primitive::kPrimChar:
783 case Primitive::kPrimShort:
784 case Primitive::kPrimInt:
785 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 break;
789
790 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000792 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794
795 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798 }
799}
800
801void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803}
804
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 DCHECK(!successor->IsExitBlock());
808
809 HBasicBlock* block = got->GetBlock();
810 HInstruction* previous = got->GetPrevious();
811
812 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000813 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
815 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
816 return;
817 }
818
819 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
820 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
821 }
822 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000823 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000824 }
825}
826
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700832 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000833}
834
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700835void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
836 Label* true_target,
837 Label* false_target,
838 Label* always_true_target) {
839 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (cond->IsIntConstant()) {
841 // Constant condition, statically compared against 1.
842 int32_t cond_value = cond->AsIntConstant()->GetValue();
843 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844 if (always_true_target != nullptr) {
845 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100846 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100848 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 DCHECK_EQ(cond_value, 0);
850 }
851 } else {
852 bool materialized =
853 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
854 // Moves do not affect the eflags register, so if the condition is
855 // evaluated just before the if, we don't need to evaluate it
856 // again.
857 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700858 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100859 if (materialized) {
860 if (!eflags_set) {
861 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500864 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 } else {
866 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
867 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700870 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 }
872 } else {
873 Location lhs = cond->GetLocations()->InAt(0);
874 Location rhs = cond->GetLocations()->InAt(1);
875 // LHS is guaranteed to be in a register (see
876 // LocationsBuilderX86::VisitCondition).
877 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500880 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
881 if (constant == 0) {
882 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
883 } else {
884 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
885 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000887 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700889 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 if (false_target != nullptr) {
893 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894 }
895}
896
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700897void LocationsBuilderX86::VisitIf(HIf* if_instr) {
898 LocationSummary* locations =
899 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
900 HInstruction* cond = if_instr->InputAt(0);
901 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
902 locations->SetInAt(0, Location::Any());
903 }
904}
905
906void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
907 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
908 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
909 Label* always_true_target = true_target;
910 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
911 if_instr->IfTrueSuccessor())) {
912 always_true_target = nullptr;
913 }
914 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
915 if_instr->IfFalseSuccessor())) {
916 false_target = nullptr;
917 }
918 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
919}
920
921void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
922 LocationSummary* locations = new (GetGraph()->GetArena())
923 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
924 HInstruction* cond = deoptimize->InputAt(0);
925 DCHECK(cond->IsCondition());
926 if (cond->AsCondition()->NeedsMaterialization()) {
927 locations->SetInAt(0, Location::Any());
928 }
929}
930
931void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
932 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
933 DeoptimizationSlowPathX86(deoptimize);
934 codegen_->AddSlowPath(slow_path);
935 Label* slow_path_entry = slow_path->GetEntryLabel();
936 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
937}
938
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941}
942
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000943void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
944 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000945}
946
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100948 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949}
950
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000951void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100952 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700953 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000954}
955
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 switch (store->InputAt(1)->GetType()) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
968 break;
969
970 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
973 break;
974
975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 }
978 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983}
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100988 locations->SetInAt(0, Location::RequiresRegister());
989 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100990 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500991 // We need a byte register.
992 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100993 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000994}
995
Dave Allison20dfc792014-06-16 20:44:29 -0700996void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
997 if (comp->NeedsMaterialization()) {
998 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001000 // Clear register: setcc only sets the low byte.
1001 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001002 Location lhs = locations->InAt(0);
1003 Location rhs = locations->InAt(1);
1004 if (rhs.IsRegister()) {
1005 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1006 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001007 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001008 if (constant == 0) {
1009 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1010 } else {
1011 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1012 }
Dave Allison20dfc792014-06-16 20:44:29 -07001013 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001014 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001015 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001016 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001017 }
Dave Allison20dfc792014-06-16 20:44:29 -07001018}
1019
1020void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1065 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
1068void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001069 LocationSummary* locations =
1070 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001071 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072}
1073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001076 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077}
1078
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001079void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1080 LocationSummary* locations =
1081 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1082 locations->SetOut(Location::ConstantLocation(constant));
1083}
1084
1085void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1086 // Will be generated at use site.
1087 UNUSED(constant);
1088}
1089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 LocationSummary* locations =
1092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001093 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094}
1095
1096void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1097 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001098 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099}
1100
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001101void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1102 LocationSummary* locations =
1103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1104 locations->SetOut(Location::ConstantLocation(constant));
1105}
1106
1107void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001110}
1111
1112void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1113 LocationSummary* locations =
1114 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1115 locations->SetOut(Location::ConstantLocation(constant));
1116}
1117
1118void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1119 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001121}
1122
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) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001199 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001200 if (intrinsic.TryDispatch(invoke)) {
1201 return;
1202 }
1203
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001204 HandleInvoke(invoke);
1205}
1206
Mark Mendell09ed1a32015-03-25 08:30:06 -04001207static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1208 if (invoke->GetLocations()->Intrinsified()) {
1209 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1210 intrinsic.Dispatch(invoke);
1211 return true;
1212 }
1213 return false;
1214}
1215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001216void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001217 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1218 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001219 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001220
Mark Mendell09ed1a32015-03-25 08:30:06 -04001221 codegen_->GenerateStaticOrDirectCall(
1222 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001223}
1224
1225void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1226 HandleInvoke(invoke);
1227}
1228
1229void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001230 LocationSummary* locations =
1231 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001232 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001233
1234 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001236 HInstruction* input = invoke->InputAt(i);
1237 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1238 }
1239
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 switch (invoke->GetType()) {
1241 case Primitive::kPrimBoolean:
1242 case Primitive::kPrimByte:
1243 case Primitive::kPrimChar:
1244 case Primitive::kPrimShort:
1245 case Primitive::kPrimInt:
1246 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001247 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 break;
1249
1250 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001251 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 break;
1253
1254 case Primitive::kPrimVoid:
1255 break;
1256
1257 case Primitive::kPrimDouble:
1258 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001259 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 break;
1261 }
1262
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001263 invoke->SetLocations(locations);
1264}
1265
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001267 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1269 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1270 LocationSummary* locations = invoke->GetLocations();
1271 Location receiver = locations->InAt(0);
1272 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1273 // temp = object->GetClass();
1274 if (receiver.IsStackSlot()) {
1275 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1276 __ movl(temp, Address(temp, class_offset));
1277 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281 // temp = temp->GetMethodAt(method_offset);
1282 __ movl(temp, Address(temp, method_offset));
1283 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001284 __ call(Address(
1285 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001286
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001287 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001289}
1290
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001291void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1292 HandleInvoke(invoke);
1293 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001294 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001295}
1296
1297void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1298 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1301 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1302 LocationSummary* locations = invoke->GetLocations();
1303 Location receiver = locations->InAt(0);
1304 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1305
1306 // Set the hidden argument.
1307 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001309
1310 // temp = object->GetClass();
1311 if (receiver.IsStackSlot()) {
1312 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1313 __ movl(temp, Address(temp, class_offset));
1314 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001317 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318 // temp = temp->GetImtEntryAt(method_offset);
1319 __ movl(temp, Address(temp, method_offset));
1320 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001321 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001322 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323
1324 DCHECK(!codegen_->IsLeafMethod());
1325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1326}
1327
Roland Levillain88cb1752014-10-20 16:36:47 +01001328void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1331 switch (neg->GetResultType()) {
1332 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001333 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetOut(Location::SameAsFirstInput());
1336 break;
1337
Roland Levillain88cb1752014-10-20 16:36:47 +01001338 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001339 locations->SetInAt(0, Location::RequiresFpuRegister());
1340 locations->SetOut(Location::SameAsFirstInput());
1341 locations->AddTemp(Location::RequiresRegister());
1342 locations->AddTemp(Location::RequiresFpuRegister());
1343 break;
1344
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001346 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001347 locations->SetOut(Location::SameAsFirstInput());
1348 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1353 }
1354}
1355
1356void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1357 LocationSummary* locations = neg->GetLocations();
1358 Location out = locations->Out();
1359 Location in = locations->InAt(0);
1360 switch (neg->GetResultType()) {
1361 case Primitive::kPrimInt:
1362 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001363 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001364 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001365 break;
1366
1367 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001368 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001369 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001370 __ negl(out.AsRegisterPairLow<Register>());
1371 // Negation is similar to subtraction from zero. The least
1372 // significant byte triggers a borrow when it is different from
1373 // zero; to take it into account, add 1 to the most significant
1374 // byte if the carry flag (CF) is set to 1 after the first NEGL
1375 // operation.
1376 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1377 __ negl(out.AsRegisterPairHigh<Register>());
1378 break;
1379
Roland Levillain5368c212014-11-27 15:03:41 +00001380 case Primitive::kPrimFloat: {
1381 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001382 Register constant = locations->GetTemp(0).AsRegister<Register>();
1383 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001384 // Implement float negation with an exclusive or with value
1385 // 0x80000000 (mask for bit 31, representing the sign of a
1386 // single-precision floating-point number).
1387 __ movl(constant, Immediate(INT32_C(0x80000000)));
1388 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001390 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001391 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001392
Roland Levillain5368c212014-11-27 15:03:41 +00001393 case Primitive::kPrimDouble: {
1394 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001395 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001396 // Implement double negation with an exclusive or with value
1397 // 0x8000000000000000 (mask for bit 63, representing the sign of
1398 // a double-precision floating-point number).
1399 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001402 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001403
1404 default:
1405 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1406 }
1407}
1408
Roland Levillaindff1f282014-11-05 14:15:05 +00001409void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001410 Primitive::Type result_type = conversion->GetResultType();
1411 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001412 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001413
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001414 // The float-to-long and double-to-long type conversions rely on a
1415 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001416 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001417 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1418 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001419 ? LocationSummary::kCall
1420 : LocationSummary::kNoCall;
1421 LocationSummary* locations =
1422 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1423
David Brazdilb2bd1c52015-03-25 11:17:37 +00001424 // The Java language does not allow treating boolean as an integral type but
1425 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001426
Roland Levillaindff1f282014-11-05 14:15:05 +00001427 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001428 case Primitive::kPrimByte:
1429 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001430 case Primitive::kPrimBoolean:
1431 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001432 case Primitive::kPrimShort:
1433 case Primitive::kPrimInt:
1434 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001435 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001436 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1437 // Make the output overlap to please the register allocator. This greatly simplifies
1438 // the validation of the linear scan implementation
1439 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001440 break;
1441
1442 default:
1443 LOG(FATAL) << "Unexpected type conversion from " << input_type
1444 << " to " << result_type;
1445 }
1446 break;
1447
Roland Levillain01a8d712014-11-14 16:27:39 +00001448 case Primitive::kPrimShort:
1449 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001450 case Primitive::kPrimBoolean:
1451 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001452 case Primitive::kPrimByte:
1453 case Primitive::kPrimInt:
1454 case Primitive::kPrimChar:
1455 // Processing a Dex `int-to-short' instruction.
1456 locations->SetInAt(0, Location::Any());
1457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1458 break;
1459
1460 default:
1461 LOG(FATAL) << "Unexpected type conversion from " << input_type
1462 << " to " << result_type;
1463 }
1464 break;
1465
Roland Levillain946e1432014-11-11 17:35:19 +00001466 case Primitive::kPrimInt:
1467 switch (input_type) {
1468 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001469 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001470 locations->SetInAt(0, Location::Any());
1471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1472 break;
1473
1474 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001475 // Processing a Dex `float-to-int' instruction.
1476 locations->SetInAt(0, Location::RequiresFpuRegister());
1477 locations->SetOut(Location::RequiresRegister());
1478 locations->AddTemp(Location::RequiresFpuRegister());
1479 break;
1480
Roland Levillain946e1432014-11-11 17:35:19 +00001481 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001482 // Processing a Dex `double-to-int' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresRegister());
1485 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001486 break;
1487
1488 default:
1489 LOG(FATAL) << "Unexpected type conversion from " << input_type
1490 << " to " << result_type;
1491 }
1492 break;
1493
Roland Levillaindff1f282014-11-05 14:15:05 +00001494 case Primitive::kPrimLong:
1495 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001496 case Primitive::kPrimBoolean:
1497 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001498 case Primitive::kPrimByte:
1499 case Primitive::kPrimShort:
1500 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001501 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001502 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001503 locations->SetInAt(0, Location::RegisterLocation(EAX));
1504 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1505 break;
1506
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001507 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001508 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001509 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001510 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001511 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1512 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1513
Vladimir Marko949c91f2015-01-27 10:48:44 +00001514 // The runtime helper puts the result in EAX, EDX.
1515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001516 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001517 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001518
1519 default:
1520 LOG(FATAL) << "Unexpected type conversion from " << input_type
1521 << " to " << result_type;
1522 }
1523 break;
1524
Roland Levillain981e4542014-11-14 11:47:14 +00001525 case Primitive::kPrimChar:
1526 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001527 case Primitive::kPrimBoolean:
1528 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001529 case Primitive::kPrimByte:
1530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001532 // Processing a Dex `int-to-char' instruction.
1533 locations->SetInAt(0, Location::Any());
1534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1535 break;
1536
1537 default:
1538 LOG(FATAL) << "Unexpected type conversion from " << input_type
1539 << " to " << result_type;
1540 }
1541 break;
1542
Roland Levillaindff1f282014-11-05 14:15:05 +00001543 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001544 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001545 case Primitive::kPrimBoolean:
1546 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001547 case Primitive::kPrimByte:
1548 case Primitive::kPrimShort:
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimChar:
1551 // Processing a Dex `int-to-float' instruction.
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetOut(Location::RequiresFpuRegister());
1554 break;
1555
1556 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001557 // Processing a Dex `long-to-float' instruction.
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetOut(Location::RequiresFpuRegister());
1560 locations->AddTemp(Location::RequiresFpuRegister());
1561 locations->AddTemp(Location::RequiresFpuRegister());
1562 break;
1563
Roland Levillaincff13742014-11-17 14:32:17 +00001564 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001565 // Processing a Dex `double-to-float' instruction.
1566 locations->SetInAt(0, Location::RequiresFpuRegister());
1567 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001568 break;
1569
1570 default:
1571 LOG(FATAL) << "Unexpected type conversion from " << input_type
1572 << " to " << result_type;
1573 };
1574 break;
1575
Roland Levillaindff1f282014-11-05 14:15:05 +00001576 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001577 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001578 case Primitive::kPrimBoolean:
1579 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001580 case Primitive::kPrimByte:
1581 case Primitive::kPrimShort:
1582 case Primitive::kPrimInt:
1583 case Primitive::kPrimChar:
1584 // Processing a Dex `int-to-double' instruction.
1585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetOut(Location::RequiresFpuRegister());
1587 break;
1588
1589 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001590 // Processing a Dex `long-to-double' instruction.
1591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetOut(Location::RequiresFpuRegister());
1593 locations->AddTemp(Location::RequiresFpuRegister());
1594 locations->AddTemp(Location::RequiresFpuRegister());
1595 break;
1596
Roland Levillaincff13742014-11-17 14:32:17 +00001597 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001598 // Processing a Dex `float-to-double' instruction.
1599 locations->SetInAt(0, Location::RequiresFpuRegister());
1600 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001601 break;
1602
1603 default:
1604 LOG(FATAL) << "Unexpected type conversion from " << input_type
1605 << " to " << result_type;
1606 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001607 break;
1608
1609 default:
1610 LOG(FATAL) << "Unexpected type conversion from " << input_type
1611 << " to " << result_type;
1612 }
1613}
1614
1615void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1616 LocationSummary* locations = conversion->GetLocations();
1617 Location out = locations->Out();
1618 Location in = locations->InAt(0);
1619 Primitive::Type result_type = conversion->GetResultType();
1620 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001621 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001622 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001623 case Primitive::kPrimByte:
1624 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001625 case Primitive::kPrimBoolean:
1626 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001627 case Primitive::kPrimShort:
1628 case Primitive::kPrimInt:
1629 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001630 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001631 if (in.IsRegister()) {
1632 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001633 } else {
1634 DCHECK(in.GetConstant()->IsIntConstant());
1635 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1636 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1637 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001638 break;
1639
1640 default:
1641 LOG(FATAL) << "Unexpected type conversion from " << input_type
1642 << " to " << result_type;
1643 }
1644 break;
1645
Roland Levillain01a8d712014-11-14 16:27:39 +00001646 case Primitive::kPrimShort:
1647 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001648 case Primitive::kPrimBoolean:
1649 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001650 case Primitive::kPrimByte:
1651 case Primitive::kPrimInt:
1652 case Primitive::kPrimChar:
1653 // Processing a Dex `int-to-short' instruction.
1654 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001655 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001656 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001657 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001658 } else {
1659 DCHECK(in.GetConstant()->IsIntConstant());
1660 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001662 }
1663 break;
1664
1665 default:
1666 LOG(FATAL) << "Unexpected type conversion from " << input_type
1667 << " to " << result_type;
1668 }
1669 break;
1670
Roland Levillain946e1432014-11-11 17:35:19 +00001671 case Primitive::kPrimInt:
1672 switch (input_type) {
1673 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001674 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001675 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001676 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001677 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001678 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001679 } else {
1680 DCHECK(in.IsConstant());
1681 DCHECK(in.GetConstant()->IsLongConstant());
1682 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001684 }
1685 break;
1686
Roland Levillain3f8f9362014-12-02 17:45:01 +00001687 case Primitive::kPrimFloat: {
1688 // Processing a Dex `float-to-int' instruction.
1689 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1690 Register output = out.AsRegister<Register>();
1691 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1692 Label done, nan;
1693
1694 __ movl(output, Immediate(kPrimIntMax));
1695 // temp = int-to-float(output)
1696 __ cvtsi2ss(temp, output);
1697 // if input >= temp goto done
1698 __ comiss(input, temp);
1699 __ j(kAboveEqual, &done);
1700 // if input == NaN goto nan
1701 __ j(kUnordered, &nan);
1702 // output = float-to-int-truncate(input)
1703 __ cvttss2si(output, input);
1704 __ jmp(&done);
1705 __ Bind(&nan);
1706 // output = 0
1707 __ xorl(output, output);
1708 __ Bind(&done);
1709 break;
1710 }
1711
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001712 case Primitive::kPrimDouble: {
1713 // Processing a Dex `double-to-int' instruction.
1714 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1715 Register output = out.AsRegister<Register>();
1716 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1717 Label done, nan;
1718
1719 __ movl(output, Immediate(kPrimIntMax));
1720 // temp = int-to-double(output)
1721 __ cvtsi2sd(temp, output);
1722 // if input >= temp goto done
1723 __ comisd(input, temp);
1724 __ j(kAboveEqual, &done);
1725 // if input == NaN goto nan
1726 __ j(kUnordered, &nan);
1727 // output = double-to-int-truncate(input)
1728 __ cvttsd2si(output, input);
1729 __ jmp(&done);
1730 __ Bind(&nan);
1731 // output = 0
1732 __ xorl(output, output);
1733 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001734 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 }
Roland Levillain946e1432014-11-11 17:35:19 +00001736
1737 default:
1738 LOG(FATAL) << "Unexpected type conversion from " << input_type
1739 << " to " << result_type;
1740 }
1741 break;
1742
Roland Levillaindff1f282014-11-05 14:15:05 +00001743 case Primitive::kPrimLong:
1744 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001745 case Primitive::kPrimBoolean:
1746 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001747 case Primitive::kPrimByte:
1748 case Primitive::kPrimShort:
1749 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001750 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001751 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1753 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001754 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001755 __ cdq();
1756 break;
1757
1758 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001759 // Processing a Dex `float-to-long' instruction.
1760 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001761 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1762 break;
1763
Roland Levillaindff1f282014-11-05 14:15:05 +00001764 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001765 // Processing a Dex `double-to-long' instruction.
1766 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1767 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001768 break;
1769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 }
1774 break;
1775
Roland Levillain981e4542014-11-14 11:47:14 +00001776 case Primitive::kPrimChar:
1777 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001778 case Primitive::kPrimBoolean:
1779 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001780 case Primitive::kPrimByte:
1781 case Primitive::kPrimShort:
1782 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001783 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1784 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001785 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001786 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001787 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001788 } else {
1789 DCHECK(in.GetConstant()->IsIntConstant());
1790 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001792 }
1793 break;
1794
1795 default:
1796 LOG(FATAL) << "Unexpected type conversion from " << input_type
1797 << " to " << result_type;
1798 }
1799 break;
1800
Roland Levillaindff1f282014-11-05 14:15:05 +00001801 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001802 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001803 case Primitive::kPrimBoolean:
1804 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001805 case Primitive::kPrimByte:
1806 case Primitive::kPrimShort:
1807 case Primitive::kPrimInt:
1808 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001809 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001810 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
Roland Levillain6d0e4832014-11-27 18:31:21 +00001813 case Primitive::kPrimLong: {
1814 // Processing a Dex `long-to-float' instruction.
1815 Register low = in.AsRegisterPairLow<Register>();
1816 Register high = in.AsRegisterPairHigh<Register>();
1817 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1818 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1819 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1820
1821 // Operations use doubles for precision reasons (each 32-bit
1822 // half of a long fits in the 53-bit mantissa of a double,
1823 // but not in the 24-bit mantissa of a float). This is
1824 // especially important for the low bits. The result is
1825 // eventually converted to float.
1826
1827 // low = low - 2^31 (to prevent bit 31 of `low` to be
1828 // interpreted as a sign bit)
1829 __ subl(low, Immediate(0x80000000));
1830 // temp = int-to-double(high)
1831 __ cvtsi2sd(temp, high);
1832 // temp = temp * 2^32
1833 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1834 __ mulsd(temp, constant);
1835 // result = int-to-double(low)
1836 __ cvtsi2sd(result, low);
1837 // result = result + 2^31 (restore the original value of `low`)
1838 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1839 __ addsd(result, constant);
1840 // result = result + temp
1841 __ addsd(result, temp);
1842 // result = double-to-float(result)
1843 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001844 // Restore low.
1845 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001846 break;
1847 }
1848
Roland Levillaincff13742014-11-17 14:32:17 +00001849 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001850 // Processing a Dex `double-to-float' instruction.
1851 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001852 break;
1853
1854 default:
1855 LOG(FATAL) << "Unexpected type conversion from " << input_type
1856 << " to " << result_type;
1857 };
1858 break;
1859
Roland Levillaindff1f282014-11-05 14:15:05 +00001860 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001861 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001862 case Primitive::kPrimBoolean:
1863 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001864 case Primitive::kPrimByte:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001868 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001869 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001870 break;
1871
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872 case Primitive::kPrimLong: {
1873 // Processing a Dex `long-to-double' instruction.
1874 Register low = in.AsRegisterPairLow<Register>();
1875 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1877 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1878 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001879
Roland Levillain647b9ed2014-11-27 12:06:00 +00001880 // low = low - 2^31 (to prevent bit 31 of `low` to be
1881 // interpreted as a sign bit)
1882 __ subl(low, Immediate(0x80000000));
1883 // temp = int-to-double(high)
1884 __ cvtsi2sd(temp, high);
1885 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001886 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001887 __ mulsd(temp, constant);
1888 // result = int-to-double(low)
1889 __ cvtsi2sd(result, low);
1890 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001891 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001892 __ addsd(result, constant);
1893 // result = result + temp
1894 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001895 // Restore low.
1896 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001897 break;
1898 }
1899
Roland Levillaincff13742014-11-17 14:32:17 +00001900 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001901 // Processing a Dex `float-to-double' instruction.
1902 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001903 break;
1904
1905 default:
1906 LOG(FATAL) << "Unexpected type conversion from " << input_type
1907 << " to " << result_type;
1908 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001909 break;
1910
1911 default:
1912 LOG(FATAL) << "Unexpected type conversion from " << input_type
1913 << " to " << result_type;
1914 }
1915}
1916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 LocationSummary* locations =
1919 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001921 case Primitive::kPrimInt: {
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1925 break;
1926 }
1927
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001929 locations->SetInAt(0, Location::RequiresRegister());
1930 locations->SetInAt(1, Location::Any());
1931 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 break;
1933 }
1934
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 case Primitive::kPrimFloat:
1936 case Primitive::kPrimDouble: {
1937 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001938 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001943 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1945 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001947}
1948
1949void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1950 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001951 Location first = locations->InAt(0);
1952 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001953 Location out = locations->Out();
1954
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001958 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1959 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1960 } else {
1961 __ leal(out.AsRegister<Register>(), Address(
1962 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1963 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001965 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1966 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1967 __ addl(out.AsRegister<Register>(), Immediate(value));
1968 } else {
1969 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1970 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001971 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001972 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001975 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 }
1977
1978 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001979 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1981 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001982 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1984 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001985 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001986 } else {
1987 DCHECK(second.IsConstant()) << second;
1988 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1989 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1990 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
1993 }
1994
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995 case Primitive::kPrimFloat: {
1996 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001997 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 }
2001
2002 case Primitive::kPrimDouble: {
2003 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 }
2006 break;
2007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002010 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002011 }
2012}
2013
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002014void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002018 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::Any());
2022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023 break;
2024 }
Calin Juravle11351682014-10-23 15:38:15 +01002025 case Primitive::kPrimFloat:
2026 case Primitive::kPrimDouble: {
2027 locations->SetInAt(0, Location::RequiresFpuRegister());
2028 locations->SetInAt(1, Location::RequiresFpuRegister());
2029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030 break;
Calin Juravle11351682014-10-23 15:38:15 +01002031 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002033 default:
Calin Juravle11351682014-10-23 15:38:15 +01002034 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036}
2037
2038void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2039 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 Location first = locations->InAt(0);
2041 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002042 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002045 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002048 __ subl(first.AsRegister<Register>(),
2049 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002050 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002052 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002053 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002054 }
2055
2056 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002057 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002058 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2059 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002060 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002061 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 __ sbbl(first.AsRegisterPairHigh<Register>(),
2063 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002064 } else {
2065 DCHECK(second.IsConstant()) << second;
2066 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2067 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2068 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 break;
2071 }
2072
Calin Juravle11351682014-10-23 15:38:15 +01002073 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075 break;
Calin Juravle11351682014-10-23 15:38:15 +01002076 }
2077
2078 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002080 break;
2081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002083 default:
Calin Juravle11351682014-10-23 15:38:15 +01002084 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002085 }
2086}
2087
Calin Juravle34bacdf2014-10-07 20:23:36 +01002088void LocationsBuilderX86::VisitMul(HMul* mul) {
2089 LocationSummary* locations =
2090 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2091 switch (mul->GetResultType()) {
2092 case Primitive::kPrimInt:
2093 locations->SetInAt(0, Location::RequiresRegister());
2094 locations->SetInAt(1, Location::Any());
2095 locations->SetOut(Location::SameAsFirstInput());
2096 break;
2097 case Primitive::kPrimLong: {
2098 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002099 locations->SetInAt(1, Location::Any());
2100 locations->SetOut(Location::SameAsFirstInput());
2101 // Needed for imul on 32bits with 64bits output.
2102 locations->AddTemp(Location::RegisterLocation(EAX));
2103 locations->AddTemp(Location::RegisterLocation(EDX));
2104 break;
2105 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002106 case Primitive::kPrimFloat:
2107 case Primitive::kPrimDouble: {
2108 locations->SetInAt(0, Location::RequiresFpuRegister());
2109 locations->SetInAt(1, Location::RequiresFpuRegister());
2110 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002112 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002113
2114 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116 }
2117}
2118
2119void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2120 LocationSummary* locations = mul->GetLocations();
2121 Location first = locations->InAt(0);
2122 Location second = locations->InAt(1);
2123 DCHECK(first.Equals(locations->Out()));
2124
2125 switch (mul->GetResultType()) {
2126 case Primitive::kPrimInt: {
2127 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002129 } else if (second.IsConstant()) {
2130 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 } else {
2133 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 }
2136 break;
2137 }
2138
2139 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 Register in1_hi = first.AsRegisterPairHigh<Register>();
2141 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 Register eax = locations->GetTemp(0).AsRegister<Register>();
2143 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144
2145 DCHECK_EQ(EAX, eax);
2146 DCHECK_EQ(EDX, edx);
2147
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002148 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002149 // output: in1
2150 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2151 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2152 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002153 if (second.IsConstant()) {
2154 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002156 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2157 int32_t low_value = Low32Bits(value);
2158 int32_t high_value = High32Bits(value);
2159 Immediate low(low_value);
2160 Immediate high(high_value);
2161
2162 __ movl(eax, high);
2163 // eax <- in1.lo * in2.hi
2164 __ imull(eax, in1_lo);
2165 // in1.hi <- in1.hi * in2.lo
2166 __ imull(in1_hi, low);
2167 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2168 __ addl(in1_hi, eax);
2169 // move in2_lo to eax to prepare for double precision
2170 __ movl(eax, low);
2171 // edx:eax <- in1.lo * in2.lo
2172 __ mull(in1_lo);
2173 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2174 __ addl(in1_hi, edx);
2175 // in1.lo <- (in1.lo * in2.lo)[31:0];
2176 __ movl(in1_lo, eax);
2177 } else if (second.IsRegisterPair()) {
2178 Register in2_hi = second.AsRegisterPairHigh<Register>();
2179 Register in2_lo = second.AsRegisterPairLow<Register>();
2180
2181 __ movl(eax, in2_hi);
2182 // eax <- in1.lo * in2.hi
2183 __ imull(eax, in1_lo);
2184 // in1.hi <- in1.hi * in2.lo
2185 __ imull(in1_hi, in2_lo);
2186 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2187 __ addl(in1_hi, eax);
2188 // move in1_lo to eax to prepare for double precision
2189 __ movl(eax, in1_lo);
2190 // edx:eax <- in1.lo * in2.lo
2191 __ mull(in2_lo);
2192 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2193 __ addl(in1_hi, edx);
2194 // in1.lo <- (in1.lo * in2.lo)[31:0];
2195 __ movl(in1_lo, eax);
2196 } else {
2197 DCHECK(second.IsDoubleStackSlot()) << second;
2198 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2199 Address in2_lo(ESP, second.GetStackIndex());
2200
2201 __ movl(eax, in2_hi);
2202 // eax <- in1.lo * in2.hi
2203 __ imull(eax, in1_lo);
2204 // in1.hi <- in1.hi * in2.lo
2205 __ imull(in1_hi, in2_lo);
2206 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2207 __ addl(in1_hi, eax);
2208 // move in1_lo to eax to prepare for double precision
2209 __ movl(eax, in1_lo);
2210 // edx:eax <- in1.lo * in2.lo
2211 __ mull(in2_lo);
2212 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2213 __ addl(in1_hi, edx);
2214 // in1.lo <- (in1.lo * in2.lo)[31:0];
2215 __ movl(in1_lo, eax);
2216 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002217
2218 break;
2219 }
2220
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002223 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 }
2225
2226 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002227 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002228 break;
2229 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002230
2231 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002232 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233 }
2234}
2235
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002236void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2237 uint32_t stack_adjustment, bool is_float) {
2238 if (source.IsStackSlot()) {
2239 DCHECK(is_float);
2240 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2241 } else if (source.IsDoubleStackSlot()) {
2242 DCHECK(!is_float);
2243 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2244 } else {
2245 // Write the value to the temporary location on the stack and load to FP stack.
2246 if (is_float) {
2247 Location stack_temp = Location::StackSlot(temp_offset);
2248 codegen_->Move32(stack_temp, source);
2249 __ flds(Address(ESP, temp_offset));
2250 } else {
2251 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2252 codegen_->Move64(stack_temp, source);
2253 __ fldl(Address(ESP, temp_offset));
2254 }
2255 }
2256}
2257
2258void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2259 Primitive::Type type = rem->GetResultType();
2260 bool is_float = type == Primitive::kPrimFloat;
2261 size_t elem_size = Primitive::ComponentSize(type);
2262 LocationSummary* locations = rem->GetLocations();
2263 Location first = locations->InAt(0);
2264 Location second = locations->InAt(1);
2265 Location out = locations->Out();
2266
2267 // Create stack space for 2 elements.
2268 // TODO: enhance register allocator to ask for stack temporaries.
2269 __ subl(ESP, Immediate(2 * elem_size));
2270
2271 // Load the values to the FP stack in reverse order, using temporaries if needed.
2272 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2273 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2274
2275 // Loop doing FPREM until we stabilize.
2276 Label retry;
2277 __ Bind(&retry);
2278 __ fprem();
2279
2280 // Move FP status to AX.
2281 __ fstsw();
2282
2283 // And see if the argument reduction is complete. This is signaled by the
2284 // C2 FPU flag bit set to 0.
2285 __ andl(EAX, Immediate(kC2ConditionMask));
2286 __ j(kNotEqual, &retry);
2287
2288 // We have settled on the final value. Retrieve it into an XMM register.
2289 // Store FP top of stack to real stack.
2290 if (is_float) {
2291 __ fsts(Address(ESP, 0));
2292 } else {
2293 __ fstl(Address(ESP, 0));
2294 }
2295
2296 // Pop the 2 items from the FP stack.
2297 __ fucompp();
2298
2299 // Load the value from the stack into an XMM register.
2300 DCHECK(out.IsFpuRegister()) << out;
2301 if (is_float) {
2302 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2303 } else {
2304 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2305 }
2306
2307 // And remove the temporary stack space we allocated.
2308 __ addl(ESP, Immediate(2 * elem_size));
2309}
2310
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002311
2312void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2313 DCHECK(instruction->IsDiv() || instruction->IsRem());
2314
2315 LocationSummary* locations = instruction->GetLocations();
2316 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002317 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002318
2319 Register out_register = locations->Out().AsRegister<Register>();
2320 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002321 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002322
2323 DCHECK(imm == 1 || imm == -1);
2324
2325 if (instruction->IsRem()) {
2326 __ xorl(out_register, out_register);
2327 } else {
2328 __ movl(out_register, input_register);
2329 if (imm == -1) {
2330 __ negl(out_register);
2331 }
2332 }
2333}
2334
2335
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002336void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002337 LocationSummary* locations = instruction->GetLocations();
2338
2339 Register out_register = locations->Out().AsRegister<Register>();
2340 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002341 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002342
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002343 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002344 Register num = locations->GetTemp(0).AsRegister<Register>();
2345
2346 __ leal(num, Address(input_register, std::abs(imm) - 1));
2347 __ testl(input_register, input_register);
2348 __ cmovl(kGreaterEqual, num, input_register);
2349 int shift = CTZ(imm);
2350 __ sarl(num, Immediate(shift));
2351
2352 if (imm < 0) {
2353 __ negl(num);
2354 }
2355
2356 __ movl(out_register, num);
2357}
2358
2359void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2360 DCHECK(instruction->IsDiv() || instruction->IsRem());
2361
2362 LocationSummary* locations = instruction->GetLocations();
2363 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2364
2365 Register eax = locations->InAt(0).AsRegister<Register>();
2366 Register out = locations->Out().AsRegister<Register>();
2367 Register num;
2368 Register edx;
2369
2370 if (instruction->IsDiv()) {
2371 edx = locations->GetTemp(0).AsRegister<Register>();
2372 num = locations->GetTemp(1).AsRegister<Register>();
2373 } else {
2374 edx = locations->Out().AsRegister<Register>();
2375 num = locations->GetTemp(0).AsRegister<Register>();
2376 }
2377
2378 DCHECK_EQ(EAX, eax);
2379 DCHECK_EQ(EDX, edx);
2380 if (instruction->IsDiv()) {
2381 DCHECK_EQ(EAX, out);
2382 } else {
2383 DCHECK_EQ(EDX, out);
2384 }
2385
2386 int64_t magic;
2387 int shift;
2388 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2389
2390 Label ndiv;
2391 Label end;
2392 // If numerator is 0, the result is 0, no computation needed.
2393 __ testl(eax, eax);
2394 __ j(kNotEqual, &ndiv);
2395
2396 __ xorl(out, out);
2397 __ jmp(&end);
2398
2399 __ Bind(&ndiv);
2400
2401 // Save the numerator.
2402 __ movl(num, eax);
2403
2404 // EAX = magic
2405 __ movl(eax, Immediate(magic));
2406
2407 // EDX:EAX = magic * numerator
2408 __ imull(num);
2409
2410 if (imm > 0 && magic < 0) {
2411 // EDX += num
2412 __ addl(edx, num);
2413 } else if (imm < 0 && magic > 0) {
2414 __ subl(edx, num);
2415 }
2416
2417 // Shift if needed.
2418 if (shift != 0) {
2419 __ sarl(edx, Immediate(shift));
2420 }
2421
2422 // EDX += 1 if EDX < 0
2423 __ movl(eax, edx);
2424 __ shrl(edx, Immediate(31));
2425 __ addl(edx, eax);
2426
2427 if (instruction->IsRem()) {
2428 __ movl(eax, num);
2429 __ imull(edx, Immediate(imm));
2430 __ subl(eax, edx);
2431 __ movl(edx, eax);
2432 } else {
2433 __ movl(eax, edx);
2434 }
2435 __ Bind(&end);
2436}
2437
Calin Juravlebacfec32014-11-14 15:54:36 +00002438void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2439 DCHECK(instruction->IsDiv() || instruction->IsRem());
2440
2441 LocationSummary* locations = instruction->GetLocations();
2442 Location out = locations->Out();
2443 Location first = locations->InAt(0);
2444 Location second = locations->InAt(1);
2445 bool is_div = instruction->IsDiv();
2446
2447 switch (instruction->GetResultType()) {
2448 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 DCHECK_EQ(EAX, first.AsRegister<Register>());
2450 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002451
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002452 if (instruction->InputAt(1)->IsIntConstant()) {
2453 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002454
2455 if (imm == 0) {
2456 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2457 } else if (imm == 1 || imm == -1) {
2458 DivRemOneOrMinusOne(instruction);
2459 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002460 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002461 } else {
2462 DCHECK(imm <= -2 || imm >= 2);
2463 GenerateDivRemWithAnyConstant(instruction);
2464 }
2465 } else {
2466 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002467 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002468 is_div);
2469 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002470
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002471 Register second_reg = second.AsRegister<Register>();
2472 // 0x80000000/-1 triggers an arithmetic exception!
2473 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2474 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002475
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002476 __ cmpl(second_reg, Immediate(-1));
2477 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002478
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002479 // edx:eax <- sign-extended of eax
2480 __ cdq();
2481 // eax = quotient, edx = remainder
2482 __ idivl(second_reg);
2483 __ Bind(slow_path->GetExitLabel());
2484 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002485 break;
2486 }
2487
2488 case Primitive::kPrimLong: {
2489 InvokeRuntimeCallingConvention calling_convention;
2490 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2491 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2492 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2493 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2494 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2495 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2496
2497 if (is_div) {
2498 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2499 } else {
2500 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2501 }
2502 uint32_t dex_pc = is_div
2503 ? instruction->AsDiv()->GetDexPc()
2504 : instruction->AsRem()->GetDexPc();
2505 codegen_->RecordPcInfo(instruction, dex_pc);
2506
2507 break;
2508 }
2509
2510 default:
2511 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2512 }
2513}
2514
Calin Juravle7c4954d2014-10-28 16:57:40 +00002515void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002516 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002517 ? LocationSummary::kCall
2518 : LocationSummary::kNoCall;
2519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2520
Calin Juravle7c4954d2014-10-28 16:57:40 +00002521 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002522 case Primitive::kPrimInt: {
2523 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002524 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002525 locations->SetOut(Location::SameAsFirstInput());
2526 // Intel uses edx:eax as the dividend.
2527 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002528 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2529 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2530 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002531 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002532 locations->AddTemp(Location::RequiresRegister());
2533 }
Calin Juravled0d48522014-11-04 16:40:20 +00002534 break;
2535 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002536 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->SetInAt(0, Location::RegisterPairLocation(
2539 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2540 locations->SetInAt(1, Location::RegisterPairLocation(
2541 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2542 // Runtime helper puts the result in EAX, EDX.
2543 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 break;
2545 }
2546 case Primitive::kPrimFloat:
2547 case Primitive::kPrimDouble: {
2548 locations->SetInAt(0, Location::RequiresFpuRegister());
2549 locations->SetInAt(1, Location::RequiresFpuRegister());
2550 locations->SetOut(Location::SameAsFirstInput());
2551 break;
2552 }
2553
2554 default:
2555 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2556 }
2557}
2558
2559void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2560 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002561 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002562 Location first = locations->InAt(0);
2563 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002564
2565 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002566 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002568 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002569 break;
2570 }
2571
2572 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002573 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002575 break;
2576 }
2577
2578 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002579 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002581 break;
2582 }
2583
2584 default:
2585 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2586 }
2587}
2588
Calin Juravlebacfec32014-11-14 15:54:36 +00002589void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002590 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002591
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002592 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2593 ? LocationSummary::kCall
2594 : LocationSummary::kNoCall;
2595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002596
Calin Juravled2ec87d2014-12-08 14:24:46 +00002597 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002598 case Primitive::kPrimInt: {
2599 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002600 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002601 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002602 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2603 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2604 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002605 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002606 locations->AddTemp(Location::RequiresRegister());
2607 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002608 break;
2609 }
2610 case Primitive::kPrimLong: {
2611 InvokeRuntimeCallingConvention calling_convention;
2612 locations->SetInAt(0, Location::RegisterPairLocation(
2613 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2614 locations->SetInAt(1, Location::RegisterPairLocation(
2615 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2616 // Runtime helper puts the result in EAX, EDX.
2617 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2618 break;
2619 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002620 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002621 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002622 locations->SetInAt(0, Location::Any());
2623 locations->SetInAt(1, Location::Any());
2624 locations->SetOut(Location::RequiresFpuRegister());
2625 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002626 break;
2627 }
2628
2629 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002630 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002631 }
2632}
2633
2634void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2635 Primitive::Type type = rem->GetResultType();
2636 switch (type) {
2637 case Primitive::kPrimInt:
2638 case Primitive::kPrimLong: {
2639 GenerateDivRemIntegral(rem);
2640 break;
2641 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002642 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002643 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002644 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002645 break;
2646 }
2647 default:
2648 LOG(FATAL) << "Unexpected rem type " << type;
2649 }
2650}
2651
Calin Juravled0d48522014-11-04 16:40:20 +00002652void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2653 LocationSummary* locations =
2654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002655 switch (instruction->GetType()) {
2656 case Primitive::kPrimInt: {
2657 locations->SetInAt(0, Location::Any());
2658 break;
2659 }
2660 case Primitive::kPrimLong: {
2661 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2662 if (!instruction->IsConstant()) {
2663 locations->AddTemp(Location::RequiresRegister());
2664 }
2665 break;
2666 }
2667 default:
2668 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2669 }
Calin Juravled0d48522014-11-04 16:40:20 +00002670 if (instruction->HasUses()) {
2671 locations->SetOut(Location::SameAsFirstInput());
2672 }
2673}
2674
2675void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2677 codegen_->AddSlowPath(slow_path);
2678
2679 LocationSummary* locations = instruction->GetLocations();
2680 Location value = locations->InAt(0);
2681
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002682 switch (instruction->GetType()) {
2683 case Primitive::kPrimInt: {
2684 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002686 __ j(kEqual, slow_path->GetEntryLabel());
2687 } else if (value.IsStackSlot()) {
2688 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2689 __ j(kEqual, slow_path->GetEntryLabel());
2690 } else {
2691 DCHECK(value.IsConstant()) << value;
2692 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2693 __ jmp(slow_path->GetEntryLabel());
2694 }
2695 }
2696 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002697 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002698 case Primitive::kPrimLong: {
2699 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002701 __ movl(temp, value.AsRegisterPairLow<Register>());
2702 __ orl(temp, value.AsRegisterPairHigh<Register>());
2703 __ j(kEqual, slow_path->GetEntryLabel());
2704 } else {
2705 DCHECK(value.IsConstant()) << value;
2706 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2707 __ jmp(slow_path->GetEntryLabel());
2708 }
2709 }
2710 break;
2711 }
2712 default:
2713 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002714 }
Calin Juravled0d48522014-11-04 16:40:20 +00002715}
2716
Calin Juravle9aec02f2014-11-18 23:06:35 +00002717void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2718 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2719
2720 LocationSummary* locations =
2721 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2722
2723 switch (op->GetResultType()) {
2724 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002725 locations->SetInAt(0, Location::RequiresRegister());
2726 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002727 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2728 locations->SetOut(Location::SameAsFirstInput());
2729 break;
2730 }
2731 case Primitive::kPrimLong: {
2732 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002733 // The shift count needs to be in CL.
2734 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002735 locations->SetOut(Location::SameAsFirstInput());
2736 break;
2737 }
2738 default:
2739 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2740 }
2741}
2742
2743void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2744 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2745
2746 LocationSummary* locations = op->GetLocations();
2747 Location first = locations->InAt(0);
2748 Location second = locations->InAt(1);
2749 DCHECK(first.Equals(locations->Out()));
2750
2751 switch (op->GetResultType()) {
2752 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002753 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002754 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002756 DCHECK_EQ(ECX, second_reg);
2757 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002758 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002759 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002760 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002761 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002762 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002763 }
2764 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002765 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2766 if (op->IsShl()) {
2767 __ shll(first_reg, imm);
2768 } else if (op->IsShr()) {
2769 __ sarl(first_reg, imm);
2770 } else {
2771 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 }
2773 }
2774 break;
2775 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 case Primitive::kPrimLong: {
2777 Register second_reg = second.AsRegister<Register>();
2778 DCHECK_EQ(ECX, second_reg);
2779 if (op->IsShl()) {
2780 GenerateShlLong(first, second_reg);
2781 } else if (op->IsShr()) {
2782 GenerateShrLong(first, second_reg);
2783 } else {
2784 GenerateUShrLong(first, second_reg);
2785 }
2786 break;
2787 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002788 default:
2789 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2790 }
2791}
2792
2793void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2794 Label done;
2795 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2796 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2797 __ testl(shifter, Immediate(32));
2798 __ j(kEqual, &done);
2799 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2800 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2801 __ Bind(&done);
2802}
2803
2804void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2805 Label done;
2806 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2807 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2808 __ testl(shifter, Immediate(32));
2809 __ j(kEqual, &done);
2810 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2811 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2812 __ Bind(&done);
2813}
2814
2815void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2816 Label done;
2817 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2818 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2819 __ testl(shifter, Immediate(32));
2820 __ j(kEqual, &done);
2821 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2822 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2823 __ Bind(&done);
2824}
2825
2826void LocationsBuilderX86::VisitShl(HShl* shl) {
2827 HandleShift(shl);
2828}
2829
2830void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2831 HandleShift(shl);
2832}
2833
2834void LocationsBuilderX86::VisitShr(HShr* shr) {
2835 HandleShift(shr);
2836}
2837
2838void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2839 HandleShift(shr);
2840}
2841
2842void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2843 HandleShift(ushr);
2844}
2845
2846void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2847 HandleShift(ushr);
2848}
2849
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002850void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002853 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002855 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2856 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002857}
2858
2859void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2860 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002861 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002862 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002863
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002864 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002865
Nicolas Geoffray39468442014-09-02 15:17:15 +01002866 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002867 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002868}
2869
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002870void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2871 LocationSummary* locations =
2872 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2873 locations->SetOut(Location::RegisterLocation(EAX));
2874 InvokeRuntimeCallingConvention calling_convention;
2875 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002876 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2877 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002878}
2879
2880void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2881 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002882 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002883 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2884
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002885 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002886
2887 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2888 DCHECK(!codegen_->IsLeafMethod());
2889}
2890
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002891void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 LocationSummary* locations =
2893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002894 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2895 if (location.IsStackSlot()) {
2896 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2897 } else if (location.IsDoubleStackSlot()) {
2898 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002899 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002900 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901}
2902
2903void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002904 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002905}
2906
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002907void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002908 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002909 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002910 locations->SetInAt(0, Location::RequiresRegister());
2911 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002912}
2913
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002914void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2915 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002916 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002917 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002918 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002919 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002920 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002922 break;
2923
2924 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002925 __ notl(out.AsRegisterPairLow<Register>());
2926 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002927 break;
2928
2929 default:
2930 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2931 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002932}
2933
David Brazdil66d126e2015-04-03 16:02:44 +01002934void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2935 LocationSummary* locations =
2936 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetOut(Location::SameAsFirstInput());
2939}
2940
2941void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002942 LocationSummary* locations = bool_not->GetLocations();
2943 Location in = locations->InAt(0);
2944 Location out = locations->Out();
2945 DCHECK(in.Equals(out));
2946 __ xorl(out.AsRegister<Register>(), Immediate(1));
2947}
2948
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002949void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002950 LocationSummary* locations =
2951 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002952 switch (compare->InputAt(0)->GetType()) {
2953 case Primitive::kPrimLong: {
2954 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002955 locations->SetInAt(1, Location::Any());
2956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2957 break;
2958 }
2959 case Primitive::kPrimFloat:
2960 case Primitive::kPrimDouble: {
2961 locations->SetInAt(0, Location::RequiresFpuRegister());
2962 locations->SetInAt(1, Location::RequiresFpuRegister());
2963 locations->SetOut(Location::RequiresRegister());
2964 break;
2965 }
2966 default:
2967 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2968 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002969}
2970
2971void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002972 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002974 Location left = locations->InAt(0);
2975 Location right = locations->InAt(1);
2976
2977 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002978 switch (compare->InputAt(0)->GetType()) {
2979 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002980 Register left_low = left.AsRegisterPairLow<Register>();
2981 Register left_high = left.AsRegisterPairHigh<Register>();
2982 int32_t val_low = 0;
2983 int32_t val_high = 0;
2984 bool right_is_const = false;
2985
2986 if (right.IsConstant()) {
2987 DCHECK(right.GetConstant()->IsLongConstant());
2988 right_is_const = true;
2989 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2990 val_low = Low32Bits(val);
2991 val_high = High32Bits(val);
2992 }
2993
Calin Juravleddb7df22014-11-25 20:56:51 +00002994 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002995 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002996 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002997 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002998 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002999 DCHECK(right_is_const) << right;
3000 if (val_high == 0) {
3001 __ testl(left_high, left_high);
3002 } else {
3003 __ cmpl(left_high, Immediate(val_high));
3004 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003006 __ j(kLess, &less); // Signed compare.
3007 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003008 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003009 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003010 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003011 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003012 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003013 DCHECK(right_is_const) << right;
3014 if (val_low == 0) {
3015 __ testl(left_low, left_low);
3016 } else {
3017 __ cmpl(left_low, Immediate(val_low));
3018 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003019 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003020 break;
3021 }
3022 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003024 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3025 break;
3026 }
3027 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003029 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003030 break;
3031 }
3032 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003033 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003034 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003035 __ movl(out, Immediate(0));
3036 __ j(kEqual, &done);
3037 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3038
3039 __ Bind(&greater);
3040 __ movl(out, Immediate(1));
3041 __ jmp(&done);
3042
3043 __ Bind(&less);
3044 __ movl(out, Immediate(-1));
3045
3046 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003047}
3048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003049void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003050 LocationSummary* locations =
3051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003052 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3053 locations->SetInAt(i, Location::Any());
3054 }
3055 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003056}
3057
3058void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003059 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003060 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003061}
3062
Calin Juravle52c48962014-12-16 17:02:57 +00003063void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3064 /*
3065 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3066 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3067 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3068 */
3069 switch (kind) {
3070 case MemBarrierKind::kAnyAny: {
3071 __ mfence();
3072 break;
3073 }
3074 case MemBarrierKind::kAnyStore:
3075 case MemBarrierKind::kLoadAny:
3076 case MemBarrierKind::kStoreStore: {
3077 // nop
3078 break;
3079 }
3080 default:
3081 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003082 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003083}
3084
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003085
Mark Mendell09ed1a32015-03-25 08:30:06 -04003086void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3087 Register temp) {
3088 // TODO: Implement all kinds of calls:
3089 // 1) boot -> boot
3090 // 2) app -> boot
3091 // 3) app -> app
3092 //
3093 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3094 // temp = method;
3095 LoadCurrentMethod(temp);
3096 if (!invoke->IsRecursive()) {
3097 // temp = temp->dex_cache_resolved_methods_;
3098 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3099 // temp = temp[index_in_cache]
3100 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3101 // (temp + offset_of_quick_compiled_code)()
3102 __ call(Address(
3103 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3104 } else {
3105 __ call(GetFrameEntryLabel());
3106 }
3107
3108 DCHECK(!IsLeafMethod());
3109 RecordPcInfo(invoke, invoke->GetDexPc());
3110}
3111
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003112void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 Label is_null;
3114 __ testl(value, value);
3115 __ j(kEqual, &is_null);
3116 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3117 __ movl(temp, object);
3118 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003119 __ movb(Address(temp, card, TIMES_1, 0),
3120 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 __ Bind(&is_null);
3122}
3123
Calin Juravle52c48962014-12-16 17:02:57 +00003124void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3125 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003126 LocationSummary* locations =
3127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003128 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003129
3130 // The output overlaps in case of long: we don't want the low move to overwrite
3131 // the object's location.
3132 locations->SetOut(Location::RequiresRegister(),
3133 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3134 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00003135
3136 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3137 // Long values can be loaded atomically into an XMM using movsd.
3138 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3139 // and then copy the XMM into the output 32bits at a time).
3140 locations->AddTemp(Location::RequiresFpuRegister());
3141 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142}
3143
Calin Juravle52c48962014-12-16 17:02:57 +00003144void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3145 const FieldInfo& field_info) {
3146 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003147
Calin Juravle52c48962014-12-16 17:02:57 +00003148 LocationSummary* locations = instruction->GetLocations();
3149 Register base = locations->InAt(0).AsRegister<Register>();
3150 Location out = locations->Out();
3151 bool is_volatile = field_info.IsVolatile();
3152 Primitive::Type field_type = field_info.GetFieldType();
3153 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3154
3155 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003156 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003157 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003158 break;
3159 }
3160
3161 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003162 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003163 break;
3164 }
3165
3166 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003167 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003168 break;
3169 }
3170
3171 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003172 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003173 break;
3174 }
3175
3176 case Primitive::kPrimInt:
3177 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003178 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003179 break;
3180 }
3181
3182 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003183 if (is_volatile) {
3184 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3185 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003186 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003187 __ movd(out.AsRegisterPairLow<Register>(), temp);
3188 __ psrlq(temp, Immediate(32));
3189 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3190 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003191 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003192 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003194 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3195 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003196 break;
3197 }
3198
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003199 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003200 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003201 break;
3202 }
3203
3204 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003205 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003206 break;
3207 }
3208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003210 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003211 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003212 }
Calin Juravle52c48962014-12-16 17:02:57 +00003213
Calin Juravle77520bc2015-01-12 18:45:46 +00003214 // Longs are handled in the switch.
3215 if (field_type != Primitive::kPrimLong) {
3216 codegen_->MaybeRecordImplicitNullCheck(instruction);
3217 }
3218
Calin Juravle52c48962014-12-16 17:02:57 +00003219 if (is_volatile) {
3220 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3221 }
3222}
3223
3224void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3225 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3226
3227 LocationSummary* locations =
3228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3229 locations->SetInAt(0, Location::RequiresRegister());
3230 bool is_volatile = field_info.IsVolatile();
3231 Primitive::Type field_type = field_info.GetFieldType();
3232 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3233 || (field_type == Primitive::kPrimByte);
3234
3235 // The register allocator does not support multiple
3236 // inputs that die at entry with one in a specific register.
3237 if (is_byte_type) {
3238 // Ensure the value is in a byte register.
3239 locations->SetInAt(1, Location::RegisterLocation(EAX));
3240 } else {
3241 locations->SetInAt(1, Location::RequiresRegister());
3242 }
3243 // Temporary registers for the write barrier.
3244 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3245 locations->AddTemp(Location::RequiresRegister());
3246 // Ensure the card is in a byte register.
3247 locations->AddTemp(Location::RegisterLocation(ECX));
3248 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3249 // 64bits value can be atomically written to an address with movsd and an XMM register.
3250 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3251 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3252 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3253 // isolated cases when we need this it isn't worth adding the extra complexity.
3254 locations->AddTemp(Location::RequiresFpuRegister());
3255 locations->AddTemp(Location::RequiresFpuRegister());
3256 }
3257}
3258
3259void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3260 const FieldInfo& field_info) {
3261 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3262
3263 LocationSummary* locations = instruction->GetLocations();
3264 Register base = locations->InAt(0).AsRegister<Register>();
3265 Location value = locations->InAt(1);
3266 bool is_volatile = field_info.IsVolatile();
3267 Primitive::Type field_type = field_info.GetFieldType();
3268 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3269
3270 if (is_volatile) {
3271 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3272 }
3273
3274 switch (field_type) {
3275 case Primitive::kPrimBoolean:
3276 case Primitive::kPrimByte: {
3277 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3278 break;
3279 }
3280
3281 case Primitive::kPrimShort:
3282 case Primitive::kPrimChar: {
3283 __ movw(Address(base, offset), value.AsRegister<Register>());
3284 break;
3285 }
3286
3287 case Primitive::kPrimInt:
3288 case Primitive::kPrimNot: {
3289 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003290 break;
3291 }
3292
3293 case Primitive::kPrimLong: {
3294 if (is_volatile) {
3295 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3296 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3297 __ movd(temp1, value.AsRegisterPairLow<Register>());
3298 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3299 __ punpckldq(temp1, temp2);
3300 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003302 } else {
3303 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003304 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003305 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3306 }
3307 break;
3308 }
3309
3310 case Primitive::kPrimFloat: {
3311 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3312 break;
3313 }
3314
3315 case Primitive::kPrimDouble: {
3316 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3317 break;
3318 }
3319
3320 case Primitive::kPrimVoid:
3321 LOG(FATAL) << "Unreachable type " << field_type;
3322 UNREACHABLE();
3323 }
3324
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 // Longs are handled in the switch.
3326 if (field_type != Primitive::kPrimLong) {
3327 codegen_->MaybeRecordImplicitNullCheck(instruction);
3328 }
3329
3330 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3331 Register temp = locations->GetTemp(0).AsRegister<Register>();
3332 Register card = locations->GetTemp(1).AsRegister<Register>();
3333 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3334 }
3335
Calin Juravle52c48962014-12-16 17:02:57 +00003336 if (is_volatile) {
3337 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3338 }
3339}
3340
3341void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3342 HandleFieldGet(instruction, instruction->GetFieldInfo());
3343}
3344
3345void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3346 HandleFieldGet(instruction, instruction->GetFieldInfo());
3347}
3348
3349void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3350 HandleFieldSet(instruction, instruction->GetFieldInfo());
3351}
3352
3353void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3354 HandleFieldSet(instruction, instruction->GetFieldInfo());
3355}
3356
3357void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3358 HandleFieldSet(instruction, instruction->GetFieldInfo());
3359}
3360
3361void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3362 HandleFieldSet(instruction, instruction->GetFieldInfo());
3363}
3364
3365void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3366 HandleFieldGet(instruction, instruction->GetFieldInfo());
3367}
3368
3369void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3370 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003371}
3372
3373void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003374 LocationSummary* locations =
3375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003376 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3377 ? Location::RequiresRegister()
3378 : Location::Any();
3379 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003380 if (instruction->HasUses()) {
3381 locations->SetOut(Location::SameAsFirstInput());
3382 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003383}
3384
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003385void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003386 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3387 return;
3388 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003389 LocationSummary* locations = instruction->GetLocations();
3390 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003391
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003392 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3393 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3394}
3395
3396void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003397 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003398 codegen_->AddSlowPath(slow_path);
3399
3400 LocationSummary* locations = instruction->GetLocations();
3401 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003402
3403 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003404 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003405 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003406 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003407 } else {
3408 DCHECK(obj.IsConstant()) << obj;
3409 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3410 __ jmp(slow_path->GetEntryLabel());
3411 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003412 }
3413 __ j(kEqual, slow_path->GetEntryLabel());
3414}
3415
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003416void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3417 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3418 GenerateImplicitNullCheck(instruction);
3419 } else {
3420 GenerateExplicitNullCheck(instruction);
3421 }
3422}
3423
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003425 LocationSummary* locations =
3426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003427 locations->SetInAt(0, Location::RequiresRegister());
3428 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003429 // The output overlaps in case of long: we don't want the low move to overwrite
3430 // the array's location.
3431 locations->SetOut(Location::RequiresRegister(),
3432 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3433 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003434}
3435
3436void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3437 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003439 Location index = locations->InAt(1);
3440
Calin Juravle77520bc2015-01-12 18:45:46 +00003441 Primitive::Type type = instruction->GetType();
3442 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003443 case Primitive::kPrimBoolean: {
3444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003446 if (index.IsConstant()) {
3447 __ movzxb(out, Address(obj,
3448 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3449 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451 }
3452 break;
3453 }
3454
3455 case Primitive::kPrimByte: {
3456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003458 if (index.IsConstant()) {
3459 __ movsxb(out, Address(obj,
3460 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3461 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003463 }
3464 break;
3465 }
3466
3467 case Primitive::kPrimShort: {
3468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003470 if (index.IsConstant()) {
3471 __ movsxw(out, Address(obj,
3472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3473 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475 }
3476 break;
3477 }
3478
3479 case Primitive::kPrimChar: {
3480 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003482 if (index.IsConstant()) {
3483 __ movzxw(out, Address(obj,
3484 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3485 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003487 }
3488 break;
3489 }
3490
3491 case Primitive::kPrimInt:
3492 case Primitive::kPrimNot: {
3493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 if (index.IsConstant()) {
3496 __ movl(out, Address(obj,
3497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3498 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500 }
3501 break;
3502 }
3503
3504 case Primitive::kPrimLong: {
3505 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003506 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003507 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003508 if (index.IsConstant()) {
3509 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003510 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003511 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003512 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003514 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003515 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003517 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 }
3520 break;
3521 }
3522
Mark Mendell7c8d0092015-01-26 11:21:33 -05003523 case Primitive::kPrimFloat: {
3524 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3525 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3526 if (index.IsConstant()) {
3527 __ movss(out, Address(obj,
3528 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3529 } else {
3530 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3531 }
3532 break;
3533 }
3534
3535 case Primitive::kPrimDouble: {
3536 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3537 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3538 if (index.IsConstant()) {
3539 __ movsd(out, Address(obj,
3540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3541 } else {
3542 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3543 }
3544 break;
3545 }
3546
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003547 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003548 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003549 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003550 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003551
3552 if (type != Primitive::kPrimLong) {
3553 codegen_->MaybeRecordImplicitNullCheck(instruction);
3554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003555}
3556
3557void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003558 // This location builder might end up asking to up to four registers, which is
3559 // not currently possible for baseline. The situation in which we need four
3560 // registers cannot be met by baseline though, because it has not run any
3561 // optimization.
3562
Nicolas Geoffray39468442014-09-02 15:17:15 +01003563 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003564 bool needs_write_barrier =
3565 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3566
Mark Mendell5f874182015-03-04 15:42:45 -05003567 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003568
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3570 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003571 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003572
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003573 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003575 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3576 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3577 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003579 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3580 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003581 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003582 // In case of a byte operation, the register allocator does not support multiple
3583 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003584 locations->SetInAt(0, Location::RequiresRegister());
3585 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003586 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003587 // Ensure the value is in a byte register.
3588 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 } else {
Serguei Katkov55501ce2015-04-08 13:26:09 +06003590 bool is_fp_type = (value_type == Primitive::kPrimFloat)
3591 || (value_type == Primitive::kPrimDouble);
3592 if (is_fp_type) {
3593 locations->SetInAt(2, Location::RequiresFpuRegister());
3594 } else {
3595 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
3596 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003597 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003598 // Temporary registers for the write barrier.
3599 if (needs_write_barrier) {
3600 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003601 // Ensure the card is in a byte register.
3602 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605}
3606
3607void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3608 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003611 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003612 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003613 bool needs_runtime_call = locations->WillCall();
3614 bool needs_write_barrier =
3615 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616
3617 switch (value_type) {
3618 case Primitive::kPrimBoolean:
3619 case Primitive::kPrimByte: {
3620 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 if (index.IsConstant()) {
3622 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003623 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003624 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003625 } else {
3626 __ movb(Address(obj, offset),
3627 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003630 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003632 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003633 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003635 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003638 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003639 break;
3640 }
3641
3642 case Primitive::kPrimShort:
3643 case Primitive::kPrimChar: {
3644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 if (index.IsConstant()) {
3646 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003647 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003649 } else {
3650 __ movw(Address(obj, offset),
3651 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003654 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3656 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003657 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003659 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3660 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 break;
3664 }
3665
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003666 case Primitive::kPrimInt:
3667 case Primitive::kPrimNot: {
3668 if (!needs_runtime_call) {
3669 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3670 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003671 size_t offset =
3672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003673 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003674 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003675 } else {
3676 DCHECK(value.IsConstant()) << value;
3677 __ movl(Address(obj, offset),
3678 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3679 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003680 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003681 DCHECK(index.IsRegister()) << index;
3682 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3684 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003685 } else {
3686 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003688 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3689 }
3690 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003691 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003692
3693 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003694 Register temp = locations->GetTemp(0).AsRegister<Register>();
3695 Register card = locations->GetTemp(1).AsRegister<Register>();
3696 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 DCHECK_EQ(value_type, Primitive::kPrimNot);
3700 DCHECK(!codegen_->IsLeafMethod());
3701 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3702 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003703 }
3704 break;
3705 }
3706
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 case Primitive::kPrimLong: {
3708 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003709 if (index.IsConstant()) {
3710 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003711 if (value.IsRegisterPair()) {
3712 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003713 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003714 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003715 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003716 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003717 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3718 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003719 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003720 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003723 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003725 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003726 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003728 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();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003733 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003734 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003736 Immediate(High32Bits(val)));
3737 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 }
3739 break;
3740 }
3741
Mark Mendell7c8d0092015-01-26 11:21:33 -05003742 case Primitive::kPrimFloat: {
3743 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3744 DCHECK(value.IsFpuRegister());
3745 if (index.IsConstant()) {
3746 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3747 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3748 } else {
3749 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3750 value.AsFpuRegister<XmmRegister>());
3751 }
3752 break;
3753 }
3754
3755 case Primitive::kPrimDouble: {
3756 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3757 DCHECK(value.IsFpuRegister());
3758 if (index.IsConstant()) {
3759 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3760 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3761 } else {
3762 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3763 value.AsFpuRegister<XmmRegister>());
3764 }
3765 break;
3766 }
3767
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003768 case Primitive::kPrimVoid:
3769 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003770 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 }
3772}
3773
3774void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3775 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003776 locations->SetInAt(0, Location::RequiresRegister());
3777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 instruction->SetLocations(locations);
3779}
3780
3781void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3782 LocationSummary* locations = instruction->GetLocations();
3783 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 Register obj = locations->InAt(0).AsRegister<Register>();
3785 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003787 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788}
3789
3790void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003791 LocationSummary* locations =
3792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003793 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003795 if (instruction->HasUses()) {
3796 locations->SetOut(Location::SameAsFirstInput());
3797 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798}
3799
3800void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3801 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003802 Location index_loc = locations->InAt(0);
3803 Location length_loc = locations->InAt(1);
3804 SlowPathCodeX86* slow_path =
3805 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 codegen_->AddSlowPath(slow_path);
3807
Mark Mendellf60c90b2015-03-04 15:12:59 -05003808 Register length = length_loc.AsRegister<Register>();
3809 if (index_loc.IsConstant()) {
3810 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3811 __ cmpl(length, Immediate(value));
3812 } else {
3813 __ cmpl(length, index_loc.AsRegister<Register>());
3814 }
3815 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816}
3817
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003818void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3819 temp->SetLocations(nullptr);
3820}
3821
3822void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3823 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003824 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003825}
3826
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003827void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003828 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003829 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003830}
3831
3832void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003833 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3834}
3835
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003836void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3837 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3838}
3839
3840void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003841 HBasicBlock* block = instruction->GetBlock();
3842 if (block->GetLoopInformation() != nullptr) {
3843 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3844 // The back edge will generate the suspend check.
3845 return;
3846 }
3847 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3848 // The goto will generate the suspend check.
3849 return;
3850 }
3851 GenerateSuspendCheck(instruction, nullptr);
3852}
3853
3854void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3855 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003856 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003857 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003858 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003859 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003860 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003861 if (successor == nullptr) {
3862 __ j(kNotEqual, slow_path->GetEntryLabel());
3863 __ Bind(slow_path->GetReturnLabel());
3864 } else {
3865 __ j(kEqual, codegen_->GetLabelOf(successor));
3866 __ jmp(slow_path->GetEntryLabel());
3867 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003868}
3869
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003870X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3871 return codegen_->GetAssembler();
3872}
3873
Mark Mendell7c8d0092015-01-26 11:21:33 -05003874void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003875 ScratchRegisterScope ensure_scratch(
3876 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3877 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3878 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3879 __ movl(temp_reg, Address(ESP, src + stack_offset));
3880 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003881}
3882
3883void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003884 ScratchRegisterScope ensure_scratch(
3885 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3886 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3887 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3888 __ movl(temp_reg, Address(ESP, src + stack_offset));
3889 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3890 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3891 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003892}
3893
3894void ParallelMoveResolverX86::EmitMove(size_t index) {
3895 MoveOperands* move = moves_.Get(index);
3896 Location source = move->GetSource();
3897 Location destination = move->GetDestination();
3898
3899 if (source.IsRegister()) {
3900 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003901 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003902 } else {
3903 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003905 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003906 } else if (source.IsFpuRegister()) {
3907 if (destination.IsFpuRegister()) {
3908 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3909 } else if (destination.IsStackSlot()) {
3910 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3911 } else {
3912 DCHECK(destination.IsDoubleStackSlot());
3913 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3914 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003915 } else if (source.IsStackSlot()) {
3916 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003917 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003918 } else if (destination.IsFpuRegister()) {
3919 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003920 } else {
3921 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003922 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3923 }
3924 } else if (source.IsDoubleStackSlot()) {
3925 if (destination.IsFpuRegister()) {
3926 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3927 } else {
3928 DCHECK(destination.IsDoubleStackSlot()) << destination;
3929 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003930 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003931 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003932 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003933 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003934 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003935 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003936 if (value == 0) {
3937 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3938 } else {
3939 __ movl(destination.AsRegister<Register>(), Immediate(value));
3940 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003941 } else {
3942 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003943 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003944 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003945 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003946 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003947 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003948 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003949 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003950 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3951 if (value == 0) {
3952 // Easy handling of 0.0.
3953 __ xorps(dest, dest);
3954 } else {
3955 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003956 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3957 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3958 __ movl(temp, Immediate(value));
3959 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003960 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003961 } else {
3962 DCHECK(destination.IsStackSlot()) << destination;
3963 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3964 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003965 } else if (constant->IsLongConstant()) {
3966 int64_t value = constant->AsLongConstant()->GetValue();
3967 int32_t low_value = Low32Bits(value);
3968 int32_t high_value = High32Bits(value);
3969 Immediate low(low_value);
3970 Immediate high(high_value);
3971 if (destination.IsDoubleStackSlot()) {
3972 __ movl(Address(ESP, destination.GetStackIndex()), low);
3973 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3974 } else {
3975 __ movl(destination.AsRegisterPairLow<Register>(), low);
3976 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3977 }
3978 } else {
3979 DCHECK(constant->IsDoubleConstant());
3980 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003981 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003982 int32_t low_value = Low32Bits(value);
3983 int32_t high_value = High32Bits(value);
3984 Immediate low(low_value);
3985 Immediate high(high_value);
3986 if (destination.IsFpuRegister()) {
3987 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3988 if (value == 0) {
3989 // Easy handling of 0.0.
3990 __ xorpd(dest, dest);
3991 } else {
3992 __ pushl(high);
3993 __ pushl(low);
3994 __ movsd(dest, Address(ESP, 0));
3995 __ addl(ESP, Immediate(8));
3996 }
3997 } else {
3998 DCHECK(destination.IsDoubleStackSlot()) << destination;
3999 __ movl(Address(ESP, destination.GetStackIndex()), low);
4000 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4001 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004002 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004003 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004004 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004005 }
4006}
4007
Mark Mendella5c19ce2015-04-01 12:51:05 -04004008void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004009 Register suggested_scratch = reg == EAX ? EBX : EAX;
4010 ScratchRegisterScope ensure_scratch(
4011 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4012
4013 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4014 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4015 __ movl(Address(ESP, mem + stack_offset), reg);
4016 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004017}
4018
Mark Mendell7c8d0092015-01-26 11:21:33 -05004019void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004020 ScratchRegisterScope ensure_scratch(
4021 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4022
4023 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4024 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4025 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4026 __ movss(Address(ESP, mem + stack_offset), reg);
4027 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004028}
4029
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004030void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004031 ScratchRegisterScope ensure_scratch1(
4032 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004033
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004034 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4035 ScratchRegisterScope ensure_scratch2(
4036 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004037
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004038 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4039 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4040 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4041 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4042 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4043 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004044}
4045
4046void ParallelMoveResolverX86::EmitSwap(size_t index) {
4047 MoveOperands* move = moves_.Get(index);
4048 Location source = move->GetSource();
4049 Location destination = move->GetDestination();
4050
4051 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004052 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004053 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004054 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004055 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004056 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004057 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4058 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004059 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4060 // Use XOR Swap algorithm to avoid a temporary.
4061 DCHECK_NE(source.reg(), destination.reg());
4062 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4063 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4064 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4065 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4066 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4067 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4068 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004069 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4070 // Take advantage of the 16 bytes in the XMM register.
4071 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4072 Address stack(ESP, destination.GetStackIndex());
4073 // Load the double into the high doubleword.
4074 __ movhpd(reg, stack);
4075
4076 // Store the low double into the destination.
4077 __ movsd(stack, reg);
4078
4079 // Move the high double to the low double.
4080 __ psrldq(reg, Immediate(8));
4081 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4082 // Take advantage of the 16 bytes in the XMM register.
4083 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4084 Address stack(ESP, source.GetStackIndex());
4085 // Load the double into the high doubleword.
4086 __ movhpd(reg, stack);
4087
4088 // Store the low double into the destination.
4089 __ movsd(stack, reg);
4090
4091 // Move the high double to the low double.
4092 __ psrldq(reg, Immediate(8));
4093 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4094 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4095 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004096 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004097 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004098 }
4099}
4100
4101void ParallelMoveResolverX86::SpillScratch(int reg) {
4102 __ pushl(static_cast<Register>(reg));
4103}
4104
4105void ParallelMoveResolverX86::RestoreScratch(int reg) {
4106 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004107}
4108
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004109void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004110 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4111 ? LocationSummary::kCallOnSlowPath
4112 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004113 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004114 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004115 locations->SetOut(Location::RequiresRegister());
4116}
4117
4118void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004119 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004120 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004121 DCHECK(!cls->CanCallRuntime());
4122 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004123 codegen_->LoadCurrentMethod(out);
4124 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4125 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004126 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004127 codegen_->LoadCurrentMethod(out);
4128 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4129 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004130
4131 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4132 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4133 codegen_->AddSlowPath(slow_path);
4134 __ testl(out, out);
4135 __ j(kEqual, slow_path->GetEntryLabel());
4136 if (cls->MustGenerateClinitCheck()) {
4137 GenerateClassInitializationCheck(slow_path, out);
4138 } else {
4139 __ Bind(slow_path->GetExitLabel());
4140 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 }
4142}
4143
4144void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4145 LocationSummary* locations =
4146 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4147 locations->SetInAt(0, Location::RequiresRegister());
4148 if (check->HasUses()) {
4149 locations->SetOut(Location::SameAsFirstInput());
4150 }
4151}
4152
4153void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004154 // We assume the class to not be null.
4155 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4156 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004157 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004158 GenerateClassInitializationCheck(slow_path,
4159 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004160}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004161
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004162void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4163 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004164 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4165 Immediate(mirror::Class::kStatusInitialized));
4166 __ j(kLess, slow_path->GetEntryLabel());
4167 __ Bind(slow_path->GetExitLabel());
4168 // No need for memory fence, thanks to the X86 memory model.
4169}
4170
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004171void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4174 locations->SetOut(Location::RequiresRegister());
4175}
4176
4177void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4178 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4179 codegen_->AddSlowPath(slow_path);
4180
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004182 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004183 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4184 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004185 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4186 __ testl(out, out);
4187 __ j(kEqual, slow_path->GetEntryLabel());
4188 __ Bind(slow_path->GetExitLabel());
4189}
4190
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004191void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4192 LocationSummary* locations =
4193 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4194 locations->SetOut(Location::RequiresRegister());
4195}
4196
4197void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4198 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004199 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004200 __ fs()->movl(address, Immediate(0));
4201}
4202
4203void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4206 InvokeRuntimeCallingConvention calling_convention;
4207 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4208}
4209
4210void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4211 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4212 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4213}
4214
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004215void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004216 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4217 ? LocationSummary::kNoCall
4218 : LocationSummary::kCallOnSlowPath;
4219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4220 locations->SetInAt(0, Location::RequiresRegister());
4221 locations->SetInAt(1, Location::Any());
4222 locations->SetOut(Location::RequiresRegister());
4223}
4224
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004225void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004226 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004228 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004229 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004230 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4231 Label done, zero;
4232 SlowPathCodeX86* slow_path = nullptr;
4233
4234 // Return 0 if `obj` is null.
4235 // TODO: avoid this check if we know obj is not null.
4236 __ testl(obj, obj);
4237 __ j(kEqual, &zero);
4238 __ movl(out, Address(obj, class_offset));
4239 // Compare the class of `obj` with `cls`.
4240 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004241 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004242 } else {
4243 DCHECK(cls.IsStackSlot()) << cls;
4244 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4245 }
4246
4247 if (instruction->IsClassFinal()) {
4248 // Classes must be equal for the instanceof to succeed.
4249 __ j(kNotEqual, &zero);
4250 __ movl(out, Immediate(1));
4251 __ jmp(&done);
4252 } else {
4253 // If the classes are not equal, we go into a slow path.
4254 DCHECK(locations->OnlyCallsOnSlowPath());
4255 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004256 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004257 codegen_->AddSlowPath(slow_path);
4258 __ j(kNotEqual, slow_path->GetEntryLabel());
4259 __ movl(out, Immediate(1));
4260 __ jmp(&done);
4261 }
4262 __ Bind(&zero);
4263 __ movl(out, Immediate(0));
4264 if (slow_path != nullptr) {
4265 __ Bind(slow_path->GetExitLabel());
4266 }
4267 __ Bind(&done);
4268}
4269
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004270void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4272 instruction, LocationSummary::kCallOnSlowPath);
4273 locations->SetInAt(0, Location::RequiresRegister());
4274 locations->SetInAt(1, Location::Any());
4275 locations->AddTemp(Location::RequiresRegister());
4276}
4277
4278void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4279 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004280 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004281 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4284 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4285 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4286 codegen_->AddSlowPath(slow_path);
4287
4288 // TODO: avoid this check if we know obj is not null.
4289 __ testl(obj, obj);
4290 __ j(kEqual, slow_path->GetExitLabel());
4291 __ movl(temp, Address(obj, class_offset));
4292
4293 // Compare the class of `obj` with `cls`.
4294 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004295 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004296 } else {
4297 DCHECK(cls.IsStackSlot()) << cls;
4298 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4299 }
4300
4301 __ j(kNotEqual, slow_path->GetEntryLabel());
4302 __ Bind(slow_path->GetExitLabel());
4303}
4304
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004305void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4306 LocationSummary* locations =
4307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4308 InvokeRuntimeCallingConvention calling_convention;
4309 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4310}
4311
4312void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4313 __ fs()->call(Address::Absolute(instruction->IsEnter()
4314 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4315 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4316 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4317}
4318
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004319void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4320void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4321void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4322
4323void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4324 LocationSummary* locations =
4325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4326 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4327 || instruction->GetResultType() == Primitive::kPrimLong);
4328 locations->SetInAt(0, Location::RequiresRegister());
4329 locations->SetInAt(1, Location::Any());
4330 locations->SetOut(Location::SameAsFirstInput());
4331}
4332
4333void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4334 HandleBitwiseOperation(instruction);
4335}
4336
4337void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4338 HandleBitwiseOperation(instruction);
4339}
4340
4341void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4342 HandleBitwiseOperation(instruction);
4343}
4344
4345void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4346 LocationSummary* locations = instruction->GetLocations();
4347 Location first = locations->InAt(0);
4348 Location second = locations->InAt(1);
4349 DCHECK(first.Equals(locations->Out()));
4350
4351 if (instruction->GetResultType() == Primitive::kPrimInt) {
4352 if (second.IsRegister()) {
4353 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004355 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004357 } else {
4358 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004359 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004360 }
4361 } else if (second.IsConstant()) {
4362 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004363 __ andl(first.AsRegister<Register>(),
4364 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004365 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004366 __ orl(first.AsRegister<Register>(),
4367 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004368 } else {
4369 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004370 __ xorl(first.AsRegister<Register>(),
4371 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004372 }
4373 } else {
4374 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004375 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004376 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004378 } else {
4379 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004380 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004381 }
4382 }
4383 } else {
4384 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4385 if (second.IsRegisterPair()) {
4386 if (instruction->IsAnd()) {
4387 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4388 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4389 } else if (instruction->IsOr()) {
4390 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4391 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4392 } else {
4393 DCHECK(instruction->IsXor());
4394 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4395 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4396 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004397 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004398 if (instruction->IsAnd()) {
4399 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4400 __ andl(first.AsRegisterPairHigh<Register>(),
4401 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4402 } else if (instruction->IsOr()) {
4403 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4404 __ orl(first.AsRegisterPairHigh<Register>(),
4405 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4406 } else {
4407 DCHECK(instruction->IsXor());
4408 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4409 __ xorl(first.AsRegisterPairHigh<Register>(),
4410 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4411 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004412 } else {
4413 DCHECK(second.IsConstant()) << second;
4414 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 int32_t low_value = Low32Bits(value);
4416 int32_t high_value = High32Bits(value);
4417 Immediate low(low_value);
4418 Immediate high(high_value);
4419 Register first_low = first.AsRegisterPairLow<Register>();
4420 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004421 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 if (low_value == 0) {
4423 __ xorl(first_low, first_low);
4424 } else if (low_value != -1) {
4425 __ andl(first_low, low);
4426 }
4427 if (high_value == 0) {
4428 __ xorl(first_high, first_high);
4429 } else if (high_value != -1) {
4430 __ andl(first_high, high);
4431 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004432 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004433 if (low_value != 0) {
4434 __ orl(first_low, low);
4435 }
4436 if (high_value != 0) {
4437 __ orl(first_high, high);
4438 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004439 } else {
4440 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004441 if (low_value != 0) {
4442 __ xorl(first_low, low);
4443 }
4444 if (high_value != 0) {
4445 __ xorl(first_high, high);
4446 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004447 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004448 }
4449 }
4450}
4451
Calin Juravleb1498f62015-02-16 13:13:29 +00004452void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4453 // Nothing to do, this should be removed during prepare for register allocator.
4454 UNUSED(instruction);
4455 LOG(FATAL) << "Unreachable";
4456}
4457
4458void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4459 // Nothing to do, this should be removed during prepare for register allocator.
4460 UNUSED(instruction);
4461 LOG(FATAL) << "Unreachable";
4462}
4463
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004464} // namespace x86
4465} // namespace art