blob: 8aa77969fc06c174c067039bc60ee52e9eff54b4 [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 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000178 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000181 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000182
183 __ jmp(GetExitLabel());
184 }
185
186 private:
187 HLoadString* const instruction_;
188
189 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
190};
191
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192class LoadClassSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 LoadClassSlowPathX86(HLoadClass* cls,
195 HInstruction* at,
196 uint32_t dex_pc,
197 bool do_clinit)
198 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
199 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
200 }
201
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000203 LocationSummary* locations = at_->GetLocations();
204 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207
208 InvokeRuntimeCallingConvention calling_convention;
209 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 __ fs()->call(Address::Absolute(do_clinit_
211 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
212 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214
215 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000216 Location out = locations->Out();
217 if (out.IsValid()) {
218 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
219 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000222 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 __ jmp(GetExitLabel());
224 }
225
226 private:
227 // The class this slow path will load.
228 HLoadClass* const cls_;
229
230 // The instruction where this slow path is happening.
231 // (Might be the load class or an initialization check).
232 HInstruction* const at_;
233
234 // The dex PC of `at_`.
235 const uint32_t dex_pc_;
236
237 // Whether to initialize the class.
238 const bool do_clinit_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
241};
242
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000243class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
244 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000245 TypeCheckSlowPathX86(HInstruction* instruction,
246 Location class_to_check,
247 Location object_class,
248 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 class_to_check_(class_to_check),
251 object_class_(object_class),
252 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000254 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000256 DCHECK(instruction_->IsCheckCast()
257 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
259 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
260 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000261 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000266 x86_codegen->EmitParallelMoves(
267 class_to_check_,
268 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100269 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000270 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100271 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
272 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000275 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
276 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000277 } else {
278 DCHECK(instruction_->IsCheckCast());
279 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
280 }
281
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 if (instruction_->IsInstanceOf()) {
284 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
285 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
288 __ jmp(GetExitLabel());
289 }
290
291 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 HInstruction* const instruction_;
293 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
298};
299
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700300class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
301 public:
302 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
303 : instruction_(instruction) {}
304
305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
309 // No need to restore live registers.
310 DCHECK(instruction_->IsDeoptimize());
311 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
312 uint32_t dex_pc = deoptimize->GetDexPc();
313 codegen->RecordPcInfo(instruction_, dex_pc, this);
314 }
315
316 private:
317 HInstruction* const instruction_;
318 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
319};
320
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100321#undef __
322#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
323
Dave Allison20dfc792014-06-16 20:44:29 -0700324inline Condition X86Condition(IfCondition cond) {
325 switch (cond) {
326 case kCondEQ: return kEqual;
327 case kCondNE: return kNotEqual;
328 case kCondLT: return kLess;
329 case kCondLE: return kLessEqual;
330 case kCondGT: return kGreater;
331 case kCondGE: return kGreaterEqual;
332 default:
333 LOG(FATAL) << "Unknown if condition";
334 }
335 return kEqual;
336}
337
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
339 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
340}
341
342void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
343 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
344}
345
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100346size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
347 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
348 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Mark Mendell7c8d0092015-01-26 11:21:33 -0500356size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
357 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
358 return GetFloatingPointSpillSlotSize();
359}
360
361size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
363 return GetFloatingPointSpillSlotSize();
364}
365
Mark Mendellfb8d2792015-03-31 22:16:59 -0400366CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
367 const X86InstructionSetFeatures& isa_features,
368 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500369 : CodeGenerator(graph,
370 kNumberOfCpuRegisters,
371 kNumberOfXmmRegisters,
372 kNumberOfRegisterPairs,
373 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
374 arraysize(kCoreCalleeSaves))
375 | (1 << kFakeReturnRegister),
376 0,
377 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100378 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100380 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400381 move_resolver_(graph->GetArena(), this),
382 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000383 // Use a fake return address register to mimic Quick.
384 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 switch (type) {
389 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 X86ManagedRegister pair =
392 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
411 X86ManagedRegister current =
412 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
413 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 }
416 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
420 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 return Location::FpuRegisterLocation(
423 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 case Primitive::kPrimVoid:
427 LOG(FATAL) << "Unreachable type " << type;
428 }
429
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431}
432
Mark Mendell5f874182015-03-04 15:42:45 -0500433void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
Mark Mendell5f874182015-03-04 15:42:45 -0500440 if (is_baseline) {
441 blocked_core_registers_[EBP] = true;
442 blocked_core_registers_[ESI] = true;
443 blocked_core_registers_[EDI] = true;
444 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100445
446 UpdateBlockedPairRegisters();
447}
448
449void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
450 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
451 X86ManagedRegister current =
452 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
453 if (blocked_core_registers_[current.AsRegisterPairLow()]
454 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
455 blocked_register_pairs_[i] = true;
456 }
457 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458}
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
461 : HGraphVisitor(graph),
462 assembler_(codegen->GetAssembler()),
463 codegen_(codegen) {}
464
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100465static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100466 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467}
468
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000469void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000471 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000472 bool skip_overflow_check =
473 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000474 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000475
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100477 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100478 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100479 }
480
Mark Mendell5f874182015-03-04 15:42:45 -0500481 if (HasEmptyFrame()) {
482 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000483 }
Mark Mendell5f874182015-03-04 15:42:45 -0500484
485 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
486 Register reg = kCoreCalleeSaves[i];
487 if (allocated_registers_.ContainsCoreRegister(reg)) {
488 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100489 __ cfi().AdjustCFAOffset(kX86WordSize);
490 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500491 }
492 }
493
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 int adjust = GetFrameSize() - FrameEntrySpillSize();
495 __ subl(ESP, Immediate(adjust));
496 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500497 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000498}
499
500void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100501 __ cfi().RememberState();
502 if (!HasEmptyFrame()) {
503 int adjust = GetFrameSize() - FrameEntrySpillSize();
504 __ addl(ESP, Immediate(adjust));
505 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500506
David Srbeckyc34dc932015-04-12 09:27:43 +0100507 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
508 Register reg = kCoreCalleeSaves[i];
509 if (allocated_registers_.ContainsCoreRegister(reg)) {
510 __ popl(reg);
511 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
512 __ cfi().Restore(DWARFReg(reg));
513 }
Mark Mendell5f874182015-03-04 15:42:45 -0500514 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000515 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100516 __ ret();
517 __ cfi().RestoreState();
518 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000519}
520
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100521void CodeGeneratorX86::Bind(HBasicBlock* block) {
522 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000523}
524
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100525void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000526 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100527 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000528}
529
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
531 switch (load->GetType()) {
532 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
536 case Primitive::kPrimInt:
537 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimBoolean:
542 case Primitive::kPrimByte:
543 case Primitive::kPrimChar:
544 case Primitive::kPrimShort:
545 case Primitive::kPrimVoid:
546 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700547 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548 }
549
550 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552}
553
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100554Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100555 switch (type) {
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimByte:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot: {
562 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000563 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100564 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000567 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100568 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100569 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000571 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 uint32_t index = gp_index_;
573 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000574 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100576 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
577 calling_convention.GetRegisterPairAt(index));
578 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100579 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000580 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
581 }
582 }
583
584 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100585 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000586 stack_index_++;
587 if (index < calling_convention.GetNumberOfFpuRegisters()) {
588 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
589 } else {
590 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
591 }
592 }
593
594 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100595 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000596 stack_index_ += 2;
597 if (index < calling_convention.GetNumberOfFpuRegisters()) {
598 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
599 } else {
600 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100601 }
602 }
603
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100604 case Primitive::kPrimVoid:
605 LOG(FATAL) << "Unexpected parameter type " << type;
606 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100607 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 return Location();
609}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100610
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611void CodeGeneratorX86::Move32(Location destination, Location source) {
612 if (source.Equals(destination)) {
613 return;
614 }
615 if (destination.IsRegister()) {
616 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 } else {
621 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else if (destination.IsFpuRegister()) {
625 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else {
630 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000634 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500639 } else if (source.IsConstant()) {
640 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000641 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500642 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
644 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100645 __ pushl(Address(ESP, source.GetStackIndex()));
646 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 }
648 }
649}
650
651void CodeGeneratorX86::Move64(Location destination, Location source) {
652 if (source.Equals(destination)) {
653 return;
654 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 if (destination.IsRegisterPair()) {
656 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000657 EmitParallelMoves(
658 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
659 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100660 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100662 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
663 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
665 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000667 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
670 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
672 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500674 if (source.IsFpuRegister()) {
675 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
676 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 } else {
679 LOG(FATAL) << "Unimplemented";
680 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000682 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100683 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000684 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000689 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000690 } else if (source.IsConstant()) {
691 HConstant* constant = source.GetConstant();
692 int64_t value;
693 if (constant->IsLongConstant()) {
694 value = constant->AsLongConstant()->GetValue();
695 } else {
696 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000697 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000698 }
699 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
700 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000702 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000703 EmitParallelMoves(
704 Location::StackSlot(source.GetStackIndex()),
705 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100706 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
709 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 }
711 }
712}
713
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100714void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 LocationSummary* locations = instruction->GetLocations();
716 if (locations != nullptr && locations->Out().Equals(location)) {
717 return;
718 }
719
720 if (locations != nullptr && locations->Out().IsConstant()) {
721 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000722 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
723 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 } else if (location.IsStackSlot()) {
727 __ movl(Address(ESP, location.GetStackIndex()), imm);
728 } else {
729 DCHECK(location.IsConstant());
730 DCHECK_EQ(location.GetConstant(), const_to_move);
731 }
732 } else if (const_to_move->IsLongConstant()) {
733 int64_t value = const_to_move->AsLongConstant()->GetValue();
734 if (location.IsRegisterPair()) {
735 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
736 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
737 } else if (location.IsDoubleStackSlot()) {
738 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000739 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
740 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000741 } else {
742 DCHECK(location.IsConstant());
743 DCHECK_EQ(location.GetConstant(), instruction);
744 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000746 } else if (instruction->IsTemporary()) {
747 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000748 if (temp_location.IsStackSlot()) {
749 Move32(location, temp_location);
750 } else {
751 DCHECK(temp_location.IsDoubleStackSlot());
752 Move64(location, temp_location);
753 }
Roland Levillain476df552014-10-09 17:51:36 +0100754 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 switch (instruction->GetType()) {
757 case Primitive::kPrimBoolean:
758 case Primitive::kPrimByte:
759 case Primitive::kPrimChar:
760 case Primitive::kPrimShort:
761 case Primitive::kPrimInt:
762 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 case Primitive::kPrimFloat:
764 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 break;
766
767 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 case Primitive::kPrimDouble:
769 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 break;
771
772 default:
773 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
774 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100776 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 switch (instruction->GetType()) {
778 case Primitive::kPrimBoolean:
779 case Primitive::kPrimByte:
780 case Primitive::kPrimChar:
781 case Primitive::kPrimShort:
782 case Primitive::kPrimInt:
783 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000785 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 break;
787
788 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000790 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 break;
792
793 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796 }
797}
798
799void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801}
802
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000803void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000804 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100805 DCHECK(!successor->IsExitBlock());
806
807 HBasicBlock* block = got->GetBlock();
808 HInstruction* previous = got->GetPrevious();
809
810 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000811 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
813 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
814 return;
815 }
816
817 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
818 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
819 }
820 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000822 }
823}
824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000825void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827}
828
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700830 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000831}
832
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700833void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
834 Label* true_target,
835 Label* false_target,
836 Label* always_true_target) {
837 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 if (cond->IsIntConstant()) {
839 // Constant condition, statically compared against 1.
840 int32_t cond_value = cond->AsIntConstant()->GetValue();
841 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700842 if (always_true_target != nullptr) {
843 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100844 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100846 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 DCHECK_EQ(cond_value, 0);
848 }
849 } else {
850 bool materialized =
851 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
852 // Moves do not affect the eflags register, so if the condition is
853 // evaluated just before the if, we don't need to evaluate it
854 // again.
855 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700856 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100857 if (materialized) {
858 if (!eflags_set) {
859 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500862 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 } else {
864 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
865 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700866 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 }
870 } else {
871 Location lhs = cond->GetLocations()->InAt(0);
872 Location rhs = cond->GetLocations()->InAt(1);
873 // LHS is guaranteed to be in a register (see
874 // LocationsBuilderX86::VisitCondition).
875 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100878 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500879 if (constant == 0) {
880 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
881 } else {
882 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
883 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100886 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700888 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100889 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700890 if (false_target != nullptr) {
891 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892 }
893}
894
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700895void LocationsBuilderX86::VisitIf(HIf* if_instr) {
896 LocationSummary* locations =
897 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
898 HInstruction* cond = if_instr->InputAt(0);
899 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
900 locations->SetInAt(0, Location::Any());
901 }
902}
903
904void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
905 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
906 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
907 Label* always_true_target = true_target;
908 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
909 if_instr->IfTrueSuccessor())) {
910 always_true_target = nullptr;
911 }
912 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
913 if_instr->IfFalseSuccessor())) {
914 false_target = nullptr;
915 }
916 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
917}
918
919void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
920 LocationSummary* locations = new (GetGraph()->GetArena())
921 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
922 HInstruction* cond = deoptimize->InputAt(0);
923 DCHECK(cond->IsCondition());
924 if (cond->AsCondition()->NeedsMaterialization()) {
925 locations->SetInAt(0, Location::Any());
926 }
927}
928
929void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
930 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
931 DeoptimizationSlowPathX86(deoptimize);
932 codegen_->AddSlowPath(slow_path);
933 Label* slow_path_entry = slow_path->GetEntryLabel();
934 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
935}
936
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000937void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000938 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000939}
940
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
942 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000943}
944
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000945void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100946 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947}
948
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000949void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100950 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700951 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000952}
953
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100955 LocationSummary* locations =
956 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 switch (store->InputAt(1)->GetType()) {
958 case Primitive::kPrimBoolean:
959 case Primitive::kPrimByte:
960 case Primitive::kPrimChar:
961 case Primitive::kPrimShort:
962 case Primitive::kPrimInt:
963 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100965 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
966 break;
967
968 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100969 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
971 break;
972
973 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 }
976 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977}
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700980 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981}
982
Dave Allison20dfc792014-06-16 20:44:29 -0700983void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100984 LocationSummary* locations =
985 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100986 locations->SetInAt(0, Location::RequiresRegister());
987 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100988 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500989 // We need a byte register.
990 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000992}
993
Dave Allison20dfc792014-06-16 20:44:29 -0700994void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
995 if (comp->NeedsMaterialization()) {
996 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100998 // Clear register: setcc only sets the low byte.
999 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001000 Location lhs = locations->InAt(0);
1001 Location rhs = locations->InAt(1);
1002 if (rhs.IsRegister()) {
1003 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1004 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001005 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001006 if (constant == 0) {
1007 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1008 } else {
1009 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1010 }
Dave Allison20dfc792014-06-16 20:44:29 -07001011 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001012 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001013 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001014 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001015 }
Dave Allison20dfc792014-06-16 20:44:29 -07001016}
1017
1018void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1047 VisitCondition(comp);
1048}
1049
1050void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1051 VisitCondition(comp);
1052}
1053
1054void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1055 VisitCondition(comp);
1056}
1057
1058void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1059 VisitCondition(comp);
1060}
1061
1062void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1063 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064}
1065
1066void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 LocationSummary* locations =
1068 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001069 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001070}
1071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001073 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001074 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001075}
1076
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001077void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1078 LocationSummary* locations =
1079 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1080 locations->SetOut(Location::ConstantLocation(constant));
1081}
1082
1083void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1084 // Will be generated at use site.
1085 UNUSED(constant);
1086}
1087
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001089 LocationSummary* locations =
1090 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001091 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092}
1093
1094void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1095 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097}
1098
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001099void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1100 LocationSummary* locations =
1101 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1102 locations->SetOut(Location::ConstantLocation(constant));
1103}
1104
1105void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1106 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001107 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001108}
1109
1110void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1113 locations->SetOut(Location::ConstantLocation(constant));
1114}
1115
1116void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1117 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001119}
1120
Calin Juravle27df7582015-04-17 19:12:31 +01001121void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1122 memory_barrier->SetLocations(nullptr);
1123}
1124
1125void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1126 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1127}
1128
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001129void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131}
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001136}
1137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001138void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001139 LocationSummary* locations =
1140 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 switch (ret->InputAt(0)->GetType()) {
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimByte:
1144 case Primitive::kPrimChar:
1145 case Primitive::kPrimShort:
1146 case Primitive::kPrimInt:
1147 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 break;
1150
1151 case Primitive::kPrimLong:
1152 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 break;
1155
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 case Primitive::kPrimFloat:
1157 case Primitive::kPrimDouble:
1158 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 break;
1161
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001165}
1166
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001167void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 if (kIsDebugBuild) {
1169 switch (ret->InputAt(0)->GetType()) {
1170 case Primitive::kPrimBoolean:
1171 case Primitive::kPrimByte:
1172 case Primitive::kPrimChar:
1173 case Primitive::kPrimShort:
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001176 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 break;
1178
1179 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1181 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 break;
1183
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimFloat:
1185 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001186 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 break;
1188
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001189 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 }
1192 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001194}
1195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001196void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001197 // When we do not run baseline, explicit clinit checks triggered by static
1198 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1199 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001200
Mark Mendellfb8d2792015-03-31 22:16:59 -04001201 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001202 if (intrinsic.TryDispatch(invoke)) {
1203 return;
1204 }
1205
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001206 HandleInvoke(invoke);
1207}
1208
Mark Mendell09ed1a32015-03-25 08:30:06 -04001209static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1210 if (invoke->GetLocations()->Intrinsified()) {
1211 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1212 intrinsic.Dispatch(invoke);
1213 return true;
1214 }
1215 return false;
1216}
1217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001218void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001219 // When we do not run baseline, explicit clinit checks triggered by static
1220 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1221 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001222
Mark Mendell09ed1a32015-03-25 08:30:06 -04001223 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1224 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001225 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 codegen_->GenerateStaticOrDirectCall(
1228 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001229 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230}
1231
1232void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1233 HandleInvoke(invoke);
1234}
1235
1236void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 LocationSummary* locations =
1238 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001239 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001240
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001241 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001242 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001243 HInstruction* input = invoke->InputAt(i);
1244 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1245 }
1246
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 switch (invoke->GetType()) {
1248 case Primitive::kPrimBoolean:
1249 case Primitive::kPrimByte:
1250 case Primitive::kPrimChar:
1251 case Primitive::kPrimShort:
1252 case Primitive::kPrimInt:
1253 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001254 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 break;
1256
1257 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimVoid:
1262 break;
1263
1264 case Primitive::kPrimDouble:
1265 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001266 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 break;
1268 }
1269
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270 invoke->SetLocations(locations);
1271}
1272
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001273void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001274 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001275 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1276 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1277 LocationSummary* locations = invoke->GetLocations();
1278 Location receiver = locations->InAt(0);
1279 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1280 // temp = object->GetClass();
1281 if (receiver.IsStackSlot()) {
1282 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1283 __ movl(temp, Address(temp, class_offset));
1284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 // temp = temp->GetMethodAt(method_offset);
1289 __ movl(temp, Address(temp, method_offset));
1290 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001291 __ call(Address(
1292 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001293
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001294 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001296}
1297
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001298void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1299 HandleInvoke(invoke);
1300 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001301 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302}
1303
1304void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1305 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1308 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1309 LocationSummary* locations = invoke->GetLocations();
1310 Location receiver = locations->InAt(0);
1311 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1312
1313 // Set the hidden argument.
1314 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316
1317 // temp = object->GetClass();
1318 if (receiver.IsStackSlot()) {
1319 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1320 __ movl(temp, Address(temp, class_offset));
1321 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001322 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001324 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001325 // temp = temp->GetImtEntryAt(method_offset);
1326 __ movl(temp, Address(temp, method_offset));
1327 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001328 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001329 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330
1331 DCHECK(!codegen_->IsLeafMethod());
1332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1333}
1334
Roland Levillain88cb1752014-10-20 16:36:47 +01001335void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1336 LocationSummary* locations =
1337 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1338 switch (neg->GetResultType()) {
1339 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001340 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001341 locations->SetInAt(0, Location::RequiresRegister());
1342 locations->SetOut(Location::SameAsFirstInput());
1343 break;
1344
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetOut(Location::SameAsFirstInput());
1348 locations->AddTemp(Location::RequiresRegister());
1349 locations->AddTemp(Location::RequiresFpuRegister());
1350 break;
1351
Roland Levillain88cb1752014-10-20 16:36:47 +01001352 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001354 locations->SetOut(Location::SameAsFirstInput());
1355 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 break;
1357
1358 default:
1359 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1360 }
1361}
1362
1363void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1364 LocationSummary* locations = neg->GetLocations();
1365 Location out = locations->Out();
1366 Location in = locations->InAt(0);
1367 switch (neg->GetResultType()) {
1368 case Primitive::kPrimInt:
1369 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001370 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001372 break;
1373
1374 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001375 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001376 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001377 __ negl(out.AsRegisterPairLow<Register>());
1378 // Negation is similar to subtraction from zero. The least
1379 // significant byte triggers a borrow when it is different from
1380 // zero; to take it into account, add 1 to the most significant
1381 // byte if the carry flag (CF) is set to 1 after the first NEGL
1382 // operation.
1383 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1384 __ negl(out.AsRegisterPairHigh<Register>());
1385 break;
1386
Roland Levillain5368c212014-11-27 15:03:41 +00001387 case Primitive::kPrimFloat: {
1388 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 Register constant = locations->GetTemp(0).AsRegister<Register>();
1390 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001391 // Implement float negation with an exclusive or with value
1392 // 0x80000000 (mask for bit 31, representing the sign of a
1393 // single-precision floating-point number).
1394 __ movl(constant, Immediate(INT32_C(0x80000000)));
1395 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001396 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001397 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001398 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001399
Roland Levillain5368c212014-11-27 15:03:41 +00001400 case Primitive::kPrimDouble: {
1401 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001402 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001403 // Implement double negation with an exclusive or with value
1404 // 0x8000000000000000 (mask for bit 63, representing the sign of
1405 // a double-precision floating-point number).
1406 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001407 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001408 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001409 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001410
1411 default:
1412 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1413 }
1414}
1415
Roland Levillaindff1f282014-11-05 14:15:05 +00001416void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001417 Primitive::Type result_type = conversion->GetResultType();
1418 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001419 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001420
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001421 // The float-to-long and double-to-long type conversions rely on a
1422 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001423 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1425 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001426 ? LocationSummary::kCall
1427 : LocationSummary::kNoCall;
1428 LocationSummary* locations =
1429 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1430
David Brazdilb2bd1c52015-03-25 11:17:37 +00001431 // The Java language does not allow treating boolean as an integral type but
1432 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001433
Roland Levillaindff1f282014-11-05 14:15:05 +00001434 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001435 case Primitive::kPrimByte:
1436 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001437 case Primitive::kPrimBoolean:
1438 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimShort:
1440 case Primitive::kPrimInt:
1441 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001442 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001443 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1444 // Make the output overlap to please the register allocator. This greatly simplifies
1445 // the validation of the linear scan implementation
1446 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001447 break;
1448
1449 default:
1450 LOG(FATAL) << "Unexpected type conversion from " << input_type
1451 << " to " << result_type;
1452 }
1453 break;
1454
Roland Levillain01a8d712014-11-14 16:27:39 +00001455 case Primitive::kPrimShort:
1456 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001457 case Primitive::kPrimBoolean:
1458 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimByte:
1460 case Primitive::kPrimInt:
1461 case Primitive::kPrimChar:
1462 // Processing a Dex `int-to-short' instruction.
1463 locations->SetInAt(0, Location::Any());
1464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1465 break;
1466
1467 default:
1468 LOG(FATAL) << "Unexpected type conversion from " << input_type
1469 << " to " << result_type;
1470 }
1471 break;
1472
Roland Levillain946e1432014-11-11 17:35:19 +00001473 case Primitive::kPrimInt:
1474 switch (input_type) {
1475 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001476 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001477 locations->SetInAt(0, Location::Any());
1478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1479 break;
1480
1481 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001482 // Processing a Dex `float-to-int' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresRegister());
1485 locations->AddTemp(Location::RequiresFpuRegister());
1486 break;
1487
Roland Levillain946e1432014-11-11 17:35:19 +00001488 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001489 // Processing a Dex `double-to-int' instruction.
1490 locations->SetInAt(0, Location::RequiresFpuRegister());
1491 locations->SetOut(Location::RequiresRegister());
1492 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001493 break;
1494
1495 default:
1496 LOG(FATAL) << "Unexpected type conversion from " << input_type
1497 << " to " << result_type;
1498 }
1499 break;
1500
Roland Levillaindff1f282014-11-05 14:15:05 +00001501 case Primitive::kPrimLong:
1502 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001503 case Primitive::kPrimBoolean:
1504 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimByte:
1506 case Primitive::kPrimShort:
1507 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001508 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001509 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001510 locations->SetInAt(0, Location::RegisterLocation(EAX));
1511 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1512 break;
1513
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001514 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001515 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001516 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001517 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1519 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1520
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 // The runtime helper puts the result in EAX, EDX.
1522 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001523 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001524 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001525
1526 default:
1527 LOG(FATAL) << "Unexpected type conversion from " << input_type
1528 << " to " << result_type;
1529 }
1530 break;
1531
Roland Levillain981e4542014-11-14 11:47:14 +00001532 case Primitive::kPrimChar:
1533 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001534 case Primitive::kPrimBoolean:
1535 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimByte:
1537 case Primitive::kPrimShort:
1538 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001539 // Processing a Dex `int-to-char' instruction.
1540 locations->SetInAt(0, Location::Any());
1541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1542 break;
1543
1544 default:
1545 LOG(FATAL) << "Unexpected type conversion from " << input_type
1546 << " to " << result_type;
1547 }
1548 break;
1549
Roland Levillaindff1f282014-11-05 14:15:05 +00001550 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001551 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001552 case Primitive::kPrimBoolean:
1553 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001554 case Primitive::kPrimByte:
1555 case Primitive::kPrimShort:
1556 case Primitive::kPrimInt:
1557 case Primitive::kPrimChar:
1558 // Processing a Dex `int-to-float' instruction.
1559 locations->SetInAt(0, Location::RequiresRegister());
1560 locations->SetOut(Location::RequiresFpuRegister());
1561 break;
1562
1563 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001564 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001565 locations->SetInAt(0, Location::Any());
1566 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001567 break;
1568
Roland Levillaincff13742014-11-17 14:32:17 +00001569 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001570 // Processing a Dex `double-to-float' instruction.
1571 locations->SetInAt(0, Location::RequiresFpuRegister());
1572 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001573 break;
1574
1575 default:
1576 LOG(FATAL) << "Unexpected type conversion from " << input_type
1577 << " to " << result_type;
1578 };
1579 break;
1580
Roland Levillaindff1f282014-11-05 14:15:05 +00001581 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001582 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001583 case Primitive::kPrimBoolean:
1584 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001585 case Primitive::kPrimByte:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimChar:
1589 // Processing a Dex `int-to-double' instruction.
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetOut(Location::RequiresFpuRegister());
1592 break;
1593
1594 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001595 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001596 locations->SetInAt(0, Location::Any());
1597 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001598 break;
1599
Roland Levillaincff13742014-11-17 14:32:17 +00001600 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001601 // Processing a Dex `float-to-double' instruction.
1602 locations->SetInAt(0, Location::RequiresFpuRegister());
1603 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001604 break;
1605
1606 default:
1607 LOG(FATAL) << "Unexpected type conversion from " << input_type
1608 << " to " << result_type;
1609 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 break;
1611
1612 default:
1613 LOG(FATAL) << "Unexpected type conversion from " << input_type
1614 << " to " << result_type;
1615 }
1616}
1617
1618void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1619 LocationSummary* locations = conversion->GetLocations();
1620 Location out = locations->Out();
1621 Location in = locations->InAt(0);
1622 Primitive::Type result_type = conversion->GetResultType();
1623 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001624 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001625 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001626 case Primitive::kPrimByte:
1627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001628 case Primitive::kPrimBoolean:
1629 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimShort:
1631 case Primitive::kPrimInt:
1632 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001633 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001634 if (in.IsRegister()) {
1635 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001636 } else {
1637 DCHECK(in.GetConstant()->IsIntConstant());
1638 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1640 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001641 break;
1642
1643 default:
1644 LOG(FATAL) << "Unexpected type conversion from " << input_type
1645 << " to " << result_type;
1646 }
1647 break;
1648
Roland Levillain01a8d712014-11-14 16:27:39 +00001649 case Primitive::kPrimShort:
1650 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001651 case Primitive::kPrimBoolean:
1652 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimByte:
1654 case Primitive::kPrimInt:
1655 case Primitive::kPrimChar:
1656 // Processing a Dex `int-to-short' instruction.
1657 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001658 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001661 } else {
1662 DCHECK(in.GetConstant()->IsIntConstant());
1663 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 }
1666 break;
1667
1668 default:
1669 LOG(FATAL) << "Unexpected type conversion from " << input_type
1670 << " to " << result_type;
1671 }
1672 break;
1673
Roland Levillain946e1432014-11-11 17:35:19 +00001674 case Primitive::kPrimInt:
1675 switch (input_type) {
1676 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001677 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001678 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001680 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001682 } else {
1683 DCHECK(in.IsConstant());
1684 DCHECK(in.GetConstant()->IsLongConstant());
1685 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001687 }
1688 break;
1689
Roland Levillain3f8f9362014-12-02 17:45:01 +00001690 case Primitive::kPrimFloat: {
1691 // Processing a Dex `float-to-int' instruction.
1692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1693 Register output = out.AsRegister<Register>();
1694 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1695 Label done, nan;
1696
1697 __ movl(output, Immediate(kPrimIntMax));
1698 // temp = int-to-float(output)
1699 __ cvtsi2ss(temp, output);
1700 // if input >= temp goto done
1701 __ comiss(input, temp);
1702 __ j(kAboveEqual, &done);
1703 // if input == NaN goto nan
1704 __ j(kUnordered, &nan);
1705 // output = float-to-int-truncate(input)
1706 __ cvttss2si(output, input);
1707 __ jmp(&done);
1708 __ Bind(&nan);
1709 // output = 0
1710 __ xorl(output, output);
1711 __ Bind(&done);
1712 break;
1713 }
1714
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 case Primitive::kPrimDouble: {
1716 // Processing a Dex `double-to-int' instruction.
1717 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1718 Register output = out.AsRegister<Register>();
1719 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1720 Label done, nan;
1721
1722 __ movl(output, Immediate(kPrimIntMax));
1723 // temp = int-to-double(output)
1724 __ cvtsi2sd(temp, output);
1725 // if input >= temp goto done
1726 __ comisd(input, temp);
1727 __ j(kAboveEqual, &done);
1728 // if input == NaN goto nan
1729 __ j(kUnordered, &nan);
1730 // output = double-to-int-truncate(input)
1731 __ cvttsd2si(output, input);
1732 __ jmp(&done);
1733 __ Bind(&nan);
1734 // output = 0
1735 __ xorl(output, output);
1736 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001737 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001738 }
Roland Levillain946e1432014-11-11 17:35:19 +00001739
1740 default:
1741 LOG(FATAL) << "Unexpected type conversion from " << input_type
1742 << " to " << result_type;
1743 }
1744 break;
1745
Roland Levillaindff1f282014-11-05 14:15:05 +00001746 case Primitive::kPrimLong:
1747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001748 case Primitive::kPrimBoolean:
1749 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimByte:
1751 case Primitive::kPrimShort:
1752 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001753 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001754 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001755 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1756 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 __ cdq();
1759 break;
1760
1761 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001762 // Processing a Dex `float-to-long' instruction.
1763 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001764 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1765 break;
1766
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001768 // Processing a Dex `double-to-long' instruction.
1769 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1770 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 break;
1772
1773 default:
1774 LOG(FATAL) << "Unexpected type conversion from " << input_type
1775 << " to " << result_type;
1776 }
1777 break;
1778
Roland Levillain981e4542014-11-14 11:47:14 +00001779 case Primitive::kPrimChar:
1780 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001781 case Primitive::kPrimBoolean:
1782 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimByte:
1784 case Primitive::kPrimShort:
1785 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001786 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1787 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001789 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001790 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001791 } else {
1792 DCHECK(in.GetConstant()->IsIntConstant());
1793 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 }
1796 break;
1797
1798 default:
1799 LOG(FATAL) << "Unexpected type conversion from " << input_type
1800 << " to " << result_type;
1801 }
1802 break;
1803
Roland Levillaindff1f282014-11-05 14:15:05 +00001804 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001805 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001806 case Primitive::kPrimBoolean:
1807 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001808 case Primitive::kPrimByte:
1809 case Primitive::kPrimShort:
1810 case Primitive::kPrimInt:
1811 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001812 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001813 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001814 break;
1815
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 case Primitive::kPrimLong: {
1817 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001818 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819
Roland Levillain232ade02015-04-20 15:14:36 +01001820 // Create stack space for the call to
1821 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1822 // TODO: enhance register allocator to ask for stack temporaries.
1823 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1824 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1825 __ subl(ESP, Immediate(adjustment));
1826 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001827
Roland Levillain232ade02015-04-20 15:14:36 +01001828 // Load the value to the FP stack, using temporaries if needed.
1829 PushOntoFPStack(in, 0, adjustment, false, true);
1830
1831 if (out.IsStackSlot()) {
1832 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1833 } else {
1834 __ fstps(Address(ESP, 0));
1835 Location stack_temp = Location::StackSlot(0);
1836 codegen_->Move32(out, stack_temp);
1837 }
1838
1839 // Remove the temporary stack space we allocated.
1840 if (adjustment != 0) {
1841 __ addl(ESP, Immediate(adjustment));
1842 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001843 break;
1844 }
1845
Roland Levillaincff13742014-11-17 14:32:17 +00001846 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001847 // Processing a Dex `double-to-float' instruction.
1848 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001849 break;
1850
1851 default:
1852 LOG(FATAL) << "Unexpected type conversion from " << input_type
1853 << " to " << result_type;
1854 };
1855 break;
1856
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001859 case Primitive::kPrimBoolean:
1860 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001861 case Primitive::kPrimByte:
1862 case Primitive::kPrimShort:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001865 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001867 break;
1868
Roland Levillain647b9ed2014-11-27 12:06:00 +00001869 case Primitive::kPrimLong: {
1870 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001871 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872
Roland Levillain232ade02015-04-20 15:14:36 +01001873 // Create stack space for the call to
1874 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1875 // TODO: enhance register allocator to ask for stack temporaries.
1876 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1877 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1878 __ subl(ESP, Immediate(adjustment));
1879 }
1880
1881 // Load the value to the FP stack, using temporaries if needed.
1882 PushOntoFPStack(in, 0, adjustment, false, true);
1883
1884 if (out.IsDoubleStackSlot()) {
1885 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1886 } else {
1887 __ fstpl(Address(ESP, 0));
1888 Location stack_temp = Location::DoubleStackSlot(0);
1889 codegen_->Move64(out, stack_temp);
1890 }
1891
1892 // Remove the temporary stack space we allocated.
1893 if (adjustment != 0) {
1894 __ addl(ESP, Immediate(adjustment));
1895 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001896 break;
1897 }
1898
Roland Levillaincff13742014-11-17 14:32:17 +00001899 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001900 // Processing a Dex `float-to-double' instruction.
1901 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
1914}
1915
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001916void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001917 LocationSummary* locations =
1918 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001919 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001920 case Primitive::kPrimInt: {
1921 locations->SetInAt(0, Location::RequiresRegister());
1922 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1924 break;
1925 }
1926
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetInAt(1, Location::Any());
1930 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932 }
1933
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 case Primitive::kPrimFloat:
1935 case Primitive::kPrimDouble: {
1936 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001937 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1944 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946}
1947
1948void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1949 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 Location first = locations->InAt(0);
1951 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001952 Location out = locations->Out();
1953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001954 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001957 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1958 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1959 } else {
1960 __ leal(out.AsRegister<Register>(), Address(
1961 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1962 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001964 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1965 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1966 __ addl(out.AsRegister<Register>(), Immediate(value));
1967 } else {
1968 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1969 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001970 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001971 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001973 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001974 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 }
1976
1977 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001978 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001979 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1980 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001981 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1983 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001985 } else {
1986 DCHECK(second.IsConstant()) << second;
1987 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1988 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1989 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 break;
1992 }
1993
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 case Primitive::kPrimFloat: {
1995 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 }
2000
2001 case Primitive::kPrimDouble: {
2002 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002003 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002004 }
2005 break;
2006 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002008 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002010 }
2011}
2012
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002013void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002014 LocationSummary* locations =
2015 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002016 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002017 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002018 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002019 locations->SetInAt(0, Location::RequiresRegister());
2020 locations->SetInAt(1, Location::Any());
2021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 break;
2023 }
Calin Juravle11351682014-10-23 15:38:15 +01002024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble: {
2026 locations->SetInAt(0, Location::RequiresFpuRegister());
2027 locations->SetInAt(1, Location::RequiresFpuRegister());
2028 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002029 break;
Calin Juravle11351682014-10-23 15:38:15 +01002030 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002031
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002032 default:
Calin Juravle11351682014-10-23 15:38:15 +01002033 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002034 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035}
2036
2037void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2038 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 Location first = locations->InAt(0);
2040 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002041 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002047 __ subl(first.AsRegister<Register>(),
2048 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002049 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002051 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002052 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002053 }
2054
2055 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002056 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002057 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2058 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002059 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002060 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 __ sbbl(first.AsRegisterPairHigh<Register>(),
2062 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002063 } else {
2064 DCHECK(second.IsConstant()) << second;
2065 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2066 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2067 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 break;
2070 }
2071
Calin Juravle11351682014-10-23 15:38:15 +01002072 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074 break;
Calin Juravle11351682014-10-23 15:38:15 +01002075 }
2076
2077 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002079 break;
2080 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002082 default:
Calin Juravle11351682014-10-23 15:38:15 +01002083 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002084 }
2085}
2086
Calin Juravle34bacdf2014-10-07 20:23:36 +01002087void LocationsBuilderX86::VisitMul(HMul* mul) {
2088 LocationSummary* locations =
2089 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2090 switch (mul->GetResultType()) {
2091 case Primitive::kPrimInt:
2092 locations->SetInAt(0, Location::RequiresRegister());
2093 locations->SetInAt(1, Location::Any());
2094 locations->SetOut(Location::SameAsFirstInput());
2095 break;
2096 case Primitive::kPrimLong: {
2097 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098 locations->SetInAt(1, Location::Any());
2099 locations->SetOut(Location::SameAsFirstInput());
2100 // Needed for imul on 32bits with 64bits output.
2101 locations->AddTemp(Location::RegisterLocation(EAX));
2102 locations->AddTemp(Location::RegisterLocation(EDX));
2103 break;
2104 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002105 case Primitive::kPrimFloat:
2106 case Primitive::kPrimDouble: {
2107 locations->SetInAt(0, Location::RequiresFpuRegister());
2108 locations->SetInAt(1, Location::RequiresFpuRegister());
2109 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002111 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002112
2113 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002114 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115 }
2116}
2117
2118void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2119 LocationSummary* locations = mul->GetLocations();
2120 Location first = locations->InAt(0);
2121 Location second = locations->InAt(1);
2122 DCHECK(first.Equals(locations->Out()));
2123
2124 switch (mul->GetResultType()) {
2125 case Primitive::kPrimInt: {
2126 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002128 } else if (second.IsConstant()) {
2129 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 } else {
2132 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 }
2135 break;
2136 }
2137
2138 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139 Register in1_hi = first.AsRegisterPairHigh<Register>();
2140 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 Register eax = locations->GetTemp(0).AsRegister<Register>();
2142 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143
2144 DCHECK_EQ(EAX, eax);
2145 DCHECK_EQ(EDX, edx);
2146
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002147 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002148 // output: in1
2149 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2150 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2151 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002152 if (second.IsConstant()) {
2153 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002154
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002155 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2156 int32_t low_value = Low32Bits(value);
2157 int32_t high_value = High32Bits(value);
2158 Immediate low(low_value);
2159 Immediate high(high_value);
2160
2161 __ movl(eax, high);
2162 // eax <- in1.lo * in2.hi
2163 __ imull(eax, in1_lo);
2164 // in1.hi <- in1.hi * in2.lo
2165 __ imull(in1_hi, low);
2166 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2167 __ addl(in1_hi, eax);
2168 // move in2_lo to eax to prepare for double precision
2169 __ movl(eax, low);
2170 // edx:eax <- in1.lo * in2.lo
2171 __ mull(in1_lo);
2172 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2173 __ addl(in1_hi, edx);
2174 // in1.lo <- (in1.lo * in2.lo)[31:0];
2175 __ movl(in1_lo, eax);
2176 } else if (second.IsRegisterPair()) {
2177 Register in2_hi = second.AsRegisterPairHigh<Register>();
2178 Register in2_lo = second.AsRegisterPairLow<Register>();
2179
2180 __ movl(eax, in2_hi);
2181 // eax <- in1.lo * in2.hi
2182 __ imull(eax, in1_lo);
2183 // in1.hi <- in1.hi * in2.lo
2184 __ imull(in1_hi, in2_lo);
2185 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2186 __ addl(in1_hi, eax);
2187 // move in1_lo to eax to prepare for double precision
2188 __ movl(eax, in1_lo);
2189 // edx:eax <- in1.lo * in2.lo
2190 __ mull(in2_lo);
2191 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2192 __ addl(in1_hi, edx);
2193 // in1.lo <- (in1.lo * in2.lo)[31:0];
2194 __ movl(in1_lo, eax);
2195 } else {
2196 DCHECK(second.IsDoubleStackSlot()) << second;
2197 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2198 Address in2_lo(ESP, second.GetStackIndex());
2199
2200 __ movl(eax, in2_hi);
2201 // eax <- in1.lo * in2.hi
2202 __ imull(eax, in1_lo);
2203 // in1.hi <- in1.hi * in2.lo
2204 __ imull(in1_hi, in2_lo);
2205 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2206 __ addl(in1_hi, eax);
2207 // move in1_lo to eax to prepare for double precision
2208 __ movl(eax, in1_lo);
2209 // edx:eax <- in1.lo * in2.lo
2210 __ mull(in2_lo);
2211 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2212 __ addl(in1_hi, edx);
2213 // in1.lo <- (in1.lo * in2.lo)[31:0];
2214 __ movl(in1_lo, eax);
2215 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002216
2217 break;
2218 }
2219
Calin Juravleb5bfa962014-10-21 18:02:24 +01002220 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002221 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 }
2224
2225 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002226 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 break;
2228 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002229
2230 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002232 }
2233}
2234
Roland Levillain232ade02015-04-20 15:14:36 +01002235void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2236 uint32_t temp_offset,
2237 uint32_t stack_adjustment,
2238 bool is_fp,
2239 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002240 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002241 DCHECK(!is_wide);
2242 if (is_fp) {
2243 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2244 } else {
2245 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2246 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002247 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002248 DCHECK(is_wide);
2249 if (is_fp) {
2250 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2251 } else {
2252 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2253 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002254 } else {
2255 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002256 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002257 Location stack_temp = Location::StackSlot(temp_offset);
2258 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002259 if (is_fp) {
2260 __ flds(Address(ESP, temp_offset));
2261 } else {
2262 __ filds(Address(ESP, temp_offset));
2263 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002264 } else {
2265 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2266 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002267 if (is_fp) {
2268 __ fldl(Address(ESP, temp_offset));
2269 } else {
2270 __ fildl(Address(ESP, temp_offset));
2271 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002272 }
2273 }
2274}
2275
2276void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2277 Primitive::Type type = rem->GetResultType();
2278 bool is_float = type == Primitive::kPrimFloat;
2279 size_t elem_size = Primitive::ComponentSize(type);
2280 LocationSummary* locations = rem->GetLocations();
2281 Location first = locations->InAt(0);
2282 Location second = locations->InAt(1);
2283 Location out = locations->Out();
2284
2285 // Create stack space for 2 elements.
2286 // TODO: enhance register allocator to ask for stack temporaries.
2287 __ subl(ESP, Immediate(2 * elem_size));
2288
2289 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002290 const bool is_wide = !is_float;
2291 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2292 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002293
2294 // Loop doing FPREM until we stabilize.
2295 Label retry;
2296 __ Bind(&retry);
2297 __ fprem();
2298
2299 // Move FP status to AX.
2300 __ fstsw();
2301
2302 // And see if the argument reduction is complete. This is signaled by the
2303 // C2 FPU flag bit set to 0.
2304 __ andl(EAX, Immediate(kC2ConditionMask));
2305 __ j(kNotEqual, &retry);
2306
2307 // We have settled on the final value. Retrieve it into an XMM register.
2308 // Store FP top of stack to real stack.
2309 if (is_float) {
2310 __ fsts(Address(ESP, 0));
2311 } else {
2312 __ fstl(Address(ESP, 0));
2313 }
2314
2315 // Pop the 2 items from the FP stack.
2316 __ fucompp();
2317
2318 // Load the value from the stack into an XMM register.
2319 DCHECK(out.IsFpuRegister()) << out;
2320 if (is_float) {
2321 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2322 } else {
2323 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2324 }
2325
2326 // And remove the temporary stack space we allocated.
2327 __ addl(ESP, Immediate(2 * elem_size));
2328}
2329
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002330
2331void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2332 DCHECK(instruction->IsDiv() || instruction->IsRem());
2333
2334 LocationSummary* locations = instruction->GetLocations();
2335 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002336 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002337
2338 Register out_register = locations->Out().AsRegister<Register>();
2339 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002340 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002341
2342 DCHECK(imm == 1 || imm == -1);
2343
2344 if (instruction->IsRem()) {
2345 __ xorl(out_register, out_register);
2346 } else {
2347 __ movl(out_register, input_register);
2348 if (imm == -1) {
2349 __ negl(out_register);
2350 }
2351 }
2352}
2353
2354
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002355void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002356 LocationSummary* locations = instruction->GetLocations();
2357
2358 Register out_register = locations->Out().AsRegister<Register>();
2359 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002360 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002361
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002362 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002363 Register num = locations->GetTemp(0).AsRegister<Register>();
2364
2365 __ leal(num, Address(input_register, std::abs(imm) - 1));
2366 __ testl(input_register, input_register);
2367 __ cmovl(kGreaterEqual, num, input_register);
2368 int shift = CTZ(imm);
2369 __ sarl(num, Immediate(shift));
2370
2371 if (imm < 0) {
2372 __ negl(num);
2373 }
2374
2375 __ movl(out_register, num);
2376}
2377
2378void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2379 DCHECK(instruction->IsDiv() || instruction->IsRem());
2380
2381 LocationSummary* locations = instruction->GetLocations();
2382 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2383
2384 Register eax = locations->InAt(0).AsRegister<Register>();
2385 Register out = locations->Out().AsRegister<Register>();
2386 Register num;
2387 Register edx;
2388
2389 if (instruction->IsDiv()) {
2390 edx = locations->GetTemp(0).AsRegister<Register>();
2391 num = locations->GetTemp(1).AsRegister<Register>();
2392 } else {
2393 edx = locations->Out().AsRegister<Register>();
2394 num = locations->GetTemp(0).AsRegister<Register>();
2395 }
2396
2397 DCHECK_EQ(EAX, eax);
2398 DCHECK_EQ(EDX, edx);
2399 if (instruction->IsDiv()) {
2400 DCHECK_EQ(EAX, out);
2401 } else {
2402 DCHECK_EQ(EDX, out);
2403 }
2404
2405 int64_t magic;
2406 int shift;
2407 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2408
2409 Label ndiv;
2410 Label end;
2411 // If numerator is 0, the result is 0, no computation needed.
2412 __ testl(eax, eax);
2413 __ j(kNotEqual, &ndiv);
2414
2415 __ xorl(out, out);
2416 __ jmp(&end);
2417
2418 __ Bind(&ndiv);
2419
2420 // Save the numerator.
2421 __ movl(num, eax);
2422
2423 // EAX = magic
2424 __ movl(eax, Immediate(magic));
2425
2426 // EDX:EAX = magic * numerator
2427 __ imull(num);
2428
2429 if (imm > 0 && magic < 0) {
2430 // EDX += num
2431 __ addl(edx, num);
2432 } else if (imm < 0 && magic > 0) {
2433 __ subl(edx, num);
2434 }
2435
2436 // Shift if needed.
2437 if (shift != 0) {
2438 __ sarl(edx, Immediate(shift));
2439 }
2440
2441 // EDX += 1 if EDX < 0
2442 __ movl(eax, edx);
2443 __ shrl(edx, Immediate(31));
2444 __ addl(edx, eax);
2445
2446 if (instruction->IsRem()) {
2447 __ movl(eax, num);
2448 __ imull(edx, Immediate(imm));
2449 __ subl(eax, edx);
2450 __ movl(edx, eax);
2451 } else {
2452 __ movl(eax, edx);
2453 }
2454 __ Bind(&end);
2455}
2456
Calin Juravlebacfec32014-11-14 15:54:36 +00002457void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2458 DCHECK(instruction->IsDiv() || instruction->IsRem());
2459
2460 LocationSummary* locations = instruction->GetLocations();
2461 Location out = locations->Out();
2462 Location first = locations->InAt(0);
2463 Location second = locations->InAt(1);
2464 bool is_div = instruction->IsDiv();
2465
2466 switch (instruction->GetResultType()) {
2467 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(EAX, first.AsRegister<Register>());
2469 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002470
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002471 if (instruction->InputAt(1)->IsIntConstant()) {
2472 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002473
2474 if (imm == 0) {
2475 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2476 } else if (imm == 1 || imm == -1) {
2477 DivRemOneOrMinusOne(instruction);
2478 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002479 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002480 } else {
2481 DCHECK(imm <= -2 || imm >= 2);
2482 GenerateDivRemWithAnyConstant(instruction);
2483 }
2484 } else {
2485 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002486 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002487 is_div);
2488 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002489
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002490 Register second_reg = second.AsRegister<Register>();
2491 // 0x80000000/-1 triggers an arithmetic exception!
2492 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2493 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495 __ cmpl(second_reg, Immediate(-1));
2496 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 // edx:eax <- sign-extended of eax
2499 __ cdq();
2500 // eax = quotient, edx = remainder
2501 __ idivl(second_reg);
2502 __ Bind(slow_path->GetExitLabel());
2503 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002504 break;
2505 }
2506
2507 case Primitive::kPrimLong: {
2508 InvokeRuntimeCallingConvention calling_convention;
2509 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2510 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2511 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2512 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2513 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2514 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2515
2516 if (is_div) {
2517 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2518 } else {
2519 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2520 }
2521 uint32_t dex_pc = is_div
2522 ? instruction->AsDiv()->GetDexPc()
2523 : instruction->AsRem()->GetDexPc();
2524 codegen_->RecordPcInfo(instruction, dex_pc);
2525
2526 break;
2527 }
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2531 }
2532}
2533
Calin Juravle7c4954d2014-10-28 16:57:40 +00002534void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002535 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002536 ? LocationSummary::kCall
2537 : LocationSummary::kNoCall;
2538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2539
Calin Juravle7c4954d2014-10-28 16:57:40 +00002540 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002541 case Primitive::kPrimInt: {
2542 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002543 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002544 locations->SetOut(Location::SameAsFirstInput());
2545 // Intel uses edx:eax as the dividend.
2546 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2548 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2549 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002550 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 locations->AddTemp(Location::RequiresRegister());
2552 }
Calin Juravled0d48522014-11-04 16:40:20 +00002553 break;
2554 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002555 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002556 InvokeRuntimeCallingConvention calling_convention;
2557 locations->SetInAt(0, Location::RegisterPairLocation(
2558 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2559 locations->SetInAt(1, Location::RegisterPairLocation(
2560 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2561 // Runtime helper puts the result in EAX, EDX.
2562 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002563 break;
2564 }
2565 case Primitive::kPrimFloat:
2566 case Primitive::kPrimDouble: {
2567 locations->SetInAt(0, Location::RequiresFpuRegister());
2568 locations->SetInAt(1, Location::RequiresFpuRegister());
2569 locations->SetOut(Location::SameAsFirstInput());
2570 break;
2571 }
2572
2573 default:
2574 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2575 }
2576}
2577
2578void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2579 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002580 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002581 Location first = locations->InAt(0);
2582 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002583
2584 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002585 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002586 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002587 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002588 break;
2589 }
2590
2591 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002592 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 break;
2595 }
2596
2597 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002598 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002600 break;
2601 }
2602
2603 default:
2604 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2605 }
2606}
2607
Calin Juravlebacfec32014-11-14 15:54:36 +00002608void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002609 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002610
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002611 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2612 ? LocationSummary::kCall
2613 : LocationSummary::kNoCall;
2614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002615
Calin Juravled2ec87d2014-12-08 14:24:46 +00002616 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002617 case Primitive::kPrimInt: {
2618 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002619 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002620 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002621 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2622 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2623 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002624 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 locations->AddTemp(Location::RequiresRegister());
2626 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002627 break;
2628 }
2629 case Primitive::kPrimLong: {
2630 InvokeRuntimeCallingConvention calling_convention;
2631 locations->SetInAt(0, Location::RegisterPairLocation(
2632 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2633 locations->SetInAt(1, Location::RegisterPairLocation(
2634 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2635 // Runtime helper puts the result in EAX, EDX.
2636 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2637 break;
2638 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002639 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002640 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002641 locations->SetInAt(0, Location::Any());
2642 locations->SetInAt(1, Location::Any());
2643 locations->SetOut(Location::RequiresFpuRegister());
2644 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002645 break;
2646 }
2647
2648 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002649 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002650 }
2651}
2652
2653void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2654 Primitive::Type type = rem->GetResultType();
2655 switch (type) {
2656 case Primitive::kPrimInt:
2657 case Primitive::kPrimLong: {
2658 GenerateDivRemIntegral(rem);
2659 break;
2660 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002661 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002662 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002663 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002664 break;
2665 }
2666 default:
2667 LOG(FATAL) << "Unexpected rem type " << type;
2668 }
2669}
2670
Calin Juravled0d48522014-11-04 16:40:20 +00002671void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2672 LocationSummary* locations =
2673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002674 switch (instruction->GetType()) {
2675 case Primitive::kPrimInt: {
2676 locations->SetInAt(0, Location::Any());
2677 break;
2678 }
2679 case Primitive::kPrimLong: {
2680 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2681 if (!instruction->IsConstant()) {
2682 locations->AddTemp(Location::RequiresRegister());
2683 }
2684 break;
2685 }
2686 default:
2687 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2688 }
Calin Juravled0d48522014-11-04 16:40:20 +00002689 if (instruction->HasUses()) {
2690 locations->SetOut(Location::SameAsFirstInput());
2691 }
2692}
2693
2694void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2695 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2696 codegen_->AddSlowPath(slow_path);
2697
2698 LocationSummary* locations = instruction->GetLocations();
2699 Location value = locations->InAt(0);
2700
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002701 switch (instruction->GetType()) {
2702 case Primitive::kPrimInt: {
2703 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002704 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002705 __ j(kEqual, slow_path->GetEntryLabel());
2706 } else if (value.IsStackSlot()) {
2707 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2708 __ j(kEqual, slow_path->GetEntryLabel());
2709 } else {
2710 DCHECK(value.IsConstant()) << value;
2711 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2712 __ jmp(slow_path->GetEntryLabel());
2713 }
2714 }
2715 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002716 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002717 case Primitive::kPrimLong: {
2718 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002720 __ movl(temp, value.AsRegisterPairLow<Register>());
2721 __ orl(temp, value.AsRegisterPairHigh<Register>());
2722 __ j(kEqual, slow_path->GetEntryLabel());
2723 } else {
2724 DCHECK(value.IsConstant()) << value;
2725 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2726 __ jmp(slow_path->GetEntryLabel());
2727 }
2728 }
2729 break;
2730 }
2731 default:
2732 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002733 }
Calin Juravled0d48522014-11-04 16:40:20 +00002734}
2735
Calin Juravle9aec02f2014-11-18 23:06:35 +00002736void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2737 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2738
2739 LocationSummary* locations =
2740 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2741
2742 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002743 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002744 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002745 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002746 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002747 // The shift count needs to be in CL or a constant.
2748 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002749 locations->SetOut(Location::SameAsFirstInput());
2750 break;
2751 }
2752 default:
2753 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2754 }
2755}
2756
2757void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2758 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2759
2760 LocationSummary* locations = op->GetLocations();
2761 Location first = locations->InAt(0);
2762 Location second = locations->InAt(1);
2763 DCHECK(first.Equals(locations->Out()));
2764
2765 switch (op->GetResultType()) {
2766 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002767 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002768 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002769 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002771 DCHECK_EQ(ECX, second_reg);
2772 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002773 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002775 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002776 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002777 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002778 }
2779 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002780 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2781 if (shift == 0) {
2782 return;
2783 }
2784 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 if (op->IsShl()) {
2786 __ shll(first_reg, imm);
2787 } else if (op->IsShr()) {
2788 __ sarl(first_reg, imm);
2789 } else {
2790 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002791 }
2792 }
2793 break;
2794 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002795 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002796 if (second.IsRegister()) {
2797 Register second_reg = second.AsRegister<Register>();
2798 DCHECK_EQ(ECX, second_reg);
2799 if (op->IsShl()) {
2800 GenerateShlLong(first, second_reg);
2801 } else if (op->IsShr()) {
2802 GenerateShrLong(first, second_reg);
2803 } else {
2804 GenerateUShrLong(first, second_reg);
2805 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002806 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002807 // Shift by a constant.
2808 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2809 // Nothing to do if the shift is 0, as the input is already the output.
2810 if (shift != 0) {
2811 if (op->IsShl()) {
2812 GenerateShlLong(first, shift);
2813 } else if (op->IsShr()) {
2814 GenerateShrLong(first, shift);
2815 } else {
2816 GenerateUShrLong(first, shift);
2817 }
2818 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002819 }
2820 break;
2821 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002822 default:
2823 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2824 }
2825}
2826
Mark P Mendell73945692015-04-29 14:56:17 +00002827void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2828 Register low = loc.AsRegisterPairLow<Register>();
2829 Register high = loc.AsRegisterPairHigh<Register>();
2830 if (shift == 32) {
2831 // Shift by 32 is easy. High gets low, and low gets 0.
2832 codegen_->EmitParallelMoves(
2833 loc.ToLow(),
2834 loc.ToHigh(),
2835 Primitive::kPrimInt,
2836 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2837 loc.ToLow(),
2838 Primitive::kPrimInt);
2839 } else if (shift > 32) {
2840 // Low part becomes 0. High part is low part << (shift-32).
2841 __ movl(high, low);
2842 __ shll(high, Immediate(shift - 32));
2843 __ xorl(low, low);
2844 } else {
2845 // Between 1 and 31.
2846 __ shld(high, low, Immediate(shift));
2847 __ shll(low, Immediate(shift));
2848 }
2849}
2850
Calin Juravle9aec02f2014-11-18 23:06:35 +00002851void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2852 Label done;
2853 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2854 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2855 __ testl(shifter, Immediate(32));
2856 __ j(kEqual, &done);
2857 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2858 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2859 __ Bind(&done);
2860}
2861
Mark P Mendell73945692015-04-29 14:56:17 +00002862void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2863 Register low = loc.AsRegisterPairLow<Register>();
2864 Register high = loc.AsRegisterPairHigh<Register>();
2865 if (shift == 32) {
2866 // Need to copy the sign.
2867 DCHECK_NE(low, high);
2868 __ movl(low, high);
2869 __ sarl(high, Immediate(31));
2870 } else if (shift > 32) {
2871 DCHECK_NE(low, high);
2872 // High part becomes sign. Low part is shifted by shift - 32.
2873 __ movl(low, high);
2874 __ sarl(high, Immediate(31));
2875 __ sarl(low, Immediate(shift - 32));
2876 } else {
2877 // Between 1 and 31.
2878 __ shrd(low, high, Immediate(shift));
2879 __ sarl(high, Immediate(shift));
2880 }
2881}
2882
Calin Juravle9aec02f2014-11-18 23:06:35 +00002883void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2884 Label done;
2885 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2886 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2887 __ testl(shifter, Immediate(32));
2888 __ j(kEqual, &done);
2889 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2890 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2891 __ Bind(&done);
2892}
2893
Mark P Mendell73945692015-04-29 14:56:17 +00002894void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2895 Register low = loc.AsRegisterPairLow<Register>();
2896 Register high = loc.AsRegisterPairHigh<Register>();
2897 if (shift == 32) {
2898 // Shift by 32 is easy. Low gets high, and high gets 0.
2899 codegen_->EmitParallelMoves(
2900 loc.ToHigh(),
2901 loc.ToLow(),
2902 Primitive::kPrimInt,
2903 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2904 loc.ToHigh(),
2905 Primitive::kPrimInt);
2906 } else if (shift > 32) {
2907 // Low part is high >> (shift - 32). High part becomes 0.
2908 __ movl(low, high);
2909 __ shrl(low, Immediate(shift - 32));
2910 __ xorl(high, high);
2911 } else {
2912 // Between 1 and 31.
2913 __ shrd(low, high, Immediate(shift));
2914 __ shrl(high, Immediate(shift));
2915 }
2916}
2917
Calin Juravle9aec02f2014-11-18 23:06:35 +00002918void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2919 Label done;
2920 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2921 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2922 __ testl(shifter, Immediate(32));
2923 __ j(kEqual, &done);
2924 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2925 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2926 __ Bind(&done);
2927}
2928
2929void LocationsBuilderX86::VisitShl(HShl* shl) {
2930 HandleShift(shl);
2931}
2932
2933void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2934 HandleShift(shl);
2935}
2936
2937void LocationsBuilderX86::VisitShr(HShr* shr) {
2938 HandleShift(shr);
2939}
2940
2941void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2942 HandleShift(shr);
2943}
2944
2945void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2946 HandleShift(ushr);
2947}
2948
2949void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2950 HandleShift(ushr);
2951}
2952
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002953void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002954 LocationSummary* locations =
2955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002956 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002957 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002958 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2959 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002960}
2961
2962void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2963 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002964 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002965 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002966
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002967 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002968
Nicolas Geoffray39468442014-09-02 15:17:15 +01002969 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002970 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002971}
2972
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002973void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2974 LocationSummary* locations =
2975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2976 locations->SetOut(Location::RegisterLocation(EAX));
2977 InvokeRuntimeCallingConvention calling_convention;
2978 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002979 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2980 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002981}
2982
2983void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2984 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002985 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002986 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2987
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002988 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002989
2990 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2991 DCHECK(!codegen_->IsLeafMethod());
2992}
2993
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002994void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002995 LocationSummary* locations =
2996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002997 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2998 if (location.IsStackSlot()) {
2999 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3000 } else if (location.IsDoubleStackSlot()) {
3001 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003002 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003003 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003004}
3005
3006void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003007 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003008}
3009
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003010void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003011 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003012 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003013 locations->SetInAt(0, Location::RequiresRegister());
3014 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003015}
3016
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003017void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3018 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003019 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003021 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003022 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003023 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003025 break;
3026
3027 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003028 __ notl(out.AsRegisterPairLow<Register>());
3029 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003030 break;
3031
3032 default:
3033 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3034 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003035}
3036
David Brazdil66d126e2015-04-03 16:02:44 +01003037void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3038 LocationSummary* locations =
3039 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3040 locations->SetInAt(0, Location::RequiresRegister());
3041 locations->SetOut(Location::SameAsFirstInput());
3042}
3043
3044void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003045 LocationSummary* locations = bool_not->GetLocations();
3046 Location in = locations->InAt(0);
3047 Location out = locations->Out();
3048 DCHECK(in.Equals(out));
3049 __ xorl(out.AsRegister<Register>(), Immediate(1));
3050}
3051
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003052void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003053 LocationSummary* locations =
3054 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003055 switch (compare->InputAt(0)->GetType()) {
3056 case Primitive::kPrimLong: {
3057 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003058 locations->SetInAt(1, Location::Any());
3059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3060 break;
3061 }
3062 case Primitive::kPrimFloat:
3063 case Primitive::kPrimDouble: {
3064 locations->SetInAt(0, Location::RequiresFpuRegister());
3065 locations->SetInAt(1, Location::RequiresFpuRegister());
3066 locations->SetOut(Location::RequiresRegister());
3067 break;
3068 }
3069 default:
3070 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3071 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003072}
3073
3074void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003075 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003077 Location left = locations->InAt(0);
3078 Location right = locations->InAt(1);
3079
3080 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003081 switch (compare->InputAt(0)->GetType()) {
3082 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003083 Register left_low = left.AsRegisterPairLow<Register>();
3084 Register left_high = left.AsRegisterPairHigh<Register>();
3085 int32_t val_low = 0;
3086 int32_t val_high = 0;
3087 bool right_is_const = false;
3088
3089 if (right.IsConstant()) {
3090 DCHECK(right.GetConstant()->IsLongConstant());
3091 right_is_const = true;
3092 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3093 val_low = Low32Bits(val);
3094 val_high = High32Bits(val);
3095 }
3096
Calin Juravleddb7df22014-11-25 20:56:51 +00003097 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003098 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003099 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003100 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003101 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003102 DCHECK(right_is_const) << right;
3103 if (val_high == 0) {
3104 __ testl(left_high, left_high);
3105 } else {
3106 __ cmpl(left_high, Immediate(val_high));
3107 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003109 __ j(kLess, &less); // Signed compare.
3110 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003111 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003112 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003113 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003114 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003115 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003116 DCHECK(right_is_const) << right;
3117 if (val_low == 0) {
3118 __ testl(left_low, left_low);
3119 } else {
3120 __ cmpl(left_low, Immediate(val_low));
3121 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003122 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003123 break;
3124 }
3125 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003126 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003127 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3128 break;
3129 }
3130 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003131 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003132 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003133 break;
3134 }
3135 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003136 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003137 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003138 __ movl(out, Immediate(0));
3139 __ j(kEqual, &done);
3140 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3141
3142 __ Bind(&greater);
3143 __ movl(out, Immediate(1));
3144 __ jmp(&done);
3145
3146 __ Bind(&less);
3147 __ movl(out, Immediate(-1));
3148
3149 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003150}
3151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003152void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003153 LocationSummary* locations =
3154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003155 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3156 locations->SetInAt(i, Location::Any());
3157 }
3158 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003159}
3160
3161void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003162 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003163 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003164}
3165
Calin Juravle52c48962014-12-16 17:02:57 +00003166void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3167 /*
3168 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3169 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3170 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3171 */
3172 switch (kind) {
3173 case MemBarrierKind::kAnyAny: {
3174 __ mfence();
3175 break;
3176 }
3177 case MemBarrierKind::kAnyStore:
3178 case MemBarrierKind::kLoadAny:
3179 case MemBarrierKind::kStoreStore: {
3180 // nop
3181 break;
3182 }
3183 default:
3184 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003185 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003186}
3187
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003188
Mark Mendell09ed1a32015-03-25 08:30:06 -04003189void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3190 Register temp) {
3191 // TODO: Implement all kinds of calls:
3192 // 1) boot -> boot
3193 // 2) app -> boot
3194 // 3) app -> app
3195 //
3196 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003197
3198 if (invoke->IsStringInit()) {
3199 // temp = thread->string_init_entrypoint
3200 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003201 // (temp + offset_of_quick_compiled_code)()
3202 __ call(Address(
3203 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3204 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003205 // temp = method;
3206 LoadCurrentMethod(temp);
3207 if (!invoke->IsRecursive()) {
3208 // temp = temp->dex_cache_resolved_methods_;
3209 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3210 // temp = temp[index_in_cache]
3211 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3212 // (temp + offset_of_quick_compiled_code)()
3213 __ call(Address(temp,
3214 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3215 } else {
3216 __ call(GetFrameEntryLabel());
3217 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003218 }
3219
3220 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003221}
3222
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003223void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003224 Label is_null;
3225 __ testl(value, value);
3226 __ j(kEqual, &is_null);
3227 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3228 __ movl(temp, object);
3229 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003230 __ movb(Address(temp, card, TIMES_1, 0),
3231 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232 __ Bind(&is_null);
3233}
3234
Calin Juravle52c48962014-12-16 17:02:57 +00003235void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3236 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003237 LocationSummary* locations =
3238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003239 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003240
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003241 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3242 locations->SetOut(Location::RequiresFpuRegister());
3243 } else {
3244 // The output overlaps in case of long: we don't want the low move to overwrite
3245 // the object's location.
3246 locations->SetOut(Location::RequiresRegister(),
3247 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3248 : Location::kNoOutputOverlap);
3249 }
Calin Juravle52c48962014-12-16 17:02:57 +00003250
3251 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3252 // Long values can be loaded atomically into an XMM using movsd.
3253 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3254 // and then copy the XMM into the output 32bits at a time).
3255 locations->AddTemp(Location::RequiresFpuRegister());
3256 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003257}
3258
Calin Juravle52c48962014-12-16 17:02:57 +00003259void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3260 const FieldInfo& field_info) {
3261 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003262
Calin Juravle52c48962014-12-16 17:02:57 +00003263 LocationSummary* locations = instruction->GetLocations();
3264 Register base = locations->InAt(0).AsRegister<Register>();
3265 Location out = locations->Out();
3266 bool is_volatile = field_info.IsVolatile();
3267 Primitive::Type field_type = field_info.GetFieldType();
3268 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3269
3270 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003271 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003272 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273 break;
3274 }
3275
3276 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003277 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278 break;
3279 }
3280
3281 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003282 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283 break;
3284 }
3285
3286 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003287 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003288 break;
3289 }
3290
3291 case Primitive::kPrimInt:
3292 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003293 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 break;
3295 }
3296
3297 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003298 if (is_volatile) {
3299 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3300 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003302 __ movd(out.AsRegisterPairLow<Register>(), temp);
3303 __ psrlq(temp, Immediate(32));
3304 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3305 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003306 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003307 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003309 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3310 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 break;
3312 }
3313
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003314 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003316 break;
3317 }
3318
3319 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003320 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003321 break;
3322 }
3323
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003324 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003325 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003326 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 }
Calin Juravle52c48962014-12-16 17:02:57 +00003328
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 // Longs are handled in the switch.
3330 if (field_type != Primitive::kPrimLong) {
3331 codegen_->MaybeRecordImplicitNullCheck(instruction);
3332 }
3333
Calin Juravle52c48962014-12-16 17:02:57 +00003334 if (is_volatile) {
3335 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3336 }
3337}
3338
3339void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3340 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3341
3342 LocationSummary* locations =
3343 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3344 locations->SetInAt(0, Location::RequiresRegister());
3345 bool is_volatile = field_info.IsVolatile();
3346 Primitive::Type field_type = field_info.GetFieldType();
3347 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3348 || (field_type == Primitive::kPrimByte);
3349
3350 // The register allocator does not support multiple
3351 // inputs that die at entry with one in a specific register.
3352 if (is_byte_type) {
3353 // Ensure the value is in a byte register.
3354 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003355 } else if (Primitive::IsFloatingPointType(field_type)) {
3356 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003357 } else {
3358 locations->SetInAt(1, Location::RequiresRegister());
3359 }
3360 // Temporary registers for the write barrier.
3361 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3362 locations->AddTemp(Location::RequiresRegister());
3363 // Ensure the card is in a byte register.
3364 locations->AddTemp(Location::RegisterLocation(ECX));
3365 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3366 // 64bits value can be atomically written to an address with movsd and an XMM register.
3367 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3368 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3369 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3370 // isolated cases when we need this it isn't worth adding the extra complexity.
3371 locations->AddTemp(Location::RequiresFpuRegister());
3372 locations->AddTemp(Location::RequiresFpuRegister());
3373 }
3374}
3375
3376void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3377 const FieldInfo& field_info) {
3378 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3379
3380 LocationSummary* locations = instruction->GetLocations();
3381 Register base = locations->InAt(0).AsRegister<Register>();
3382 Location value = locations->InAt(1);
3383 bool is_volatile = field_info.IsVolatile();
3384 Primitive::Type field_type = field_info.GetFieldType();
3385 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3386
3387 if (is_volatile) {
3388 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3389 }
3390
3391 switch (field_type) {
3392 case Primitive::kPrimBoolean:
3393 case Primitive::kPrimByte: {
3394 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3395 break;
3396 }
3397
3398 case Primitive::kPrimShort:
3399 case Primitive::kPrimChar: {
3400 __ movw(Address(base, offset), value.AsRegister<Register>());
3401 break;
3402 }
3403
3404 case Primitive::kPrimInt:
3405 case Primitive::kPrimNot: {
3406 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003407 break;
3408 }
3409
3410 case Primitive::kPrimLong: {
3411 if (is_volatile) {
3412 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3413 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3414 __ movd(temp1, value.AsRegisterPairLow<Register>());
3415 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3416 __ punpckldq(temp1, temp2);
3417 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003419 } else {
3420 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003422 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3423 }
3424 break;
3425 }
3426
3427 case Primitive::kPrimFloat: {
3428 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3429 break;
3430 }
3431
3432 case Primitive::kPrimDouble: {
3433 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3434 break;
3435 }
3436
3437 case Primitive::kPrimVoid:
3438 LOG(FATAL) << "Unreachable type " << field_type;
3439 UNREACHABLE();
3440 }
3441
Calin Juravle77520bc2015-01-12 18:45:46 +00003442 // Longs are handled in the switch.
3443 if (field_type != Primitive::kPrimLong) {
3444 codegen_->MaybeRecordImplicitNullCheck(instruction);
3445 }
3446
3447 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3448 Register temp = locations->GetTemp(0).AsRegister<Register>();
3449 Register card = locations->GetTemp(1).AsRegister<Register>();
3450 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3451 }
3452
Calin Juravle52c48962014-12-16 17:02:57 +00003453 if (is_volatile) {
3454 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3455 }
3456}
3457
3458void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3459 HandleFieldGet(instruction, instruction->GetFieldInfo());
3460}
3461
3462void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3463 HandleFieldGet(instruction, instruction->GetFieldInfo());
3464}
3465
3466void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3467 HandleFieldSet(instruction, instruction->GetFieldInfo());
3468}
3469
3470void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3471 HandleFieldSet(instruction, instruction->GetFieldInfo());
3472}
3473
3474void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3475 HandleFieldSet(instruction, instruction->GetFieldInfo());
3476}
3477
3478void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3479 HandleFieldSet(instruction, instruction->GetFieldInfo());
3480}
3481
3482void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3483 HandleFieldGet(instruction, instruction->GetFieldInfo());
3484}
3485
3486void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3487 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003488}
3489
3490void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003491 LocationSummary* locations =
3492 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003493 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3494 ? Location::RequiresRegister()
3495 : Location::Any();
3496 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003497 if (instruction->HasUses()) {
3498 locations->SetOut(Location::SameAsFirstInput());
3499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003500}
3501
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003502void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003503 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3504 return;
3505 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003506 LocationSummary* locations = instruction->GetLocations();
3507 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003508
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003509 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3510 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3511}
3512
3513void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003514 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003515 codegen_->AddSlowPath(slow_path);
3516
3517 LocationSummary* locations = instruction->GetLocations();
3518 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003519
3520 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003521 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003522 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003523 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003524 } else {
3525 DCHECK(obj.IsConstant()) << obj;
3526 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3527 __ jmp(slow_path->GetEntryLabel());
3528 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003529 }
3530 __ j(kEqual, slow_path->GetEntryLabel());
3531}
3532
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003533void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3534 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3535 GenerateImplicitNullCheck(instruction);
3536 } else {
3537 GenerateExplicitNullCheck(instruction);
3538 }
3539}
3540
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003544 locations->SetInAt(0, Location::RequiresRegister());
3545 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003546 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3547 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3548 } else {
3549 // The output overlaps in case of long: we don't want the low move to overwrite
3550 // the array's location.
3551 locations->SetOut(Location::RequiresRegister(),
3552 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3553 : Location::kNoOutputOverlap);
3554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003555}
3556
3557void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3558 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 Location index = locations->InAt(1);
3561
Calin Juravle77520bc2015-01-12 18:45:46 +00003562 Primitive::Type type = instruction->GetType();
3563 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003564 case Primitive::kPrimBoolean: {
3565 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 if (index.IsConstant()) {
3568 __ movzxb(out, Address(obj,
3569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3570 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003571 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572 }
3573 break;
3574 }
3575
3576 case Primitive::kPrimByte: {
3577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 if (index.IsConstant()) {
3580 __ movsxb(out, Address(obj,
3581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3582 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 }
3585 break;
3586 }
3587
3588 case Primitive::kPrimShort: {
3589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003591 if (index.IsConstant()) {
3592 __ movsxw(out, Address(obj,
3593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 }
3597 break;
3598 }
3599
3600 case Primitive::kPrimChar: {
3601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 if (index.IsConstant()) {
3604 __ movzxw(out, Address(obj,
3605 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3606 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 }
3609 break;
3610 }
3611
3612 case Primitive::kPrimInt:
3613 case Primitive::kPrimNot: {
3614 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 if (index.IsConstant()) {
3617 __ movl(out, Address(obj,
3618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3619 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 }
3622 break;
3623 }
3624
3625 case Primitive::kPrimLong: {
3626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003627 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003628 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 if (index.IsConstant()) {
3630 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003631 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003632 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003633 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003635 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003638 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640 }
3641 break;
3642 }
3643
Mark Mendell7c8d0092015-01-26 11:21:33 -05003644 case Primitive::kPrimFloat: {
3645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3646 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3647 if (index.IsConstant()) {
3648 __ movss(out, Address(obj,
3649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3650 } else {
3651 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3652 }
3653 break;
3654 }
3655
3656 case Primitive::kPrimDouble: {
3657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3658 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3659 if (index.IsConstant()) {
3660 __ movsd(out, Address(obj,
3661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3662 } else {
3663 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3664 }
3665 break;
3666 }
3667
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003668 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003669 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003670 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003671 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003672
3673 if (type != Primitive::kPrimLong) {
3674 codegen_->MaybeRecordImplicitNullCheck(instruction);
3675 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003676}
3677
3678void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003679 // This location builder might end up asking to up to four registers, which is
3680 // not currently possible for baseline. The situation in which we need four
3681 // registers cannot be met by baseline though, because it has not run any
3682 // optimization.
3683
Nicolas Geoffray39468442014-09-02 15:17:15 +01003684 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003685 bool needs_write_barrier =
3686 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3687
Mark Mendell5f874182015-03-04 15:42:45 -05003688 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003689
Nicolas Geoffray39468442014-09-02 15:17:15 +01003690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3691 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003692 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003693
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003694 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003696 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3697 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3698 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003699 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003700 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3701 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003702 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003703 // In case of a byte operation, the register allocator does not support multiple
3704 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003705 locations->SetInAt(0, Location::RequiresRegister());
3706 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003707 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003708 // Ensure the value is in a byte register.
3709 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003710 } else if (Primitive::IsFloatingPointType(value_type)) {
3711 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003713 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003714 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003715 // Temporary registers for the write barrier.
3716 if (needs_write_barrier) {
3717 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003718 // Ensure the card is in a byte register.
3719 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003720 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722}
3723
3724void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3725 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003728 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003729 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003730 bool needs_runtime_call = locations->WillCall();
3731 bool needs_write_barrier =
3732 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733
3734 switch (value_type) {
3735 case Primitive::kPrimBoolean:
3736 case Primitive::kPrimByte: {
3737 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 if (index.IsConstant()) {
3739 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003740 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003741 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003742 } else {
3743 __ movb(Address(obj, offset),
3744 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003747 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003749 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003750 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003752 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3753 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003754 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003755 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 break;
3757 }
3758
3759 case Primitive::kPrimShort:
3760 case Primitive::kPrimChar: {
3761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 if (index.IsConstant()) {
3763 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003764 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003765 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003766 } else {
3767 __ movw(Address(obj, offset),
3768 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3769 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003771 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3773 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003774 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003776 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3777 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780 break;
3781 }
3782
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003783 case Primitive::kPrimInt:
3784 case Primitive::kPrimNot: {
3785 if (!needs_runtime_call) {
3786 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3787 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003788 size_t offset =
3789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003790 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003792 } else {
3793 DCHECK(value.IsConstant()) << value;
3794 __ movl(Address(obj, offset),
3795 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3796 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003797 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003798 DCHECK(index.IsRegister()) << index;
3799 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003800 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3801 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003802 } else {
3803 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003804 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003805 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3806 }
3807 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003808 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003809
3810 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 Register temp = locations->GetTemp(0).AsRegister<Register>();
3812 Register card = locations->GetTemp(1).AsRegister<Register>();
3813 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003814 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003816 DCHECK_EQ(value_type, Primitive::kPrimNot);
3817 DCHECK(!codegen_->IsLeafMethod());
3818 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3819 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 }
3821 break;
3822 }
3823
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 case Primitive::kPrimLong: {
3825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 if (index.IsConstant()) {
3827 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003828 if (value.IsRegisterPair()) {
3829 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003830 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003831 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003832 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003833 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003834 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3835 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003836 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003837 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3838 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003840 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003842 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003845 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003846 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003847 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003848 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003850 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003851 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003853 Immediate(High32Bits(val)));
3854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003855 }
3856 break;
3857 }
3858
Mark Mendell7c8d0092015-01-26 11:21:33 -05003859 case Primitive::kPrimFloat: {
3860 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3861 DCHECK(value.IsFpuRegister());
3862 if (index.IsConstant()) {
3863 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3864 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3865 } else {
3866 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3867 value.AsFpuRegister<XmmRegister>());
3868 }
3869 break;
3870 }
3871
3872 case Primitive::kPrimDouble: {
3873 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3874 DCHECK(value.IsFpuRegister());
3875 if (index.IsConstant()) {
3876 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3877 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3878 } else {
3879 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3880 value.AsFpuRegister<XmmRegister>());
3881 }
3882 break;
3883 }
3884
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 case Primitive::kPrimVoid:
3886 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003887 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 }
3889}
3890
3891void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003893 locations->SetInAt(0, Location::RequiresRegister());
3894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895 instruction->SetLocations(locations);
3896}
3897
3898void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3899 LocationSummary* locations = instruction->GetLocations();
3900 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003901 Register obj = locations->InAt(0).AsRegister<Register>();
3902 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003904 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003905}
3906
3907void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003908 LocationSummary* locations =
3909 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003910 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003911 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003912 if (instruction->HasUses()) {
3913 locations->SetOut(Location::SameAsFirstInput());
3914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003915}
3916
3917void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3918 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003919 Location index_loc = locations->InAt(0);
3920 Location length_loc = locations->InAt(1);
3921 SlowPathCodeX86* slow_path =
3922 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923
Mark Mendell99dbd682015-04-22 16:18:52 -04003924 if (length_loc.IsConstant()) {
3925 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3926 if (index_loc.IsConstant()) {
3927 // BCE will remove the bounds check if we are guarenteed to pass.
3928 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3929 if (index < 0 || index >= length) {
3930 codegen_->AddSlowPath(slow_path);
3931 __ jmp(slow_path->GetEntryLabel());
3932 } else {
3933 // Some optimization after BCE may have generated this, and we should not
3934 // generate a bounds check if it is a valid range.
3935 }
3936 return;
3937 }
3938
3939 // We have to reverse the jump condition because the length is the constant.
3940 Register index_reg = index_loc.AsRegister<Register>();
3941 __ cmpl(index_reg, Immediate(length));
3942 codegen_->AddSlowPath(slow_path);
3943 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003944 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003945 Register length = length_loc.AsRegister<Register>();
3946 if (index_loc.IsConstant()) {
3947 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3948 __ cmpl(length, Immediate(value));
3949 } else {
3950 __ cmpl(length, index_loc.AsRegister<Register>());
3951 }
3952 codegen_->AddSlowPath(slow_path);
3953 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003954 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955}
3956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003957void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3958 temp->SetLocations(nullptr);
3959}
3960
3961void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3962 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003963 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003964}
3965
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003966void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003967 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003968 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003969}
3970
3971void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003972 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3973}
3974
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003975void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3977}
3978
3979void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003980 HBasicBlock* block = instruction->GetBlock();
3981 if (block->GetLoopInformation() != nullptr) {
3982 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3983 // The back edge will generate the suspend check.
3984 return;
3985 }
3986 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3987 // The goto will generate the suspend check.
3988 return;
3989 }
3990 GenerateSuspendCheck(instruction, nullptr);
3991}
3992
3993void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3994 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003995 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003996 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003997 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003998 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003999 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004000 if (successor == nullptr) {
4001 __ j(kNotEqual, slow_path->GetEntryLabel());
4002 __ Bind(slow_path->GetReturnLabel());
4003 } else {
4004 __ j(kEqual, codegen_->GetLabelOf(successor));
4005 __ jmp(slow_path->GetEntryLabel());
4006 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004007}
4008
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004009X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4010 return codegen_->GetAssembler();
4011}
4012
Mark Mendell7c8d0092015-01-26 11:21:33 -05004013void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004014 ScratchRegisterScope ensure_scratch(
4015 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4016 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4017 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4018 __ movl(temp_reg, Address(ESP, src + stack_offset));
4019 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004020}
4021
4022void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004023 ScratchRegisterScope ensure_scratch(
4024 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4025 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4026 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4027 __ movl(temp_reg, Address(ESP, src + stack_offset));
4028 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4029 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4030 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004031}
4032
4033void ParallelMoveResolverX86::EmitMove(size_t index) {
4034 MoveOperands* move = moves_.Get(index);
4035 Location source = move->GetSource();
4036 Location destination = move->GetDestination();
4037
4038 if (source.IsRegister()) {
4039 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004041 } else {
4042 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004044 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004045 } else if (source.IsFpuRegister()) {
4046 if (destination.IsFpuRegister()) {
4047 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4048 } else if (destination.IsStackSlot()) {
4049 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4050 } else {
4051 DCHECK(destination.IsDoubleStackSlot());
4052 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4053 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004054 } else if (source.IsStackSlot()) {
4055 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004056 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004057 } else if (destination.IsFpuRegister()) {
4058 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004059 } else {
4060 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004061 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4062 }
4063 } else if (source.IsDoubleStackSlot()) {
4064 if (destination.IsFpuRegister()) {
4065 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4066 } else {
4067 DCHECK(destination.IsDoubleStackSlot()) << destination;
4068 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004069 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004070 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004071 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004072 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004073 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004074 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004075 if (value == 0) {
4076 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4077 } else {
4078 __ movl(destination.AsRegister<Register>(), Immediate(value));
4079 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004080 } else {
4081 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004082 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004083 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004084 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004085 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004086 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004087 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004088 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004089 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4090 if (value == 0) {
4091 // Easy handling of 0.0.
4092 __ xorps(dest, dest);
4093 } else {
4094 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004095 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4096 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4097 __ movl(temp, Immediate(value));
4098 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004099 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004100 } else {
4101 DCHECK(destination.IsStackSlot()) << destination;
4102 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4103 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004104 } else if (constant->IsLongConstant()) {
4105 int64_t value = constant->AsLongConstant()->GetValue();
4106 int32_t low_value = Low32Bits(value);
4107 int32_t high_value = High32Bits(value);
4108 Immediate low(low_value);
4109 Immediate high(high_value);
4110 if (destination.IsDoubleStackSlot()) {
4111 __ movl(Address(ESP, destination.GetStackIndex()), low);
4112 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4113 } else {
4114 __ movl(destination.AsRegisterPairLow<Register>(), low);
4115 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4116 }
4117 } else {
4118 DCHECK(constant->IsDoubleConstant());
4119 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004120 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004121 int32_t low_value = Low32Bits(value);
4122 int32_t high_value = High32Bits(value);
4123 Immediate low(low_value);
4124 Immediate high(high_value);
4125 if (destination.IsFpuRegister()) {
4126 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4127 if (value == 0) {
4128 // Easy handling of 0.0.
4129 __ xorpd(dest, dest);
4130 } else {
4131 __ pushl(high);
4132 __ pushl(low);
4133 __ movsd(dest, Address(ESP, 0));
4134 __ addl(ESP, Immediate(8));
4135 }
4136 } else {
4137 DCHECK(destination.IsDoubleStackSlot()) << destination;
4138 __ movl(Address(ESP, destination.GetStackIndex()), low);
4139 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4140 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004141 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004142 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004143 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004144 }
4145}
4146
Mark Mendella5c19ce2015-04-01 12:51:05 -04004147void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004148 Register suggested_scratch = reg == EAX ? EBX : EAX;
4149 ScratchRegisterScope ensure_scratch(
4150 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4151
4152 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4153 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4154 __ movl(Address(ESP, mem + stack_offset), reg);
4155 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004156}
4157
Mark Mendell7c8d0092015-01-26 11:21:33 -05004158void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004159 ScratchRegisterScope ensure_scratch(
4160 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4161
4162 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4163 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4164 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4165 __ movss(Address(ESP, mem + stack_offset), reg);
4166 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004167}
4168
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004169void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004170 ScratchRegisterScope ensure_scratch1(
4171 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004172
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004173 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4174 ScratchRegisterScope ensure_scratch2(
4175 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004176
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004177 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4178 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4179 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4180 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4181 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4182 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004183}
4184
4185void ParallelMoveResolverX86::EmitSwap(size_t index) {
4186 MoveOperands* move = moves_.Get(index);
4187 Location source = move->GetSource();
4188 Location destination = move->GetDestination();
4189
4190 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004191 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004192 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004193 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004194 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004196 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4197 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004198 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4199 // Use XOR Swap algorithm to avoid a temporary.
4200 DCHECK_NE(source.reg(), destination.reg());
4201 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4202 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4203 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4204 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4205 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4206 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4207 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004208 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4209 // Take advantage of the 16 bytes in the XMM register.
4210 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4211 Address stack(ESP, destination.GetStackIndex());
4212 // Load the double into the high doubleword.
4213 __ movhpd(reg, stack);
4214
4215 // Store the low double into the destination.
4216 __ movsd(stack, reg);
4217
4218 // Move the high double to the low double.
4219 __ psrldq(reg, Immediate(8));
4220 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4221 // Take advantage of the 16 bytes in the XMM register.
4222 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4223 Address stack(ESP, source.GetStackIndex());
4224 // Load the double into the high doubleword.
4225 __ movhpd(reg, stack);
4226
4227 // Store the low double into the destination.
4228 __ movsd(stack, reg);
4229
4230 // Move the high double to the low double.
4231 __ psrldq(reg, Immediate(8));
4232 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4233 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4234 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004235 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004236 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004237 }
4238}
4239
4240void ParallelMoveResolverX86::SpillScratch(int reg) {
4241 __ pushl(static_cast<Register>(reg));
4242}
4243
4244void ParallelMoveResolverX86::RestoreScratch(int reg) {
4245 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004246}
4247
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004248void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004249 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4250 ? LocationSummary::kCallOnSlowPath
4251 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004252 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004253 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004254 locations->SetOut(Location::RequiresRegister());
4255}
4256
4257void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004258 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004259 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004260 DCHECK(!cls->CanCallRuntime());
4261 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004262 codegen_->LoadCurrentMethod(out);
4263 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4264 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004265 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004266 codegen_->LoadCurrentMethod(out);
4267 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4268 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004269
4270 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4271 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4272 codegen_->AddSlowPath(slow_path);
4273 __ testl(out, out);
4274 __ j(kEqual, slow_path->GetEntryLabel());
4275 if (cls->MustGenerateClinitCheck()) {
4276 GenerateClassInitializationCheck(slow_path, out);
4277 } else {
4278 __ Bind(slow_path->GetExitLabel());
4279 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280 }
4281}
4282
4283void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4284 LocationSummary* locations =
4285 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4286 locations->SetInAt(0, Location::RequiresRegister());
4287 if (check->HasUses()) {
4288 locations->SetOut(Location::SameAsFirstInput());
4289 }
4290}
4291
4292void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004293 // We assume the class to not be null.
4294 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4295 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004296 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004297 GenerateClassInitializationCheck(slow_path,
4298 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004299}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004300
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004301void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4302 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004303 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4304 Immediate(mirror::Class::kStatusInitialized));
4305 __ j(kLess, slow_path->GetEntryLabel());
4306 __ Bind(slow_path->GetExitLabel());
4307 // No need for memory fence, thanks to the X86 memory model.
4308}
4309
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004310void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4311 LocationSummary* locations =
4312 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4313 locations->SetOut(Location::RequiresRegister());
4314}
4315
4316void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4317 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4318 codegen_->AddSlowPath(slow_path);
4319
Roland Levillain271ab9c2014-11-27 15:23:57 +00004320 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004321 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004322 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4323 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004324 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4325 __ testl(out, out);
4326 __ j(kEqual, slow_path->GetEntryLabel());
4327 __ Bind(slow_path->GetExitLabel());
4328}
4329
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004330void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4331 LocationSummary* locations =
4332 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4333 locations->SetOut(Location::RequiresRegister());
4334}
4335
4336void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4337 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004338 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004339 __ fs()->movl(address, Immediate(0));
4340}
4341
4342void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4343 LocationSummary* locations =
4344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4345 InvokeRuntimeCallingConvention calling_convention;
4346 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4347}
4348
4349void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4350 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4351 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4352}
4353
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004354void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004355 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4356 ? LocationSummary::kNoCall
4357 : LocationSummary::kCallOnSlowPath;
4358 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4359 locations->SetInAt(0, Location::RequiresRegister());
4360 locations->SetInAt(1, Location::Any());
4361 locations->SetOut(Location::RequiresRegister());
4362}
4363
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004364void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004365 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004366 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004367 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004369 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4370 Label done, zero;
4371 SlowPathCodeX86* slow_path = nullptr;
4372
4373 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004374 // Avoid null check if we know obj is not null.
4375 if (instruction->MustDoNullCheck()) {
4376 __ testl(obj, obj);
4377 __ j(kEqual, &zero);
4378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004379 __ movl(out, Address(obj, class_offset));
4380 // Compare the class of `obj` with `cls`.
4381 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004383 } else {
4384 DCHECK(cls.IsStackSlot()) << cls;
4385 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4386 }
4387
4388 if (instruction->IsClassFinal()) {
4389 // Classes must be equal for the instanceof to succeed.
4390 __ j(kNotEqual, &zero);
4391 __ movl(out, Immediate(1));
4392 __ jmp(&done);
4393 } else {
4394 // If the classes are not equal, we go into a slow path.
4395 DCHECK(locations->OnlyCallsOnSlowPath());
4396 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004397 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004398 codegen_->AddSlowPath(slow_path);
4399 __ j(kNotEqual, slow_path->GetEntryLabel());
4400 __ movl(out, Immediate(1));
4401 __ jmp(&done);
4402 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004403
4404 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4405 __ Bind(&zero);
4406 __ movl(out, Immediate(0));
4407 }
4408
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 if (slow_path != nullptr) {
4410 __ Bind(slow_path->GetExitLabel());
4411 }
4412 __ Bind(&done);
4413}
4414
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004415void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4416 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4417 instruction, LocationSummary::kCallOnSlowPath);
4418 locations->SetInAt(0, Location::RequiresRegister());
4419 locations->SetInAt(1, Location::Any());
4420 locations->AddTemp(Location::RequiresRegister());
4421}
4422
4423void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4424 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004425 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004426 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4429 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4430 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4431 codegen_->AddSlowPath(slow_path);
4432
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004433 // Avoid null check if we know obj is not null.
4434 if (instruction->MustDoNullCheck()) {
4435 __ testl(obj, obj);
4436 __ j(kEqual, slow_path->GetExitLabel());
4437 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004438
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004439 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004440 // Compare the class of `obj` with `cls`.
4441 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004443 } else {
4444 DCHECK(cls.IsStackSlot()) << cls;
4445 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4446 }
4447
4448 __ j(kNotEqual, slow_path->GetEntryLabel());
4449 __ Bind(slow_path->GetExitLabel());
4450}
4451
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004452void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4453 LocationSummary* locations =
4454 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4455 InvokeRuntimeCallingConvention calling_convention;
4456 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4457}
4458
4459void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4460 __ fs()->call(Address::Absolute(instruction->IsEnter()
4461 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4462 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4463 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4464}
4465
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004466void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4467void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4468void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4469
4470void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4471 LocationSummary* locations =
4472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4473 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4474 || instruction->GetResultType() == Primitive::kPrimLong);
4475 locations->SetInAt(0, Location::RequiresRegister());
4476 locations->SetInAt(1, Location::Any());
4477 locations->SetOut(Location::SameAsFirstInput());
4478}
4479
4480void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4481 HandleBitwiseOperation(instruction);
4482}
4483
4484void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4485 HandleBitwiseOperation(instruction);
4486}
4487
4488void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4489 HandleBitwiseOperation(instruction);
4490}
4491
4492void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4493 LocationSummary* locations = instruction->GetLocations();
4494 Location first = locations->InAt(0);
4495 Location second = locations->InAt(1);
4496 DCHECK(first.Equals(locations->Out()));
4497
4498 if (instruction->GetResultType() == Primitive::kPrimInt) {
4499 if (second.IsRegister()) {
4500 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004502 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004504 } else {
4505 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004506 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004507 }
4508 } else if (second.IsConstant()) {
4509 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004510 __ andl(first.AsRegister<Register>(),
4511 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004512 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004513 __ orl(first.AsRegister<Register>(),
4514 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004515 } else {
4516 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004517 __ xorl(first.AsRegister<Register>(),
4518 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004519 }
4520 } else {
4521 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004522 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004523 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004524 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004525 } else {
4526 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004527 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004528 }
4529 }
4530 } else {
4531 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4532 if (second.IsRegisterPair()) {
4533 if (instruction->IsAnd()) {
4534 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4535 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4536 } else if (instruction->IsOr()) {
4537 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4538 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4539 } else {
4540 DCHECK(instruction->IsXor());
4541 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4542 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4543 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004544 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004545 if (instruction->IsAnd()) {
4546 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4547 __ andl(first.AsRegisterPairHigh<Register>(),
4548 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4549 } else if (instruction->IsOr()) {
4550 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4551 __ orl(first.AsRegisterPairHigh<Register>(),
4552 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4553 } else {
4554 DCHECK(instruction->IsXor());
4555 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4556 __ xorl(first.AsRegisterPairHigh<Register>(),
4557 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4558 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004559 } else {
4560 DCHECK(second.IsConstant()) << second;
4561 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004562 int32_t low_value = Low32Bits(value);
4563 int32_t high_value = High32Bits(value);
4564 Immediate low(low_value);
4565 Immediate high(high_value);
4566 Register first_low = first.AsRegisterPairLow<Register>();
4567 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004568 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004569 if (low_value == 0) {
4570 __ xorl(first_low, first_low);
4571 } else if (low_value != -1) {
4572 __ andl(first_low, low);
4573 }
4574 if (high_value == 0) {
4575 __ xorl(first_high, first_high);
4576 } else if (high_value != -1) {
4577 __ andl(first_high, high);
4578 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004579 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004580 if (low_value != 0) {
4581 __ orl(first_low, low);
4582 }
4583 if (high_value != 0) {
4584 __ orl(first_high, high);
4585 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004586 } else {
4587 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004588 if (low_value != 0) {
4589 __ xorl(first_low, low);
4590 }
4591 if (high_value != 0) {
4592 __ xorl(first_high, high);
4593 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004594 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004595 }
4596 }
4597}
4598
Calin Juravleb1498f62015-02-16 13:13:29 +00004599void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4600 // Nothing to do, this should be removed during prepare for register allocator.
4601 UNUSED(instruction);
4602 LOG(FATAL) << "Unreachable";
4603}
4604
4605void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4606 // Nothing to do, this should be removed during prepare for register allocator.
4607 UNUSED(instruction);
4608 LOG(FATAL) << "Unreachable";
4609}
4610
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004611} // namespace x86
4612} // namespace art