blob: 3dcfca6a0c7b4274eda3e39238234ab0982a8d22 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
156 private:
157 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 Label return_label_;
160
161 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
162};
163
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000164class LoadStringSlowPathX86 : public SlowPathCodeX86 {
165 public:
166 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
167
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169 LocationSummary* locations = instruction_->GetLocations();
170 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
171
172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000175
176 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800177 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
178 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000181 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183
184 __ jmp(GetExitLabel());
185 }
186
187 private:
188 HLoadString* const instruction_;
189
190 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
191};
192
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193class LoadClassSlowPathX86 : public SlowPathCodeX86 {
194 public:
195 LoadClassSlowPathX86(HLoadClass* cls,
196 HInstruction* at,
197 uint32_t dex_pc,
198 bool do_clinit)
199 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
200 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201 }
202
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LocationSummary* locations = at_->GetLocations();
205 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
206 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208
209 InvokeRuntimeCallingConvention calling_convention;
210 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
211 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
212 __ fs()->call(Address::Absolute(do_clinit_
213 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
214 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216
217 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000218 Location out = locations->Out();
219 if (out.IsValid()) {
220 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
221 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000223
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000224 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 // The class this slow path will load.
230 HLoadClass* const cls_;
231
232 // The instruction where this slow path is happening.
233 // (Might be the load class or an initialization check).
234 HInstruction* const at_;
235
236 // The dex PC of `at_`.
237 const uint32_t dex_pc_;
238
239 // Whether to initialize the class.
240 const bool do_clinit_;
241
242 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
243};
244
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
246 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000247 TypeCheckSlowPathX86(HInstruction* instruction,
248 Location class_to_check,
249 Location object_class,
250 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000252 class_to_check_(class_to_check),
253 object_class_(object_class),
254 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000258 DCHECK(instruction_->IsCheckCast()
259 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260
261 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000268 x86_codegen->EmitParallelMoves(
269 class_to_check_,
270 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100271 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000272 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100273 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
274 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000276 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000277 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
278 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 } else {
280 DCHECK(instruction_->IsCheckCast());
281 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
282 }
283
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 if (instruction_->IsInstanceOf()) {
286 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
287 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
290 __ jmp(GetExitLabel());
291 }
292
293 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 HInstruction* const instruction_;
295 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
300};
301
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700302class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
303 public:
304 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
305 : instruction_(instruction) {}
306
307 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
308 __ Bind(GetEntryLabel());
309 SaveLiveRegisters(codegen, instruction_->GetLocations());
310 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
311 // No need to restore live registers.
312 DCHECK(instruction_->IsDeoptimize());
313 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
314 uint32_t dex_pc = deoptimize->GetDexPc();
315 codegen->RecordPcInfo(instruction_, dex_pc, this);
316 }
317
318 private:
319 HInstruction* const instruction_;
320 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
321};
322
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100323#undef __
324#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
325
Dave Allison20dfc792014-06-16 20:44:29 -0700326inline Condition X86Condition(IfCondition cond) {
327 switch (cond) {
328 case kCondEQ: return kEqual;
329 case kCondNE: return kNotEqual;
330 case kCondLT: return kLess;
331 case kCondLE: return kLessEqual;
332 case kCondGT: return kGreater;
333 case kCondGE: return kGreaterEqual;
334 default:
335 LOG(FATAL) << "Unknown if condition";
336 }
337 return kEqual;
338}
339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
341 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
342}
343
344void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
346}
347
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
349 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
350 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Mark Mendell7c8d0092015-01-26 11:21:33 -0500358size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
359 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
360 return GetFloatingPointSpillSlotSize();
361}
362
363size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
365 return GetFloatingPointSpillSlotSize();
366}
367
Mark Mendellfb8d2792015-03-31 22:16:59 -0400368CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
369 const X86InstructionSetFeatures& isa_features,
370 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500371 : CodeGenerator(graph,
372 kNumberOfCpuRegisters,
373 kNumberOfXmmRegisters,
374 kNumberOfRegisterPairs,
375 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
376 arraysize(kCoreCalleeSaves))
377 | (1 << kFakeReturnRegister),
378 0,
379 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100380 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400383 move_resolver_(graph->GetArena(), this),
384 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 // Use a fake return address register to mimic Quick.
386 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100387}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 switch (type) {
391 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 X86ManagedRegister pair =
394 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100395 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100397 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
398 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100399 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100400 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401 }
402
403 case Primitive::kPrimByte:
404 case Primitive::kPrimBoolean:
405 case Primitive::kPrimChar:
406 case Primitive::kPrimShort:
407 case Primitive::kPrimInt:
408 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100410 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
413 X86ManagedRegister current =
414 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
415 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420 }
421
422 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 return Location::FpuRegisterLocation(
425 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427
428 case Primitive::kPrimVoid:
429 LOG(FATAL) << "Unreachable type " << type;
430 }
431
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433}
434
Mark Mendell5f874182015-03-04 15:42:45 -0500435void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438
439 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
Mark Mendell5f874182015-03-04 15:42:45 -0500442 if (is_baseline) {
443 blocked_core_registers_[EBP] = true;
444 blocked_core_registers_[ESI] = true;
445 blocked_core_registers_[EDI] = true;
446 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100447
448 UpdateBlockedPairRegisters();
449}
450
451void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
452 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
453 X86ManagedRegister current =
454 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
455 if (blocked_core_registers_[current.AsRegisterPairLow()]
456 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
457 blocked_register_pairs_[i] = true;
458 }
459 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100460}
461
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
463 : HGraphVisitor(graph),
464 assembler_(codegen->GetAssembler()),
465 codegen_(codegen) {}
466
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100468 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100469}
470
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000471void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000473 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000474 bool skip_overflow_check =
475 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000477
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100479 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100480 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100481 }
482
Mark Mendell5f874182015-03-04 15:42:45 -0500483 if (HasEmptyFrame()) {
484 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000485 }
Mark Mendell5f874182015-03-04 15:42:45 -0500486
487 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
488 Register reg = kCoreCalleeSaves[i];
489 if (allocated_registers_.ContainsCoreRegister(reg)) {
490 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 __ cfi().AdjustCFAOffset(kX86WordSize);
492 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500493 }
494 }
495
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496 int adjust = GetFrameSize() - FrameEntrySpillSize();
497 __ subl(ESP, Immediate(adjust));
498 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500499 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500}
501
502void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100503 __ cfi().RememberState();
504 if (!HasEmptyFrame()) {
505 int adjust = GetFrameSize() - FrameEntrySpillSize();
506 __ addl(ESP, Immediate(adjust));
507 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500508
David Srbeckyc34dc932015-04-12 09:27:43 +0100509 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
510 Register reg = kCoreCalleeSaves[i];
511 if (allocated_registers_.ContainsCoreRegister(reg)) {
512 __ popl(reg);
513 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
514 __ cfi().Restore(DWARFReg(reg));
515 }
Mark Mendell5f874182015-03-04 15:42:45 -0500516 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000517 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100518 __ ret();
519 __ cfi().RestoreState();
520 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100523void CodeGeneratorX86::Bind(HBasicBlock* block) {
524 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525}
526
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100527void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000528 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100529 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000530}
531
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
533 switch (load->GetType()) {
534 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537
538 case Primitive::kPrimInt:
539 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542
543 case Primitive::kPrimBoolean:
544 case Primitive::kPrimByte:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimVoid:
548 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700549 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550 }
551
552 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700553 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100554}
555
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
557 switch (type) {
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimByte:
560 case Primitive::kPrimChar:
561 case Primitive::kPrimShort:
562 case Primitive::kPrimInt:
563 case Primitive::kPrimNot: {
564 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000565 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100567 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000569 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100570 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000573 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574 uint32_t index = gp_index_;
575 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100578 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
579 calling_convention.GetRegisterPairAt(index));
580 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100581 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000582 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
583 }
584 }
585
586 case Primitive::kPrimFloat: {
587 uint32_t index = fp_index_++;
588 stack_index_++;
589 if (index < calling_convention.GetNumberOfFpuRegisters()) {
590 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
591 } else {
592 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
593 }
594 }
595
596 case Primitive::kPrimDouble: {
597 uint32_t index = fp_index_++;
598 stack_index_ += 2;
599 if (index < calling_convention.GetNumberOfFpuRegisters()) {
600 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
601 } else {
602 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 }
604 }
605
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 case Primitive::kPrimVoid:
607 LOG(FATAL) << "Unexpected parameter type " << type;
608 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100609 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100610 return Location();
611}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100612
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613void CodeGeneratorX86::Move32(Location destination, Location source) {
614 if (source.Equals(destination)) {
615 return;
616 }
617 if (destination.IsRegister()) {
618 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 } else {
623 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (destination.IsFpuRegister()) {
627 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else {
632 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000636 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500641 } else if (source.IsConstant()) {
642 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000643 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500644 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 } else {
646 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100647 __ pushl(Address(ESP, source.GetStackIndex()));
648 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 }
650 }
651}
652
653void CodeGeneratorX86::Move64(Location destination, Location source) {
654 if (source.Equals(destination)) {
655 return;
656 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 if (destination.IsRegisterPair()) {
658 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 EmitParallelMoves(
660 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
661 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100662 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100664 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
665 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (source.IsFpuRegister()) {
667 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000669 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
672 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
674 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500676 if (source.IsFpuRegister()) {
677 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
678 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000679 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100680 } else {
681 LOG(FATAL) << "Unimplemented";
682 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000684 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000686 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000691 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000692 } else if (source.IsConstant()) {
693 HConstant* constant = source.GetConstant();
694 int64_t value;
695 if (constant->IsLongConstant()) {
696 value = constant->AsLongConstant()->GetValue();
697 } else {
698 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000699 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000700 }
701 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
702 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000704 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000705 EmitParallelMoves(
706 Location::StackSlot(source.GetStackIndex()),
707 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000709 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100710 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
711 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 }
713 }
714}
715
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100716void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000717 LocationSummary* locations = instruction->GetLocations();
718 if (locations != nullptr && locations->Out().Equals(location)) {
719 return;
720 }
721
722 if (locations != nullptr && locations->Out().IsConstant()) {
723 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000724 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
725 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000728 } else if (location.IsStackSlot()) {
729 __ movl(Address(ESP, location.GetStackIndex()), imm);
730 } else {
731 DCHECK(location.IsConstant());
732 DCHECK_EQ(location.GetConstant(), const_to_move);
733 }
734 } else if (const_to_move->IsLongConstant()) {
735 int64_t value = const_to_move->AsLongConstant()->GetValue();
736 if (location.IsRegisterPair()) {
737 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
738 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
739 } else if (location.IsDoubleStackSlot()) {
740 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000741 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
742 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 } else {
744 DCHECK(location.IsConstant());
745 DCHECK_EQ(location.GetConstant(), instruction);
746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000748 } else if (instruction->IsTemporary()) {
749 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000750 if (temp_location.IsStackSlot()) {
751 Move32(location, temp_location);
752 } else {
753 DCHECK(temp_location.IsDoubleStackSlot());
754 Move64(location, temp_location);
755 }
Roland Levillain476df552014-10-09 17:51:36 +0100756 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100758 switch (instruction->GetType()) {
759 case Primitive::kPrimBoolean:
760 case Primitive::kPrimByte:
761 case Primitive::kPrimChar:
762 case Primitive::kPrimShort:
763 case Primitive::kPrimInt:
764 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 case Primitive::kPrimFloat:
766 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 break;
768
769 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 case Primitive::kPrimDouble:
771 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 break;
773
774 default:
775 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
776 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100778 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 switch (instruction->GetType()) {
780 case Primitive::kPrimBoolean:
781 case Primitive::kPrimByte:
782 case Primitive::kPrimChar:
783 case Primitive::kPrimShort:
784 case Primitive::kPrimInt:
785 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 break;
789
790 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000792 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794
795 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798 }
799}
800
801void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803}
804
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 DCHECK(!successor->IsExitBlock());
808
809 HBasicBlock* block = got->GetBlock();
810 HInstruction* previous = got->GetPrevious();
811
812 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000813 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
815 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
816 return;
817 }
818
819 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
820 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
821 }
822 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000823 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000824 }
825}
826
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700832 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000833}
834
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700835void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
836 Label* true_target,
837 Label* false_target,
838 Label* always_true_target) {
839 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (cond->IsIntConstant()) {
841 // Constant condition, statically compared against 1.
842 int32_t cond_value = cond->AsIntConstant()->GetValue();
843 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844 if (always_true_target != nullptr) {
845 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100846 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100848 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 DCHECK_EQ(cond_value, 0);
850 }
851 } else {
852 bool materialized =
853 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
854 // Moves do not affect the eflags register, so if the condition is
855 // evaluated just before the if, we don't need to evaluate it
856 // again.
857 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700858 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100859 if (materialized) {
860 if (!eflags_set) {
861 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500864 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 } else {
866 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
867 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700870 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 }
872 } else {
873 Location lhs = cond->GetLocations()->InAt(0);
874 Location rhs = cond->GetLocations()->InAt(1);
875 // LHS is guaranteed to be in a register (see
876 // LocationsBuilderX86::VisitCondition).
877 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100880 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500881 if (constant == 0) {
882 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
883 } else {
884 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
885 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000887 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700889 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 if (false_target != nullptr) {
893 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894 }
895}
896
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700897void LocationsBuilderX86::VisitIf(HIf* if_instr) {
898 LocationSummary* locations =
899 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
900 HInstruction* cond = if_instr->InputAt(0);
901 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
902 locations->SetInAt(0, Location::Any());
903 }
904}
905
906void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
907 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
908 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
909 Label* always_true_target = true_target;
910 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
911 if_instr->IfTrueSuccessor())) {
912 always_true_target = nullptr;
913 }
914 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
915 if_instr->IfFalseSuccessor())) {
916 false_target = nullptr;
917 }
918 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
919}
920
921void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
922 LocationSummary* locations = new (GetGraph()->GetArena())
923 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
924 HInstruction* cond = deoptimize->InputAt(0);
925 DCHECK(cond->IsCondition());
926 if (cond->AsCondition()->NeedsMaterialization()) {
927 locations->SetInAt(0, Location::Any());
928 }
929}
930
931void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
932 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
933 DeoptimizationSlowPathX86(deoptimize);
934 codegen_->AddSlowPath(slow_path);
935 Label* slow_path_entry = slow_path->GetEntryLabel();
936 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
937}
938
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941}
942
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000943void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
944 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000945}
946
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100948 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949}
950
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000951void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100952 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700953 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000954}
955
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 switch (store->InputAt(1)->GetType()) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
968 break;
969
970 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
973 break;
974
975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 }
978 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983}
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100988 locations->SetInAt(0, Location::RequiresRegister());
989 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100990 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500991 // We need a byte register.
992 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100993 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000994}
995
Dave Allison20dfc792014-06-16 20:44:29 -0700996void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
997 if (comp->NeedsMaterialization()) {
998 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000999 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001000 // Clear register: setcc only sets the low byte.
1001 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001002 Location lhs = locations->InAt(0);
1003 Location rhs = locations->InAt(1);
1004 if (rhs.IsRegister()) {
1005 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1006 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001007 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001008 if (constant == 0) {
1009 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1010 } else {
1011 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1012 }
Dave Allison20dfc792014-06-16 20:44:29 -07001013 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001014 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001015 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001016 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001017 }
Dave Allison20dfc792014-06-16 20:44:29 -07001018}
1019
1020void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1065 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
1068void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001069 LocationSummary* locations =
1070 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001071 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072}
1073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001076 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077}
1078
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001079void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1080 LocationSummary* locations =
1081 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1082 locations->SetOut(Location::ConstantLocation(constant));
1083}
1084
1085void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1086 // Will be generated at use site.
1087 UNUSED(constant);
1088}
1089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 LocationSummary* locations =
1092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001093 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094}
1095
1096void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1097 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001098 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099}
1100
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001101void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1102 LocationSummary* locations =
1103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1104 locations->SetOut(Location::ConstantLocation(constant));
1105}
1106
1107void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001110}
1111
1112void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1113 LocationSummary* locations =
1114 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1115 locations->SetOut(Location::ConstantLocation(constant));
1116}
1117
1118void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1119 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001121}
1122
Calin Juravle27df7582015-04-17 19:12:31 +01001123void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1124 memory_barrier->SetLocations(nullptr);
1125}
1126
1127void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1128 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1129}
1130
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001132 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133}
1134
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001136 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001138}
1139
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001141 LocationSummary* locations =
1142 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 switch (ret->InputAt(0)->GetType()) {
1144 case Primitive::kPrimBoolean:
1145 case Primitive::kPrimByte:
1146 case Primitive::kPrimChar:
1147 case Primitive::kPrimShort:
1148 case Primitive::kPrimInt:
1149 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001150 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 break;
1152
1153 case Primitive::kPrimLong:
1154 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 break;
1157
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 case Primitive::kPrimFloat:
1159 case Primitive::kPrimDouble:
1160 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001162 break;
1163
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167}
1168
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001169void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 if (kIsDebugBuild) {
1171 switch (ret->InputAt(0)->GetType()) {
1172 case Primitive::kPrimBoolean:
1173 case Primitive::kPrimByte:
1174 case Primitive::kPrimChar:
1175 case Primitive::kPrimShort:
1176 case Primitive::kPrimInt:
1177 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001178 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 break;
1180
1181 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1183 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 break;
1185
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 case Primitive::kPrimFloat:
1187 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 break;
1190
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 }
1194 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001198void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
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>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001223 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001224}
1225
1226void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1227 HandleInvoke(invoke);
1228}
1229
1230void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001231 LocationSummary* locations =
1232 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001233 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001234
1235 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001236 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001237 HInstruction* input = invoke->InputAt(i);
1238 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1239 }
1240
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 switch (invoke->GetType()) {
1242 case Primitive::kPrimBoolean:
1243 case Primitive::kPrimByte:
1244 case Primitive::kPrimChar:
1245 case Primitive::kPrimShort:
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001248 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 break;
1250
1251 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001252 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 break;
1254
1255 case Primitive::kPrimVoid:
1256 break;
1257
1258 case Primitive::kPrimDouble:
1259 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001260 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001261 break;
1262 }
1263
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001264 invoke->SetLocations(locations);
1265}
1266
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001267void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001268 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001269 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1270 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1271 LocationSummary* locations = invoke->GetLocations();
1272 Location receiver = locations->InAt(0);
1273 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1274 // temp = object->GetClass();
1275 if (receiver.IsStackSlot()) {
1276 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1277 __ movl(temp, Address(temp, class_offset));
1278 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001279 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001280 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001281 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282 // temp = temp->GetMethodAt(method_offset);
1283 __ movl(temp, Address(temp, method_offset));
1284 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001285 __ call(Address(
1286 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001287
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001288 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001289 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001290}
1291
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001292void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1293 HandleInvoke(invoke);
1294 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001295 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001296}
1297
1298void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1299 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1302 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1303 LocationSummary* locations = invoke->GetLocations();
1304 Location receiver = locations->InAt(0);
1305 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1306
1307 // Set the hidden argument.
1308 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310
1311 // temp = object->GetClass();
1312 if (receiver.IsStackSlot()) {
1313 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1314 __ movl(temp, Address(temp, class_offset));
1315 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001317 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001318 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319 // temp = temp->GetImtEntryAt(method_offset);
1320 __ movl(temp, Address(temp, method_offset));
1321 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001322 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001323 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001324
1325 DCHECK(!codegen_->IsLeafMethod());
1326 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1327}
1328
Roland Levillain88cb1752014-10-20 16:36:47 +01001329void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1330 LocationSummary* locations =
1331 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1332 switch (neg->GetResultType()) {
1333 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001334 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 locations->SetInAt(0, Location::RequiresRegister());
1336 locations->SetOut(Location::SameAsFirstInput());
1337 break;
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001340 locations->SetInAt(0, Location::RequiresFpuRegister());
1341 locations->SetOut(Location::SameAsFirstInput());
1342 locations->AddTemp(Location::RequiresRegister());
1343 locations->AddTemp(Location::RequiresFpuRegister());
1344 break;
1345
Roland Levillain88cb1752014-10-20 16:36:47 +01001346 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001348 locations->SetOut(Location::SameAsFirstInput());
1349 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1354 }
1355}
1356
1357void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1358 LocationSummary* locations = neg->GetLocations();
1359 Location out = locations->Out();
1360 Location in = locations->InAt(0);
1361 switch (neg->GetResultType()) {
1362 case Primitive::kPrimInt:
1363 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001366 break;
1367
1368 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001369 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001370 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001371 __ negl(out.AsRegisterPairLow<Register>());
1372 // Negation is similar to subtraction from zero. The least
1373 // significant byte triggers a borrow when it is different from
1374 // zero; to take it into account, add 1 to the most significant
1375 // byte if the carry flag (CF) is set to 1 after the first NEGL
1376 // operation.
1377 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1378 __ negl(out.AsRegisterPairHigh<Register>());
1379 break;
1380
Roland Levillain5368c212014-11-27 15:03:41 +00001381 case Primitive::kPrimFloat: {
1382 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 Register constant = locations->GetTemp(0).AsRegister<Register>();
1384 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001385 // Implement float negation with an exclusive or with value
1386 // 0x80000000 (mask for bit 31, representing the sign of a
1387 // single-precision floating-point number).
1388 __ movl(constant, Immediate(INT32_C(0x80000000)));
1389 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001391 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001392 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001393
Roland Levillain5368c212014-11-27 15:03:41 +00001394 case Primitive::kPrimDouble: {
1395 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001396 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001397 // Implement double negation with an exclusive or with value
1398 // 0x8000000000000000 (mask for bit 63, representing the sign of
1399 // a double-precision floating-point number).
1400 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001402 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001403 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001404
1405 default:
1406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1407 }
1408}
1409
Roland Levillaindff1f282014-11-05 14:15:05 +00001410void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 Primitive::Type result_type = conversion->GetResultType();
1412 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001413 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001414
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001415 // The float-to-long and double-to-long type conversions rely on a
1416 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001417 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001418 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1419 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001420 ? LocationSummary::kCall
1421 : LocationSummary::kNoCall;
1422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1424
David Brazdilb2bd1c52015-03-25 11:17:37 +00001425 // The Java language does not allow treating boolean as an integral type but
1426 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001427
Roland Levillaindff1f282014-11-05 14:15:05 +00001428 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001429 case Primitive::kPrimByte:
1430 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001431 case Primitive::kPrimBoolean:
1432 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001433 case Primitive::kPrimShort:
1434 case Primitive::kPrimInt:
1435 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001436 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001437 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1438 // Make the output overlap to please the register allocator. This greatly simplifies
1439 // the validation of the linear scan implementation
1440 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001441 break;
1442
1443 default:
1444 LOG(FATAL) << "Unexpected type conversion from " << input_type
1445 << " to " << result_type;
1446 }
1447 break;
1448
Roland Levillain01a8d712014-11-14 16:27:39 +00001449 case Primitive::kPrimShort:
1450 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001451 case Primitive::kPrimBoolean:
1452 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001453 case Primitive::kPrimByte:
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimChar:
1456 // Processing a Dex `int-to-short' instruction.
1457 locations->SetInAt(0, Location::Any());
1458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1459 break;
1460
1461 default:
1462 LOG(FATAL) << "Unexpected type conversion from " << input_type
1463 << " to " << result_type;
1464 }
1465 break;
1466
Roland Levillain946e1432014-11-11 17:35:19 +00001467 case Primitive::kPrimInt:
1468 switch (input_type) {
1469 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001470 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001471 locations->SetInAt(0, Location::Any());
1472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1473 break;
1474
1475 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001476 // Processing a Dex `float-to-int' instruction.
1477 locations->SetInAt(0, Location::RequiresFpuRegister());
1478 locations->SetOut(Location::RequiresRegister());
1479 locations->AddTemp(Location::RequiresFpuRegister());
1480 break;
1481
Roland Levillain946e1432014-11-11 17:35:19 +00001482 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001483 // Processing a Dex `double-to-int' instruction.
1484 locations->SetInAt(0, Location::RequiresFpuRegister());
1485 locations->SetOut(Location::RequiresRegister());
1486 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001487 break;
1488
1489 default:
1490 LOG(FATAL) << "Unexpected type conversion from " << input_type
1491 << " to " << result_type;
1492 }
1493 break;
1494
Roland Levillaindff1f282014-11-05 14:15:05 +00001495 case Primitive::kPrimLong:
1496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001497 case Primitive::kPrimBoolean:
1498 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001499 case Primitive::kPrimByte:
1500 case Primitive::kPrimShort:
1501 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001502 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001503 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 locations->SetInAt(0, Location::RegisterLocation(EAX));
1505 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1506 break;
1507
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001508 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001509 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001510 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001511 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001512 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1513 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1514
Vladimir Marko949c91f2015-01-27 10:48:44 +00001515 // The runtime helper puts the result in EAX, EDX.
1516 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001517 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001519
1520 default:
1521 LOG(FATAL) << "Unexpected type conversion from " << input_type
1522 << " to " << result_type;
1523 }
1524 break;
1525
Roland Levillain981e4542014-11-14 11:47:14 +00001526 case Primitive::kPrimChar:
1527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001528 case Primitive::kPrimBoolean:
1529 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001530 case Primitive::kPrimByte:
1531 case Primitive::kPrimShort:
1532 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001533 // Processing a Dex `int-to-char' instruction.
1534 locations->SetInAt(0, Location::Any());
1535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001545 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001546 case Primitive::kPrimBoolean:
1547 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001548 case Primitive::kPrimByte:
1549 case Primitive::kPrimShort:
1550 case Primitive::kPrimInt:
1551 case Primitive::kPrimChar:
1552 // Processing a Dex `int-to-float' instruction.
1553 locations->SetInAt(0, Location::RequiresRegister());
1554 locations->SetOut(Location::RequiresFpuRegister());
1555 break;
1556
1557 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001558 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001559 locations->SetInAt(0, Location::Any());
1560 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001561 break;
1562
Roland Levillaincff13742014-11-17 14:32:17 +00001563 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001564 // Processing a Dex `double-to-float' instruction.
1565 locations->SetInAt(0, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001567 break;
1568
1569 default:
1570 LOG(FATAL) << "Unexpected type conversion from " << input_type
1571 << " to " << result_type;
1572 };
1573 break;
1574
Roland Levillaindff1f282014-11-05 14:15:05 +00001575 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001576 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001577 case Primitive::kPrimBoolean:
1578 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001579 case Primitive::kPrimByte:
1580 case Primitive::kPrimShort:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 // Processing a Dex `int-to-double' instruction.
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetOut(Location::RequiresFpuRegister());
1586 break;
1587
1588 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001589 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001590 locations->SetInAt(0, Location::Any());
1591 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001592 break;
1593
Roland Levillaincff13742014-11-17 14:32:17 +00001594 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001595 // Processing a Dex `float-to-double' instruction.
1596 locations->SetInAt(0, Location::RequiresFpuRegister());
1597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001598 break;
1599
1600 default:
1601 LOG(FATAL) << "Unexpected type conversion from " << input_type
1602 << " to " << result_type;
1603 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001604 break;
1605
1606 default:
1607 LOG(FATAL) << "Unexpected type conversion from " << input_type
1608 << " to " << result_type;
1609 }
1610}
1611
1612void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1613 LocationSummary* locations = conversion->GetLocations();
1614 Location out = locations->Out();
1615 Location in = locations->InAt(0);
1616 Primitive::Type result_type = conversion->GetResultType();
1617 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001618 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001619 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001620 case Primitive::kPrimByte:
1621 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001622 case Primitive::kPrimBoolean:
1623 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001627 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001628 if (in.IsRegister()) {
1629 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001630 } else {
1631 DCHECK(in.GetConstant()->IsIntConstant());
1632 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1634 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001635 break;
1636
1637 default:
1638 LOG(FATAL) << "Unexpected type conversion from " << input_type
1639 << " to " << result_type;
1640 }
1641 break;
1642
Roland Levillain01a8d712014-11-14 16:27:39 +00001643 case Primitive::kPrimShort:
1644 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001645 case Primitive::kPrimBoolean:
1646 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001647 case Primitive::kPrimByte:
1648 case Primitive::kPrimInt:
1649 case Primitive::kPrimChar:
1650 // Processing a Dex `int-to-short' instruction.
1651 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001655 } else {
1656 DCHECK(in.GetConstant()->IsIntConstant());
1657 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001658 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 }
1660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 }
1666 break;
1667
Roland Levillain946e1432014-11-11 17:35:19 +00001668 case Primitive::kPrimInt:
1669 switch (input_type) {
1670 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001671 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001672 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001674 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001676 } else {
1677 DCHECK(in.IsConstant());
1678 DCHECK(in.GetConstant()->IsLongConstant());
1679 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001681 }
1682 break;
1683
Roland Levillain3f8f9362014-12-02 17:45:01 +00001684 case Primitive::kPrimFloat: {
1685 // Processing a Dex `float-to-int' instruction.
1686 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1687 Register output = out.AsRegister<Register>();
1688 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1689 Label done, nan;
1690
1691 __ movl(output, Immediate(kPrimIntMax));
1692 // temp = int-to-float(output)
1693 __ cvtsi2ss(temp, output);
1694 // if input >= temp goto done
1695 __ comiss(input, temp);
1696 __ j(kAboveEqual, &done);
1697 // if input == NaN goto nan
1698 __ j(kUnordered, &nan);
1699 // output = float-to-int-truncate(input)
1700 __ cvttss2si(output, input);
1701 __ jmp(&done);
1702 __ Bind(&nan);
1703 // output = 0
1704 __ xorl(output, output);
1705 __ Bind(&done);
1706 break;
1707 }
1708
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001709 case Primitive::kPrimDouble: {
1710 // Processing a Dex `double-to-int' instruction.
1711 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1712 Register output = out.AsRegister<Register>();
1713 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1714 Label done, nan;
1715
1716 __ movl(output, Immediate(kPrimIntMax));
1717 // temp = int-to-double(output)
1718 __ cvtsi2sd(temp, output);
1719 // if input >= temp goto done
1720 __ comisd(input, temp);
1721 __ j(kAboveEqual, &done);
1722 // if input == NaN goto nan
1723 __ j(kUnordered, &nan);
1724 // output = double-to-int-truncate(input)
1725 __ cvttsd2si(output, input);
1726 __ jmp(&done);
1727 __ Bind(&nan);
1728 // output = 0
1729 __ xorl(output, output);
1730 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001731 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001732 }
Roland Levillain946e1432014-11-11 17:35:19 +00001733
1734 default:
1735 LOG(FATAL) << "Unexpected type conversion from " << input_type
1736 << " to " << result_type;
1737 }
1738 break;
1739
Roland Levillaindff1f282014-11-05 14:15:05 +00001740 case Primitive::kPrimLong:
1741 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001742 case Primitive::kPrimBoolean:
1743 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 case Primitive::kPrimByte:
1745 case Primitive::kPrimShort:
1746 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001747 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001748 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1750 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001751 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 __ cdq();
1753 break;
1754
1755 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001756 // Processing a Dex `float-to-long' instruction.
1757 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001758 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1759 break;
1760
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001762 // Processing a Dex `double-to-long' instruction.
1763 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1764 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 break;
1766
1767 default:
1768 LOG(FATAL) << "Unexpected type conversion from " << input_type
1769 << " to " << result_type;
1770 }
1771 break;
1772
Roland Levillain981e4542014-11-14 11:47:14 +00001773 case Primitive::kPrimChar:
1774 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001775 case Primitive::kPrimBoolean:
1776 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001777 case Primitive::kPrimByte:
1778 case Primitive::kPrimShort:
1779 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001780 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1781 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001783 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001784 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001785 } else {
1786 DCHECK(in.GetConstant()->IsIntConstant());
1787 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001789 }
1790 break;
1791
1792 default:
1793 LOG(FATAL) << "Unexpected type conversion from " << input_type
1794 << " to " << result_type;
1795 }
1796 break;
1797
Roland Levillaindff1f282014-11-05 14:15:05 +00001798 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001799 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001800 case Primitive::kPrimBoolean:
1801 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
1805 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001807 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001808 break;
1809
Roland Levillain6d0e4832014-11-27 18:31:21 +00001810 case Primitive::kPrimLong: {
1811 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001812 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001813
Roland Levillain232ade02015-04-20 15:14:36 +01001814 // Create stack space for the call to
1815 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1816 // TODO: enhance register allocator to ask for stack temporaries.
1817 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1818 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1819 __ subl(ESP, Immediate(adjustment));
1820 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001821
Roland Levillain232ade02015-04-20 15:14:36 +01001822 // Load the value to the FP stack, using temporaries if needed.
1823 PushOntoFPStack(in, 0, adjustment, false, true);
1824
1825 if (out.IsStackSlot()) {
1826 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1827 } else {
1828 __ fstps(Address(ESP, 0));
1829 Location stack_temp = Location::StackSlot(0);
1830 codegen_->Move32(out, stack_temp);
1831 }
1832
1833 // Remove the temporary stack space we allocated.
1834 if (adjustment != 0) {
1835 __ addl(ESP, Immediate(adjustment));
1836 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001837 break;
1838 }
1839
Roland Levillaincff13742014-11-17 14:32:17 +00001840 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001841 // Processing a Dex `double-to-float' instruction.
1842 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001843 break;
1844
1845 default:
1846 LOG(FATAL) << "Unexpected type conversion from " << input_type
1847 << " to " << result_type;
1848 };
1849 break;
1850
Roland Levillaindff1f282014-11-05 14:15:05 +00001851 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001852 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001853 case Primitive::kPrimBoolean:
1854 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimByte:
1856 case Primitive::kPrimShort:
1857 case Primitive::kPrimInt:
1858 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001859 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001861 break;
1862
Roland Levillain647b9ed2014-11-27 12:06:00 +00001863 case Primitive::kPrimLong: {
1864 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001865 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001866
Roland Levillain232ade02015-04-20 15:14:36 +01001867 // Create stack space for the call to
1868 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1869 // TODO: enhance register allocator to ask for stack temporaries.
1870 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1871 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1872 __ subl(ESP, Immediate(adjustment));
1873 }
1874
1875 // Load the value to the FP stack, using temporaries if needed.
1876 PushOntoFPStack(in, 0, adjustment, false, true);
1877
1878 if (out.IsDoubleStackSlot()) {
1879 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1880 } else {
1881 __ fstpl(Address(ESP, 0));
1882 Location stack_temp = Location::DoubleStackSlot(0);
1883 codegen_->Move64(out, stack_temp);
1884 }
1885
1886 // Remove the temporary stack space we allocated.
1887 if (adjustment != 0) {
1888 __ addl(ESP, Immediate(adjustment));
1889 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001890 break;
1891 }
1892
Roland Levillaincff13742014-11-17 14:32:17 +00001893 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001894 // Processing a Dex `float-to-double' instruction.
1895 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001896 break;
1897
1898 default:
1899 LOG(FATAL) << "Unexpected type conversion from " << input_type
1900 << " to " << result_type;
1901 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 }
1908}
1909
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001910void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001911 LocationSummary* locations =
1912 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001913 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001914 case Primitive::kPrimInt: {
1915 locations->SetInAt(0, Location::RequiresRegister());
1916 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1918 break;
1919 }
1920
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetInAt(1, Location::Any());
1924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926 }
1927
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001928 case Primitive::kPrimFloat:
1929 case Primitive::kPrimDouble: {
1930 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001931 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001936 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1938 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001940}
1941
1942void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1943 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 Location first = locations->InAt(0);
1945 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001946 Location out = locations->Out();
1947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1952 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1953 } else {
1954 __ leal(out.AsRegister<Register>(), Address(
1955 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1956 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001958 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1959 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1960 __ addl(out.AsRegister<Register>(), Immediate(value));
1961 } else {
1962 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1963 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001964 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001965 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001966 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001967 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001968 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001969 }
1970
1971 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001972 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1974 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001975 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1977 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001978 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001979 } else {
1980 DCHECK(second.IsConstant()) << second;
1981 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1982 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1983 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001984 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001985 break;
1986 }
1987
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 case Primitive::kPrimFloat: {
1989 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 }
1994
1995 case Primitive::kPrimDouble: {
1996 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001997 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 }
1999 break;
2000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002002 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002004 }
2005}
2006
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002007void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002008 LocationSummary* locations =
2009 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002010 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002011 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, Location::Any());
2015 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002016 break;
2017 }
Calin Juravle11351682014-10-23 15:38:15 +01002018 case Primitive::kPrimFloat:
2019 case Primitive::kPrimDouble: {
2020 locations->SetInAt(0, Location::RequiresFpuRegister());
2021 locations->SetInAt(1, Location::RequiresFpuRegister());
2022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023 break;
Calin Juravle11351682014-10-23 15:38:15 +01002024 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002026 default:
Calin Juravle11351682014-10-23 15:38:15 +01002027 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002028 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002029}
2030
2031void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2032 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 Location first = locations->InAt(0);
2034 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002035 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002038 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002041 __ subl(first.AsRegister<Register>(),
2042 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002045 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 }
2048
2049 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002050 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002051 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2052 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002053 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002054 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002055 __ sbbl(first.AsRegisterPairHigh<Register>(),
2056 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002057 } else {
2058 DCHECK(second.IsConstant()) << second;
2059 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2060 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2061 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002062 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002063 break;
2064 }
2065
Calin Juravle11351682014-10-23 15:38:15 +01002066 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002067 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002068 break;
Calin Juravle11351682014-10-23 15:38:15 +01002069 }
2070
2071 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002072 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002073 break;
2074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002076 default:
Calin Juravle11351682014-10-23 15:38:15 +01002077 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002078 }
2079}
2080
Calin Juravle34bacdf2014-10-07 20:23:36 +01002081void LocationsBuilderX86::VisitMul(HMul* mul) {
2082 LocationSummary* locations =
2083 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2084 switch (mul->GetResultType()) {
2085 case Primitive::kPrimInt:
2086 locations->SetInAt(0, Location::RequiresRegister());
2087 locations->SetInAt(1, Location::Any());
2088 locations->SetOut(Location::SameAsFirstInput());
2089 break;
2090 case Primitive::kPrimLong: {
2091 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002092 locations->SetInAt(1, Location::Any());
2093 locations->SetOut(Location::SameAsFirstInput());
2094 // Needed for imul on 32bits with 64bits output.
2095 locations->AddTemp(Location::RegisterLocation(EAX));
2096 locations->AddTemp(Location::RegisterLocation(EDX));
2097 break;
2098 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002099 case Primitive::kPrimFloat:
2100 case Primitive::kPrimDouble: {
2101 locations->SetInAt(0, Location::RequiresFpuRegister());
2102 locations->SetInAt(1, Location::RequiresFpuRegister());
2103 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002104 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002105 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002106
2107 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002108 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002109 }
2110}
2111
2112void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2113 LocationSummary* locations = mul->GetLocations();
2114 Location first = locations->InAt(0);
2115 Location second = locations->InAt(1);
2116 DCHECK(first.Equals(locations->Out()));
2117
2118 switch (mul->GetResultType()) {
2119 case Primitive::kPrimInt: {
2120 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002122 } else if (second.IsConstant()) {
2123 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002125 } else {
2126 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002128 }
2129 break;
2130 }
2131
2132 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 Register in1_hi = first.AsRegisterPairHigh<Register>();
2134 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 Register eax = locations->GetTemp(0).AsRegister<Register>();
2136 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002137
2138 DCHECK_EQ(EAX, eax);
2139 DCHECK_EQ(EDX, edx);
2140
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002141 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002142 // output: in1
2143 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2144 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2145 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002146 if (second.IsConstant()) {
2147 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002148
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002149 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2150 int32_t low_value = Low32Bits(value);
2151 int32_t high_value = High32Bits(value);
2152 Immediate low(low_value);
2153 Immediate high(high_value);
2154
2155 __ movl(eax, high);
2156 // eax <- in1.lo * in2.hi
2157 __ imull(eax, in1_lo);
2158 // in1.hi <- in1.hi * in2.lo
2159 __ imull(in1_hi, low);
2160 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2161 __ addl(in1_hi, eax);
2162 // move in2_lo to eax to prepare for double precision
2163 __ movl(eax, low);
2164 // edx:eax <- in1.lo * in2.lo
2165 __ mull(in1_lo);
2166 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2167 __ addl(in1_hi, edx);
2168 // in1.lo <- (in1.lo * in2.lo)[31:0];
2169 __ movl(in1_lo, eax);
2170 } else if (second.IsRegisterPair()) {
2171 Register in2_hi = second.AsRegisterPairHigh<Register>();
2172 Register in2_lo = second.AsRegisterPairLow<Register>();
2173
2174 __ movl(eax, in2_hi);
2175 // eax <- in1.lo * in2.hi
2176 __ imull(eax, in1_lo);
2177 // in1.hi <- in1.hi * in2.lo
2178 __ imull(in1_hi, in2_lo);
2179 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2180 __ addl(in1_hi, eax);
2181 // move in1_lo to eax to prepare for double precision
2182 __ movl(eax, in1_lo);
2183 // edx:eax <- in1.lo * in2.lo
2184 __ mull(in2_lo);
2185 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2186 __ addl(in1_hi, edx);
2187 // in1.lo <- (in1.lo * in2.lo)[31:0];
2188 __ movl(in1_lo, eax);
2189 } else {
2190 DCHECK(second.IsDoubleStackSlot()) << second;
2191 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2192 Address in2_lo(ESP, second.GetStackIndex());
2193
2194 __ movl(eax, in2_hi);
2195 // eax <- in1.lo * in2.hi
2196 __ imull(eax, in1_lo);
2197 // in1.hi <- in1.hi * in2.lo
2198 __ imull(in1_hi, in2_lo);
2199 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2200 __ addl(in1_hi, eax);
2201 // move in1_lo to eax to prepare for double precision
2202 __ movl(eax, in1_lo);
2203 // edx:eax <- in1.lo * in2.lo
2204 __ mull(in2_lo);
2205 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2206 __ addl(in1_hi, edx);
2207 // in1.lo <- (in1.lo * in2.lo)[31:0];
2208 __ movl(in1_lo, eax);
2209 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002210
2211 break;
2212 }
2213
Calin Juravleb5bfa962014-10-21 18:02:24 +01002214 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002215 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002216 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002217 }
2218
2219 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002220 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 break;
2222 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002223
2224 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002225 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002226 }
2227}
2228
Roland Levillain232ade02015-04-20 15:14:36 +01002229void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2230 uint32_t temp_offset,
2231 uint32_t stack_adjustment,
2232 bool is_fp,
2233 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002234 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002235 DCHECK(!is_wide);
2236 if (is_fp) {
2237 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2238 } else {
2239 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2240 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002241 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002242 DCHECK(is_wide);
2243 if (is_fp) {
2244 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2245 } else {
2246 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2247 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002248 } else {
2249 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002250 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002251 Location stack_temp = Location::StackSlot(temp_offset);
2252 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002253 if (is_fp) {
2254 __ flds(Address(ESP, temp_offset));
2255 } else {
2256 __ filds(Address(ESP, temp_offset));
2257 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 } else {
2259 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2260 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002261 if (is_fp) {
2262 __ fldl(Address(ESP, temp_offset));
2263 } else {
2264 __ fildl(Address(ESP, temp_offset));
2265 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002266 }
2267 }
2268}
2269
2270void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2271 Primitive::Type type = rem->GetResultType();
2272 bool is_float = type == Primitive::kPrimFloat;
2273 size_t elem_size = Primitive::ComponentSize(type);
2274 LocationSummary* locations = rem->GetLocations();
2275 Location first = locations->InAt(0);
2276 Location second = locations->InAt(1);
2277 Location out = locations->Out();
2278
2279 // Create stack space for 2 elements.
2280 // TODO: enhance register allocator to ask for stack temporaries.
2281 __ subl(ESP, Immediate(2 * elem_size));
2282
2283 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002284 const bool is_wide = !is_float;
2285 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2286 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002287
2288 // Loop doing FPREM until we stabilize.
2289 Label retry;
2290 __ Bind(&retry);
2291 __ fprem();
2292
2293 // Move FP status to AX.
2294 __ fstsw();
2295
2296 // And see if the argument reduction is complete. This is signaled by the
2297 // C2 FPU flag bit set to 0.
2298 __ andl(EAX, Immediate(kC2ConditionMask));
2299 __ j(kNotEqual, &retry);
2300
2301 // We have settled on the final value. Retrieve it into an XMM register.
2302 // Store FP top of stack to real stack.
2303 if (is_float) {
2304 __ fsts(Address(ESP, 0));
2305 } else {
2306 __ fstl(Address(ESP, 0));
2307 }
2308
2309 // Pop the 2 items from the FP stack.
2310 __ fucompp();
2311
2312 // Load the value from the stack into an XMM register.
2313 DCHECK(out.IsFpuRegister()) << out;
2314 if (is_float) {
2315 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2316 } else {
2317 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2318 }
2319
2320 // And remove the temporary stack space we allocated.
2321 __ addl(ESP, Immediate(2 * elem_size));
2322}
2323
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002324
2325void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2326 DCHECK(instruction->IsDiv() || instruction->IsRem());
2327
2328 LocationSummary* locations = instruction->GetLocations();
2329 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002330 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002331
2332 Register out_register = locations->Out().AsRegister<Register>();
2333 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002334 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002335
2336 DCHECK(imm == 1 || imm == -1);
2337
2338 if (instruction->IsRem()) {
2339 __ xorl(out_register, out_register);
2340 } else {
2341 __ movl(out_register, input_register);
2342 if (imm == -1) {
2343 __ negl(out_register);
2344 }
2345 }
2346}
2347
2348
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002349void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002350 LocationSummary* locations = instruction->GetLocations();
2351
2352 Register out_register = locations->Out().AsRegister<Register>();
2353 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002354 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002355
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002356 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002357 Register num = locations->GetTemp(0).AsRegister<Register>();
2358
2359 __ leal(num, Address(input_register, std::abs(imm) - 1));
2360 __ testl(input_register, input_register);
2361 __ cmovl(kGreaterEqual, num, input_register);
2362 int shift = CTZ(imm);
2363 __ sarl(num, Immediate(shift));
2364
2365 if (imm < 0) {
2366 __ negl(num);
2367 }
2368
2369 __ movl(out_register, num);
2370}
2371
2372void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2373 DCHECK(instruction->IsDiv() || instruction->IsRem());
2374
2375 LocationSummary* locations = instruction->GetLocations();
2376 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2377
2378 Register eax = locations->InAt(0).AsRegister<Register>();
2379 Register out = locations->Out().AsRegister<Register>();
2380 Register num;
2381 Register edx;
2382
2383 if (instruction->IsDiv()) {
2384 edx = locations->GetTemp(0).AsRegister<Register>();
2385 num = locations->GetTemp(1).AsRegister<Register>();
2386 } else {
2387 edx = locations->Out().AsRegister<Register>();
2388 num = locations->GetTemp(0).AsRegister<Register>();
2389 }
2390
2391 DCHECK_EQ(EAX, eax);
2392 DCHECK_EQ(EDX, edx);
2393 if (instruction->IsDiv()) {
2394 DCHECK_EQ(EAX, out);
2395 } else {
2396 DCHECK_EQ(EDX, out);
2397 }
2398
2399 int64_t magic;
2400 int shift;
2401 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2402
2403 Label ndiv;
2404 Label end;
2405 // If numerator is 0, the result is 0, no computation needed.
2406 __ testl(eax, eax);
2407 __ j(kNotEqual, &ndiv);
2408
2409 __ xorl(out, out);
2410 __ jmp(&end);
2411
2412 __ Bind(&ndiv);
2413
2414 // Save the numerator.
2415 __ movl(num, eax);
2416
2417 // EAX = magic
2418 __ movl(eax, Immediate(magic));
2419
2420 // EDX:EAX = magic * numerator
2421 __ imull(num);
2422
2423 if (imm > 0 && magic < 0) {
2424 // EDX += num
2425 __ addl(edx, num);
2426 } else if (imm < 0 && magic > 0) {
2427 __ subl(edx, num);
2428 }
2429
2430 // Shift if needed.
2431 if (shift != 0) {
2432 __ sarl(edx, Immediate(shift));
2433 }
2434
2435 // EDX += 1 if EDX < 0
2436 __ movl(eax, edx);
2437 __ shrl(edx, Immediate(31));
2438 __ addl(edx, eax);
2439
2440 if (instruction->IsRem()) {
2441 __ movl(eax, num);
2442 __ imull(edx, Immediate(imm));
2443 __ subl(eax, edx);
2444 __ movl(edx, eax);
2445 } else {
2446 __ movl(eax, edx);
2447 }
2448 __ Bind(&end);
2449}
2450
Calin Juravlebacfec32014-11-14 15:54:36 +00002451void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2452 DCHECK(instruction->IsDiv() || instruction->IsRem());
2453
2454 LocationSummary* locations = instruction->GetLocations();
2455 Location out = locations->Out();
2456 Location first = locations->InAt(0);
2457 Location second = locations->InAt(1);
2458 bool is_div = instruction->IsDiv();
2459
2460 switch (instruction->GetResultType()) {
2461 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 DCHECK_EQ(EAX, first.AsRegister<Register>());
2463 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002464
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002465 if (instruction->InputAt(1)->IsIntConstant()) {
2466 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002467
2468 if (imm == 0) {
2469 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2470 } else if (imm == 1 || imm == -1) {
2471 DivRemOneOrMinusOne(instruction);
2472 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002473 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002474 } else {
2475 DCHECK(imm <= -2 || imm >= 2);
2476 GenerateDivRemWithAnyConstant(instruction);
2477 }
2478 } else {
2479 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002480 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002481 is_div);
2482 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002483
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 Register second_reg = second.AsRegister<Register>();
2485 // 0x80000000/-1 triggers an arithmetic exception!
2486 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2487 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002488
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002489 __ cmpl(second_reg, Immediate(-1));
2490 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002491
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002492 // edx:eax <- sign-extended of eax
2493 __ cdq();
2494 // eax = quotient, edx = remainder
2495 __ idivl(second_reg);
2496 __ Bind(slow_path->GetExitLabel());
2497 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002498 break;
2499 }
2500
2501 case Primitive::kPrimLong: {
2502 InvokeRuntimeCallingConvention calling_convention;
2503 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2504 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2505 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2506 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2507 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2508 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2509
2510 if (is_div) {
2511 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2512 } else {
2513 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2514 }
2515 uint32_t dex_pc = is_div
2516 ? instruction->AsDiv()->GetDexPc()
2517 : instruction->AsRem()->GetDexPc();
2518 codegen_->RecordPcInfo(instruction, dex_pc);
2519
2520 break;
2521 }
2522
2523 default:
2524 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2525 }
2526}
2527
Calin Juravle7c4954d2014-10-28 16:57:40 +00002528void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002529 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002530 ? LocationSummary::kCall
2531 : LocationSummary::kNoCall;
2532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2533
Calin Juravle7c4954d2014-10-28 16:57:40 +00002534 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002535 case Primitive::kPrimInt: {
2536 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002537 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002538 locations->SetOut(Location::SameAsFirstInput());
2539 // Intel uses edx:eax as the dividend.
2540 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002541 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2542 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2543 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002544 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002545 locations->AddTemp(Location::RequiresRegister());
2546 }
Calin Juravled0d48522014-11-04 16:40:20 +00002547 break;
2548 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002549 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002550 InvokeRuntimeCallingConvention calling_convention;
2551 locations->SetInAt(0, Location::RegisterPairLocation(
2552 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2553 locations->SetInAt(1, Location::RegisterPairLocation(
2554 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2555 // Runtime helper puts the result in EAX, EDX.
2556 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002557 break;
2558 }
2559 case Primitive::kPrimFloat:
2560 case Primitive::kPrimDouble: {
2561 locations->SetInAt(0, Location::RequiresFpuRegister());
2562 locations->SetInAt(1, Location::RequiresFpuRegister());
2563 locations->SetOut(Location::SameAsFirstInput());
2564 break;
2565 }
2566
2567 default:
2568 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2569 }
2570}
2571
2572void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2573 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002574 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002575 Location first = locations->InAt(0);
2576 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002577
2578 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002579 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002580 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002581 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002582 break;
2583 }
2584
2585 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002586 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002587 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002588 break;
2589 }
2590
2591 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002592 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 break;
2595 }
2596
2597 default:
2598 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2599 }
2600}
2601
Calin Juravlebacfec32014-11-14 15:54:36 +00002602void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002603 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002604
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002605 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2606 ? LocationSummary::kCall
2607 : LocationSummary::kNoCall;
2608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002609
Calin Juravled2ec87d2014-12-08 14:24:46 +00002610 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002611 case Primitive::kPrimInt: {
2612 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002613 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002614 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002615 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2616 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2617 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002618 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002619 locations->AddTemp(Location::RequiresRegister());
2620 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 break;
2622 }
2623 case Primitive::kPrimLong: {
2624 InvokeRuntimeCallingConvention calling_convention;
2625 locations->SetInAt(0, Location::RegisterPairLocation(
2626 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2627 locations->SetInAt(1, Location::RegisterPairLocation(
2628 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2629 // Runtime helper puts the result in EAX, EDX.
2630 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2631 break;
2632 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002633 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002634 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002635 locations->SetInAt(0, Location::Any());
2636 locations->SetInAt(1, Location::Any());
2637 locations->SetOut(Location::RequiresFpuRegister());
2638 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002639 break;
2640 }
2641
2642 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002643 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002644 }
2645}
2646
2647void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2648 Primitive::Type type = rem->GetResultType();
2649 switch (type) {
2650 case Primitive::kPrimInt:
2651 case Primitive::kPrimLong: {
2652 GenerateDivRemIntegral(rem);
2653 break;
2654 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002655 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002656 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002657 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002658 break;
2659 }
2660 default:
2661 LOG(FATAL) << "Unexpected rem type " << type;
2662 }
2663}
2664
Calin Juravled0d48522014-11-04 16:40:20 +00002665void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2666 LocationSummary* locations =
2667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002668 switch (instruction->GetType()) {
2669 case Primitive::kPrimInt: {
2670 locations->SetInAt(0, Location::Any());
2671 break;
2672 }
2673 case Primitive::kPrimLong: {
2674 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2675 if (!instruction->IsConstant()) {
2676 locations->AddTemp(Location::RequiresRegister());
2677 }
2678 break;
2679 }
2680 default:
2681 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2682 }
Calin Juravled0d48522014-11-04 16:40:20 +00002683 if (instruction->HasUses()) {
2684 locations->SetOut(Location::SameAsFirstInput());
2685 }
2686}
2687
2688void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2689 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2690 codegen_->AddSlowPath(slow_path);
2691
2692 LocationSummary* locations = instruction->GetLocations();
2693 Location value = locations->InAt(0);
2694
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002695 switch (instruction->GetType()) {
2696 case Primitive::kPrimInt: {
2697 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002699 __ j(kEqual, slow_path->GetEntryLabel());
2700 } else if (value.IsStackSlot()) {
2701 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2702 __ j(kEqual, slow_path->GetEntryLabel());
2703 } else {
2704 DCHECK(value.IsConstant()) << value;
2705 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2706 __ jmp(slow_path->GetEntryLabel());
2707 }
2708 }
2709 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002710 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002711 case Primitive::kPrimLong: {
2712 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002714 __ movl(temp, value.AsRegisterPairLow<Register>());
2715 __ orl(temp, value.AsRegisterPairHigh<Register>());
2716 __ j(kEqual, slow_path->GetEntryLabel());
2717 } else {
2718 DCHECK(value.IsConstant()) << value;
2719 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2720 __ jmp(slow_path->GetEntryLabel());
2721 }
2722 }
2723 break;
2724 }
2725 default:
2726 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002727 }
Calin Juravled0d48522014-11-04 16:40:20 +00002728}
2729
Calin Juravle9aec02f2014-11-18 23:06:35 +00002730void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2731 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2732
2733 LocationSummary* locations =
2734 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2735
2736 switch (op->GetResultType()) {
2737 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002738 locations->SetInAt(0, Location::RequiresRegister());
2739 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2741 locations->SetOut(Location::SameAsFirstInput());
2742 break;
2743 }
2744 case Primitive::kPrimLong: {
2745 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002746 // The shift count needs to be in CL.
2747 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002748 locations->SetOut(Location::SameAsFirstInput());
2749 break;
2750 }
2751 default:
2752 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2753 }
2754}
2755
2756void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2757 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2758
2759 LocationSummary* locations = op->GetLocations();
2760 Location first = locations->InAt(0);
2761 Location second = locations->InAt(1);
2762 DCHECK(first.Equals(locations->Out()));
2763
2764 switch (op->GetResultType()) {
2765 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002766 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002767 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002769 DCHECK_EQ(ECX, second_reg);
2770 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002771 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002773 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002775 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002776 }
2777 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002778 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2779 if (op->IsShl()) {
2780 __ shll(first_reg, imm);
2781 } else if (op->IsShr()) {
2782 __ sarl(first_reg, imm);
2783 } else {
2784 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002785 }
2786 }
2787 break;
2788 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002789 case Primitive::kPrimLong: {
2790 Register second_reg = second.AsRegister<Register>();
2791 DCHECK_EQ(ECX, second_reg);
2792 if (op->IsShl()) {
2793 GenerateShlLong(first, second_reg);
2794 } else if (op->IsShr()) {
2795 GenerateShrLong(first, second_reg);
2796 } else {
2797 GenerateUShrLong(first, second_reg);
2798 }
2799 break;
2800 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002801 default:
2802 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2803 }
2804}
2805
2806void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2807 Label done;
2808 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2809 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2810 __ testl(shifter, Immediate(32));
2811 __ j(kEqual, &done);
2812 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2813 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2814 __ Bind(&done);
2815}
2816
2817void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2818 Label done;
2819 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2820 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2821 __ testl(shifter, Immediate(32));
2822 __ j(kEqual, &done);
2823 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2824 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2825 __ Bind(&done);
2826}
2827
2828void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2829 Label done;
2830 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2831 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2832 __ testl(shifter, Immediate(32));
2833 __ j(kEqual, &done);
2834 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2835 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2836 __ Bind(&done);
2837}
2838
2839void LocationsBuilderX86::VisitShl(HShl* shl) {
2840 HandleShift(shl);
2841}
2842
2843void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2844 HandleShift(shl);
2845}
2846
2847void LocationsBuilderX86::VisitShr(HShr* shr) {
2848 HandleShift(shr);
2849}
2850
2851void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2852 HandleShift(shr);
2853}
2854
2855void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2856 HandleShift(ushr);
2857}
2858
2859void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2860 HandleShift(ushr);
2861}
2862
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002863void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002864 LocationSummary* locations =
2865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002866 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002867 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002868 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2869 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002870}
2871
2872void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2873 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002874 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002875 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002876
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002877 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002878
Nicolas Geoffray39468442014-09-02 15:17:15 +01002879 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002880 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002881}
2882
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002883void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2884 LocationSummary* locations =
2885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2886 locations->SetOut(Location::RegisterLocation(EAX));
2887 InvokeRuntimeCallingConvention calling_convention;
2888 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002889 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2890 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002891}
2892
2893void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2894 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002895 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002896 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2897
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002898 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002899
2900 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2901 DCHECK(!codegen_->IsLeafMethod());
2902}
2903
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002904void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002905 LocationSummary* locations =
2906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002907 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2908 if (location.IsStackSlot()) {
2909 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2910 } else if (location.IsDoubleStackSlot()) {
2911 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002912 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002913 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914}
2915
2916void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002917 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002918}
2919
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002920void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002921 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002922 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002923 locations->SetInAt(0, Location::RequiresRegister());
2924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002925}
2926
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002927void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2928 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002929 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002930 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002931 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002932 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002933 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002934 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002935 break;
2936
2937 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002938 __ notl(out.AsRegisterPairLow<Register>());
2939 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002940 break;
2941
2942 default:
2943 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2944 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002945}
2946
David Brazdil66d126e2015-04-03 16:02:44 +01002947void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2948 LocationSummary* locations =
2949 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetOut(Location::SameAsFirstInput());
2952}
2953
2954void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002955 LocationSummary* locations = bool_not->GetLocations();
2956 Location in = locations->InAt(0);
2957 Location out = locations->Out();
2958 DCHECK(in.Equals(out));
2959 __ xorl(out.AsRegister<Register>(), Immediate(1));
2960}
2961
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002962void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002963 LocationSummary* locations =
2964 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002965 switch (compare->InputAt(0)->GetType()) {
2966 case Primitive::kPrimLong: {
2967 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002968 locations->SetInAt(1, Location::Any());
2969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2970 break;
2971 }
2972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
2975 locations->SetInAt(1, Location::RequiresFpuRegister());
2976 locations->SetOut(Location::RequiresRegister());
2977 break;
2978 }
2979 default:
2980 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2981 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002982}
2983
2984void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002985 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002987 Location left = locations->InAt(0);
2988 Location right = locations->InAt(1);
2989
2990 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002991 switch (compare->InputAt(0)->GetType()) {
2992 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002993 Register left_low = left.AsRegisterPairLow<Register>();
2994 Register left_high = left.AsRegisterPairHigh<Register>();
2995 int32_t val_low = 0;
2996 int32_t val_high = 0;
2997 bool right_is_const = false;
2998
2999 if (right.IsConstant()) {
3000 DCHECK(right.GetConstant()->IsLongConstant());
3001 right_is_const = true;
3002 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3003 val_low = Low32Bits(val);
3004 val_high = High32Bits(val);
3005 }
3006
Calin Juravleddb7df22014-11-25 20:56:51 +00003007 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003008 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003009 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003010 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003011 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003012 DCHECK(right_is_const) << right;
3013 if (val_high == 0) {
3014 __ testl(left_high, left_high);
3015 } else {
3016 __ cmpl(left_high, Immediate(val_high));
3017 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003018 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003019 __ j(kLess, &less); // Signed compare.
3020 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003021 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003022 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003023 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003024 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003025 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003026 DCHECK(right_is_const) << right;
3027 if (val_low == 0) {
3028 __ testl(left_low, left_low);
3029 } else {
3030 __ cmpl(left_low, Immediate(val_low));
3031 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003032 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003033 break;
3034 }
3035 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003037 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3038 break;
3039 }
3040 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003042 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003043 break;
3044 }
3045 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003046 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003047 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003048 __ movl(out, Immediate(0));
3049 __ j(kEqual, &done);
3050 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3051
3052 __ Bind(&greater);
3053 __ movl(out, Immediate(1));
3054 __ jmp(&done);
3055
3056 __ Bind(&less);
3057 __ movl(out, Immediate(-1));
3058
3059 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003060}
3061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003062void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
3064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003065 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3066 locations->SetInAt(i, Location::Any());
3067 }
3068 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003069}
3070
3071void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003072 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003073 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003074}
3075
Calin Juravle52c48962014-12-16 17:02:57 +00003076void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3077 /*
3078 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3079 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3080 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3081 */
3082 switch (kind) {
3083 case MemBarrierKind::kAnyAny: {
3084 __ mfence();
3085 break;
3086 }
3087 case MemBarrierKind::kAnyStore:
3088 case MemBarrierKind::kLoadAny:
3089 case MemBarrierKind::kStoreStore: {
3090 // nop
3091 break;
3092 }
3093 default:
3094 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003095 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003096}
3097
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003098
Mark Mendell09ed1a32015-03-25 08:30:06 -04003099void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3100 Register temp) {
3101 // TODO: Implement all kinds of calls:
3102 // 1) boot -> boot
3103 // 2) app -> boot
3104 // 3) app -> app
3105 //
3106 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3107 // temp = method;
3108 LoadCurrentMethod(temp);
3109 if (!invoke->IsRecursive()) {
3110 // temp = temp->dex_cache_resolved_methods_;
3111 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3112 // temp = temp[index_in_cache]
3113 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3114 // (temp + offset_of_quick_compiled_code)()
3115 __ call(Address(
3116 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3117 } else {
3118 __ call(GetFrameEntryLabel());
3119 }
3120
3121 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003122}
3123
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003124void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 Label is_null;
3126 __ testl(value, value);
3127 __ j(kEqual, &is_null);
3128 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3129 __ movl(temp, object);
3130 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003131 __ movb(Address(temp, card, TIMES_1, 0),
3132 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 __ Bind(&is_null);
3134}
3135
Calin Juravle52c48962014-12-16 17:02:57 +00003136void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3137 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003138 LocationSummary* locations =
3139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003140 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003141
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003142 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3143 locations->SetOut(Location::RequiresFpuRegister());
3144 } else {
3145 // The output overlaps in case of long: we don't want the low move to overwrite
3146 // the object's location.
3147 locations->SetOut(Location::RequiresRegister(),
3148 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3149 : Location::kNoOutputOverlap);
3150 }
Calin Juravle52c48962014-12-16 17:02:57 +00003151
3152 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3153 // Long values can be loaded atomically into an XMM using movsd.
3154 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3155 // and then copy the XMM into the output 32bits at a time).
3156 locations->AddTemp(Location::RequiresFpuRegister());
3157 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003158}
3159
Calin Juravle52c48962014-12-16 17:02:57 +00003160void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3161 const FieldInfo& field_info) {
3162 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003163
Calin Juravle52c48962014-12-16 17:02:57 +00003164 LocationSummary* locations = instruction->GetLocations();
3165 Register base = locations->InAt(0).AsRegister<Register>();
3166 Location out = locations->Out();
3167 bool is_volatile = field_info.IsVolatile();
3168 Primitive::Type field_type = field_info.GetFieldType();
3169 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3170
3171 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003172 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003173 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174 break;
3175 }
3176
3177 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003178 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003179 break;
3180 }
3181
3182 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003183 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184 break;
3185 }
3186
3187 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003188 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003189 break;
3190 }
3191
3192 case Primitive::kPrimInt:
3193 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003194 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003195 break;
3196 }
3197
3198 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003199 if (is_volatile) {
3200 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3201 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003202 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003203 __ movd(out.AsRegisterPairLow<Register>(), temp);
3204 __ psrlq(temp, Immediate(32));
3205 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3206 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003207 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003208 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003210 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3211 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003212 break;
3213 }
3214
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003215 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003216 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003217 break;
3218 }
3219
3220 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003221 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003222 break;
3223 }
3224
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003225 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003226 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003227 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003228 }
Calin Juravle52c48962014-12-16 17:02:57 +00003229
Calin Juravle77520bc2015-01-12 18:45:46 +00003230 // Longs are handled in the switch.
3231 if (field_type != Primitive::kPrimLong) {
3232 codegen_->MaybeRecordImplicitNullCheck(instruction);
3233 }
3234
Calin Juravle52c48962014-12-16 17:02:57 +00003235 if (is_volatile) {
3236 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3237 }
3238}
3239
3240void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3241 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3242
3243 LocationSummary* locations =
3244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3245 locations->SetInAt(0, Location::RequiresRegister());
3246 bool is_volatile = field_info.IsVolatile();
3247 Primitive::Type field_type = field_info.GetFieldType();
3248 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3249 || (field_type == Primitive::kPrimByte);
3250
3251 // The register allocator does not support multiple
3252 // inputs that die at entry with one in a specific register.
3253 if (is_byte_type) {
3254 // Ensure the value is in a byte register.
3255 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003256 } else if (Primitive::IsFloatingPointType(field_type)) {
3257 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003258 } else {
3259 locations->SetInAt(1, Location::RequiresRegister());
3260 }
3261 // Temporary registers for the write barrier.
3262 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3263 locations->AddTemp(Location::RequiresRegister());
3264 // Ensure the card is in a byte register.
3265 locations->AddTemp(Location::RegisterLocation(ECX));
3266 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3267 // 64bits value can be atomically written to an address with movsd and an XMM register.
3268 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3269 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3270 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3271 // isolated cases when we need this it isn't worth adding the extra complexity.
3272 locations->AddTemp(Location::RequiresFpuRegister());
3273 locations->AddTemp(Location::RequiresFpuRegister());
3274 }
3275}
3276
3277void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3278 const FieldInfo& field_info) {
3279 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 Register base = locations->InAt(0).AsRegister<Register>();
3283 Location value = locations->InAt(1);
3284 bool is_volatile = field_info.IsVolatile();
3285 Primitive::Type field_type = field_info.GetFieldType();
3286 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3287
3288 if (is_volatile) {
3289 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3290 }
3291
3292 switch (field_type) {
3293 case Primitive::kPrimBoolean:
3294 case Primitive::kPrimByte: {
3295 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3296 break;
3297 }
3298
3299 case Primitive::kPrimShort:
3300 case Primitive::kPrimChar: {
3301 __ movw(Address(base, offset), value.AsRegister<Register>());
3302 break;
3303 }
3304
3305 case Primitive::kPrimInt:
3306 case Primitive::kPrimNot: {
3307 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003308 break;
3309 }
3310
3311 case Primitive::kPrimLong: {
3312 if (is_volatile) {
3313 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3314 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3315 __ movd(temp1, value.AsRegisterPairLow<Register>());
3316 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3317 __ punpckldq(temp1, temp2);
3318 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003320 } else {
3321 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003322 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003323 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3324 }
3325 break;
3326 }
3327
3328 case Primitive::kPrimFloat: {
3329 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3330 break;
3331 }
3332
3333 case Primitive::kPrimDouble: {
3334 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3335 break;
3336 }
3337
3338 case Primitive::kPrimVoid:
3339 LOG(FATAL) << "Unreachable type " << field_type;
3340 UNREACHABLE();
3341 }
3342
Calin Juravle77520bc2015-01-12 18:45:46 +00003343 // Longs are handled in the switch.
3344 if (field_type != Primitive::kPrimLong) {
3345 codegen_->MaybeRecordImplicitNullCheck(instruction);
3346 }
3347
3348 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3349 Register temp = locations->GetTemp(0).AsRegister<Register>();
3350 Register card = locations->GetTemp(1).AsRegister<Register>();
3351 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3352 }
3353
Calin Juravle52c48962014-12-16 17:02:57 +00003354 if (is_volatile) {
3355 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3356 }
3357}
3358
3359void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3360 HandleFieldGet(instruction, instruction->GetFieldInfo());
3361}
3362
3363void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3364 HandleFieldGet(instruction, instruction->GetFieldInfo());
3365}
3366
3367void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3368 HandleFieldSet(instruction, instruction->GetFieldInfo());
3369}
3370
3371void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3372 HandleFieldSet(instruction, instruction->GetFieldInfo());
3373}
3374
3375void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3376 HandleFieldSet(instruction, instruction->GetFieldInfo());
3377}
3378
3379void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3380 HandleFieldSet(instruction, instruction->GetFieldInfo());
3381}
3382
3383void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3384 HandleFieldGet(instruction, instruction->GetFieldInfo());
3385}
3386
3387void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3388 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003389}
3390
3391void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003394 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3395 ? Location::RequiresRegister()
3396 : Location::Any();
3397 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003398 if (instruction->HasUses()) {
3399 locations->SetOut(Location::SameAsFirstInput());
3400 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003401}
3402
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003403void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003404 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3405 return;
3406 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003407 LocationSummary* locations = instruction->GetLocations();
3408 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003409
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003410 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3411 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3412}
3413
3414void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003415 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416 codegen_->AddSlowPath(slow_path);
3417
3418 LocationSummary* locations = instruction->GetLocations();
3419 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420
3421 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003422 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003423 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 } else {
3426 DCHECK(obj.IsConstant()) << obj;
3427 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3428 __ jmp(slow_path->GetEntryLabel());
3429 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430 }
3431 __ j(kEqual, slow_path->GetEntryLabel());
3432}
3433
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003434void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3435 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3436 GenerateImplicitNullCheck(instruction);
3437 } else {
3438 GenerateExplicitNullCheck(instruction);
3439 }
3440}
3441
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003442void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003443 LocationSummary* locations =
3444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003445 locations->SetInAt(0, Location::RequiresRegister());
3446 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003447 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3448 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3449 } else {
3450 // The output overlaps in case of long: we don't want the low move to overwrite
3451 // the array's location.
3452 locations->SetOut(Location::RequiresRegister(),
3453 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3454 : Location::kNoOutputOverlap);
3455 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456}
3457
3458void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3459 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003460 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461 Location index = locations->InAt(1);
3462
Calin Juravle77520bc2015-01-12 18:45:46 +00003463 Primitive::Type type = instruction->GetType();
3464 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003465 case Primitive::kPrimBoolean: {
3466 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003467 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003468 if (index.IsConstant()) {
3469 __ movzxb(out, Address(obj,
3470 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3471 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003473 }
3474 break;
3475 }
3476
3477 case Primitive::kPrimByte: {
3478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 if (index.IsConstant()) {
3481 __ movsxb(out, Address(obj,
3482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3483 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003485 }
3486 break;
3487 }
3488
3489 case Primitive::kPrimShort: {
3490 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003492 if (index.IsConstant()) {
3493 __ movsxw(out, Address(obj,
3494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3495 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003497 }
3498 break;
3499 }
3500
3501 case Primitive::kPrimChar: {
3502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003504 if (index.IsConstant()) {
3505 __ movzxw(out, Address(obj,
3506 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3507 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003509 }
3510 break;
3511 }
3512
3513 case Primitive::kPrimInt:
3514 case Primitive::kPrimNot: {
3515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 if (index.IsConstant()) {
3518 __ movl(out, Address(obj,
3519 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3520 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003521 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003522 }
3523 break;
3524 }
3525
3526 case Primitive::kPrimLong: {
3527 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003528 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003529 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003530 if (index.IsConstant()) {
3531 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003532 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003534 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003535 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003536 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003538 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003539 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541 }
3542 break;
3543 }
3544
Mark Mendell7c8d0092015-01-26 11:21:33 -05003545 case Primitive::kPrimFloat: {
3546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3547 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3548 if (index.IsConstant()) {
3549 __ movss(out, Address(obj,
3550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3551 } else {
3552 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3553 }
3554 break;
3555 }
3556
3557 case Primitive::kPrimDouble: {
3558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3559 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3560 if (index.IsConstant()) {
3561 __ movsd(out, Address(obj,
3562 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3563 } else {
3564 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3565 }
3566 break;
3567 }
3568
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003569 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003570 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003571 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003573
3574 if (type != Primitive::kPrimLong) {
3575 codegen_->MaybeRecordImplicitNullCheck(instruction);
3576 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003577}
3578
3579void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003580 // This location builder might end up asking to up to four registers, which is
3581 // not currently possible for baseline. The situation in which we need four
3582 // registers cannot be met by baseline though, because it has not run any
3583 // optimization.
3584
Nicolas Geoffray39468442014-09-02 15:17:15 +01003585 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003586 bool needs_write_barrier =
3587 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3588
Mark Mendell5f874182015-03-04 15:42:45 -05003589 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003590
Nicolas Geoffray39468442014-09-02 15:17:15 +01003591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3592 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003593 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003594
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003595 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003597 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3598 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3599 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003601 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3602 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003603 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003604 // In case of a byte operation, the register allocator does not support multiple
3605 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003606 locations->SetInAt(0, Location::RequiresRegister());
3607 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003608 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003609 // Ensure the value is in a byte register.
3610 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003611 } else if (Primitive::IsFloatingPointType(value_type)) {
3612 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003614 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003616 // Temporary registers for the write barrier.
3617 if (needs_write_barrier) {
3618 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003619 // Ensure the card is in a byte register.
3620 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623}
3624
3625void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3626 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003629 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003630 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003631 bool needs_runtime_call = locations->WillCall();
3632 bool needs_write_barrier =
3633 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634
3635 switch (value_type) {
3636 case Primitive::kPrimBoolean:
3637 case Primitive::kPrimByte: {
3638 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003639 if (index.IsConstant()) {
3640 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003641 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003642 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003643 } else {
3644 __ movb(Address(obj, offset),
3645 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3646 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003648 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003650 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003651 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003653 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3654 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003655 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003656 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003657 break;
3658 }
3659
3660 case Primitive::kPrimShort:
3661 case Primitive::kPrimChar: {
3662 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 if (index.IsConstant()) {
3664 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003665 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003667 } else {
3668 __ movw(Address(obj, offset),
3669 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3670 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003671 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003672 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003673 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3674 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003675 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003677 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003680 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003681 break;
3682 }
3683
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003684 case Primitive::kPrimInt:
3685 case Primitive::kPrimNot: {
3686 if (!needs_runtime_call) {
3687 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3688 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003689 size_t offset =
3690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003693 } else {
3694 DCHECK(value.IsConstant()) << value;
3695 __ movl(Address(obj, offset),
3696 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3697 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003698 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 DCHECK(index.IsRegister()) << index;
3700 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3702 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003703 } else {
3704 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003706 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3707 }
3708 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003709 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003710
3711 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003712 Register temp = locations->GetTemp(0).AsRegister<Register>();
3713 Register card = locations->GetTemp(1).AsRegister<Register>();
3714 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003715 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003716 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003717 DCHECK_EQ(value_type, Primitive::kPrimNot);
3718 DCHECK(!codegen_->IsLeafMethod());
3719 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3720 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 }
3722 break;
3723 }
3724
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725 case Primitive::kPrimLong: {
3726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 if (index.IsConstant()) {
3728 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003729 if (value.IsRegisterPair()) {
3730 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003731 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003732 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003733 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003734 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003735 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3736 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003737 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003738 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003741 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003743 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003744 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003746 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003747 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003748 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003749 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003751 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003752 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003754 Immediate(High32Bits(val)));
3755 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 }
3757 break;
3758 }
3759
Mark Mendell7c8d0092015-01-26 11:21:33 -05003760 case Primitive::kPrimFloat: {
3761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3762 DCHECK(value.IsFpuRegister());
3763 if (index.IsConstant()) {
3764 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3765 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3766 } else {
3767 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3768 value.AsFpuRegister<XmmRegister>());
3769 }
3770 break;
3771 }
3772
3773 case Primitive::kPrimDouble: {
3774 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3775 DCHECK(value.IsFpuRegister());
3776 if (index.IsConstant()) {
3777 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3778 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3779 } else {
3780 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3781 value.AsFpuRegister<XmmRegister>());
3782 }
3783 break;
3784 }
3785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 case Primitive::kPrimVoid:
3787 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003788 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003789 }
3790}
3791
3792void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003794 locations->SetInAt(0, Location::RequiresRegister());
3795 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 instruction->SetLocations(locations);
3797}
3798
3799void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3800 LocationSummary* locations = instruction->GetLocations();
3801 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003802 Register obj = locations->InAt(0).AsRegister<Register>();
3803 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003805 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806}
3807
3808void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003809 LocationSummary* locations =
3810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003811 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003813 if (instruction->HasUses()) {
3814 locations->SetOut(Location::SameAsFirstInput());
3815 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816}
3817
3818void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3819 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003820 Location index_loc = locations->InAt(0);
3821 Location length_loc = locations->InAt(1);
3822 SlowPathCodeX86* slow_path =
3823 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 codegen_->AddSlowPath(slow_path);
3825
Mark Mendellf60c90b2015-03-04 15:12:59 -05003826 Register length = length_loc.AsRegister<Register>();
3827 if (index_loc.IsConstant()) {
3828 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3829 __ cmpl(length, Immediate(value));
3830 } else {
3831 __ cmpl(length, index_loc.AsRegister<Register>());
3832 }
3833 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834}
3835
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003836void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3837 temp->SetLocations(nullptr);
3838}
3839
3840void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3841 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003842 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843}
3844
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003845void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003846 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003847 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003848}
3849
3850void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003851 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3852}
3853
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003854void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3856}
3857
3858void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003859 HBasicBlock* block = instruction->GetBlock();
3860 if (block->GetLoopInformation() != nullptr) {
3861 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3862 // The back edge will generate the suspend check.
3863 return;
3864 }
3865 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3866 // The goto will generate the suspend check.
3867 return;
3868 }
3869 GenerateSuspendCheck(instruction, nullptr);
3870}
3871
3872void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3873 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003874 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003875 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003876 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003877 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003878 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003879 if (successor == nullptr) {
3880 __ j(kNotEqual, slow_path->GetEntryLabel());
3881 __ Bind(slow_path->GetReturnLabel());
3882 } else {
3883 __ j(kEqual, codegen_->GetLabelOf(successor));
3884 __ jmp(slow_path->GetEntryLabel());
3885 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003886}
3887
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003888X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3889 return codegen_->GetAssembler();
3890}
3891
Mark Mendell7c8d0092015-01-26 11:21:33 -05003892void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003893 ScratchRegisterScope ensure_scratch(
3894 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3895 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3896 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3897 __ movl(temp_reg, Address(ESP, src + stack_offset));
3898 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003899}
3900
3901void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003902 ScratchRegisterScope ensure_scratch(
3903 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3904 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3905 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3906 __ movl(temp_reg, Address(ESP, src + stack_offset));
3907 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3908 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3909 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003910}
3911
3912void ParallelMoveResolverX86::EmitMove(size_t index) {
3913 MoveOperands* move = moves_.Get(index);
3914 Location source = move->GetSource();
3915 Location destination = move->GetDestination();
3916
3917 if (source.IsRegister()) {
3918 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003919 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003920 } else {
3921 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003922 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003923 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003924 } else if (source.IsFpuRegister()) {
3925 if (destination.IsFpuRegister()) {
3926 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3927 } else if (destination.IsStackSlot()) {
3928 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3929 } else {
3930 DCHECK(destination.IsDoubleStackSlot());
3931 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3932 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003933 } else if (source.IsStackSlot()) {
3934 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003935 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003936 } else if (destination.IsFpuRegister()) {
3937 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003938 } else {
3939 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003940 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3941 }
3942 } else if (source.IsDoubleStackSlot()) {
3943 if (destination.IsFpuRegister()) {
3944 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3945 } else {
3946 DCHECK(destination.IsDoubleStackSlot()) << destination;
3947 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003948 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003949 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003950 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003951 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003952 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003953 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003954 if (value == 0) {
3955 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3956 } else {
3957 __ movl(destination.AsRegister<Register>(), Immediate(value));
3958 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003959 } else {
3960 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003961 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003962 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003963 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003964 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003965 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003966 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003967 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003968 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3969 if (value == 0) {
3970 // Easy handling of 0.0.
3971 __ xorps(dest, dest);
3972 } else {
3973 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003974 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3975 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3976 __ movl(temp, Immediate(value));
3977 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003978 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003979 } else {
3980 DCHECK(destination.IsStackSlot()) << destination;
3981 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3982 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003983 } else if (constant->IsLongConstant()) {
3984 int64_t value = constant->AsLongConstant()->GetValue();
3985 int32_t low_value = Low32Bits(value);
3986 int32_t high_value = High32Bits(value);
3987 Immediate low(low_value);
3988 Immediate high(high_value);
3989 if (destination.IsDoubleStackSlot()) {
3990 __ movl(Address(ESP, destination.GetStackIndex()), low);
3991 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3992 } else {
3993 __ movl(destination.AsRegisterPairLow<Register>(), low);
3994 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3995 }
3996 } else {
3997 DCHECK(constant->IsDoubleConstant());
3998 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003999 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004000 int32_t low_value = Low32Bits(value);
4001 int32_t high_value = High32Bits(value);
4002 Immediate low(low_value);
4003 Immediate high(high_value);
4004 if (destination.IsFpuRegister()) {
4005 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4006 if (value == 0) {
4007 // Easy handling of 0.0.
4008 __ xorpd(dest, dest);
4009 } else {
4010 __ pushl(high);
4011 __ pushl(low);
4012 __ movsd(dest, Address(ESP, 0));
4013 __ addl(ESP, Immediate(8));
4014 }
4015 } else {
4016 DCHECK(destination.IsDoubleStackSlot()) << destination;
4017 __ movl(Address(ESP, destination.GetStackIndex()), low);
4018 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4019 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004020 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004021 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004022 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004023 }
4024}
4025
Mark Mendella5c19ce2015-04-01 12:51:05 -04004026void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004027 Register suggested_scratch = reg == EAX ? EBX : EAX;
4028 ScratchRegisterScope ensure_scratch(
4029 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4030
4031 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4032 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4033 __ movl(Address(ESP, mem + stack_offset), reg);
4034 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004035}
4036
Mark Mendell7c8d0092015-01-26 11:21:33 -05004037void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004038 ScratchRegisterScope ensure_scratch(
4039 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4040
4041 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4042 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4043 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4044 __ movss(Address(ESP, mem + stack_offset), reg);
4045 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004046}
4047
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004048void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004049 ScratchRegisterScope ensure_scratch1(
4050 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004051
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004052 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4053 ScratchRegisterScope ensure_scratch2(
4054 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004055
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004056 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4057 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4058 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4059 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4060 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4061 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004062}
4063
4064void ParallelMoveResolverX86::EmitSwap(size_t index) {
4065 MoveOperands* move = moves_.Get(index);
4066 Location source = move->GetSource();
4067 Location destination = move->GetDestination();
4068
4069 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004070 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004071 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004073 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004075 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4076 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004077 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4078 // Use XOR Swap algorithm to avoid a temporary.
4079 DCHECK_NE(source.reg(), destination.reg());
4080 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4081 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4082 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4083 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4084 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4085 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4086 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004087 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4088 // Take advantage of the 16 bytes in the XMM register.
4089 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4090 Address stack(ESP, destination.GetStackIndex());
4091 // Load the double into the high doubleword.
4092 __ movhpd(reg, stack);
4093
4094 // Store the low double into the destination.
4095 __ movsd(stack, reg);
4096
4097 // Move the high double to the low double.
4098 __ psrldq(reg, Immediate(8));
4099 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4100 // Take advantage of the 16 bytes in the XMM register.
4101 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4102 Address stack(ESP, source.GetStackIndex());
4103 // Load the double into the high doubleword.
4104 __ movhpd(reg, stack);
4105
4106 // Store the low double into the destination.
4107 __ movsd(stack, reg);
4108
4109 // Move the high double to the low double.
4110 __ psrldq(reg, Immediate(8));
4111 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4112 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4113 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004114 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004115 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004116 }
4117}
4118
4119void ParallelMoveResolverX86::SpillScratch(int reg) {
4120 __ pushl(static_cast<Register>(reg));
4121}
4122
4123void ParallelMoveResolverX86::RestoreScratch(int reg) {
4124 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004125}
4126
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004127void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004128 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4129 ? LocationSummary::kCallOnSlowPath
4130 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004131 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004132 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004133 locations->SetOut(Location::RequiresRegister());
4134}
4135
4136void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004137 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004138 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004139 DCHECK(!cls->CanCallRuntime());
4140 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 codegen_->LoadCurrentMethod(out);
4142 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4143 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004144 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004145 codegen_->LoadCurrentMethod(out);
4146 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4147 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004148
4149 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4150 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4151 codegen_->AddSlowPath(slow_path);
4152 __ testl(out, out);
4153 __ j(kEqual, slow_path->GetEntryLabel());
4154 if (cls->MustGenerateClinitCheck()) {
4155 GenerateClassInitializationCheck(slow_path, out);
4156 } else {
4157 __ Bind(slow_path->GetExitLabel());
4158 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004159 }
4160}
4161
4162void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4163 LocationSummary* locations =
4164 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4165 locations->SetInAt(0, Location::RequiresRegister());
4166 if (check->HasUses()) {
4167 locations->SetOut(Location::SameAsFirstInput());
4168 }
4169}
4170
4171void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004172 // We assume the class to not be null.
4173 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4174 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004175 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004176 GenerateClassInitializationCheck(slow_path,
4177 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004178}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004179
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004180void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4181 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004182 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4183 Immediate(mirror::Class::kStatusInitialized));
4184 __ j(kLess, slow_path->GetEntryLabel());
4185 __ Bind(slow_path->GetExitLabel());
4186 // No need for memory fence, thanks to the X86 memory model.
4187}
4188
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004189void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4190 LocationSummary* locations =
4191 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4192 locations->SetOut(Location::RequiresRegister());
4193}
4194
4195void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4196 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4197 codegen_->AddSlowPath(slow_path);
4198
Roland Levillain271ab9c2014-11-27 15:23:57 +00004199 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004200 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004201 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4202 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004203 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4204 __ testl(out, out);
4205 __ j(kEqual, slow_path->GetEntryLabel());
4206 __ Bind(slow_path->GetExitLabel());
4207}
4208
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004209void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4210 LocationSummary* locations =
4211 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4212 locations->SetOut(Location::RequiresRegister());
4213}
4214
4215void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4216 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004217 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004218 __ fs()->movl(address, Immediate(0));
4219}
4220
4221void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4222 LocationSummary* locations =
4223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4224 InvokeRuntimeCallingConvention calling_convention;
4225 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4226}
4227
4228void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4229 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4230 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4231}
4232
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004233void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004234 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4235 ? LocationSummary::kNoCall
4236 : LocationSummary::kCallOnSlowPath;
4237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4238 locations->SetInAt(0, Location::RequiresRegister());
4239 locations->SetInAt(1, Location::Any());
4240 locations->SetOut(Location::RequiresRegister());
4241}
4242
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004243void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004244 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004246 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004247 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4249 Label done, zero;
4250 SlowPathCodeX86* slow_path = nullptr;
4251
4252 // Return 0 if `obj` is null.
4253 // TODO: avoid this check if we know obj is not null.
4254 __ testl(obj, obj);
4255 __ j(kEqual, &zero);
4256 __ movl(out, Address(obj, class_offset));
4257 // Compare the class of `obj` with `cls`.
4258 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004259 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004260 } else {
4261 DCHECK(cls.IsStackSlot()) << cls;
4262 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4263 }
4264
4265 if (instruction->IsClassFinal()) {
4266 // Classes must be equal for the instanceof to succeed.
4267 __ j(kNotEqual, &zero);
4268 __ movl(out, Immediate(1));
4269 __ jmp(&done);
4270 } else {
4271 // If the classes are not equal, we go into a slow path.
4272 DCHECK(locations->OnlyCallsOnSlowPath());
4273 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004274 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004275 codegen_->AddSlowPath(slow_path);
4276 __ j(kNotEqual, slow_path->GetEntryLabel());
4277 __ movl(out, Immediate(1));
4278 __ jmp(&done);
4279 }
4280 __ Bind(&zero);
4281 __ movl(out, Immediate(0));
4282 if (slow_path != nullptr) {
4283 __ Bind(slow_path->GetExitLabel());
4284 }
4285 __ Bind(&done);
4286}
4287
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004288void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4290 instruction, LocationSummary::kCallOnSlowPath);
4291 locations->SetInAt(0, Location::RequiresRegister());
4292 locations->SetInAt(1, Location::Any());
4293 locations->AddTemp(Location::RequiresRegister());
4294}
4295
4296void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4297 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004298 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004299 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004300 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004301 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4302 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4303 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4304 codegen_->AddSlowPath(slow_path);
4305
4306 // TODO: avoid this check if we know obj is not null.
4307 __ testl(obj, obj);
4308 __ j(kEqual, slow_path->GetExitLabel());
4309 __ movl(temp, Address(obj, class_offset));
4310
4311 // Compare the class of `obj` with `cls`.
4312 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004313 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004314 } else {
4315 DCHECK(cls.IsStackSlot()) << cls;
4316 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4317 }
4318
4319 __ j(kNotEqual, slow_path->GetEntryLabel());
4320 __ Bind(slow_path->GetExitLabel());
4321}
4322
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004323void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4324 LocationSummary* locations =
4325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4326 InvokeRuntimeCallingConvention calling_convention;
4327 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4328}
4329
4330void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4331 __ fs()->call(Address::Absolute(instruction->IsEnter()
4332 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4333 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4334 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4335}
4336
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004337void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4338void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4339void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4340
4341void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4342 LocationSummary* locations =
4343 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4344 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4345 || instruction->GetResultType() == Primitive::kPrimLong);
4346 locations->SetInAt(0, Location::RequiresRegister());
4347 locations->SetInAt(1, Location::Any());
4348 locations->SetOut(Location::SameAsFirstInput());
4349}
4350
4351void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4352 HandleBitwiseOperation(instruction);
4353}
4354
4355void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4356 HandleBitwiseOperation(instruction);
4357}
4358
4359void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4360 HandleBitwiseOperation(instruction);
4361}
4362
4363void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4364 LocationSummary* locations = instruction->GetLocations();
4365 Location first = locations->InAt(0);
4366 Location second = locations->InAt(1);
4367 DCHECK(first.Equals(locations->Out()));
4368
4369 if (instruction->GetResultType() == Primitive::kPrimInt) {
4370 if (second.IsRegister()) {
4371 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004372 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004373 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004374 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004375 } else {
4376 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004378 }
4379 } else if (second.IsConstant()) {
4380 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004381 __ andl(first.AsRegister<Register>(),
4382 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004383 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004384 __ orl(first.AsRegister<Register>(),
4385 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004386 } else {
4387 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004388 __ xorl(first.AsRegister<Register>(),
4389 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004390 }
4391 } else {
4392 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004394 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004396 } else {
4397 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004399 }
4400 }
4401 } else {
4402 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4403 if (second.IsRegisterPair()) {
4404 if (instruction->IsAnd()) {
4405 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4406 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4407 } else if (instruction->IsOr()) {
4408 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4409 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4410 } else {
4411 DCHECK(instruction->IsXor());
4412 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4413 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4414 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004415 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004416 if (instruction->IsAnd()) {
4417 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4418 __ andl(first.AsRegisterPairHigh<Register>(),
4419 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4420 } else if (instruction->IsOr()) {
4421 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4422 __ orl(first.AsRegisterPairHigh<Register>(),
4423 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4424 } else {
4425 DCHECK(instruction->IsXor());
4426 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4427 __ xorl(first.AsRegisterPairHigh<Register>(),
4428 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4429 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004430 } else {
4431 DCHECK(second.IsConstant()) << second;
4432 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004433 int32_t low_value = Low32Bits(value);
4434 int32_t high_value = High32Bits(value);
4435 Immediate low(low_value);
4436 Immediate high(high_value);
4437 Register first_low = first.AsRegisterPairLow<Register>();
4438 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004439 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004440 if (low_value == 0) {
4441 __ xorl(first_low, first_low);
4442 } else if (low_value != -1) {
4443 __ andl(first_low, low);
4444 }
4445 if (high_value == 0) {
4446 __ xorl(first_high, first_high);
4447 } else if (high_value != -1) {
4448 __ andl(first_high, high);
4449 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004450 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004451 if (low_value != 0) {
4452 __ orl(first_low, low);
4453 }
4454 if (high_value != 0) {
4455 __ orl(first_high, high);
4456 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004457 } else {
4458 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004459 if (low_value != 0) {
4460 __ xorl(first_low, low);
4461 }
4462 if (high_value != 0) {
4463 __ xorl(first_high, high);
4464 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004465 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004466 }
4467 }
4468}
4469
Calin Juravleb1498f62015-02-16 13:13:29 +00004470void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4471 // Nothing to do, this should be removed during prepare for register allocator.
4472 UNUSED(instruction);
4473 LOG(FATAL) << "Unreachable";
4474}
4475
4476void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4477 // Nothing to do, this should be removed during prepare for register allocator.
4478 UNUSED(instruction);
4479 LOG(FATAL) << "Unreachable";
4480}
4481
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004482} // namespace x86
4483} // namespace art