blob: 86e84ac66ae71dedf44eb24d8e9b56e098b1f332 [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.
1559 locations->SetInAt(0, Location::RequiresRegister());
1560 locations->SetOut(Location::RequiresFpuRegister());
1561 locations->AddTemp(Location::RequiresFpuRegister());
1562 locations->AddTemp(Location::RequiresFpuRegister());
1563 break;
1564
Roland Levillaincff13742014-11-17 14:32:17 +00001565 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001566 // Processing a Dex `double-to-float' instruction.
1567 locations->SetInAt(0, Location::RequiresFpuRegister());
1568 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001569 break;
1570
1571 default:
1572 LOG(FATAL) << "Unexpected type conversion from " << input_type
1573 << " to " << result_type;
1574 };
1575 break;
1576
Roland Levillaindff1f282014-11-05 14:15:05 +00001577 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001578 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001579 case Primitive::kPrimBoolean:
1580 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001581 case Primitive::kPrimByte:
1582 case Primitive::kPrimShort:
1583 case Primitive::kPrimInt:
1584 case Primitive::kPrimChar:
1585 // Processing a Dex `int-to-double' instruction.
1586 locations->SetInAt(0, Location::RequiresRegister());
1587 locations->SetOut(Location::RequiresFpuRegister());
1588 break;
1589
1590 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001591 // Processing a Dex `long-to-double' instruction.
1592 locations->SetInAt(0, Location::RequiresRegister());
1593 locations->SetOut(Location::RequiresFpuRegister());
1594 locations->AddTemp(Location::RequiresFpuRegister());
1595 locations->AddTemp(Location::RequiresFpuRegister());
1596 break;
1597
Roland Levillaincff13742014-11-17 14:32:17 +00001598 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001599 // Processing a Dex `float-to-double' instruction.
1600 locations->SetInAt(0, Location::RequiresFpuRegister());
1601 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001602 break;
1603
1604 default:
1605 LOG(FATAL) << "Unexpected type conversion from " << input_type
1606 << " to " << result_type;
1607 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
1614}
1615
1616void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1617 LocationSummary* locations = conversion->GetLocations();
1618 Location out = locations->Out();
1619 Location in = locations->InAt(0);
1620 Primitive::Type result_type = conversion->GetResultType();
1621 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001622 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001623 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001624 case Primitive::kPrimByte:
1625 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001626 case Primitive::kPrimBoolean:
1627 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001628 case Primitive::kPrimShort:
1629 case Primitive::kPrimInt:
1630 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001631 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001632 if (in.IsRegister()) {
1633 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001634 } else {
1635 DCHECK(in.GetConstant()->IsIntConstant());
1636 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1637 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1638 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001639 break;
1640
1641 default:
1642 LOG(FATAL) << "Unexpected type conversion from " << input_type
1643 << " to " << result_type;
1644 }
1645 break;
1646
Roland Levillain01a8d712014-11-14 16:27:39 +00001647 case Primitive::kPrimShort:
1648 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001649 case Primitive::kPrimBoolean:
1650 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001651 case Primitive::kPrimByte:
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimChar:
1654 // Processing a Dex `int-to-short' instruction.
1655 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001656 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001657 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001658 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 } else {
1660 DCHECK(in.GetConstant()->IsIntConstant());
1661 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 }
1664 break;
1665
1666 default:
1667 LOG(FATAL) << "Unexpected type conversion from " << input_type
1668 << " to " << result_type;
1669 }
1670 break;
1671
Roland Levillain946e1432014-11-11 17:35:19 +00001672 case Primitive::kPrimInt:
1673 switch (input_type) {
1674 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001675 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001676 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001678 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001680 } else {
1681 DCHECK(in.IsConstant());
1682 DCHECK(in.GetConstant()->IsLongConstant());
1683 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001685 }
1686 break;
1687
Roland Levillain3f8f9362014-12-02 17:45:01 +00001688 case Primitive::kPrimFloat: {
1689 // Processing a Dex `float-to-int' instruction.
1690 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1691 Register output = out.AsRegister<Register>();
1692 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1693 Label done, nan;
1694
1695 __ movl(output, Immediate(kPrimIntMax));
1696 // temp = int-to-float(output)
1697 __ cvtsi2ss(temp, output);
1698 // if input >= temp goto done
1699 __ comiss(input, temp);
1700 __ j(kAboveEqual, &done);
1701 // if input == NaN goto nan
1702 __ j(kUnordered, &nan);
1703 // output = float-to-int-truncate(input)
1704 __ cvttss2si(output, input);
1705 __ jmp(&done);
1706 __ Bind(&nan);
1707 // output = 0
1708 __ xorl(output, output);
1709 __ Bind(&done);
1710 break;
1711 }
1712
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001713 case Primitive::kPrimDouble: {
1714 // Processing a Dex `double-to-int' instruction.
1715 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1716 Register output = out.AsRegister<Register>();
1717 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1718 Label done, nan;
1719
1720 __ movl(output, Immediate(kPrimIntMax));
1721 // temp = int-to-double(output)
1722 __ cvtsi2sd(temp, output);
1723 // if input >= temp goto done
1724 __ comisd(input, temp);
1725 __ j(kAboveEqual, &done);
1726 // if input == NaN goto nan
1727 __ j(kUnordered, &nan);
1728 // output = double-to-int-truncate(input)
1729 __ cvttsd2si(output, input);
1730 __ jmp(&done);
1731 __ Bind(&nan);
1732 // output = 0
1733 __ xorl(output, output);
1734 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001735 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001736 }
Roland Levillain946e1432014-11-11 17:35:19 +00001737
1738 default:
1739 LOG(FATAL) << "Unexpected type conversion from " << input_type
1740 << " to " << result_type;
1741 }
1742 break;
1743
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 case Primitive::kPrimLong:
1745 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001746 case Primitive::kPrimBoolean:
1747 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001748 case Primitive::kPrimByte:
1749 case Primitive::kPrimShort:
1750 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001751 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001752 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001753 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1754 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 __ cdq();
1757 break;
1758
1759 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001760 // Processing a Dex `float-to-long' instruction.
1761 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001762 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1763 break;
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001766 // Processing a Dex `double-to-long' instruction.
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1768 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillain981e4542014-11-14 11:47:14 +00001777 case Primitive::kPrimChar:
1778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001784 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1785 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001786 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001787 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001789 } else {
1790 DCHECK(in.GetConstant()->IsIntConstant());
1791 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001792 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001793 }
1794 break;
1795
1796 default:
1797 LOG(FATAL) << "Unexpected type conversion from " << input_type
1798 << " to " << result_type;
1799 }
1800 break;
1801
Roland Levillaindff1f282014-11-05 14:15:05 +00001802 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001803 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001804 case Primitive::kPrimBoolean:
1805 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001806 case Primitive::kPrimByte:
1807 case Primitive::kPrimShort:
1808 case Primitive::kPrimInt:
1809 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001810 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001811 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001812 break;
1813
Roland Levillain6d0e4832014-11-27 18:31:21 +00001814 case Primitive::kPrimLong: {
1815 // Processing a Dex `long-to-float' instruction.
1816 Register low = in.AsRegisterPairLow<Register>();
1817 Register high = in.AsRegisterPairHigh<Register>();
1818 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1819 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1820 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1821
1822 // Operations use doubles for precision reasons (each 32-bit
1823 // half of a long fits in the 53-bit mantissa of a double,
1824 // but not in the 24-bit mantissa of a float). This is
1825 // especially important for the low bits. The result is
1826 // eventually converted to float.
1827
1828 // low = low - 2^31 (to prevent bit 31 of `low` to be
1829 // interpreted as a sign bit)
1830 __ subl(low, Immediate(0x80000000));
1831 // temp = int-to-double(high)
1832 __ cvtsi2sd(temp, high);
1833 // temp = temp * 2^32
1834 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1835 __ mulsd(temp, constant);
1836 // result = int-to-double(low)
1837 __ cvtsi2sd(result, low);
1838 // result = result + 2^31 (restore the original value of `low`)
1839 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1840 __ addsd(result, constant);
1841 // result = result + temp
1842 __ addsd(result, temp);
1843 // result = double-to-float(result)
1844 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001845 // Restore low.
1846 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 break;
1848 }
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 };
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001869 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001871 break;
1872
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 case Primitive::kPrimLong: {
1874 // Processing a Dex `long-to-double' instruction.
1875 Register low = in.AsRegisterPairLow<Register>();
1876 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1878 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1879 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001880
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 // low = low - 2^31 (to prevent bit 31 of `low` to be
1882 // interpreted as a sign bit)
1883 __ subl(low, Immediate(0x80000000));
1884 // temp = int-to-double(high)
1885 __ cvtsi2sd(temp, high);
1886 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001887 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001888 __ mulsd(temp, constant);
1889 // result = int-to-double(low)
1890 __ cvtsi2sd(result, low);
1891 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001892 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001893 __ addsd(result, constant);
1894 // result = result + temp
1895 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001896 // Restore low.
1897 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001898 break;
1899 }
1900
Roland Levillaincff13742014-11-17 14:32:17 +00001901 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001902 // Processing a Dex `float-to-double' instruction.
1903 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001904 break;
1905
1906 default:
1907 LOG(FATAL) << "Unexpected type conversion from " << input_type
1908 << " to " << result_type;
1909 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001910 break;
1911
1912 default:
1913 LOG(FATAL) << "Unexpected type conversion from " << input_type
1914 << " to " << result_type;
1915 }
1916}
1917
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001918void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001919 LocationSummary* locations =
1920 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001921 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001922 case Primitive::kPrimInt: {
1923 locations->SetInAt(0, Location::RequiresRegister());
1924 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1926 break;
1927 }
1928
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001930 locations->SetInAt(0, Location::RequiresRegister());
1931 locations->SetInAt(1, Location::Any());
1932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 break;
1934 }
1935
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 case Primitive::kPrimFloat:
1937 case Primitive::kPrimDouble: {
1938 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001939 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001945 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1946 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001947 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948}
1949
1950void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1951 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001952 Location first = locations->InAt(0);
1953 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001954 Location out = locations->Out();
1955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001956 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001958 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001959 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1960 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1961 } else {
1962 __ leal(out.AsRegister<Register>(), Address(
1963 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1964 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001966 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1968 __ addl(out.AsRegister<Register>(), Immediate(value));
1969 } else {
1970 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1971 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001972 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001973 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001974 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001975 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001976 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001977 }
1978
1979 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001980 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001981 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1982 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001983 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001984 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1985 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001986 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001987 } else {
1988 DCHECK(second.IsConstant()) << second;
1989 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1990 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1991 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001992 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001993 break;
1994 }
1995
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001996 case Primitive::kPrimFloat: {
1997 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001998 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 }
2002
2003 case Primitive::kPrimDouble: {
2004 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002005 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002006 }
2007 break;
2008 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002009
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002010 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002011 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 }
2013}
2014
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002015void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002016 LocationSummary* locations =
2017 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002018 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002019 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002021 locations->SetInAt(0, Location::RequiresRegister());
2022 locations->SetInAt(1, Location::Any());
2023 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024 break;
2025 }
Calin Juravle11351682014-10-23 15:38:15 +01002026 case Primitive::kPrimFloat:
2027 case Primitive::kPrimDouble: {
2028 locations->SetInAt(0, Location::RequiresFpuRegister());
2029 locations->SetInAt(1, Location::RequiresFpuRegister());
2030 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002031 break;
Calin Juravle11351682014-10-23 15:38:15 +01002032 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002034 default:
Calin Juravle11351682014-10-23 15:38:15 +01002035 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037}
2038
2039void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2040 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002041 Location first = locations->InAt(0);
2042 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002043 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002044 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002049 __ subl(first.AsRegister<Register>(),
2050 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002051 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002054 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002055 }
2056
2057 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002058 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002059 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2060 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002061 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002062 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002063 __ sbbl(first.AsRegisterPairHigh<Register>(),
2064 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002065 } else {
2066 DCHECK(second.IsConstant()) << second;
2067 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2068 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2069 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002070 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002071 break;
2072 }
2073
Calin Juravle11351682014-10-23 15:38:15 +01002074 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002075 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002076 break;
Calin Juravle11351682014-10-23 15:38:15 +01002077 }
2078
2079 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002081 break;
2082 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002084 default:
Calin Juravle11351682014-10-23 15:38:15 +01002085 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002086 }
2087}
2088
Calin Juravle34bacdf2014-10-07 20:23:36 +01002089void LocationsBuilderX86::VisitMul(HMul* mul) {
2090 LocationSummary* locations =
2091 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2092 switch (mul->GetResultType()) {
2093 case Primitive::kPrimInt:
2094 locations->SetInAt(0, Location::RequiresRegister());
2095 locations->SetInAt(1, Location::Any());
2096 locations->SetOut(Location::SameAsFirstInput());
2097 break;
2098 case Primitive::kPrimLong: {
2099 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002100 locations->SetInAt(1, Location::Any());
2101 locations->SetOut(Location::SameAsFirstInput());
2102 // Needed for imul on 32bits with 64bits output.
2103 locations->AddTemp(Location::RegisterLocation(EAX));
2104 locations->AddTemp(Location::RegisterLocation(EDX));
2105 break;
2106 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002107 case Primitive::kPrimFloat:
2108 case Primitive::kPrimDouble: {
2109 locations->SetInAt(0, Location::RequiresFpuRegister());
2110 locations->SetInAt(1, Location::RequiresFpuRegister());
2111 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002112 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002113 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114
2115 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002116 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002117 }
2118}
2119
2120void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2121 LocationSummary* locations = mul->GetLocations();
2122 Location first = locations->InAt(0);
2123 Location second = locations->InAt(1);
2124 DCHECK(first.Equals(locations->Out()));
2125
2126 switch (mul->GetResultType()) {
2127 case Primitive::kPrimInt: {
2128 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002130 } else if (second.IsConstant()) {
2131 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 } else {
2134 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136 }
2137 break;
2138 }
2139
2140 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002141 Register in1_hi = first.AsRegisterPairHigh<Register>();
2142 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 Register eax = locations->GetTemp(0).AsRegister<Register>();
2144 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002145
2146 DCHECK_EQ(EAX, eax);
2147 DCHECK_EQ(EDX, edx);
2148
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002149 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002150 // output: in1
2151 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2152 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2153 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002154 if (second.IsConstant()) {
2155 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002156
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002157 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2158 int32_t low_value = Low32Bits(value);
2159 int32_t high_value = High32Bits(value);
2160 Immediate low(low_value);
2161 Immediate high(high_value);
2162
2163 __ movl(eax, high);
2164 // eax <- in1.lo * in2.hi
2165 __ imull(eax, in1_lo);
2166 // in1.hi <- in1.hi * in2.lo
2167 __ imull(in1_hi, low);
2168 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2169 __ addl(in1_hi, eax);
2170 // move in2_lo to eax to prepare for double precision
2171 __ movl(eax, low);
2172 // edx:eax <- in1.lo * in2.lo
2173 __ mull(in1_lo);
2174 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2175 __ addl(in1_hi, edx);
2176 // in1.lo <- (in1.lo * in2.lo)[31:0];
2177 __ movl(in1_lo, eax);
2178 } else if (second.IsRegisterPair()) {
2179 Register in2_hi = second.AsRegisterPairHigh<Register>();
2180 Register in2_lo = second.AsRegisterPairLow<Register>();
2181
2182 __ movl(eax, in2_hi);
2183 // eax <- in1.lo * in2.hi
2184 __ imull(eax, in1_lo);
2185 // in1.hi <- in1.hi * in2.lo
2186 __ imull(in1_hi, in2_lo);
2187 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2188 __ addl(in1_hi, eax);
2189 // move in1_lo to eax to prepare for double precision
2190 __ movl(eax, in1_lo);
2191 // edx:eax <- in1.lo * in2.lo
2192 __ mull(in2_lo);
2193 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2194 __ addl(in1_hi, edx);
2195 // in1.lo <- (in1.lo * in2.lo)[31:0];
2196 __ movl(in1_lo, eax);
2197 } else {
2198 DCHECK(second.IsDoubleStackSlot()) << second;
2199 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2200 Address in2_lo(ESP, second.GetStackIndex());
2201
2202 __ movl(eax, in2_hi);
2203 // eax <- in1.lo * in2.hi
2204 __ imull(eax, in1_lo);
2205 // in1.hi <- in1.hi * in2.lo
2206 __ imull(in1_hi, in2_lo);
2207 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2208 __ addl(in1_hi, eax);
2209 // move in1_lo to eax to prepare for double precision
2210 __ movl(eax, in1_lo);
2211 // edx:eax <- in1.lo * in2.lo
2212 __ mull(in2_lo);
2213 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2214 __ addl(in1_hi, edx);
2215 // in1.lo <- (in1.lo * in2.lo)[31:0];
2216 __ movl(in1_lo, eax);
2217 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002218
2219 break;
2220 }
2221
Calin Juravleb5bfa962014-10-21 18:02:24 +01002222 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002223 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002224 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002225 }
2226
2227 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002228 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002229 break;
2230 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002231
2232 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002233 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002234 }
2235}
2236
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002237void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2238 uint32_t stack_adjustment, bool is_float) {
2239 if (source.IsStackSlot()) {
2240 DCHECK(is_float);
2241 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2242 } else if (source.IsDoubleStackSlot()) {
2243 DCHECK(!is_float);
2244 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2245 } else {
2246 // Write the value to the temporary location on the stack and load to FP stack.
2247 if (is_float) {
2248 Location stack_temp = Location::StackSlot(temp_offset);
2249 codegen_->Move32(stack_temp, source);
2250 __ flds(Address(ESP, temp_offset));
2251 } else {
2252 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2253 codegen_->Move64(stack_temp, source);
2254 __ fldl(Address(ESP, temp_offset));
2255 }
2256 }
2257}
2258
2259void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2260 Primitive::Type type = rem->GetResultType();
2261 bool is_float = type == Primitive::kPrimFloat;
2262 size_t elem_size = Primitive::ComponentSize(type);
2263 LocationSummary* locations = rem->GetLocations();
2264 Location first = locations->InAt(0);
2265 Location second = locations->InAt(1);
2266 Location out = locations->Out();
2267
2268 // Create stack space for 2 elements.
2269 // TODO: enhance register allocator to ask for stack temporaries.
2270 __ subl(ESP, Immediate(2 * elem_size));
2271
2272 // Load the values to the FP stack in reverse order, using temporaries if needed.
2273 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2274 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2275
2276 // Loop doing FPREM until we stabilize.
2277 Label retry;
2278 __ Bind(&retry);
2279 __ fprem();
2280
2281 // Move FP status to AX.
2282 __ fstsw();
2283
2284 // And see if the argument reduction is complete. This is signaled by the
2285 // C2 FPU flag bit set to 0.
2286 __ andl(EAX, Immediate(kC2ConditionMask));
2287 __ j(kNotEqual, &retry);
2288
2289 // We have settled on the final value. Retrieve it into an XMM register.
2290 // Store FP top of stack to real stack.
2291 if (is_float) {
2292 __ fsts(Address(ESP, 0));
2293 } else {
2294 __ fstl(Address(ESP, 0));
2295 }
2296
2297 // Pop the 2 items from the FP stack.
2298 __ fucompp();
2299
2300 // Load the value from the stack into an XMM register.
2301 DCHECK(out.IsFpuRegister()) << out;
2302 if (is_float) {
2303 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2304 } else {
2305 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2306 }
2307
2308 // And remove the temporary stack space we allocated.
2309 __ addl(ESP, Immediate(2 * elem_size));
2310}
2311
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002312
2313void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2314 DCHECK(instruction->IsDiv() || instruction->IsRem());
2315
2316 LocationSummary* locations = instruction->GetLocations();
2317 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002318 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002319
2320 Register out_register = locations->Out().AsRegister<Register>();
2321 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002322 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002323
2324 DCHECK(imm == 1 || imm == -1);
2325
2326 if (instruction->IsRem()) {
2327 __ xorl(out_register, out_register);
2328 } else {
2329 __ movl(out_register, input_register);
2330 if (imm == -1) {
2331 __ negl(out_register);
2332 }
2333 }
2334}
2335
2336
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002337void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002338 LocationSummary* locations = instruction->GetLocations();
2339
2340 Register out_register = locations->Out().AsRegister<Register>();
2341 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002342 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002343
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002344 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002345 Register num = locations->GetTemp(0).AsRegister<Register>();
2346
2347 __ leal(num, Address(input_register, std::abs(imm) - 1));
2348 __ testl(input_register, input_register);
2349 __ cmovl(kGreaterEqual, num, input_register);
2350 int shift = CTZ(imm);
2351 __ sarl(num, Immediate(shift));
2352
2353 if (imm < 0) {
2354 __ negl(num);
2355 }
2356
2357 __ movl(out_register, num);
2358}
2359
2360void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2361 DCHECK(instruction->IsDiv() || instruction->IsRem());
2362
2363 LocationSummary* locations = instruction->GetLocations();
2364 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2365
2366 Register eax = locations->InAt(0).AsRegister<Register>();
2367 Register out = locations->Out().AsRegister<Register>();
2368 Register num;
2369 Register edx;
2370
2371 if (instruction->IsDiv()) {
2372 edx = locations->GetTemp(0).AsRegister<Register>();
2373 num = locations->GetTemp(1).AsRegister<Register>();
2374 } else {
2375 edx = locations->Out().AsRegister<Register>();
2376 num = locations->GetTemp(0).AsRegister<Register>();
2377 }
2378
2379 DCHECK_EQ(EAX, eax);
2380 DCHECK_EQ(EDX, edx);
2381 if (instruction->IsDiv()) {
2382 DCHECK_EQ(EAX, out);
2383 } else {
2384 DCHECK_EQ(EDX, out);
2385 }
2386
2387 int64_t magic;
2388 int shift;
2389 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2390
2391 Label ndiv;
2392 Label end;
2393 // If numerator is 0, the result is 0, no computation needed.
2394 __ testl(eax, eax);
2395 __ j(kNotEqual, &ndiv);
2396
2397 __ xorl(out, out);
2398 __ jmp(&end);
2399
2400 __ Bind(&ndiv);
2401
2402 // Save the numerator.
2403 __ movl(num, eax);
2404
2405 // EAX = magic
2406 __ movl(eax, Immediate(magic));
2407
2408 // EDX:EAX = magic * numerator
2409 __ imull(num);
2410
2411 if (imm > 0 && magic < 0) {
2412 // EDX += num
2413 __ addl(edx, num);
2414 } else if (imm < 0 && magic > 0) {
2415 __ subl(edx, num);
2416 }
2417
2418 // Shift if needed.
2419 if (shift != 0) {
2420 __ sarl(edx, Immediate(shift));
2421 }
2422
2423 // EDX += 1 if EDX < 0
2424 __ movl(eax, edx);
2425 __ shrl(edx, Immediate(31));
2426 __ addl(edx, eax);
2427
2428 if (instruction->IsRem()) {
2429 __ movl(eax, num);
2430 __ imull(edx, Immediate(imm));
2431 __ subl(eax, edx);
2432 __ movl(edx, eax);
2433 } else {
2434 __ movl(eax, edx);
2435 }
2436 __ Bind(&end);
2437}
2438
Calin Juravlebacfec32014-11-14 15:54:36 +00002439void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2440 DCHECK(instruction->IsDiv() || instruction->IsRem());
2441
2442 LocationSummary* locations = instruction->GetLocations();
2443 Location out = locations->Out();
2444 Location first = locations->InAt(0);
2445 Location second = locations->InAt(1);
2446 bool is_div = instruction->IsDiv();
2447
2448 switch (instruction->GetResultType()) {
2449 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 DCHECK_EQ(EAX, first.AsRegister<Register>());
2451 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002452
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002453 if (instruction->InputAt(1)->IsIntConstant()) {
2454 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002455
2456 if (imm == 0) {
2457 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2458 } else if (imm == 1 || imm == -1) {
2459 DivRemOneOrMinusOne(instruction);
2460 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002461 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002462 } else {
2463 DCHECK(imm <= -2 || imm >= 2);
2464 GenerateDivRemWithAnyConstant(instruction);
2465 }
2466 } else {
2467 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002468 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002469 is_div);
2470 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002471
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002472 Register second_reg = second.AsRegister<Register>();
2473 // 0x80000000/-1 triggers an arithmetic exception!
2474 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2475 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002476
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477 __ cmpl(second_reg, Immediate(-1));
2478 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002479
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002480 // edx:eax <- sign-extended of eax
2481 __ cdq();
2482 // eax = quotient, edx = remainder
2483 __ idivl(second_reg);
2484 __ Bind(slow_path->GetExitLabel());
2485 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002486 break;
2487 }
2488
2489 case Primitive::kPrimLong: {
2490 InvokeRuntimeCallingConvention calling_convention;
2491 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2492 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2493 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2494 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2495 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2496 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2497
2498 if (is_div) {
2499 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2500 } else {
2501 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2502 }
2503 uint32_t dex_pc = is_div
2504 ? instruction->AsDiv()->GetDexPc()
2505 : instruction->AsRem()->GetDexPc();
2506 codegen_->RecordPcInfo(instruction, dex_pc);
2507
2508 break;
2509 }
2510
2511 default:
2512 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2513 }
2514}
2515
Calin Juravle7c4954d2014-10-28 16:57:40 +00002516void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002517 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002518 ? LocationSummary::kCall
2519 : LocationSummary::kNoCall;
2520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2521
Calin Juravle7c4954d2014-10-28 16:57:40 +00002522 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002523 case Primitive::kPrimInt: {
2524 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002525 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002526 locations->SetOut(Location::SameAsFirstInput());
2527 // Intel uses edx:eax as the dividend.
2528 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002529 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2530 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2531 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002532 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002533 locations->AddTemp(Location::RequiresRegister());
2534 }
Calin Juravled0d48522014-11-04 16:40:20 +00002535 break;
2536 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002538 InvokeRuntimeCallingConvention calling_convention;
2539 locations->SetInAt(0, Location::RegisterPairLocation(
2540 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2541 locations->SetInAt(1, Location::RegisterPairLocation(
2542 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2543 // Runtime helper puts the result in EAX, EDX.
2544 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002545 break;
2546 }
2547 case Primitive::kPrimFloat:
2548 case Primitive::kPrimDouble: {
2549 locations->SetInAt(0, Location::RequiresFpuRegister());
2550 locations->SetInAt(1, Location::RequiresFpuRegister());
2551 locations->SetOut(Location::SameAsFirstInput());
2552 break;
2553 }
2554
2555 default:
2556 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2557 }
2558}
2559
2560void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2561 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002562 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002563 Location first = locations->InAt(0);
2564 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002565
2566 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002567 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002568 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002569 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002570 break;
2571 }
2572
2573 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002574 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002576 break;
2577 }
2578
2579 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002580 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002581 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002582 break;
2583 }
2584
2585 default:
2586 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2587 }
2588}
2589
Calin Juravlebacfec32014-11-14 15:54:36 +00002590void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002591 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002592
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002593 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2594 ? LocationSummary::kCall
2595 : LocationSummary::kNoCall;
2596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002597
Calin Juravled2ec87d2014-12-08 14:24:46 +00002598 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002599 case Primitive::kPrimInt: {
2600 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002601 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002602 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002603 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2604 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2605 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002606 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002607 locations->AddTemp(Location::RequiresRegister());
2608 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002609 break;
2610 }
2611 case Primitive::kPrimLong: {
2612 InvokeRuntimeCallingConvention calling_convention;
2613 locations->SetInAt(0, Location::RegisterPairLocation(
2614 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2615 locations->SetInAt(1, Location::RegisterPairLocation(
2616 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2617 // Runtime helper puts the result in EAX, EDX.
2618 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2619 break;
2620 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002621 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002622 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002623 locations->SetInAt(0, Location::Any());
2624 locations->SetInAt(1, Location::Any());
2625 locations->SetOut(Location::RequiresFpuRegister());
2626 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002627 break;
2628 }
2629
2630 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002631 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002632 }
2633}
2634
2635void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2636 Primitive::Type type = rem->GetResultType();
2637 switch (type) {
2638 case Primitive::kPrimInt:
2639 case Primitive::kPrimLong: {
2640 GenerateDivRemIntegral(rem);
2641 break;
2642 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002643 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002644 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002646 break;
2647 }
2648 default:
2649 LOG(FATAL) << "Unexpected rem type " << type;
2650 }
2651}
2652
Calin Juravled0d48522014-11-04 16:40:20 +00002653void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2654 LocationSummary* locations =
2655 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002656 switch (instruction->GetType()) {
2657 case Primitive::kPrimInt: {
2658 locations->SetInAt(0, Location::Any());
2659 break;
2660 }
2661 case Primitive::kPrimLong: {
2662 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2663 if (!instruction->IsConstant()) {
2664 locations->AddTemp(Location::RequiresRegister());
2665 }
2666 break;
2667 }
2668 default:
2669 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2670 }
Calin Juravled0d48522014-11-04 16:40:20 +00002671 if (instruction->HasUses()) {
2672 locations->SetOut(Location::SameAsFirstInput());
2673 }
2674}
2675
2676void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2677 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2678 codegen_->AddSlowPath(slow_path);
2679
2680 LocationSummary* locations = instruction->GetLocations();
2681 Location value = locations->InAt(0);
2682
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002683 switch (instruction->GetType()) {
2684 case Primitive::kPrimInt: {
2685 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002687 __ j(kEqual, slow_path->GetEntryLabel());
2688 } else if (value.IsStackSlot()) {
2689 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2690 __ j(kEqual, slow_path->GetEntryLabel());
2691 } else {
2692 DCHECK(value.IsConstant()) << value;
2693 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2694 __ jmp(slow_path->GetEntryLabel());
2695 }
2696 }
2697 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002698 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002699 case Primitive::kPrimLong: {
2700 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002702 __ movl(temp, value.AsRegisterPairLow<Register>());
2703 __ orl(temp, value.AsRegisterPairHigh<Register>());
2704 __ j(kEqual, slow_path->GetEntryLabel());
2705 } else {
2706 DCHECK(value.IsConstant()) << value;
2707 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2708 __ jmp(slow_path->GetEntryLabel());
2709 }
2710 }
2711 break;
2712 }
2713 default:
2714 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002715 }
Calin Juravled0d48522014-11-04 16:40:20 +00002716}
2717
Calin Juravle9aec02f2014-11-18 23:06:35 +00002718void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2719 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2720
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2723
2724 switch (op->GetResultType()) {
2725 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002726 locations->SetInAt(0, Location::RequiresRegister());
2727 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002728 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2729 locations->SetOut(Location::SameAsFirstInput());
2730 break;
2731 }
2732 case Primitive::kPrimLong: {
2733 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002734 // The shift count needs to be in CL.
2735 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002736 locations->SetOut(Location::SameAsFirstInput());
2737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2741 }
2742}
2743
2744void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2745 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2746
2747 LocationSummary* locations = op->GetLocations();
2748 Location first = locations->InAt(0);
2749 Location second = locations->InAt(1);
2750 DCHECK(first.Equals(locations->Out()));
2751
2752 switch (op->GetResultType()) {
2753 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002754 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002755 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002757 DCHECK_EQ(ECX, second_reg);
2758 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002759 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002760 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002761 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002762 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002763 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002764 }
2765 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002766 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2767 if (op->IsShl()) {
2768 __ shll(first_reg, imm);
2769 } else if (op->IsShr()) {
2770 __ sarl(first_reg, imm);
2771 } else {
2772 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002773 }
2774 }
2775 break;
2776 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002777 case Primitive::kPrimLong: {
2778 Register second_reg = second.AsRegister<Register>();
2779 DCHECK_EQ(ECX, second_reg);
2780 if (op->IsShl()) {
2781 GenerateShlLong(first, second_reg);
2782 } else if (op->IsShr()) {
2783 GenerateShrLong(first, second_reg);
2784 } else {
2785 GenerateUShrLong(first, second_reg);
2786 }
2787 break;
2788 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002789 default:
2790 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2791 }
2792}
2793
2794void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2795 Label done;
2796 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2797 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2798 __ testl(shifter, Immediate(32));
2799 __ j(kEqual, &done);
2800 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2801 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2802 __ Bind(&done);
2803}
2804
2805void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2806 Label done;
2807 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2808 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2809 __ testl(shifter, Immediate(32));
2810 __ j(kEqual, &done);
2811 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2812 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2813 __ Bind(&done);
2814}
2815
2816void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2817 Label done;
2818 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2819 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2820 __ testl(shifter, Immediate(32));
2821 __ j(kEqual, &done);
2822 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2823 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2824 __ Bind(&done);
2825}
2826
2827void LocationsBuilderX86::VisitShl(HShl* shl) {
2828 HandleShift(shl);
2829}
2830
2831void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2832 HandleShift(shl);
2833}
2834
2835void LocationsBuilderX86::VisitShr(HShr* shr) {
2836 HandleShift(shr);
2837}
2838
2839void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2840 HandleShift(shr);
2841}
2842
2843void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2844 HandleShift(ushr);
2845}
2846
2847void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2848 HandleShift(ushr);
2849}
2850
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002851void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002852 LocationSummary* locations =
2853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002854 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002855 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002856 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2857 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002858}
2859
2860void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2861 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002862 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002863 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002864
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002865 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002866
Nicolas Geoffray39468442014-09-02 15:17:15 +01002867 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002868 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002869}
2870
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002871void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2872 LocationSummary* locations =
2873 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2874 locations->SetOut(Location::RegisterLocation(EAX));
2875 InvokeRuntimeCallingConvention calling_convention;
2876 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002877 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2878 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002879}
2880
2881void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2882 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002883 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002884 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2885
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002886 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002887
2888 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2889 DCHECK(!codegen_->IsLeafMethod());
2890}
2891
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002892void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893 LocationSummary* locations =
2894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002895 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2896 if (location.IsStackSlot()) {
2897 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2898 } else if (location.IsDoubleStackSlot()) {
2899 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002900 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002901 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002902}
2903
2904void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002905 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002906}
2907
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002908void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002909 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002910 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002911 locations->SetInAt(0, Location::RequiresRegister());
2912 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002913}
2914
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002915void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2916 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002917 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002918 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002919 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002920 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002921 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002922 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002923 break;
2924
2925 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002926 __ notl(out.AsRegisterPairLow<Register>());
2927 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002928 break;
2929
2930 default:
2931 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2932 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002933}
2934
David Brazdil66d126e2015-04-03 16:02:44 +01002935void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2936 LocationSummary* locations =
2937 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2938 locations->SetInAt(0, Location::RequiresRegister());
2939 locations->SetOut(Location::SameAsFirstInput());
2940}
2941
2942void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002943 LocationSummary* locations = bool_not->GetLocations();
2944 Location in = locations->InAt(0);
2945 Location out = locations->Out();
2946 DCHECK(in.Equals(out));
2947 __ xorl(out.AsRegister<Register>(), Immediate(1));
2948}
2949
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002950void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002951 LocationSummary* locations =
2952 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002953 switch (compare->InputAt(0)->GetType()) {
2954 case Primitive::kPrimLong: {
2955 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002956 locations->SetInAt(1, Location::Any());
2957 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2958 break;
2959 }
2960 case Primitive::kPrimFloat:
2961 case Primitive::kPrimDouble: {
2962 locations->SetInAt(0, Location::RequiresFpuRegister());
2963 locations->SetInAt(1, Location::RequiresFpuRegister());
2964 locations->SetOut(Location::RequiresRegister());
2965 break;
2966 }
2967 default:
2968 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2969 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002970}
2971
2972void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002973 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002975 Location left = locations->InAt(0);
2976 Location right = locations->InAt(1);
2977
2978 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002979 switch (compare->InputAt(0)->GetType()) {
2980 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002981 Register left_low = left.AsRegisterPairLow<Register>();
2982 Register left_high = left.AsRegisterPairHigh<Register>();
2983 int32_t val_low = 0;
2984 int32_t val_high = 0;
2985 bool right_is_const = false;
2986
2987 if (right.IsConstant()) {
2988 DCHECK(right.GetConstant()->IsLongConstant());
2989 right_is_const = true;
2990 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2991 val_low = Low32Bits(val);
2992 val_high = High32Bits(val);
2993 }
2994
Calin Juravleddb7df22014-11-25 20:56:51 +00002995 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002996 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002997 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002998 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002999 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003000 DCHECK(right_is_const) << right;
3001 if (val_high == 0) {
3002 __ testl(left_high, left_high);
3003 } else {
3004 __ cmpl(left_high, Immediate(val_high));
3005 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003006 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003007 __ j(kLess, &less); // Signed compare.
3008 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003009 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003010 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003011 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003012 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003013 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003014 DCHECK(right_is_const) << right;
3015 if (val_low == 0) {
3016 __ testl(left_low, left_low);
3017 } else {
3018 __ cmpl(left_low, Immediate(val_low));
3019 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003020 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003021 break;
3022 }
3023 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003025 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3026 break;
3027 }
3028 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003030 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003031 break;
3032 }
3033 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003034 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003035 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003036 __ movl(out, Immediate(0));
3037 __ j(kEqual, &done);
3038 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3039
3040 __ Bind(&greater);
3041 __ movl(out, Immediate(1));
3042 __ jmp(&done);
3043
3044 __ Bind(&less);
3045 __ movl(out, Immediate(-1));
3046
3047 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003048}
3049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003050void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003053 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3054 locations->SetInAt(i, Location::Any());
3055 }
3056 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003057}
3058
3059void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003060 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003061 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003062}
3063
Calin Juravle52c48962014-12-16 17:02:57 +00003064void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3065 /*
3066 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3067 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3068 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3069 */
3070 switch (kind) {
3071 case MemBarrierKind::kAnyAny: {
3072 __ mfence();
3073 break;
3074 }
3075 case MemBarrierKind::kAnyStore:
3076 case MemBarrierKind::kLoadAny:
3077 case MemBarrierKind::kStoreStore: {
3078 // nop
3079 break;
3080 }
3081 default:
3082 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003083 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003084}
3085
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003086
Mark Mendell09ed1a32015-03-25 08:30:06 -04003087void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3088 Register temp) {
3089 // TODO: Implement all kinds of calls:
3090 // 1) boot -> boot
3091 // 2) app -> boot
3092 // 3) app -> app
3093 //
3094 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3095 // temp = method;
3096 LoadCurrentMethod(temp);
3097 if (!invoke->IsRecursive()) {
3098 // temp = temp->dex_cache_resolved_methods_;
3099 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3100 // temp = temp[index_in_cache]
3101 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3102 // (temp + offset_of_quick_compiled_code)()
3103 __ call(Address(
3104 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3105 } else {
3106 __ call(GetFrameEntryLabel());
3107 }
3108
3109 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003110}
3111
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003112void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 Label is_null;
3114 __ testl(value, value);
3115 __ j(kEqual, &is_null);
3116 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3117 __ movl(temp, object);
3118 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003119 __ movb(Address(temp, card, TIMES_1, 0),
3120 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 __ Bind(&is_null);
3122}
3123
Calin Juravle52c48962014-12-16 17:02:57 +00003124void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3125 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003126 LocationSummary* locations =
3127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003128 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003129
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003130 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3131 locations->SetOut(Location::RequiresFpuRegister());
3132 } else {
3133 // The output overlaps in case of long: we don't want the low move to overwrite
3134 // the object's location.
3135 locations->SetOut(Location::RequiresRegister(),
3136 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3137 : Location::kNoOutputOverlap);
3138 }
Calin Juravle52c48962014-12-16 17:02:57 +00003139
3140 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3141 // Long values can be loaded atomically into an XMM using movsd.
3142 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3143 // and then copy the XMM into the output 32bits at a time).
3144 locations->AddTemp(Location::RequiresFpuRegister());
3145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146}
3147
Calin Juravle52c48962014-12-16 17:02:57 +00003148void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3149 const FieldInfo& field_info) {
3150 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003151
Calin Juravle52c48962014-12-16 17:02:57 +00003152 LocationSummary* locations = instruction->GetLocations();
3153 Register base = locations->InAt(0).AsRegister<Register>();
3154 Location out = locations->Out();
3155 bool is_volatile = field_info.IsVolatile();
3156 Primitive::Type field_type = field_info.GetFieldType();
3157 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3158
3159 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003161 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003162 break;
3163 }
3164
3165 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003166 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003167 break;
3168 }
3169
3170 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003171 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003172 break;
3173 }
3174
3175 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003176 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177 break;
3178 }
3179
3180 case Primitive::kPrimInt:
3181 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003182 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003183 break;
3184 }
3185
3186 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003187 if (is_volatile) {
3188 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3189 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003191 __ movd(out.AsRegisterPairLow<Register>(), temp);
3192 __ psrlq(temp, Immediate(32));
3193 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3194 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003195 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003196 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003197 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003198 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3199 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003200 break;
3201 }
3202
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003203 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003204 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003205 break;
3206 }
3207
3208 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003209 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003210 break;
3211 }
3212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003213 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003214 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003215 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003216 }
Calin Juravle52c48962014-12-16 17:02:57 +00003217
Calin Juravle77520bc2015-01-12 18:45:46 +00003218 // Longs are handled in the switch.
3219 if (field_type != Primitive::kPrimLong) {
3220 codegen_->MaybeRecordImplicitNullCheck(instruction);
3221 }
3222
Calin Juravle52c48962014-12-16 17:02:57 +00003223 if (is_volatile) {
3224 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3225 }
3226}
3227
3228void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3229 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3230
3231 LocationSummary* locations =
3232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3233 locations->SetInAt(0, Location::RequiresRegister());
3234 bool is_volatile = field_info.IsVolatile();
3235 Primitive::Type field_type = field_info.GetFieldType();
3236 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3237 || (field_type == Primitive::kPrimByte);
3238
3239 // The register allocator does not support multiple
3240 // inputs that die at entry with one in a specific register.
3241 if (is_byte_type) {
3242 // Ensure the value is in a byte register.
3243 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003244 } else if (Primitive::IsFloatingPointType(field_type)) {
3245 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003246 } else {
3247 locations->SetInAt(1, Location::RequiresRegister());
3248 }
3249 // Temporary registers for the write barrier.
3250 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3251 locations->AddTemp(Location::RequiresRegister());
3252 // Ensure the card is in a byte register.
3253 locations->AddTemp(Location::RegisterLocation(ECX));
3254 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3255 // 64bits value can be atomically written to an address with movsd and an XMM register.
3256 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3257 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3258 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3259 // isolated cases when we need this it isn't worth adding the extra complexity.
3260 locations->AddTemp(Location::RequiresFpuRegister());
3261 locations->AddTemp(Location::RequiresFpuRegister());
3262 }
3263}
3264
3265void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3266 const FieldInfo& field_info) {
3267 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3268
3269 LocationSummary* locations = instruction->GetLocations();
3270 Register base = locations->InAt(0).AsRegister<Register>();
3271 Location value = locations->InAt(1);
3272 bool is_volatile = field_info.IsVolatile();
3273 Primitive::Type field_type = field_info.GetFieldType();
3274 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3275
3276 if (is_volatile) {
3277 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3278 }
3279
3280 switch (field_type) {
3281 case Primitive::kPrimBoolean:
3282 case Primitive::kPrimByte: {
3283 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3284 break;
3285 }
3286
3287 case Primitive::kPrimShort:
3288 case Primitive::kPrimChar: {
3289 __ movw(Address(base, offset), value.AsRegister<Register>());
3290 break;
3291 }
3292
3293 case Primitive::kPrimInt:
3294 case Primitive::kPrimNot: {
3295 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003296 break;
3297 }
3298
3299 case Primitive::kPrimLong: {
3300 if (is_volatile) {
3301 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3302 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3303 __ movd(temp1, value.AsRegisterPairLow<Register>());
3304 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3305 __ punpckldq(temp1, temp2);
3306 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003307 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003308 } else {
3309 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003311 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3312 }
3313 break;
3314 }
3315
3316 case Primitive::kPrimFloat: {
3317 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3318 break;
3319 }
3320
3321 case Primitive::kPrimDouble: {
3322 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3323 break;
3324 }
3325
3326 case Primitive::kPrimVoid:
3327 LOG(FATAL) << "Unreachable type " << field_type;
3328 UNREACHABLE();
3329 }
3330
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 // Longs are handled in the switch.
3332 if (field_type != Primitive::kPrimLong) {
3333 codegen_->MaybeRecordImplicitNullCheck(instruction);
3334 }
3335
3336 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3337 Register temp = locations->GetTemp(0).AsRegister<Register>();
3338 Register card = locations->GetTemp(1).AsRegister<Register>();
3339 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3340 }
3341
Calin Juravle52c48962014-12-16 17:02:57 +00003342 if (is_volatile) {
3343 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3344 }
3345}
3346
3347void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3348 HandleFieldGet(instruction, instruction->GetFieldInfo());
3349}
3350
3351void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3352 HandleFieldGet(instruction, instruction->GetFieldInfo());
3353}
3354
3355void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3356 HandleFieldSet(instruction, instruction->GetFieldInfo());
3357}
3358
3359void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3360 HandleFieldSet(instruction, instruction->GetFieldInfo());
3361}
3362
3363void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3364 HandleFieldSet(instruction, instruction->GetFieldInfo());
3365}
3366
3367void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3368 HandleFieldSet(instruction, instruction->GetFieldInfo());
3369}
3370
3371void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3372 HandleFieldGet(instruction, instruction->GetFieldInfo());
3373}
3374
3375void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3376 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003377}
3378
3379void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003380 LocationSummary* locations =
3381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003382 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3383 ? Location::RequiresRegister()
3384 : Location::Any();
3385 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003386 if (instruction->HasUses()) {
3387 locations->SetOut(Location::SameAsFirstInput());
3388 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003389}
3390
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003391void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003392 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3393 return;
3394 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003395 LocationSummary* locations = instruction->GetLocations();
3396 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003397
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003398 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3399 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3400}
3401
3402void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003403 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003404 codegen_->AddSlowPath(slow_path);
3405
3406 LocationSummary* locations = instruction->GetLocations();
3407 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003408
3409 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003410 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003411 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003412 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003413 } else {
3414 DCHECK(obj.IsConstant()) << obj;
3415 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3416 __ jmp(slow_path->GetEntryLabel());
3417 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003418 }
3419 __ j(kEqual, slow_path->GetEntryLabel());
3420}
3421
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003422void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3423 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3424 GenerateImplicitNullCheck(instruction);
3425 } else {
3426 GenerateExplicitNullCheck(instruction);
3427 }
3428}
3429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003430void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003431 LocationSummary* locations =
3432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003433 locations->SetInAt(0, Location::RequiresRegister());
3434 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003435 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3436 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3437 } else {
3438 // The output overlaps in case of long: we don't want the low move to overwrite
3439 // the array's location.
3440 locations->SetOut(Location::RequiresRegister(),
3441 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3442 : Location::kNoOutputOverlap);
3443 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003444}
3445
3446void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3447 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003449 Location index = locations->InAt(1);
3450
Calin Juravle77520bc2015-01-12 18:45:46 +00003451 Primitive::Type type = instruction->GetType();
3452 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 case Primitive::kPrimBoolean: {
3454 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 if (index.IsConstant()) {
3457 __ movzxb(out, Address(obj,
3458 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3459 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003460 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461 }
3462 break;
3463 }
3464
3465 case Primitive::kPrimByte: {
3466 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_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 __ movsxb(out, Address(obj,
3470 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3471 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ movsxb(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::kPrimShort: {
3478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_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 __ movsxw(out, Address(obj,
3482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3483 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003485 }
3486 break;
3487 }
3488
3489 case Primitive::kPrimChar: {
3490 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
3494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3495 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 __ movzxw(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::kPrimInt:
3502 case Primitive::kPrimNot: {
3503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003504 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003505 if (index.IsConstant()) {
3506 __ movl(out, Address(obj,
3507 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3508 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003509 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510 }
3511 break;
3512 }
3513
3514 case Primitive::kPrimLong: {
3515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003516 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003517 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003518 if (index.IsConstant()) {
3519 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003520 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003522 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003524 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003525 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003526 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003527 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003529 }
3530 break;
3531 }
3532
Mark Mendell7c8d0092015-01-26 11:21:33 -05003533 case Primitive::kPrimFloat: {
3534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3535 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3536 if (index.IsConstant()) {
3537 __ movss(out, Address(obj,
3538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3539 } else {
3540 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3541 }
3542 break;
3543 }
3544
3545 case Primitive::kPrimDouble: {
3546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3547 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3548 if (index.IsConstant()) {
3549 __ movsd(out, Address(obj,
3550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3551 } else {
3552 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3553 }
3554 break;
3555 }
3556
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003557 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003558 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003559 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003561
3562 if (type != Primitive::kPrimLong) {
3563 codegen_->MaybeRecordImplicitNullCheck(instruction);
3564 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565}
3566
3567void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003568 // This location builder might end up asking to up to four registers, which is
3569 // not currently possible for baseline. The situation in which we need four
3570 // registers cannot be met by baseline though, because it has not run any
3571 // optimization.
3572
Nicolas Geoffray39468442014-09-02 15:17:15 +01003573 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003574 bool needs_write_barrier =
3575 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3576
Mark Mendell5f874182015-03-04 15:42:45 -05003577 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003578
Nicolas Geoffray39468442014-09-02 15:17:15 +01003579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3580 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003581 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003582
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003583 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003585 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3586 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3587 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003588 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003589 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3590 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003591 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003592 // In case of a byte operation, the register allocator does not support multiple
3593 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003594 locations->SetInAt(0, Location::RequiresRegister());
3595 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003596 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003597 // Ensure the value is in a byte register.
3598 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003599 } else if (Primitive::IsFloatingPointType(value_type)) {
3600 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003602 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003604 // Temporary registers for the write barrier.
3605 if (needs_write_barrier) {
3606 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003607 // Ensure the card is in a byte register.
3608 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003609 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611}
3612
3613void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3614 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003617 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003618 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003619 bool needs_runtime_call = locations->WillCall();
3620 bool needs_write_barrier =
3621 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622
3623 switch (value_type) {
3624 case Primitive::kPrimBoolean:
3625 case Primitive::kPrimByte: {
3626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003627 if (index.IsConstant()) {
3628 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003629 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003630 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003631 } else {
3632 __ movb(Address(obj, offset),
3633 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3634 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003636 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003638 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003639 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003641 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 break;
3646 }
3647
3648 case Primitive::kPrimShort:
3649 case Primitive::kPrimChar: {
3650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003651 if (index.IsConstant()) {
3652 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003653 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003655 } else {
3656 __ movw(Address(obj, offset),
3657 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3658 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003660 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003661 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3662 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003663 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003664 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003665 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003668 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003669 break;
3670 }
3671
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003672 case Primitive::kPrimInt:
3673 case Primitive::kPrimNot: {
3674 if (!needs_runtime_call) {
3675 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3676 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003677 size_t offset =
3678 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003679 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003681 } else {
3682 DCHECK(value.IsConstant()) << value;
3683 __ movl(Address(obj, offset),
3684 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3685 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003686 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003687 DCHECK(index.IsRegister()) << index;
3688 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3690 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003691 } else {
3692 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003694 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3695 }
3696 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003697 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003698
3699 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 Register temp = locations->GetTemp(0).AsRegister<Register>();
3701 Register card = locations->GetTemp(1).AsRegister<Register>();
3702 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003705 DCHECK_EQ(value_type, Primitive::kPrimNot);
3706 DCHECK(!codegen_->IsLeafMethod());
3707 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3708 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003709 }
3710 break;
3711 }
3712
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003713 case Primitive::kPrimLong: {
3714 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003715 if (index.IsConstant()) {
3716 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003717 if (value.IsRegisterPair()) {
3718 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003719 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003720 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003721 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003722 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003723 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3724 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003725 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003726 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3727 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003729 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003731 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003732 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003734 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003735 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003736 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003737 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003739 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003742 Immediate(High32Bits(val)));
3743 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 }
3745 break;
3746 }
3747
Mark Mendell7c8d0092015-01-26 11:21:33 -05003748 case Primitive::kPrimFloat: {
3749 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3750 DCHECK(value.IsFpuRegister());
3751 if (index.IsConstant()) {
3752 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3753 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3754 } else {
3755 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3756 value.AsFpuRegister<XmmRegister>());
3757 }
3758 break;
3759 }
3760
3761 case Primitive::kPrimDouble: {
3762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3763 DCHECK(value.IsFpuRegister());
3764 if (index.IsConstant()) {
3765 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3766 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3767 } else {
3768 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3769 value.AsFpuRegister<XmmRegister>());
3770 }
3771 break;
3772 }
3773
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003774 case Primitive::kPrimVoid:
3775 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003776 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003777 }
3778}
3779
3780void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003782 locations->SetInAt(0, Location::RequiresRegister());
3783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784 instruction->SetLocations(locations);
3785}
3786
3787void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3788 LocationSummary* locations = instruction->GetLocations();
3789 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 Register obj = locations->InAt(0).AsRegister<Register>();
3791 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794}
3795
3796void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003797 LocationSummary* locations =
3798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003799 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003800 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003801 if (instruction->HasUses()) {
3802 locations->SetOut(Location::SameAsFirstInput());
3803 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804}
3805
3806void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3807 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003808 Location index_loc = locations->InAt(0);
3809 Location length_loc = locations->InAt(1);
3810 SlowPathCodeX86* slow_path =
3811 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 codegen_->AddSlowPath(slow_path);
3813
Mark Mendellf60c90b2015-03-04 15:12:59 -05003814 Register length = length_loc.AsRegister<Register>();
3815 if (index_loc.IsConstant()) {
3816 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3817 __ cmpl(length, Immediate(value));
3818 } else {
3819 __ cmpl(length, index_loc.AsRegister<Register>());
3820 }
3821 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822}
3823
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003824void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3825 temp->SetLocations(nullptr);
3826}
3827
3828void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3829 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003830 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831}
3832
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003833void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003834 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003835 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003836}
3837
3838void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003839 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3840}
3841
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003842void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3843 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3844}
3845
3846void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003847 HBasicBlock* block = instruction->GetBlock();
3848 if (block->GetLoopInformation() != nullptr) {
3849 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3850 // The back edge will generate the suspend check.
3851 return;
3852 }
3853 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3854 // The goto will generate the suspend check.
3855 return;
3856 }
3857 GenerateSuspendCheck(instruction, nullptr);
3858}
3859
3860void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3861 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003862 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003863 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003864 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003865 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003866 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003867 if (successor == nullptr) {
3868 __ j(kNotEqual, slow_path->GetEntryLabel());
3869 __ Bind(slow_path->GetReturnLabel());
3870 } else {
3871 __ j(kEqual, codegen_->GetLabelOf(successor));
3872 __ jmp(slow_path->GetEntryLabel());
3873 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003874}
3875
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003876X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3877 return codegen_->GetAssembler();
3878}
3879
Mark Mendell7c8d0092015-01-26 11:21:33 -05003880void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003881 ScratchRegisterScope ensure_scratch(
3882 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3883 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3884 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3885 __ movl(temp_reg, Address(ESP, src + stack_offset));
3886 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003887}
3888
3889void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003890 ScratchRegisterScope ensure_scratch(
3891 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3892 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3893 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3894 __ movl(temp_reg, Address(ESP, src + stack_offset));
3895 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3896 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3897 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003898}
3899
3900void ParallelMoveResolverX86::EmitMove(size_t index) {
3901 MoveOperands* move = moves_.Get(index);
3902 Location source = move->GetSource();
3903 Location destination = move->GetDestination();
3904
3905 if (source.IsRegister()) {
3906 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003907 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003908 } else {
3909 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003911 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003912 } else if (source.IsFpuRegister()) {
3913 if (destination.IsFpuRegister()) {
3914 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3915 } else if (destination.IsStackSlot()) {
3916 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3917 } else {
3918 DCHECK(destination.IsDoubleStackSlot());
3919 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3920 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003921 } else if (source.IsStackSlot()) {
3922 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003923 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003924 } else if (destination.IsFpuRegister()) {
3925 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003926 } else {
3927 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003928 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3929 }
3930 } else if (source.IsDoubleStackSlot()) {
3931 if (destination.IsFpuRegister()) {
3932 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3933 } else {
3934 DCHECK(destination.IsDoubleStackSlot()) << destination;
3935 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003936 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003937 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003938 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003939 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003940 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003941 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003942 if (value == 0) {
3943 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3944 } else {
3945 __ movl(destination.AsRegister<Register>(), Immediate(value));
3946 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003947 } else {
3948 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003949 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003950 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003951 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003952 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003953 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003954 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003955 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003956 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3957 if (value == 0) {
3958 // Easy handling of 0.0.
3959 __ xorps(dest, dest);
3960 } else {
3961 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003962 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3963 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3964 __ movl(temp, Immediate(value));
3965 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003966 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003967 } else {
3968 DCHECK(destination.IsStackSlot()) << destination;
3969 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3970 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003971 } else if (constant->IsLongConstant()) {
3972 int64_t value = constant->AsLongConstant()->GetValue();
3973 int32_t low_value = Low32Bits(value);
3974 int32_t high_value = High32Bits(value);
3975 Immediate low(low_value);
3976 Immediate high(high_value);
3977 if (destination.IsDoubleStackSlot()) {
3978 __ movl(Address(ESP, destination.GetStackIndex()), low);
3979 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3980 } else {
3981 __ movl(destination.AsRegisterPairLow<Register>(), low);
3982 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3983 }
3984 } else {
3985 DCHECK(constant->IsDoubleConstant());
3986 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003987 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003988 int32_t low_value = Low32Bits(value);
3989 int32_t high_value = High32Bits(value);
3990 Immediate low(low_value);
3991 Immediate high(high_value);
3992 if (destination.IsFpuRegister()) {
3993 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3994 if (value == 0) {
3995 // Easy handling of 0.0.
3996 __ xorpd(dest, dest);
3997 } else {
3998 __ pushl(high);
3999 __ pushl(low);
4000 __ movsd(dest, Address(ESP, 0));
4001 __ addl(ESP, Immediate(8));
4002 }
4003 } else {
4004 DCHECK(destination.IsDoubleStackSlot()) << destination;
4005 __ movl(Address(ESP, destination.GetStackIndex()), low);
4006 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4007 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004008 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004009 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004010 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004011 }
4012}
4013
Mark Mendella5c19ce2015-04-01 12:51:05 -04004014void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004015 Register suggested_scratch = reg == EAX ? EBX : EAX;
4016 ScratchRegisterScope ensure_scratch(
4017 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4018
4019 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4020 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4021 __ movl(Address(ESP, mem + stack_offset), reg);
4022 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004023}
4024
Mark Mendell7c8d0092015-01-26 11:21:33 -05004025void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004026 ScratchRegisterScope ensure_scratch(
4027 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4028
4029 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4030 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4031 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4032 __ movss(Address(ESP, mem + stack_offset), reg);
4033 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004034}
4035
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004036void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004037 ScratchRegisterScope ensure_scratch1(
4038 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004039
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004040 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4041 ScratchRegisterScope ensure_scratch2(
4042 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004043
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004044 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4045 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4046 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4047 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4048 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4049 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004050}
4051
4052void ParallelMoveResolverX86::EmitSwap(size_t index) {
4053 MoveOperands* move = moves_.Get(index);
4054 Location source = move->GetSource();
4055 Location destination = move->GetDestination();
4056
4057 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004058 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004059 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004060 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004061 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004062 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004063 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4064 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004065 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4066 // Use XOR Swap algorithm to avoid a temporary.
4067 DCHECK_NE(source.reg(), destination.reg());
4068 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4069 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4070 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4071 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4072 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4073 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4074 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004075 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4076 // Take advantage of the 16 bytes in the XMM register.
4077 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4078 Address stack(ESP, destination.GetStackIndex());
4079 // Load the double into the high doubleword.
4080 __ movhpd(reg, stack);
4081
4082 // Store the low double into the destination.
4083 __ movsd(stack, reg);
4084
4085 // Move the high double to the low double.
4086 __ psrldq(reg, Immediate(8));
4087 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4088 // Take advantage of the 16 bytes in the XMM register.
4089 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4090 Address stack(ESP, source.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.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4100 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4101 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004102 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004103 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004104 }
4105}
4106
4107void ParallelMoveResolverX86::SpillScratch(int reg) {
4108 __ pushl(static_cast<Register>(reg));
4109}
4110
4111void ParallelMoveResolverX86::RestoreScratch(int reg) {
4112 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004113}
4114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004115void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004116 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4117 ? LocationSummary::kCallOnSlowPath
4118 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004119 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004120 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004121 locations->SetOut(Location::RequiresRegister());
4122}
4123
4124void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004125 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004126 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004127 DCHECK(!cls->CanCallRuntime());
4128 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004129 codegen_->LoadCurrentMethod(out);
4130 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4131 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004132 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004133 codegen_->LoadCurrentMethod(out);
4134 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4135 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004136
4137 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4138 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4139 codegen_->AddSlowPath(slow_path);
4140 __ testl(out, out);
4141 __ j(kEqual, slow_path->GetEntryLabel());
4142 if (cls->MustGenerateClinitCheck()) {
4143 GenerateClassInitializationCheck(slow_path, out);
4144 } else {
4145 __ Bind(slow_path->GetExitLabel());
4146 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004147 }
4148}
4149
4150void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4151 LocationSummary* locations =
4152 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4153 locations->SetInAt(0, Location::RequiresRegister());
4154 if (check->HasUses()) {
4155 locations->SetOut(Location::SameAsFirstInput());
4156 }
4157}
4158
4159void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004160 // We assume the class to not be null.
4161 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4162 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004163 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004164 GenerateClassInitializationCheck(slow_path,
4165 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004166}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004167
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004168void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4169 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004170 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4171 Immediate(mirror::Class::kStatusInitialized));
4172 __ j(kLess, slow_path->GetEntryLabel());
4173 __ Bind(slow_path->GetExitLabel());
4174 // No need for memory fence, thanks to the X86 memory model.
4175}
4176
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004177void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4178 LocationSummary* locations =
4179 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4180 locations->SetOut(Location::RequiresRegister());
4181}
4182
4183void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4184 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4185 codegen_->AddSlowPath(slow_path);
4186
Roland Levillain271ab9c2014-11-27 15:23:57 +00004187 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004188 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004189 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4190 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004191 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4192 __ testl(out, out);
4193 __ j(kEqual, slow_path->GetEntryLabel());
4194 __ Bind(slow_path->GetExitLabel());
4195}
4196
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004197void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4198 LocationSummary* locations =
4199 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4200 locations->SetOut(Location::RequiresRegister());
4201}
4202
4203void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4204 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004205 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004206 __ fs()->movl(address, Immediate(0));
4207}
4208
4209void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4210 LocationSummary* locations =
4211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4212 InvokeRuntimeCallingConvention calling_convention;
4213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4214}
4215
4216void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4217 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4218 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4219}
4220
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004221void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004222 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4223 ? LocationSummary::kNoCall
4224 : LocationSummary::kCallOnSlowPath;
4225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4226 locations->SetInAt(0, Location::RequiresRegister());
4227 locations->SetInAt(1, Location::Any());
4228 locations->SetOut(Location::RequiresRegister());
4229}
4230
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004231void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004232 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004233 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004234 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004235 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004236 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4237 Label done, zero;
4238 SlowPathCodeX86* slow_path = nullptr;
4239
4240 // Return 0 if `obj` is null.
4241 // TODO: avoid this check if we know obj is not null.
4242 __ testl(obj, obj);
4243 __ j(kEqual, &zero);
4244 __ movl(out, Address(obj, class_offset));
4245 // Compare the class of `obj` with `cls`.
4246 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004247 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004248 } else {
4249 DCHECK(cls.IsStackSlot()) << cls;
4250 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4251 }
4252
4253 if (instruction->IsClassFinal()) {
4254 // Classes must be equal for the instanceof to succeed.
4255 __ j(kNotEqual, &zero);
4256 __ movl(out, Immediate(1));
4257 __ jmp(&done);
4258 } else {
4259 // If the classes are not equal, we go into a slow path.
4260 DCHECK(locations->OnlyCallsOnSlowPath());
4261 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004262 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004263 codegen_->AddSlowPath(slow_path);
4264 __ j(kNotEqual, slow_path->GetEntryLabel());
4265 __ movl(out, Immediate(1));
4266 __ jmp(&done);
4267 }
4268 __ Bind(&zero);
4269 __ movl(out, Immediate(0));
4270 if (slow_path != nullptr) {
4271 __ Bind(slow_path->GetExitLabel());
4272 }
4273 __ Bind(&done);
4274}
4275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004276void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4278 instruction, LocationSummary::kCallOnSlowPath);
4279 locations->SetInAt(0, Location::RequiresRegister());
4280 locations->SetInAt(1, Location::Any());
4281 locations->AddTemp(Location::RequiresRegister());
4282}
4283
4284void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4285 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004287 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004289 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4290 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4291 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4292 codegen_->AddSlowPath(slow_path);
4293
4294 // TODO: avoid this check if we know obj is not null.
4295 __ testl(obj, obj);
4296 __ j(kEqual, slow_path->GetExitLabel());
4297 __ movl(temp, Address(obj, class_offset));
4298
4299 // Compare the class of `obj` with `cls`.
4300 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004301 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004302 } else {
4303 DCHECK(cls.IsStackSlot()) << cls;
4304 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4305 }
4306
4307 __ j(kNotEqual, slow_path->GetEntryLabel());
4308 __ Bind(slow_path->GetExitLabel());
4309}
4310
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004311void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4312 LocationSummary* locations =
4313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4314 InvokeRuntimeCallingConvention calling_convention;
4315 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4316}
4317
4318void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4319 __ fs()->call(Address::Absolute(instruction->IsEnter()
4320 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4321 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4322 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4323}
4324
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004325void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4326void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4327void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4328
4329void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4330 LocationSummary* locations =
4331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4332 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4333 || instruction->GetResultType() == Primitive::kPrimLong);
4334 locations->SetInAt(0, Location::RequiresRegister());
4335 locations->SetInAt(1, Location::Any());
4336 locations->SetOut(Location::SameAsFirstInput());
4337}
4338
4339void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4340 HandleBitwiseOperation(instruction);
4341}
4342
4343void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4344 HandleBitwiseOperation(instruction);
4345}
4346
4347void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4348 HandleBitwiseOperation(instruction);
4349}
4350
4351void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4352 LocationSummary* locations = instruction->GetLocations();
4353 Location first = locations->InAt(0);
4354 Location second = locations->InAt(1);
4355 DCHECK(first.Equals(locations->Out()));
4356
4357 if (instruction->GetResultType() == Primitive::kPrimInt) {
4358 if (second.IsRegister()) {
4359 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004361 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004363 } else {
4364 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004365 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004366 }
4367 } else if (second.IsConstant()) {
4368 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004369 __ andl(first.AsRegister<Register>(),
4370 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004371 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004372 __ orl(first.AsRegister<Register>(),
4373 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004374 } else {
4375 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004376 __ xorl(first.AsRegister<Register>(),
4377 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004378 }
4379 } else {
4380 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004382 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004383 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004384 } else {
4385 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004386 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004387 }
4388 }
4389 } else {
4390 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4391 if (second.IsRegisterPair()) {
4392 if (instruction->IsAnd()) {
4393 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4394 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4395 } else if (instruction->IsOr()) {
4396 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4397 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4398 } else {
4399 DCHECK(instruction->IsXor());
4400 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4401 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4402 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004403 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004404 if (instruction->IsAnd()) {
4405 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4406 __ andl(first.AsRegisterPairHigh<Register>(),
4407 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4408 } else if (instruction->IsOr()) {
4409 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4410 __ orl(first.AsRegisterPairHigh<Register>(),
4411 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4412 } else {
4413 DCHECK(instruction->IsXor());
4414 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4415 __ xorl(first.AsRegisterPairHigh<Register>(),
4416 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4417 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004418 } else {
4419 DCHECK(second.IsConstant()) << second;
4420 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004421 int32_t low_value = Low32Bits(value);
4422 int32_t high_value = High32Bits(value);
4423 Immediate low(low_value);
4424 Immediate high(high_value);
4425 Register first_low = first.AsRegisterPairLow<Register>();
4426 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004427 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004428 if (low_value == 0) {
4429 __ xorl(first_low, first_low);
4430 } else if (low_value != -1) {
4431 __ andl(first_low, low);
4432 }
4433 if (high_value == 0) {
4434 __ xorl(first_high, first_high);
4435 } else if (high_value != -1) {
4436 __ andl(first_high, high);
4437 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004438 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004439 if (low_value != 0) {
4440 __ orl(first_low, low);
4441 }
4442 if (high_value != 0) {
4443 __ orl(first_high, high);
4444 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004445 } else {
4446 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004447 if (low_value != 0) {
4448 __ xorl(first_low, low);
4449 }
4450 if (high_value != 0) {
4451 __ xorl(first_high, high);
4452 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004453 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004454 }
4455 }
4456}
4457
Calin Juravleb1498f62015-02-16 13:13:29 +00004458void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4459 // Nothing to do, this should be removed during prepare for register allocator.
4460 UNUSED(instruction);
4461 LOG(FATAL) << "Unreachable";
4462}
4463
4464void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4465 // Nothing to do, this should be removed during prepare for register allocator.
4466 UNUSED(instruction);
4467 LOG(FATAL) << "Unreachable";
4468}
4469
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004470} // namespace x86
4471} // namespace art