blob: f0c14f67009aaf9ee54d040c3611d16660f0cade [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
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
555 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: {
585 uint32_t index = fp_index_++;
586 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: {
595 uint32_t index = fp_index_++;
596 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 Levillain4c0eb422015-04-24 16:43:49 +01001197 // Explicit clinit checks triggered by static invokes must have been
Roland Levillain0379f822015-04-24 23:01:24 +01001198 // pruned by art::PrepareForRegisterAllocation, but this step is not
1199 // run in baseline. So we remove them manually here if we find them.
1200 // TODO: Instead of this local workaround, address this properly.
1201 if (invoke->IsStaticWithExplicitClinitCheck()) {
1202 invoke->RemoveClinitCheckOrLoadClassAsLastInput();
1203 }
Roland Levillain4c0eb422015-04-24 16:43:49 +01001204
Mark Mendellfb8d2792015-03-31 22:16:59 -04001205 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001206 if (intrinsic.TryDispatch(invoke)) {
1207 return;
1208 }
1209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 HandleInvoke(invoke);
1211}
1212
Mark Mendell09ed1a32015-03-25 08:30:06 -04001213static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1214 if (invoke->GetLocations()->Intrinsified()) {
1215 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1216 intrinsic.Dispatch(invoke);
1217 return true;
1218 }
1219 return false;
1220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001223 // Explicit clinit checks triggered by static invokes must have been
1224 // pruned by art::PrepareForRegisterAllocation.
1225 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1228 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001229 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230
Mark Mendell09ed1a32015-03-25 08:30:06 -04001231 codegen_->GenerateStaticOrDirectCall(
1232 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001233 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234}
1235
1236void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1237 HandleInvoke(invoke);
1238}
1239
1240void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001243 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001244
1245 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001247 HInstruction* input = invoke->InputAt(i);
1248 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1249 }
1250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 switch (invoke->GetType()) {
1252 case Primitive::kPrimBoolean:
1253 case Primitive::kPrimByte:
1254 case Primitive::kPrimChar:
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001262 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 break;
1264
1265 case Primitive::kPrimVoid:
1266 break;
1267
1268 case Primitive::kPrimDouble:
1269 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001270 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 break;
1272 }
1273
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001274 invoke->SetLocations(locations);
1275}
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1280 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
1286 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
1288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 // temp = temp->GetMethodAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001295 __ call(Address(
1296 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001300}
1301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1303 HandleInvoke(invoke);
1304 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001305 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001306}
1307
1308void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001311 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1312 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1313 LocationSummary* locations = invoke->GetLocations();
1314 Location receiver = locations->InAt(0);
1315 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1316
1317 // Set the hidden argument.
1318 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
1323 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
1325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 // temp = temp->GetImtEntryAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001332 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001334
1335 DCHECK(!codegen_->IsLeafMethod());
1336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1337}
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001344 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 break;
1348
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001350 locations->SetInAt(0, Location::RequiresFpuRegister());
1351 locations->SetOut(Location::SameAsFirstInput());
1352 locations->AddTemp(Location::RequiresRegister());
1353 locations->AddTemp(Location::RequiresFpuRegister());
1354 break;
1355
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001357 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001358 locations->SetOut(Location::SameAsFirstInput());
1359 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1364 }
1365}
1366
1367void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1368 LocationSummary* locations = neg->GetLocations();
1369 Location out = locations->Out();
1370 Location in = locations->InAt(0);
1371 switch (neg->GetResultType()) {
1372 case Primitive::kPrimInt:
1373 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001374 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001376 break;
1377
1378 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001379 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 __ negl(out.AsRegisterPairLow<Register>());
1382 // Negation is similar to subtraction from zero. The least
1383 // significant byte triggers a borrow when it is different from
1384 // zero; to take it into account, add 1 to the most significant
1385 // byte if the carry flag (CF) is set to 1 after the first NEGL
1386 // operation.
1387 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1388 __ negl(out.AsRegisterPairHigh<Register>());
1389 break;
1390
Roland Levillain5368c212014-11-27 15:03:41 +00001391 case Primitive::kPrimFloat: {
1392 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 Register constant = locations->GetTemp(0).AsRegister<Register>();
1394 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001395 // Implement float negation with an exclusive or with value
1396 // 0x80000000 (mask for bit 31, representing the sign of a
1397 // single-precision floating-point number).
1398 __ movl(constant, Immediate(INT32_C(0x80000000)));
1399 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001402 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403
Roland Levillain5368c212014-11-27 15:03:41 +00001404 case Primitive::kPrimDouble: {
1405 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001407 // Implement double negation with an exclusive or with value
1408 // 0x8000000000000000 (mask for bit 63, representing the sign of
1409 // a double-precision floating-point number).
1410 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001411 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001412 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001413 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001414
1415 default:
1416 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1417 }
1418}
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001421 Primitive::Type result_type = conversion->GetResultType();
1422 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001423 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001424
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001425 // The float-to-long and double-to-long type conversions rely on a
1426 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001427 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1429 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001430 ? LocationSummary::kCall
1431 : LocationSummary::kNoCall;
1432 LocationSummary* locations =
1433 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1434
David Brazdilb2bd1c52015-03-25 11:17:37 +00001435 // The Java language does not allow treating boolean as an integral type but
1436 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001437
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimByte:
1440 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001441 case Primitive::kPrimBoolean:
1442 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1448 // Make the output overlap to please the register allocator. This greatly simplifies
1449 // the validation of the linear scan implementation
1450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unexpected type conversion from " << input_type
1455 << " to " << result_type;
1456 }
1457 break;
1458
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimShort:
1460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001461 case Primitive::kPrimBoolean:
1462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001463 case Primitive::kPrimByte:
1464 case Primitive::kPrimInt:
1465 case Primitive::kPrimChar:
1466 // Processing a Dex `int-to-short' instruction.
1467 locations->SetInAt(0, Location::Any());
1468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillain946e1432014-11-11 17:35:19 +00001477 case Primitive::kPrimInt:
1478 switch (input_type) {
1479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001481 locations->SetInAt(0, Location::Any());
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 break;
1484
1485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001486 // Processing a Dex `float-to-int' instruction.
1487 locations->SetInAt(0, Location::RequiresFpuRegister());
1488 locations->SetOut(Location::RequiresRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 break;
1491
Roland Levillain946e1432014-11-11 17:35:19 +00001492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001493 // Processing a Dex `double-to-int' instruction.
1494 locations->SetInAt(0, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::RequiresRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503 break;
1504
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimLong:
1506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001507 case Primitive::kPrimBoolean:
1508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 case Primitive::kPrimByte:
1510 case Primitive::kPrimShort:
1511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 locations->SetInAt(0, Location::RegisterLocation(EAX));
1515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1516 break;
1517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1524
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 // The runtime helper puts the result in EAX, EDX.
1526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
1534 break;
1535
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimChar:
1537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001538 case Primitive::kPrimBoolean:
1539 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001540 case Primitive::kPrimByte:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001543 // Processing a Dex `int-to-char' instruction.
1544 locations->SetInAt(0, Location::Any());
1545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1546 break;
1547
1548 default:
1549 LOG(FATAL) << "Unexpected type conversion from " << input_type
1550 << " to " << result_type;
1551 }
1552 break;
1553
Roland Levillaindff1f282014-11-05 14:15:05 +00001554 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001555 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001556 case Primitive::kPrimBoolean:
1557 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001558 case Primitive::kPrimByte:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimChar:
1562 // Processing a Dex `int-to-float' instruction.
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetOut(Location::RequiresFpuRegister());
1565 break;
1566
1567 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001568 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001569 locations->SetInAt(0, Location::Any());
1570 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001571 break;
1572
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001574 // Processing a Dex `double-to-float' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 };
1583 break;
1584
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001587 case Primitive::kPrimBoolean:
1588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimByte:
1590 case Primitive::kPrimShort:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-double' instruction.
1594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetOut(Location::RequiresFpuRegister());
1596 break;
1597
1598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001600 locations->SetInAt(0, Location::Any());
1601 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001602 break;
1603
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001605 // Processing a Dex `float-to-double' instruction.
1606 locations->SetInAt(0, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620}
1621
1622void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1623 LocationSummary* locations = conversion->GetLocations();
1624 Location out = locations->Out();
1625 Location in = locations->InAt(0);
1626 Primitive::Type result_type = conversion->GetResultType();
1627 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001628 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimByte:
1631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001632 case Primitive::kPrimBoolean:
1633 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001637 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001638 if (in.IsRegister()) {
1639 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 } else {
1641 DCHECK(in.GetConstant()->IsIntConstant());
1642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1644 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001645 break;
1646
1647 default:
1648 LOG(FATAL) << "Unexpected type conversion from " << input_type
1649 << " to " << result_type;
1650 }
1651 break;
1652
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimShort:
1654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001655 case Primitive::kPrimBoolean:
1656 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001657 case Primitive::kPrimByte:
1658 case Primitive::kPrimInt:
1659 case Primitive::kPrimChar:
1660 // Processing a Dex `int-to-short' instruction.
1661 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else {
1666 DCHECK(in.GetConstant()->IsIntConstant());
1667 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001669 }
1670 break;
1671
1672 default:
1673 LOG(FATAL) << "Unexpected type conversion from " << input_type
1674 << " to " << result_type;
1675 }
1676 break;
1677
Roland Levillain946e1432014-11-11 17:35:19 +00001678 case Primitive::kPrimInt:
1679 switch (input_type) {
1680 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001681 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001682 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001684 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else {
1687 DCHECK(in.IsConstant());
1688 DCHECK(in.GetConstant()->IsLongConstant());
1689 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001691 }
1692 break;
1693
Roland Levillain3f8f9362014-12-02 17:45:01 +00001694 case Primitive::kPrimFloat: {
1695 // Processing a Dex `float-to-int' instruction.
1696 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1697 Register output = out.AsRegister<Register>();
1698 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1699 Label done, nan;
1700
1701 __ movl(output, Immediate(kPrimIntMax));
1702 // temp = int-to-float(output)
1703 __ cvtsi2ss(temp, output);
1704 // if input >= temp goto done
1705 __ comiss(input, temp);
1706 __ j(kAboveEqual, &done);
1707 // if input == NaN goto nan
1708 __ j(kUnordered, &nan);
1709 // output = float-to-int-truncate(input)
1710 __ cvttss2si(output, input);
1711 __ jmp(&done);
1712 __ Bind(&nan);
1713 // output = 0
1714 __ xorl(output, output);
1715 __ Bind(&done);
1716 break;
1717 }
1718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 case Primitive::kPrimDouble: {
1720 // Processing a Dex `double-to-int' instruction.
1721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1722 Register output = out.AsRegister<Register>();
1723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1724 Label done, nan;
1725
1726 __ movl(output, Immediate(kPrimIntMax));
1727 // temp = int-to-double(output)
1728 __ cvtsi2sd(temp, output);
1729 // if input >= temp goto done
1730 __ comisd(input, temp);
1731 __ j(kAboveEqual, &done);
1732 // if input == NaN goto nan
1733 __ j(kUnordered, &nan);
1734 // output = double-to-int-truncate(input)
1735 __ cvttsd2si(output, input);
1736 __ jmp(&done);
1737 __ Bind(&nan);
1738 // output = 0
1739 __ xorl(output, output);
1740 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001741 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001742 }
Roland Levillain946e1432014-11-11 17:35:19 +00001743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimLong:
1751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001752 case Primitive::kPrimBoolean:
1753 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Primitive::kPrimByte:
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001757 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001758 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001759 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1760 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 __ cdq();
1763 break;
1764
1765 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001766 // Processing a Dex `float-to-long' instruction.
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001768 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1769 break;
1770
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001772 // Processing a Dex `double-to-long' instruction.
1773 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1774 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 break;
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type conversion from " << input_type
1779 << " to " << result_type;
1780 }
1781 break;
1782
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimChar:
1784 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001785 case Primitive::kPrimBoolean:
1786 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimByte:
1788 case Primitive::kPrimShort:
1789 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1791 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001792 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001793 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else {
1796 DCHECK(in.GetConstant()->IsIntConstant());
1797 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001799 }
1800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
Roland Levillain6d0e4832014-11-27 18:31:21 +00001820 case Primitive::kPrimLong: {
1821 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001822 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001823
Roland Levillain232ade02015-04-20 15:14:36 +01001824 // Create stack space for the call to
1825 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1826 // TODO: enhance register allocator to ask for stack temporaries.
1827 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1828 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1829 __ subl(ESP, Immediate(adjustment));
1830 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831
Roland Levillain232ade02015-04-20 15:14:36 +01001832 // Load the value to the FP stack, using temporaries if needed.
1833 PushOntoFPStack(in, 0, adjustment, false, true);
1834
1835 if (out.IsStackSlot()) {
1836 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1837 } else {
1838 __ fstps(Address(ESP, 0));
1839 Location stack_temp = Location::StackSlot(0);
1840 codegen_->Move32(out, stack_temp);
1841 }
1842
1843 // Remove the temporary stack space we allocated.
1844 if (adjustment != 0) {
1845 __ addl(ESP, Immediate(adjustment));
1846 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 break;
1848 }
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 };
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001869 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001871 break;
1872
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 case Primitive::kPrimLong: {
1874 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001875 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876
Roland Levillain232ade02015-04-20 15:14:36 +01001877 // Create stack space for the call to
1878 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1879 // TODO: enhance register allocator to ask for stack temporaries.
1880 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1881 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1882 __ subl(ESP, Immediate(adjustment));
1883 }
1884
1885 // Load the value to the FP stack, using temporaries if needed.
1886 PushOntoFPStack(in, 0, adjustment, false, true);
1887
1888 if (out.IsDoubleStackSlot()) {
1889 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1890 } else {
1891 __ fstpl(Address(ESP, 0));
1892 Location stack_temp = Location::DoubleStackSlot(0);
1893 codegen_->Move64(out, stack_temp);
1894 }
1895
1896 // Remove the temporary stack space we allocated.
1897 if (adjustment != 0) {
1898 __ addl(ESP, Immediate(adjustment));
1899 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001900 break;
1901 }
1902
Roland Levillaincff13742014-11-17 14:32:17 +00001903 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001904 // Processing a Dex `float-to-double' instruction.
1905 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918}
1919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001923 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001924 case Primitive::kPrimInt: {
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1928 break;
1929 }
1930
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetInAt(1, Location::Any());
1934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936 }
1937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 case Primitive::kPrimFloat:
1939 case Primitive::kPrimDouble: {
1940 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001941 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1948 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950}
1951
1952void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1953 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 Location first = locations->InAt(0);
1955 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001956 Location out = locations->Out();
1957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001958 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1962 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1963 } else {
1964 __ leal(out.AsRegister<Register>(), Address(
1965 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1966 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001968 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1969 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1970 __ addl(out.AsRegister<Register>(), Immediate(value));
1971 } else {
1972 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1973 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001975 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001976 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001977 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001978 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001979 }
1980
1981 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001982 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1984 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001985 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1987 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001989 } else {
1990 DCHECK(second.IsConstant()) << second;
1991 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1992 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1993 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001994 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001995 break;
1996 }
1997
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 case Primitive::kPrimFloat: {
1999 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002002 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 }
2004
2005 case Primitive::kPrimDouble: {
2006 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002008 }
2009 break;
2010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002013 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 }
2015}
2016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002020 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002021 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::Any());
2025 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026 break;
2027 }
Calin Juravle11351682014-10-23 15:38:15 +01002028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble: {
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 break;
Calin Juravle11351682014-10-23 15:38:15 +01002034 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036 default:
Calin Juravle11351682014-10-23 15:38:15 +01002037 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002039}
2040
2041void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2042 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002043 Location first = locations->InAt(0);
2044 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002045 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002051 __ subl(first.AsRegister<Register>(),
2052 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002055 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002056 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 }
2058
2059 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002060 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002061 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2062 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002063 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002064 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 __ sbbl(first.AsRegisterPairHigh<Register>(),
2066 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002067 } else {
2068 DCHECK(second.IsConstant()) << second;
2069 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2070 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2071 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002073 break;
2074 }
2075
Calin Juravle11351682014-10-23 15:38:15 +01002076 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 break;
Calin Juravle11351682014-10-23 15:38:15 +01002079 }
2080
2081 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002083 break;
2084 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002086 default:
Calin Juravle11351682014-10-23 15:38:15 +01002087 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002088 }
2089}
2090
Calin Juravle34bacdf2014-10-07 20:23:36 +01002091void LocationsBuilderX86::VisitMul(HMul* mul) {
2092 LocationSummary* locations =
2093 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2094 switch (mul->GetResultType()) {
2095 case Primitive::kPrimInt:
2096 locations->SetInAt(0, Location::RequiresRegister());
2097 locations->SetInAt(1, Location::Any());
2098 locations->SetOut(Location::SameAsFirstInput());
2099 break;
2100 case Primitive::kPrimLong: {
2101 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102 locations->SetInAt(1, Location::Any());
2103 locations->SetOut(Location::SameAsFirstInput());
2104 // Needed for imul on 32bits with 64bits output.
2105 locations->AddTemp(Location::RegisterLocation(EAX));
2106 locations->AddTemp(Location::RegisterLocation(EDX));
2107 break;
2108 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002109 case Primitive::kPrimFloat:
2110 case Primitive::kPrimDouble: {
2111 locations->SetInAt(0, Location::RequiresFpuRegister());
2112 locations->SetInAt(1, Location::RequiresFpuRegister());
2113 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116
2117 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 }
2120}
2121
2122void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2123 LocationSummary* locations = mul->GetLocations();
2124 Location first = locations->InAt(0);
2125 Location second = locations->InAt(1);
2126 DCHECK(first.Equals(locations->Out()));
2127
2128 switch (mul->GetResultType()) {
2129 case Primitive::kPrimInt: {
2130 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 } else if (second.IsConstant()) {
2133 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 } else {
2136 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 }
2139 break;
2140 }
2141
2142 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143 Register in1_hi = first.AsRegisterPairHigh<Register>();
2144 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 Register eax = locations->GetTemp(0).AsRegister<Register>();
2146 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147
2148 DCHECK_EQ(EAX, eax);
2149 DCHECK_EQ(EDX, edx);
2150
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002151 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002152 // output: in1
2153 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2154 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2155 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002156 if (second.IsConstant()) {
2157 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002158
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002159 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2160 int32_t low_value = Low32Bits(value);
2161 int32_t high_value = High32Bits(value);
2162 Immediate low(low_value);
2163 Immediate high(high_value);
2164
2165 __ movl(eax, high);
2166 // eax <- in1.lo * in2.hi
2167 __ imull(eax, in1_lo);
2168 // in1.hi <- in1.hi * in2.lo
2169 __ imull(in1_hi, low);
2170 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2171 __ addl(in1_hi, eax);
2172 // move in2_lo to eax to prepare for double precision
2173 __ movl(eax, low);
2174 // edx:eax <- in1.lo * in2.lo
2175 __ mull(in1_lo);
2176 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2177 __ addl(in1_hi, edx);
2178 // in1.lo <- (in1.lo * in2.lo)[31:0];
2179 __ movl(in1_lo, eax);
2180 } else if (second.IsRegisterPair()) {
2181 Register in2_hi = second.AsRegisterPairHigh<Register>();
2182 Register in2_lo = second.AsRegisterPairLow<Register>();
2183
2184 __ movl(eax, in2_hi);
2185 // eax <- in1.lo * in2.hi
2186 __ imull(eax, in1_lo);
2187 // in1.hi <- in1.hi * in2.lo
2188 __ imull(in1_hi, in2_lo);
2189 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2190 __ addl(in1_hi, eax);
2191 // move in1_lo to eax to prepare for double precision
2192 __ movl(eax, in1_lo);
2193 // edx:eax <- in1.lo * in2.lo
2194 __ mull(in2_lo);
2195 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2196 __ addl(in1_hi, edx);
2197 // in1.lo <- (in1.lo * in2.lo)[31:0];
2198 __ movl(in1_lo, eax);
2199 } else {
2200 DCHECK(second.IsDoubleStackSlot()) << second;
2201 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2202 Address in2_lo(ESP, second.GetStackIndex());
2203
2204 __ movl(eax, in2_hi);
2205 // eax <- in1.lo * in2.hi
2206 __ imull(eax, in1_lo);
2207 // in1.hi <- in1.hi * in2.lo
2208 __ imull(in1_hi, in2_lo);
2209 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2210 __ addl(in1_hi, eax);
2211 // move in1_lo to eax to prepare for double precision
2212 __ movl(eax, in1_lo);
2213 // edx:eax <- in1.lo * in2.lo
2214 __ mull(in2_lo);
2215 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2216 __ addl(in1_hi, edx);
2217 // in1.lo <- (in1.lo * in2.lo)[31:0];
2218 __ movl(in1_lo, eax);
2219 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002220
2221 break;
2222 }
2223
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002225 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002226 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 }
2228
2229 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 break;
2232 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233
2234 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002236 }
2237}
2238
Roland Levillain232ade02015-04-20 15:14:36 +01002239void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2240 uint32_t temp_offset,
2241 uint32_t stack_adjustment,
2242 bool is_fp,
2243 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002244 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002245 DCHECK(!is_wide);
2246 if (is_fp) {
2247 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2248 } else {
2249 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2250 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002251 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002252 DCHECK(is_wide);
2253 if (is_fp) {
2254 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2255 } else {
2256 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2257 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 } else {
2259 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002260 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 Location stack_temp = Location::StackSlot(temp_offset);
2262 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002263 if (is_fp) {
2264 __ flds(Address(ESP, temp_offset));
2265 } else {
2266 __ filds(Address(ESP, temp_offset));
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 } else {
2269 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2270 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002271 if (is_fp) {
2272 __ fldl(Address(ESP, temp_offset));
2273 } else {
2274 __ fildl(Address(ESP, temp_offset));
2275 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002276 }
2277 }
2278}
2279
2280void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2281 Primitive::Type type = rem->GetResultType();
2282 bool is_float = type == Primitive::kPrimFloat;
2283 size_t elem_size = Primitive::ComponentSize(type);
2284 LocationSummary* locations = rem->GetLocations();
2285 Location first = locations->InAt(0);
2286 Location second = locations->InAt(1);
2287 Location out = locations->Out();
2288
2289 // Create stack space for 2 elements.
2290 // TODO: enhance register allocator to ask for stack temporaries.
2291 __ subl(ESP, Immediate(2 * elem_size));
2292
2293 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 const bool is_wide = !is_float;
2295 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2296 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002297
2298 // Loop doing FPREM until we stabilize.
2299 Label retry;
2300 __ Bind(&retry);
2301 __ fprem();
2302
2303 // Move FP status to AX.
2304 __ fstsw();
2305
2306 // And see if the argument reduction is complete. This is signaled by the
2307 // C2 FPU flag bit set to 0.
2308 __ andl(EAX, Immediate(kC2ConditionMask));
2309 __ j(kNotEqual, &retry);
2310
2311 // We have settled on the final value. Retrieve it into an XMM register.
2312 // Store FP top of stack to real stack.
2313 if (is_float) {
2314 __ fsts(Address(ESP, 0));
2315 } else {
2316 __ fstl(Address(ESP, 0));
2317 }
2318
2319 // Pop the 2 items from the FP stack.
2320 __ fucompp();
2321
2322 // Load the value from the stack into an XMM register.
2323 DCHECK(out.IsFpuRegister()) << out;
2324 if (is_float) {
2325 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2326 } else {
2327 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2328 }
2329
2330 // And remove the temporary stack space we allocated.
2331 __ addl(ESP, Immediate(2 * elem_size));
2332}
2333
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002334
2335void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2336 DCHECK(instruction->IsDiv() || instruction->IsRem());
2337
2338 LocationSummary* locations = instruction->GetLocations();
2339 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002340 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002341
2342 Register out_register = locations->Out().AsRegister<Register>();
2343 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002344 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002345
2346 DCHECK(imm == 1 || imm == -1);
2347
2348 if (instruction->IsRem()) {
2349 __ xorl(out_register, out_register);
2350 } else {
2351 __ movl(out_register, input_register);
2352 if (imm == -1) {
2353 __ negl(out_register);
2354 }
2355 }
2356}
2357
2358
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002359void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002360 LocationSummary* locations = instruction->GetLocations();
2361
2362 Register out_register = locations->Out().AsRegister<Register>();
2363 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002364 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002365
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002366 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002367 Register num = locations->GetTemp(0).AsRegister<Register>();
2368
2369 __ leal(num, Address(input_register, std::abs(imm) - 1));
2370 __ testl(input_register, input_register);
2371 __ cmovl(kGreaterEqual, num, input_register);
2372 int shift = CTZ(imm);
2373 __ sarl(num, Immediate(shift));
2374
2375 if (imm < 0) {
2376 __ negl(num);
2377 }
2378
2379 __ movl(out_register, num);
2380}
2381
2382void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2383 DCHECK(instruction->IsDiv() || instruction->IsRem());
2384
2385 LocationSummary* locations = instruction->GetLocations();
2386 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2387
2388 Register eax = locations->InAt(0).AsRegister<Register>();
2389 Register out = locations->Out().AsRegister<Register>();
2390 Register num;
2391 Register edx;
2392
2393 if (instruction->IsDiv()) {
2394 edx = locations->GetTemp(0).AsRegister<Register>();
2395 num = locations->GetTemp(1).AsRegister<Register>();
2396 } else {
2397 edx = locations->Out().AsRegister<Register>();
2398 num = locations->GetTemp(0).AsRegister<Register>();
2399 }
2400
2401 DCHECK_EQ(EAX, eax);
2402 DCHECK_EQ(EDX, edx);
2403 if (instruction->IsDiv()) {
2404 DCHECK_EQ(EAX, out);
2405 } else {
2406 DCHECK_EQ(EDX, out);
2407 }
2408
2409 int64_t magic;
2410 int shift;
2411 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2412
2413 Label ndiv;
2414 Label end;
2415 // If numerator is 0, the result is 0, no computation needed.
2416 __ testl(eax, eax);
2417 __ j(kNotEqual, &ndiv);
2418
2419 __ xorl(out, out);
2420 __ jmp(&end);
2421
2422 __ Bind(&ndiv);
2423
2424 // Save the numerator.
2425 __ movl(num, eax);
2426
2427 // EAX = magic
2428 __ movl(eax, Immediate(magic));
2429
2430 // EDX:EAX = magic * numerator
2431 __ imull(num);
2432
2433 if (imm > 0 && magic < 0) {
2434 // EDX += num
2435 __ addl(edx, num);
2436 } else if (imm < 0 && magic > 0) {
2437 __ subl(edx, num);
2438 }
2439
2440 // Shift if needed.
2441 if (shift != 0) {
2442 __ sarl(edx, Immediate(shift));
2443 }
2444
2445 // EDX += 1 if EDX < 0
2446 __ movl(eax, edx);
2447 __ shrl(edx, Immediate(31));
2448 __ addl(edx, eax);
2449
2450 if (instruction->IsRem()) {
2451 __ movl(eax, num);
2452 __ imull(edx, Immediate(imm));
2453 __ subl(eax, edx);
2454 __ movl(edx, eax);
2455 } else {
2456 __ movl(eax, edx);
2457 }
2458 __ Bind(&end);
2459}
2460
Calin Juravlebacfec32014-11-14 15:54:36 +00002461void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2462 DCHECK(instruction->IsDiv() || instruction->IsRem());
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location out = locations->Out();
2466 Location first = locations->InAt(0);
2467 Location second = locations->InAt(1);
2468 bool is_div = instruction->IsDiv();
2469
2470 switch (instruction->GetResultType()) {
2471 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 DCHECK_EQ(EAX, first.AsRegister<Register>());
2473 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002474
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002475 if (instruction->InputAt(1)->IsIntConstant()) {
2476 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477
2478 if (imm == 0) {
2479 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2480 } else if (imm == 1 || imm == -1) {
2481 DivRemOneOrMinusOne(instruction);
2482 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002483 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 } else {
2485 DCHECK(imm <= -2 || imm >= 2);
2486 GenerateDivRemWithAnyConstant(instruction);
2487 }
2488 } else {
2489 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002490 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002491 is_div);
2492 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002493
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494 Register second_reg = second.AsRegister<Register>();
2495 // 0x80000000/-1 triggers an arithmetic exception!
2496 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2497 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002498
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002499 __ cmpl(second_reg, Immediate(-1));
2500 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002501
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002502 // edx:eax <- sign-extended of eax
2503 __ cdq();
2504 // eax = quotient, edx = remainder
2505 __ idivl(second_reg);
2506 __ Bind(slow_path->GetExitLabel());
2507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002508 break;
2509 }
2510
2511 case Primitive::kPrimLong: {
2512 InvokeRuntimeCallingConvention calling_convention;
2513 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2514 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2516 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2517 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2518 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2519
2520 if (is_div) {
2521 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2522 } else {
2523 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2524 }
2525 uint32_t dex_pc = is_div
2526 ? instruction->AsDiv()->GetDexPc()
2527 : instruction->AsRem()->GetDexPc();
2528 codegen_->RecordPcInfo(instruction, dex_pc);
2529
2530 break;
2531 }
2532
2533 default:
2534 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2535 }
2536}
2537
Calin Juravle7c4954d2014-10-28 16:57:40 +00002538void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002539 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002540 ? LocationSummary::kCall
2541 : LocationSummary::kNoCall;
2542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2543
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002545 case Primitive::kPrimInt: {
2546 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002548 locations->SetOut(Location::SameAsFirstInput());
2549 // Intel uses edx:eax as the dividend.
2550 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2552 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2553 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002554 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 locations->AddTemp(Location::RequiresRegister());
2556 }
Calin Juravled0d48522014-11-04 16:40:20 +00002557 break;
2558 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002560 InvokeRuntimeCallingConvention calling_convention;
2561 locations->SetInAt(0, Location::RegisterPairLocation(
2562 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2563 locations->SetInAt(1, Location::RegisterPairLocation(
2564 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2565 // Runtime helper puts the result in EAX, EDX.
2566 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 break;
2568 }
2569 case Primitive::kPrimFloat:
2570 case Primitive::kPrimDouble: {
2571 locations->SetInAt(0, Location::RequiresFpuRegister());
2572 locations->SetInAt(1, Location::RequiresFpuRegister());
2573 locations->SetOut(Location::SameAsFirstInput());
2574 break;
2575 }
2576
2577 default:
2578 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2579 }
2580}
2581
2582void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2583 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002584 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002585 Location first = locations->InAt(0);
2586 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587
2588 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002589 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 break;
2593 }
2594
2595 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 break;
2599 }
2600
2601 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002602 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604 break;
2605 }
2606
2607 default:
2608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2609 }
2610}
2611
Calin Juravlebacfec32014-11-14 15:54:36 +00002612void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002613 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002614
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002615 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2616 ? LocationSummary::kCall
2617 : LocationSummary::kNoCall;
2618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002619
Calin Juravled2ec87d2014-12-08 14:24:46 +00002620 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 case Primitive::kPrimInt: {
2622 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002623 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002624 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2626 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2627 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002628 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002629 locations->AddTemp(Location::RequiresRegister());
2630 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002631 break;
2632 }
2633 case Primitive::kPrimLong: {
2634 InvokeRuntimeCallingConvention calling_convention;
2635 locations->SetInAt(0, Location::RegisterPairLocation(
2636 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2637 locations->SetInAt(1, Location::RegisterPairLocation(
2638 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2639 // Runtime helper puts the result in EAX, EDX.
2640 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2641 break;
2642 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002643 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002644 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 locations->SetInAt(0, Location::Any());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::RequiresFpuRegister());
2648 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002649 break;
2650 }
2651
2652 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002653 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002654 }
2655}
2656
2657void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2658 Primitive::Type type = rem->GetResultType();
2659 switch (type) {
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimLong: {
2662 GenerateDivRemIntegral(rem);
2663 break;
2664 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002665 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002666 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002667 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002668 break;
2669 }
2670 default:
2671 LOG(FATAL) << "Unexpected rem type " << type;
2672 }
2673}
2674
Calin Juravled0d48522014-11-04 16:40:20 +00002675void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 LocationSummary* locations =
2677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002678 switch (instruction->GetType()) {
2679 case Primitive::kPrimInt: {
2680 locations->SetInAt(0, Location::Any());
2681 break;
2682 }
2683 case Primitive::kPrimLong: {
2684 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2685 if (!instruction->IsConstant()) {
2686 locations->AddTemp(Location::RequiresRegister());
2687 }
2688 break;
2689 }
2690 default:
2691 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2692 }
Calin Juravled0d48522014-11-04 16:40:20 +00002693 if (instruction->HasUses()) {
2694 locations->SetOut(Location::SameAsFirstInput());
2695 }
2696}
2697
2698void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2699 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2700 codegen_->AddSlowPath(slow_path);
2701
2702 LocationSummary* locations = instruction->GetLocations();
2703 Location value = locations->InAt(0);
2704
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002705 switch (instruction->GetType()) {
2706 case Primitive::kPrimInt: {
2707 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002709 __ j(kEqual, slow_path->GetEntryLabel());
2710 } else if (value.IsStackSlot()) {
2711 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2712 __ j(kEqual, slow_path->GetEntryLabel());
2713 } else {
2714 DCHECK(value.IsConstant()) << value;
2715 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2716 __ jmp(slow_path->GetEntryLabel());
2717 }
2718 }
2719 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002720 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002721 case Primitive::kPrimLong: {
2722 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002724 __ movl(temp, value.AsRegisterPairLow<Register>());
2725 __ orl(temp, value.AsRegisterPairHigh<Register>());
2726 __ j(kEqual, slow_path->GetEntryLabel());
2727 } else {
2728 DCHECK(value.IsConstant()) << value;
2729 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2730 __ jmp(slow_path->GetEntryLabel());
2731 }
2732 }
2733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002737 }
Calin Juravled0d48522014-11-04 16:40:20 +00002738}
2739
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2741 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2742
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2745
2746 switch (op->GetResultType()) {
2747 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002748 locations->SetInAt(0, Location::RequiresRegister());
2749 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002750 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2751 locations->SetOut(Location::SameAsFirstInput());
2752 break;
2753 }
2754 case Primitive::kPrimLong: {
2755 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002756 // The shift count needs to be in CL.
2757 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002758 locations->SetOut(Location::SameAsFirstInput());
2759 break;
2760 }
2761 default:
2762 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2763 }
2764}
2765
2766void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2767 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2768
2769 LocationSummary* locations = op->GetLocations();
2770 Location first = locations->InAt(0);
2771 Location second = locations->InAt(1);
2772 DCHECK(first.Equals(locations->Out()));
2773
2774 switch (op->GetResultType()) {
2775 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 DCHECK_EQ(ECX, second_reg);
2780 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002783 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002784 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002786 }
2787 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002788 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2789 if (op->IsShl()) {
2790 __ shll(first_reg, imm);
2791 } else if (op->IsShr()) {
2792 __ sarl(first_reg, imm);
2793 } else {
2794 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795 }
2796 }
2797 break;
2798 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002799 case Primitive::kPrimLong: {
2800 Register second_reg = second.AsRegister<Register>();
2801 DCHECK_EQ(ECX, second_reg);
2802 if (op->IsShl()) {
2803 GenerateShlLong(first, second_reg);
2804 } else if (op->IsShr()) {
2805 GenerateShrLong(first, second_reg);
2806 } else {
2807 GenerateUShrLong(first, second_reg);
2808 }
2809 break;
2810 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002811 default:
2812 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2813 }
2814}
2815
2816void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2817 Label done;
2818 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2819 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2820 __ testl(shifter, Immediate(32));
2821 __ j(kEqual, &done);
2822 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2823 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2824 __ Bind(&done);
2825}
2826
2827void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2828 Label done;
2829 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2830 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2831 __ testl(shifter, Immediate(32));
2832 __ j(kEqual, &done);
2833 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2834 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2835 __ Bind(&done);
2836}
2837
2838void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2839 Label done;
2840 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2841 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2842 __ testl(shifter, Immediate(32));
2843 __ j(kEqual, &done);
2844 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2845 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2846 __ Bind(&done);
2847}
2848
2849void LocationsBuilderX86::VisitShl(HShl* shl) {
2850 HandleShift(shl);
2851}
2852
2853void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2854 HandleShift(shl);
2855}
2856
2857void LocationsBuilderX86::VisitShr(HShr* shr) {
2858 HandleShift(shr);
2859}
2860
2861void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2862 HandleShift(shr);
2863}
2864
2865void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2866 HandleShift(ushr);
2867}
2868
2869void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2870 HandleShift(ushr);
2871}
2872
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002873void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002876 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002877 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2879 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002880}
2881
2882void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2883 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002884 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002885 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002886
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002887 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002888
Nicolas Geoffray39468442014-09-02 15:17:15 +01002889 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002890 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002891}
2892
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002893void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2894 LocationSummary* locations =
2895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2896 locations->SetOut(Location::RegisterLocation(EAX));
2897 InvokeRuntimeCallingConvention calling_convention;
2898 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002899 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002901}
2902
2903void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2904 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002905 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002906 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2907
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002908 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002909
2910 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2911 DCHECK(!codegen_->IsLeafMethod());
2912}
2913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002915 LocationSummary* locations =
2916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002917 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2918 if (location.IsStackSlot()) {
2919 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2920 } else if (location.IsDoubleStackSlot()) {
2921 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002923 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924}
2925
2926void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002927 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002928}
2929
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002930void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002931 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002932 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002935}
2936
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002937void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2938 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002939 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002940 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002941 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002942 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002943 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002945 break;
2946
2947 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002948 __ notl(out.AsRegisterPairLow<Register>());
2949 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2954 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002955}
2956
David Brazdil66d126e2015-04-03 16:02:44 +01002957void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2958 LocationSummary* locations =
2959 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetOut(Location::SameAsFirstInput());
2962}
2963
2964void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002965 LocationSummary* locations = bool_not->GetLocations();
2966 Location in = locations->InAt(0);
2967 Location out = locations->Out();
2968 DCHECK(in.Equals(out));
2969 __ xorl(out.AsRegister<Register>(), Immediate(1));
2970}
2971
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002972void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002975 switch (compare->InputAt(0)->GetType()) {
2976 case Primitive::kPrimLong: {
2977 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002978 locations->SetInAt(1, Location::Any());
2979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2980 break;
2981 }
2982 case Primitive::kPrimFloat:
2983 case Primitive::kPrimDouble: {
2984 locations->SetInAt(0, Location::RequiresFpuRegister());
2985 locations->SetInAt(1, Location::RequiresFpuRegister());
2986 locations->SetOut(Location::RequiresRegister());
2987 break;
2988 }
2989 default:
2990 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2991 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002992}
2993
2994void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002995 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002997 Location left = locations->InAt(0);
2998 Location right = locations->InAt(1);
2999
3000 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003001 switch (compare->InputAt(0)->GetType()) {
3002 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003003 Register left_low = left.AsRegisterPairLow<Register>();
3004 Register left_high = left.AsRegisterPairHigh<Register>();
3005 int32_t val_low = 0;
3006 int32_t val_high = 0;
3007 bool right_is_const = false;
3008
3009 if (right.IsConstant()) {
3010 DCHECK(right.GetConstant()->IsLongConstant());
3011 right_is_const = true;
3012 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3013 val_low = Low32Bits(val);
3014 val_high = High32Bits(val);
3015 }
3016
Calin Juravleddb7df22014-11-25 20:56:51 +00003017 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003018 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003020 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003022 DCHECK(right_is_const) << right;
3023 if (val_high == 0) {
3024 __ testl(left_high, left_high);
3025 } else {
3026 __ cmpl(left_high, Immediate(val_high));
3027 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003029 __ j(kLess, &less); // Signed compare.
3030 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003031 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003032 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003033 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003034 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003035 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003036 DCHECK(right_is_const) << right;
3037 if (val_low == 0) {
3038 __ testl(left_low, left_low);
3039 } else {
3040 __ cmpl(left_low, Immediate(val_low));
3041 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003042 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003043 break;
3044 }
3045 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003047 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3048 break;
3049 }
3050 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003052 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003053 break;
3054 }
3055 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003056 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003057 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003058 __ movl(out, Immediate(0));
3059 __ j(kEqual, &done);
3060 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3061
3062 __ Bind(&greater);
3063 __ movl(out, Immediate(1));
3064 __ jmp(&done);
3065
3066 __ Bind(&less);
3067 __ movl(out, Immediate(-1));
3068
3069 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003070}
3071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003072void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003075 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3076 locations->SetInAt(i, Location::Any());
3077 }
3078 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003079}
3080
3081void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003082 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003083 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003084}
3085
Calin Juravle52c48962014-12-16 17:02:57 +00003086void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3087 /*
3088 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3089 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3090 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3091 */
3092 switch (kind) {
3093 case MemBarrierKind::kAnyAny: {
3094 __ mfence();
3095 break;
3096 }
3097 case MemBarrierKind::kAnyStore:
3098 case MemBarrierKind::kLoadAny:
3099 case MemBarrierKind::kStoreStore: {
3100 // nop
3101 break;
3102 }
3103 default:
3104 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003105 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003106}
3107
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108
Mark Mendell09ed1a32015-03-25 08:30:06 -04003109void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3110 Register temp) {
3111 // TODO: Implement all kinds of calls:
3112 // 1) boot -> boot
3113 // 2) app -> boot
3114 // 3) app -> app
3115 //
3116 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3117 // temp = method;
3118 LoadCurrentMethod(temp);
3119 if (!invoke->IsRecursive()) {
3120 // temp = temp->dex_cache_resolved_methods_;
3121 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3122 // temp = temp[index_in_cache]
3123 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3124 // (temp + offset_of_quick_compiled_code)()
3125 __ call(Address(
3126 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3127 } else {
3128 __ call(GetFrameEntryLabel());
3129 }
3130
3131 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003132}
3133
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003134void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003135 Label is_null;
3136 __ testl(value, value);
3137 __ j(kEqual, &is_null);
3138 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3139 __ movl(temp, object);
3140 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003141 __ movb(Address(temp, card, TIMES_1, 0),
3142 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003143 __ Bind(&is_null);
3144}
3145
Calin Juravle52c48962014-12-16 17:02:57 +00003146void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3147 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003148 LocationSummary* locations =
3149 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003150 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003151
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003152 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3153 locations->SetOut(Location::RequiresFpuRegister());
3154 } else {
3155 // The output overlaps in case of long: we don't want the low move to overwrite
3156 // the object's location.
3157 locations->SetOut(Location::RequiresRegister(),
3158 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3159 : Location::kNoOutputOverlap);
3160 }
Calin Juravle52c48962014-12-16 17:02:57 +00003161
3162 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3163 // Long values can be loaded atomically into an XMM using movsd.
3164 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3165 // and then copy the XMM into the output 32bits at a time).
3166 locations->AddTemp(Location::RequiresFpuRegister());
3167 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003168}
3169
Calin Juravle52c48962014-12-16 17:02:57 +00003170void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3171 const FieldInfo& field_info) {
3172 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003173
Calin Juravle52c48962014-12-16 17:02:57 +00003174 LocationSummary* locations = instruction->GetLocations();
3175 Register base = locations->InAt(0).AsRegister<Register>();
3176 Location out = locations->Out();
3177 bool is_volatile = field_info.IsVolatile();
3178 Primitive::Type field_type = field_info.GetFieldType();
3179 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3180
3181 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003182 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003183 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184 break;
3185 }
3186
3187 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003188 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003189 break;
3190 }
3191
3192 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003193 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194 break;
3195 }
3196
3197 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003198 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199 break;
3200 }
3201
3202 case Primitive::kPrimInt:
3203 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003204 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205 break;
3206 }
3207
3208 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003209 if (is_volatile) {
3210 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3211 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003212 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003213 __ movd(out.AsRegisterPairLow<Register>(), temp);
3214 __ psrlq(temp, Immediate(32));
3215 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3216 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003217 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003218 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003220 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3221 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003222 break;
3223 }
3224
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003225 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003226 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003227 break;
3228 }
3229
3230 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003231 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003232 break;
3233 }
3234
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003235 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003236 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003237 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238 }
Calin Juravle52c48962014-12-16 17:02:57 +00003239
Calin Juravle77520bc2015-01-12 18:45:46 +00003240 // Longs are handled in the switch.
3241 if (field_type != Primitive::kPrimLong) {
3242 codegen_->MaybeRecordImplicitNullCheck(instruction);
3243 }
3244
Calin Juravle52c48962014-12-16 17:02:57 +00003245 if (is_volatile) {
3246 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3247 }
3248}
3249
3250void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3251 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3252
3253 LocationSummary* locations =
3254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3255 locations->SetInAt(0, Location::RequiresRegister());
3256 bool is_volatile = field_info.IsVolatile();
3257 Primitive::Type field_type = field_info.GetFieldType();
3258 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3259 || (field_type == Primitive::kPrimByte);
3260
3261 // The register allocator does not support multiple
3262 // inputs that die at entry with one in a specific register.
3263 if (is_byte_type) {
3264 // Ensure the value is in a byte register.
3265 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003266 } else if (Primitive::IsFloatingPointType(field_type)) {
3267 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003268 } else {
3269 locations->SetInAt(1, Location::RequiresRegister());
3270 }
3271 // Temporary registers for the write barrier.
3272 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3273 locations->AddTemp(Location::RequiresRegister());
3274 // Ensure the card is in a byte register.
3275 locations->AddTemp(Location::RegisterLocation(ECX));
3276 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3277 // 64bits value can be atomically written to an address with movsd and an XMM register.
3278 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3279 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3280 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3281 // isolated cases when we need this it isn't worth adding the extra complexity.
3282 locations->AddTemp(Location::RequiresFpuRegister());
3283 locations->AddTemp(Location::RequiresFpuRegister());
3284 }
3285}
3286
3287void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3288 const FieldInfo& field_info) {
3289 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3290
3291 LocationSummary* locations = instruction->GetLocations();
3292 Register base = locations->InAt(0).AsRegister<Register>();
3293 Location value = locations->InAt(1);
3294 bool is_volatile = field_info.IsVolatile();
3295 Primitive::Type field_type = field_info.GetFieldType();
3296 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3297
3298 if (is_volatile) {
3299 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3300 }
3301
3302 switch (field_type) {
3303 case Primitive::kPrimBoolean:
3304 case Primitive::kPrimByte: {
3305 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3306 break;
3307 }
3308
3309 case Primitive::kPrimShort:
3310 case Primitive::kPrimChar: {
3311 __ movw(Address(base, offset), value.AsRegister<Register>());
3312 break;
3313 }
3314
3315 case Primitive::kPrimInt:
3316 case Primitive::kPrimNot: {
3317 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003318 break;
3319 }
3320
3321 case Primitive::kPrimLong: {
3322 if (is_volatile) {
3323 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3324 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3325 __ movd(temp1, value.AsRegisterPairLow<Register>());
3326 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3327 __ punpckldq(temp1, temp2);
3328 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003330 } else {
3331 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003332 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003333 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3334 }
3335 break;
3336 }
3337
3338 case Primitive::kPrimFloat: {
3339 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3340 break;
3341 }
3342
3343 case Primitive::kPrimDouble: {
3344 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3345 break;
3346 }
3347
3348 case Primitive::kPrimVoid:
3349 LOG(FATAL) << "Unreachable type " << field_type;
3350 UNREACHABLE();
3351 }
3352
Calin Juravle77520bc2015-01-12 18:45:46 +00003353 // Longs are handled in the switch.
3354 if (field_type != Primitive::kPrimLong) {
3355 codegen_->MaybeRecordImplicitNullCheck(instruction);
3356 }
3357
3358 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3359 Register temp = locations->GetTemp(0).AsRegister<Register>();
3360 Register card = locations->GetTemp(1).AsRegister<Register>();
3361 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3362 }
3363
Calin Juravle52c48962014-12-16 17:02:57 +00003364 if (is_volatile) {
3365 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3366 }
3367}
3368
3369void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3370 HandleFieldGet(instruction, instruction->GetFieldInfo());
3371}
3372
3373void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3374 HandleFieldGet(instruction, instruction->GetFieldInfo());
3375}
3376
3377void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3378 HandleFieldSet(instruction, instruction->GetFieldInfo());
3379}
3380
3381void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3382 HandleFieldSet(instruction, instruction->GetFieldInfo());
3383}
3384
3385void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3386 HandleFieldSet(instruction, instruction->GetFieldInfo());
3387}
3388
3389void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3390 HandleFieldSet(instruction, instruction->GetFieldInfo());
3391}
3392
3393void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3394 HandleFieldGet(instruction, instruction->GetFieldInfo());
3395}
3396
3397void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3398 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003399}
3400
3401void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003402 LocationSummary* locations =
3403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003404 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3405 ? Location::RequiresRegister()
3406 : Location::Any();
3407 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003408 if (instruction->HasUses()) {
3409 locations->SetOut(Location::SameAsFirstInput());
3410 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003411}
3412
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003413void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003414 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3415 return;
3416 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003417 LocationSummary* locations = instruction->GetLocations();
3418 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003419
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003420 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3421 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3422}
3423
3424void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003425 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 codegen_->AddSlowPath(slow_path);
3427
3428 LocationSummary* locations = instruction->GetLocations();
3429 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430
3431 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003432 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003433 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003434 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003435 } else {
3436 DCHECK(obj.IsConstant()) << obj;
3437 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3438 __ jmp(slow_path->GetEntryLabel());
3439 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003440 }
3441 __ j(kEqual, slow_path->GetEntryLabel());
3442}
3443
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003444void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3445 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3446 GenerateImplicitNullCheck(instruction);
3447 } else {
3448 GenerateExplicitNullCheck(instruction);
3449 }
3450}
3451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003452void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003453 LocationSummary* locations =
3454 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003455 locations->SetInAt(0, Location::RequiresRegister());
3456 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003457 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3458 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3459 } else {
3460 // The output overlaps in case of long: we don't want the low move to overwrite
3461 // the array's location.
3462 locations->SetOut(Location::RequiresRegister(),
3463 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3464 : Location::kNoOutputOverlap);
3465 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466}
3467
3468void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3469 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003470 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471 Location index = locations->InAt(1);
3472
Calin Juravle77520bc2015-01-12 18:45:46 +00003473 Primitive::Type type = instruction->GetType();
3474 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475 case Primitive::kPrimBoolean: {
3476 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003478 if (index.IsConstant()) {
3479 __ movzxb(out, Address(obj,
3480 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3481 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003483 }
3484 break;
3485 }
3486
3487 case Primitive::kPrimByte: {
3488 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003490 if (index.IsConstant()) {
3491 __ movsxb(out, Address(obj,
3492 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3493 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003495 }
3496 break;
3497 }
3498
3499 case Primitive::kPrimShort: {
3500 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003502 if (index.IsConstant()) {
3503 __ movsxw(out, Address(obj,
3504 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3505 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003506 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003507 }
3508 break;
3509 }
3510
3511 case Primitive::kPrimChar: {
3512 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514 if (index.IsConstant()) {
3515 __ movzxw(out, Address(obj,
3516 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3517 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003518 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 }
3520 break;
3521 }
3522
3523 case Primitive::kPrimInt:
3524 case Primitive::kPrimNot: {
3525 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003527 if (index.IsConstant()) {
3528 __ movl(out, Address(obj,
3529 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3530 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003532 }
3533 break;
3534 }
3535
3536 case Primitive::kPrimLong: {
3537 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003538 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003539 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003540 if (index.IsConstant()) {
3541 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003542 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003543 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003544 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003545 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003546 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003549 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003551 }
3552 break;
3553 }
3554
Mark Mendell7c8d0092015-01-26 11:21:33 -05003555 case Primitive::kPrimFloat: {
3556 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3557 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3558 if (index.IsConstant()) {
3559 __ movss(out, Address(obj,
3560 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3561 } else {
3562 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3563 }
3564 break;
3565 }
3566
3567 case Primitive::kPrimDouble: {
3568 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3569 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3570 if (index.IsConstant()) {
3571 __ movsd(out, Address(obj,
3572 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3573 } else {
3574 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3575 }
3576 break;
3577 }
3578
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003580 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003581 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003583
3584 if (type != Primitive::kPrimLong) {
3585 codegen_->MaybeRecordImplicitNullCheck(instruction);
3586 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003587}
3588
3589void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003590 // This location builder might end up asking to up to four registers, which is
3591 // not currently possible for baseline. The situation in which we need four
3592 // registers cannot be met by baseline though, because it has not run any
3593 // optimization.
3594
Nicolas Geoffray39468442014-09-02 15:17:15 +01003595 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003596 bool needs_write_barrier =
3597 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3598
Mark Mendell5f874182015-03-04 15:42:45 -05003599 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003600
Nicolas Geoffray39468442014-09-02 15:17:15 +01003601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3602 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003603 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003604
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003605 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003607 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3608 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3609 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003611 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3612 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003613 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003614 // In case of a byte operation, the register allocator does not support multiple
3615 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003616 locations->SetInAt(0, Location::RequiresRegister());
3617 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003618 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003619 // Ensure the value is in a byte register.
3620 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003621 } else if (Primitive::IsFloatingPointType(value_type)) {
3622 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003624 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003625 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003626 // Temporary registers for the write barrier.
3627 if (needs_write_barrier) {
3628 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003629 // Ensure the card is in a byte register.
3630 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003631 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633}
3634
3635void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3636 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003639 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003640 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003641 bool needs_runtime_call = locations->WillCall();
3642 bool needs_write_barrier =
3643 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003644
3645 switch (value_type) {
3646 case Primitive::kPrimBoolean:
3647 case Primitive::kPrimByte: {
3648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003649 if (index.IsConstant()) {
3650 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003651 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003652 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003653 } else {
3654 __ movb(Address(obj, offset),
3655 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3656 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003657 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003658 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003660 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003661 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003663 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3664 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003665 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003666 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 break;
3668 }
3669
3670 case Primitive::kPrimShort:
3671 case Primitive::kPrimChar: {
3672 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003673 if (index.IsConstant()) {
3674 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003675 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003677 } else {
3678 __ movw(Address(obj, offset),
3679 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003681 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003682 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3684 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003685 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003687 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3688 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003689 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003690 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 break;
3692 }
3693
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003694 case Primitive::kPrimInt:
3695 case Primitive::kPrimNot: {
3696 if (!needs_runtime_call) {
3697 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3698 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003699 size_t offset =
3700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003701 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003702 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003703 } else {
3704 DCHECK(value.IsConstant()) << value;
3705 __ movl(Address(obj, offset),
3706 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3707 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003708 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 DCHECK(index.IsRegister()) << index;
3710 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3712 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003713 } else {
3714 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003716 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3717 }
3718 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003719 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003720
3721 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 Register temp = locations->GetTemp(0).AsRegister<Register>();
3723 Register card = locations->GetTemp(1).AsRegister<Register>();
3724 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003725 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003726 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003727 DCHECK_EQ(value_type, Primitive::kPrimNot);
3728 DCHECK(!codegen_->IsLeafMethod());
3729 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3730 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 }
3732 break;
3733 }
3734
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735 case Primitive::kPrimLong: {
3736 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003737 if (index.IsConstant()) {
3738 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003739 if (value.IsRegisterPair()) {
3740 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003741 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003742 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003743 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003744 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003745 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3746 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003748 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3749 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003750 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003751 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003752 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003753 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003754 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003756 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003758 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003759 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003761 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003763 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003764 Immediate(High32Bits(val)));
3765 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766 }
3767 break;
3768 }
3769
Mark Mendell7c8d0092015-01-26 11:21:33 -05003770 case Primitive::kPrimFloat: {
3771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3772 DCHECK(value.IsFpuRegister());
3773 if (index.IsConstant()) {
3774 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3775 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3776 } else {
3777 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3778 value.AsFpuRegister<XmmRegister>());
3779 }
3780 break;
3781 }
3782
3783 case Primitive::kPrimDouble: {
3784 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3785 DCHECK(value.IsFpuRegister());
3786 if (index.IsConstant()) {
3787 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3788 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3789 } else {
3790 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3791 value.AsFpuRegister<XmmRegister>());
3792 }
3793 break;
3794 }
3795
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 case Primitive::kPrimVoid:
3797 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003798 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 }
3800}
3801
3802void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003804 locations->SetInAt(0, Location::RequiresRegister());
3805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 instruction->SetLocations(locations);
3807}
3808
3809void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3810 LocationSummary* locations = instruction->GetLocations();
3811 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 Register obj = locations->InAt(0).AsRegister<Register>();
3813 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816}
3817
3818void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003819 LocationSummary* locations =
3820 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003821 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003823 if (instruction->HasUses()) {
3824 locations->SetOut(Location::SameAsFirstInput());
3825 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826}
3827
3828void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3829 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003830 Location index_loc = locations->InAt(0);
3831 Location length_loc = locations->InAt(1);
3832 SlowPathCodeX86* slow_path =
3833 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 codegen_->AddSlowPath(slow_path);
3835
Mark Mendellf60c90b2015-03-04 15:12:59 -05003836 Register length = length_loc.AsRegister<Register>();
3837 if (index_loc.IsConstant()) {
3838 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3839 __ cmpl(length, Immediate(value));
3840 } else {
3841 __ cmpl(length, index_loc.AsRegister<Register>());
3842 }
3843 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003844}
3845
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3847 temp->SetLocations(nullptr);
3848}
3849
3850void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3851 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003852 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853}
3854
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003855void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003856 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003857 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003858}
3859
3860void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003861 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3862}
3863
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003864void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3866}
3867
3868void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003869 HBasicBlock* block = instruction->GetBlock();
3870 if (block->GetLoopInformation() != nullptr) {
3871 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3872 // The back edge will generate the suspend check.
3873 return;
3874 }
3875 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3876 // The goto will generate the suspend check.
3877 return;
3878 }
3879 GenerateSuspendCheck(instruction, nullptr);
3880}
3881
3882void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3883 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003884 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003885 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003886 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003887 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003888 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003889 if (successor == nullptr) {
3890 __ j(kNotEqual, slow_path->GetEntryLabel());
3891 __ Bind(slow_path->GetReturnLabel());
3892 } else {
3893 __ j(kEqual, codegen_->GetLabelOf(successor));
3894 __ jmp(slow_path->GetEntryLabel());
3895 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003896}
3897
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003898X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3899 return codegen_->GetAssembler();
3900}
3901
Mark Mendell7c8d0092015-01-26 11:21:33 -05003902void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003903 ScratchRegisterScope ensure_scratch(
3904 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3905 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3906 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3907 __ movl(temp_reg, Address(ESP, src + stack_offset));
3908 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003909}
3910
3911void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003912 ScratchRegisterScope ensure_scratch(
3913 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3914 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3915 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3916 __ movl(temp_reg, Address(ESP, src + stack_offset));
3917 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3918 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3919 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003920}
3921
3922void ParallelMoveResolverX86::EmitMove(size_t index) {
3923 MoveOperands* move = moves_.Get(index);
3924 Location source = move->GetSource();
3925 Location destination = move->GetDestination();
3926
3927 if (source.IsRegister()) {
3928 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003930 } else {
3931 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003933 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003934 } else if (source.IsFpuRegister()) {
3935 if (destination.IsFpuRegister()) {
3936 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3937 } else if (destination.IsStackSlot()) {
3938 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3939 } else {
3940 DCHECK(destination.IsDoubleStackSlot());
3941 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3942 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003943 } else if (source.IsStackSlot()) {
3944 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003946 } else if (destination.IsFpuRegister()) {
3947 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003948 } else {
3949 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003950 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3951 }
3952 } else if (source.IsDoubleStackSlot()) {
3953 if (destination.IsFpuRegister()) {
3954 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3955 } else {
3956 DCHECK(destination.IsDoubleStackSlot()) << destination;
3957 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003958 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003959 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003960 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003961 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003962 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003963 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003964 if (value == 0) {
3965 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3966 } else {
3967 __ movl(destination.AsRegister<Register>(), Immediate(value));
3968 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003969 } else {
3970 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003971 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003972 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003973 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003974 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003975 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003976 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003977 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003978 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3979 if (value == 0) {
3980 // Easy handling of 0.0.
3981 __ xorps(dest, dest);
3982 } else {
3983 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003984 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3985 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3986 __ movl(temp, Immediate(value));
3987 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003988 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003989 } else {
3990 DCHECK(destination.IsStackSlot()) << destination;
3991 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3992 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003993 } else if (constant->IsLongConstant()) {
3994 int64_t value = constant->AsLongConstant()->GetValue();
3995 int32_t low_value = Low32Bits(value);
3996 int32_t high_value = High32Bits(value);
3997 Immediate low(low_value);
3998 Immediate high(high_value);
3999 if (destination.IsDoubleStackSlot()) {
4000 __ movl(Address(ESP, destination.GetStackIndex()), low);
4001 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4002 } else {
4003 __ movl(destination.AsRegisterPairLow<Register>(), low);
4004 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4005 }
4006 } else {
4007 DCHECK(constant->IsDoubleConstant());
4008 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004009 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004010 int32_t low_value = Low32Bits(value);
4011 int32_t high_value = High32Bits(value);
4012 Immediate low(low_value);
4013 Immediate high(high_value);
4014 if (destination.IsFpuRegister()) {
4015 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4016 if (value == 0) {
4017 // Easy handling of 0.0.
4018 __ xorpd(dest, dest);
4019 } else {
4020 __ pushl(high);
4021 __ pushl(low);
4022 __ movsd(dest, Address(ESP, 0));
4023 __ addl(ESP, Immediate(8));
4024 }
4025 } else {
4026 DCHECK(destination.IsDoubleStackSlot()) << destination;
4027 __ movl(Address(ESP, destination.GetStackIndex()), low);
4028 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4029 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004030 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004031 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004032 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004033 }
4034}
4035
Mark Mendella5c19ce2015-04-01 12:51:05 -04004036void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004037 Register suggested_scratch = reg == EAX ? EBX : EAX;
4038 ScratchRegisterScope ensure_scratch(
4039 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4040
4041 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4042 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4043 __ movl(Address(ESP, mem + stack_offset), reg);
4044 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004045}
4046
Mark Mendell7c8d0092015-01-26 11:21:33 -05004047void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004048 ScratchRegisterScope ensure_scratch(
4049 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4050
4051 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4052 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4053 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4054 __ movss(Address(ESP, mem + stack_offset), reg);
4055 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004056}
4057
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004058void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004059 ScratchRegisterScope ensure_scratch1(
4060 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004061
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004062 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4063 ScratchRegisterScope ensure_scratch2(
4064 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004065
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004066 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4067 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4068 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4069 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4070 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4071 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004072}
4073
4074void ParallelMoveResolverX86::EmitSwap(size_t index) {
4075 MoveOperands* move = moves_.Get(index);
4076 Location source = move->GetSource();
4077 Location destination = move->GetDestination();
4078
4079 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004080 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004081 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004082 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004083 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004085 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4086 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004087 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4088 // Use XOR Swap algorithm to avoid a temporary.
4089 DCHECK_NE(source.reg(), destination.reg());
4090 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4091 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4092 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4093 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4094 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4095 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4096 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004097 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4098 // Take advantage of the 16 bytes in the XMM register.
4099 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4100 Address stack(ESP, destination.GetStackIndex());
4101 // Load the double into the high doubleword.
4102 __ movhpd(reg, stack);
4103
4104 // Store the low double into the destination.
4105 __ movsd(stack, reg);
4106
4107 // Move the high double to the low double.
4108 __ psrldq(reg, Immediate(8));
4109 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4110 // Take advantage of the 16 bytes in the XMM register.
4111 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4112 Address stack(ESP, source.GetStackIndex());
4113 // Load the double into the high doubleword.
4114 __ movhpd(reg, stack);
4115
4116 // Store the low double into the destination.
4117 __ movsd(stack, reg);
4118
4119 // Move the high double to the low double.
4120 __ psrldq(reg, Immediate(8));
4121 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4122 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4123 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004124 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004125 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004126 }
4127}
4128
4129void ParallelMoveResolverX86::SpillScratch(int reg) {
4130 __ pushl(static_cast<Register>(reg));
4131}
4132
4133void ParallelMoveResolverX86::RestoreScratch(int reg) {
4134 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004135}
4136
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004137void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004138 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4139 ? LocationSummary::kCallOnSlowPath
4140 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004141 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004142 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004143 locations->SetOut(Location::RequiresRegister());
4144}
4145
4146void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004148 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004149 DCHECK(!cls->CanCallRuntime());
4150 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004151 codegen_->LoadCurrentMethod(out);
4152 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4153 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004154 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004155 codegen_->LoadCurrentMethod(out);
4156 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4157 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004158
4159 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4160 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4161 codegen_->AddSlowPath(slow_path);
4162 __ testl(out, out);
4163 __ j(kEqual, slow_path->GetEntryLabel());
4164 if (cls->MustGenerateClinitCheck()) {
4165 GenerateClassInitializationCheck(slow_path, out);
4166 } else {
4167 __ Bind(slow_path->GetExitLabel());
4168 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004169 }
4170}
4171
4172void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4173 LocationSummary* locations =
4174 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4175 locations->SetInAt(0, Location::RequiresRegister());
4176 if (check->HasUses()) {
4177 locations->SetOut(Location::SameAsFirstInput());
4178 }
4179}
4180
4181void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004182 // We assume the class to not be null.
4183 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4184 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004185 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004186 GenerateClassInitializationCheck(slow_path,
4187 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004188}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004189
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004190void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4191 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004192 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4193 Immediate(mirror::Class::kStatusInitialized));
4194 __ j(kLess, slow_path->GetEntryLabel());
4195 __ Bind(slow_path->GetExitLabel());
4196 // No need for memory fence, thanks to the X86 memory model.
4197}
4198
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004199void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4200 LocationSummary* locations =
4201 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4202 locations->SetOut(Location::RequiresRegister());
4203}
4204
4205void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4206 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4207 codegen_->AddSlowPath(slow_path);
4208
Roland Levillain271ab9c2014-11-27 15:23:57 +00004209 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004210 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004211 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4212 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004213 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4214 __ testl(out, out);
4215 __ j(kEqual, slow_path->GetEntryLabel());
4216 __ Bind(slow_path->GetExitLabel());
4217}
4218
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004219void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4220 LocationSummary* locations =
4221 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4222 locations->SetOut(Location::RequiresRegister());
4223}
4224
4225void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4226 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004228 __ fs()->movl(address, Immediate(0));
4229}
4230
4231void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4232 LocationSummary* locations =
4233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4234 InvokeRuntimeCallingConvention calling_convention;
4235 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4236}
4237
4238void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4239 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4240 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4241}
4242
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004243void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004244 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4245 ? LocationSummary::kNoCall
4246 : LocationSummary::kCallOnSlowPath;
4247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetInAt(1, Location::Any());
4250 locations->SetOut(Location::RequiresRegister());
4251}
4252
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004253void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004254 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004255 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004256 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004257 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004258 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4259 Label done, zero;
4260 SlowPathCodeX86* slow_path = nullptr;
4261
4262 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004263 // Avoid null check if we know obj is not null.
4264 if (instruction->MustDoNullCheck()) {
4265 __ testl(obj, obj);
4266 __ j(kEqual, &zero);
4267 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004268 __ movl(out, Address(obj, class_offset));
4269 // Compare the class of `obj` with `cls`.
4270 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004271 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004272 } else {
4273 DCHECK(cls.IsStackSlot()) << cls;
4274 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4275 }
4276
4277 if (instruction->IsClassFinal()) {
4278 // Classes must be equal for the instanceof to succeed.
4279 __ j(kNotEqual, &zero);
4280 __ movl(out, Immediate(1));
4281 __ jmp(&done);
4282 } else {
4283 // If the classes are not equal, we go into a slow path.
4284 DCHECK(locations->OnlyCallsOnSlowPath());
4285 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004286 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004287 codegen_->AddSlowPath(slow_path);
4288 __ j(kNotEqual, slow_path->GetEntryLabel());
4289 __ movl(out, Immediate(1));
4290 __ jmp(&done);
4291 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004292
4293 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4294 __ Bind(&zero);
4295 __ movl(out, Immediate(0));
4296 }
4297
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004298 if (slow_path != nullptr) {
4299 __ Bind(slow_path->GetExitLabel());
4300 }
4301 __ Bind(&done);
4302}
4303
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004304void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4305 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4306 instruction, LocationSummary::kCallOnSlowPath);
4307 locations->SetInAt(0, Location::RequiresRegister());
4308 locations->SetInAt(1, Location::Any());
4309 locations->AddTemp(Location::RequiresRegister());
4310}
4311
4312void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4313 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004314 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004315 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004317 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4318 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4319 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4320 codegen_->AddSlowPath(slow_path);
4321
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004322 // Avoid null check if we know obj is not null.
4323 if (instruction->MustDoNullCheck()) {
4324 __ testl(obj, obj);
4325 __ j(kEqual, slow_path->GetExitLabel());
4326 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004327
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004328 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004329 // Compare the class of `obj` with `cls`.
4330 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004331 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004332 } else {
4333 DCHECK(cls.IsStackSlot()) << cls;
4334 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4335 }
4336
4337 __ j(kNotEqual, slow_path->GetEntryLabel());
4338 __ Bind(slow_path->GetExitLabel());
4339}
4340
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004341void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4342 LocationSummary* locations =
4343 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4344 InvokeRuntimeCallingConvention calling_convention;
4345 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4346}
4347
4348void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4349 __ fs()->call(Address::Absolute(instruction->IsEnter()
4350 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4351 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4352 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4353}
4354
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004355void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4356void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4357void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4358
4359void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4360 LocationSummary* locations =
4361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4362 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4363 || instruction->GetResultType() == Primitive::kPrimLong);
4364 locations->SetInAt(0, Location::RequiresRegister());
4365 locations->SetInAt(1, Location::Any());
4366 locations->SetOut(Location::SameAsFirstInput());
4367}
4368
4369void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4370 HandleBitwiseOperation(instruction);
4371}
4372
4373void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4374 HandleBitwiseOperation(instruction);
4375}
4376
4377void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4378 HandleBitwiseOperation(instruction);
4379}
4380
4381void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4382 LocationSummary* locations = instruction->GetLocations();
4383 Location first = locations->InAt(0);
4384 Location second = locations->InAt(1);
4385 DCHECK(first.Equals(locations->Out()));
4386
4387 if (instruction->GetResultType() == Primitive::kPrimInt) {
4388 if (second.IsRegister()) {
4389 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004391 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004392 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004393 } else {
4394 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004396 }
4397 } else if (second.IsConstant()) {
4398 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004399 __ andl(first.AsRegister<Register>(),
4400 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004401 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004402 __ orl(first.AsRegister<Register>(),
4403 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004404 } else {
4405 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004406 __ xorl(first.AsRegister<Register>(),
4407 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004408 }
4409 } else {
4410 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004412 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004414 } else {
4415 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004416 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004417 }
4418 }
4419 } else {
4420 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4421 if (second.IsRegisterPair()) {
4422 if (instruction->IsAnd()) {
4423 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4424 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4425 } else if (instruction->IsOr()) {
4426 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4427 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4428 } else {
4429 DCHECK(instruction->IsXor());
4430 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4431 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4432 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004433 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004434 if (instruction->IsAnd()) {
4435 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4436 __ andl(first.AsRegisterPairHigh<Register>(),
4437 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4438 } else if (instruction->IsOr()) {
4439 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4440 __ orl(first.AsRegisterPairHigh<Register>(),
4441 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4442 } else {
4443 DCHECK(instruction->IsXor());
4444 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4445 __ xorl(first.AsRegisterPairHigh<Register>(),
4446 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4447 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004448 } else {
4449 DCHECK(second.IsConstant()) << second;
4450 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004451 int32_t low_value = Low32Bits(value);
4452 int32_t high_value = High32Bits(value);
4453 Immediate low(low_value);
4454 Immediate high(high_value);
4455 Register first_low = first.AsRegisterPairLow<Register>();
4456 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004457 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004458 if (low_value == 0) {
4459 __ xorl(first_low, first_low);
4460 } else if (low_value != -1) {
4461 __ andl(first_low, low);
4462 }
4463 if (high_value == 0) {
4464 __ xorl(first_high, first_high);
4465 } else if (high_value != -1) {
4466 __ andl(first_high, high);
4467 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004468 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004469 if (low_value != 0) {
4470 __ orl(first_low, low);
4471 }
4472 if (high_value != 0) {
4473 __ orl(first_high, high);
4474 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004475 } else {
4476 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004477 if (low_value != 0) {
4478 __ xorl(first_low, low);
4479 }
4480 if (high_value != 0) {
4481 __ xorl(first_high, high);
4482 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004483 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004484 }
4485 }
4486}
4487
Calin Juravleb1498f62015-02-16 13:13:29 +00004488void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4489 // Nothing to do, this should be removed during prepare for register allocator.
4490 UNUSED(instruction);
4491 LOG(FATAL) << "Unreachable";
4492}
4493
4494void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4495 // Nothing to do, this should be removed during prepare for register allocator.
4496 UNUSED(instruction);
4497 LOG(FATAL) << "Unreachable";
4498}
4499
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004500} // namespace x86
4501} // namespace art