blob: 50c0b1e437d8143a8251c0460c548854e56a983c [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 Levillain3e3d7332015-04-28 11:00:54 +01001197 // When we do not run baseline, explicit clinit checks triggered by static
1198 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1199 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001200
Mark Mendellfb8d2792015-03-31 22:16:59 -04001201 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001202 if (intrinsic.TryDispatch(invoke)) {
1203 return;
1204 }
1205
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001206 HandleInvoke(invoke);
1207}
1208
Mark Mendell09ed1a32015-03-25 08:30:06 -04001209static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1210 if (invoke->GetLocations()->Intrinsified()) {
1211 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1212 intrinsic.Dispatch(invoke);
1213 return true;
1214 }
1215 return false;
1216}
1217
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001218void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001219 // When we do not run baseline, explicit clinit checks triggered by static
1220 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1221 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001222
Mark Mendell09ed1a32015-03-25 08:30:06 -04001223 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1224 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001225 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 codegen_->GenerateStaticOrDirectCall(
1228 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001229 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230}
1231
1232void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1233 HandleInvoke(invoke);
1234}
1235
1236void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 LocationSummary* locations =
1238 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001239 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001240
1241 InvokeDexCallingConventionVisitor calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001242 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001243 HInstruction* input = invoke->InputAt(i);
1244 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1245 }
1246
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 switch (invoke->GetType()) {
1248 case Primitive::kPrimBoolean:
1249 case Primitive::kPrimByte:
1250 case Primitive::kPrimChar:
1251 case Primitive::kPrimShort:
1252 case Primitive::kPrimInt:
1253 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001254 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 break;
1256
1257 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimVoid:
1262 break;
1263
1264 case Primitive::kPrimDouble:
1265 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001266 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 break;
1268 }
1269
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270 invoke->SetLocations(locations);
1271}
1272
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001273void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001274 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001275 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1276 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1277 LocationSummary* locations = invoke->GetLocations();
1278 Location receiver = locations->InAt(0);
1279 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1280 // temp = object->GetClass();
1281 if (receiver.IsStackSlot()) {
1282 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1283 __ movl(temp, Address(temp, class_offset));
1284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 // temp = temp->GetMethodAt(method_offset);
1289 __ movl(temp, Address(temp, method_offset));
1290 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001291 __ call(Address(
1292 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001293
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001294 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001296}
1297
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001298void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1299 HandleInvoke(invoke);
1300 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001301 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302}
1303
1304void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1305 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1308 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1309 LocationSummary* locations = invoke->GetLocations();
1310 Location receiver = locations->InAt(0);
1311 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1312
1313 // Set the hidden argument.
1314 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001316
1317 // temp = object->GetClass();
1318 if (receiver.IsStackSlot()) {
1319 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1320 __ movl(temp, Address(temp, class_offset));
1321 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001322 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001324 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001325 // temp = temp->GetImtEntryAt(method_offset);
1326 __ movl(temp, Address(temp, method_offset));
1327 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001328 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001329 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330
1331 DCHECK(!codegen_->IsLeafMethod());
1332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1333}
1334
Roland Levillain88cb1752014-10-20 16:36:47 +01001335void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1336 LocationSummary* locations =
1337 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1338 switch (neg->GetResultType()) {
1339 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001340 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001341 locations->SetInAt(0, Location::RequiresRegister());
1342 locations->SetOut(Location::SameAsFirstInput());
1343 break;
1344
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetOut(Location::SameAsFirstInput());
1348 locations->AddTemp(Location::RequiresRegister());
1349 locations->AddTemp(Location::RequiresFpuRegister());
1350 break;
1351
Roland Levillain88cb1752014-10-20 16:36:47 +01001352 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001354 locations->SetOut(Location::SameAsFirstInput());
1355 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 break;
1357
1358 default:
1359 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1360 }
1361}
1362
1363void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1364 LocationSummary* locations = neg->GetLocations();
1365 Location out = locations->Out();
1366 Location in = locations->InAt(0);
1367 switch (neg->GetResultType()) {
1368 case Primitive::kPrimInt:
1369 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001370 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001372 break;
1373
1374 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001375 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001376 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001377 __ negl(out.AsRegisterPairLow<Register>());
1378 // Negation is similar to subtraction from zero. The least
1379 // significant byte triggers a borrow when it is different from
1380 // zero; to take it into account, add 1 to the most significant
1381 // byte if the carry flag (CF) is set to 1 after the first NEGL
1382 // operation.
1383 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1384 __ negl(out.AsRegisterPairHigh<Register>());
1385 break;
1386
Roland Levillain5368c212014-11-27 15:03:41 +00001387 case Primitive::kPrimFloat: {
1388 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 Register constant = locations->GetTemp(0).AsRegister<Register>();
1390 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001391 // Implement float negation with an exclusive or with value
1392 // 0x80000000 (mask for bit 31, representing the sign of a
1393 // single-precision floating-point number).
1394 __ movl(constant, Immediate(INT32_C(0x80000000)));
1395 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001396 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001397 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001398 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001399
Roland Levillain5368c212014-11-27 15:03:41 +00001400 case Primitive::kPrimDouble: {
1401 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001402 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001403 // Implement double negation with an exclusive or with value
1404 // 0x8000000000000000 (mask for bit 63, representing the sign of
1405 // a double-precision floating-point number).
1406 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001407 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001408 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001409 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001410
1411 default:
1412 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1413 }
1414}
1415
Roland Levillaindff1f282014-11-05 14:15:05 +00001416void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001417 Primitive::Type result_type = conversion->GetResultType();
1418 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001419 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001420
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001421 // The float-to-long and double-to-long type conversions rely on a
1422 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001423 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1425 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001426 ? LocationSummary::kCall
1427 : LocationSummary::kNoCall;
1428 LocationSummary* locations =
1429 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1430
David Brazdilb2bd1c52015-03-25 11:17:37 +00001431 // The Java language does not allow treating boolean as an integral type but
1432 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001433
Roland Levillaindff1f282014-11-05 14:15:05 +00001434 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001435 case Primitive::kPrimByte:
1436 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001437 case Primitive::kPrimBoolean:
1438 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimShort:
1440 case Primitive::kPrimInt:
1441 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001442 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001443 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1444 // Make the output overlap to please the register allocator. This greatly simplifies
1445 // the validation of the linear scan implementation
1446 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001447 break;
1448
1449 default:
1450 LOG(FATAL) << "Unexpected type conversion from " << input_type
1451 << " to " << result_type;
1452 }
1453 break;
1454
Roland Levillain01a8d712014-11-14 16:27:39 +00001455 case Primitive::kPrimShort:
1456 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001457 case Primitive::kPrimBoolean:
1458 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimByte:
1460 case Primitive::kPrimInt:
1461 case Primitive::kPrimChar:
1462 // Processing a Dex `int-to-short' instruction.
1463 locations->SetInAt(0, Location::Any());
1464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1465 break;
1466
1467 default:
1468 LOG(FATAL) << "Unexpected type conversion from " << input_type
1469 << " to " << result_type;
1470 }
1471 break;
1472
Roland Levillain946e1432014-11-11 17:35:19 +00001473 case Primitive::kPrimInt:
1474 switch (input_type) {
1475 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001476 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001477 locations->SetInAt(0, Location::Any());
1478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1479 break;
1480
1481 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001482 // Processing a Dex `float-to-int' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresRegister());
1485 locations->AddTemp(Location::RequiresFpuRegister());
1486 break;
1487
Roland Levillain946e1432014-11-11 17:35:19 +00001488 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001489 // Processing a Dex `double-to-int' instruction.
1490 locations->SetInAt(0, Location::RequiresFpuRegister());
1491 locations->SetOut(Location::RequiresRegister());
1492 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001493 break;
1494
1495 default:
1496 LOG(FATAL) << "Unexpected type conversion from " << input_type
1497 << " to " << result_type;
1498 }
1499 break;
1500
Roland Levillaindff1f282014-11-05 14:15:05 +00001501 case Primitive::kPrimLong:
1502 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001503 case Primitive::kPrimBoolean:
1504 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimByte:
1506 case Primitive::kPrimShort:
1507 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001508 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001509 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001510 locations->SetInAt(0, Location::RegisterLocation(EAX));
1511 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1512 break;
1513
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001514 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001515 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001516 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001517 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1519 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1520
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 // The runtime helper puts the result in EAX, EDX.
1522 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001523 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001524 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001525
1526 default:
1527 LOG(FATAL) << "Unexpected type conversion from " << input_type
1528 << " to " << result_type;
1529 }
1530 break;
1531
Roland Levillain981e4542014-11-14 11:47:14 +00001532 case Primitive::kPrimChar:
1533 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001534 case Primitive::kPrimBoolean:
1535 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimByte:
1537 case Primitive::kPrimShort:
1538 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001539 // Processing a Dex `int-to-char' instruction.
1540 locations->SetInAt(0, Location::Any());
1541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1542 break;
1543
1544 default:
1545 LOG(FATAL) << "Unexpected type conversion from " << input_type
1546 << " to " << result_type;
1547 }
1548 break;
1549
Roland Levillaindff1f282014-11-05 14:15:05 +00001550 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001551 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001552 case Primitive::kPrimBoolean:
1553 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001554 case Primitive::kPrimByte:
1555 case Primitive::kPrimShort:
1556 case Primitive::kPrimInt:
1557 case Primitive::kPrimChar:
1558 // Processing a Dex `int-to-float' instruction.
1559 locations->SetInAt(0, Location::RequiresRegister());
1560 locations->SetOut(Location::RequiresFpuRegister());
1561 break;
1562
1563 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001564 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001565 locations->SetInAt(0, Location::Any());
1566 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001567 break;
1568
Roland Levillaincff13742014-11-17 14:32:17 +00001569 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001570 // Processing a Dex `double-to-float' instruction.
1571 locations->SetInAt(0, Location::RequiresFpuRegister());
1572 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001573 break;
1574
1575 default:
1576 LOG(FATAL) << "Unexpected type conversion from " << input_type
1577 << " to " << result_type;
1578 };
1579 break;
1580
Roland Levillaindff1f282014-11-05 14:15:05 +00001581 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001582 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001583 case Primitive::kPrimBoolean:
1584 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001585 case Primitive::kPrimByte:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimChar:
1589 // Processing a Dex `int-to-double' instruction.
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetOut(Location::RequiresFpuRegister());
1592 break;
1593
1594 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001595 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001596 locations->SetInAt(0, Location::Any());
1597 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001598 break;
1599
Roland Levillaincff13742014-11-17 14:32:17 +00001600 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001601 // Processing a Dex `float-to-double' instruction.
1602 locations->SetInAt(0, Location::RequiresFpuRegister());
1603 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001604 break;
1605
1606 default:
1607 LOG(FATAL) << "Unexpected type conversion from " << input_type
1608 << " to " << result_type;
1609 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 break;
1611
1612 default:
1613 LOG(FATAL) << "Unexpected type conversion from " << input_type
1614 << " to " << result_type;
1615 }
1616}
1617
1618void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1619 LocationSummary* locations = conversion->GetLocations();
1620 Location out = locations->Out();
1621 Location in = locations->InAt(0);
1622 Primitive::Type result_type = conversion->GetResultType();
1623 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001624 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001625 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001626 case Primitive::kPrimByte:
1627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001628 case Primitive::kPrimBoolean:
1629 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimShort:
1631 case Primitive::kPrimInt:
1632 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001633 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001634 if (in.IsRegister()) {
1635 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001636 } else {
1637 DCHECK(in.GetConstant()->IsIntConstant());
1638 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1640 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001641 break;
1642
1643 default:
1644 LOG(FATAL) << "Unexpected type conversion from " << input_type
1645 << " to " << result_type;
1646 }
1647 break;
1648
Roland Levillain01a8d712014-11-14 16:27:39 +00001649 case Primitive::kPrimShort:
1650 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001651 case Primitive::kPrimBoolean:
1652 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimByte:
1654 case Primitive::kPrimInt:
1655 case Primitive::kPrimChar:
1656 // Processing a Dex `int-to-short' instruction.
1657 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001658 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001661 } else {
1662 DCHECK(in.GetConstant()->IsIntConstant());
1663 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 }
1666 break;
1667
1668 default:
1669 LOG(FATAL) << "Unexpected type conversion from " << input_type
1670 << " to " << result_type;
1671 }
1672 break;
1673
Roland Levillain946e1432014-11-11 17:35:19 +00001674 case Primitive::kPrimInt:
1675 switch (input_type) {
1676 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001677 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001678 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001680 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001682 } else {
1683 DCHECK(in.IsConstant());
1684 DCHECK(in.GetConstant()->IsLongConstant());
1685 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001687 }
1688 break;
1689
Roland Levillain3f8f9362014-12-02 17:45:01 +00001690 case Primitive::kPrimFloat: {
1691 // Processing a Dex `float-to-int' instruction.
1692 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1693 Register output = out.AsRegister<Register>();
1694 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1695 Label done, nan;
1696
1697 __ movl(output, Immediate(kPrimIntMax));
1698 // temp = int-to-float(output)
1699 __ cvtsi2ss(temp, output);
1700 // if input >= temp goto done
1701 __ comiss(input, temp);
1702 __ j(kAboveEqual, &done);
1703 // if input == NaN goto nan
1704 __ j(kUnordered, &nan);
1705 // output = float-to-int-truncate(input)
1706 __ cvttss2si(output, input);
1707 __ jmp(&done);
1708 __ Bind(&nan);
1709 // output = 0
1710 __ xorl(output, output);
1711 __ Bind(&done);
1712 break;
1713 }
1714
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 case Primitive::kPrimDouble: {
1716 // Processing a Dex `double-to-int' instruction.
1717 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1718 Register output = out.AsRegister<Register>();
1719 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1720 Label done, nan;
1721
1722 __ movl(output, Immediate(kPrimIntMax));
1723 // temp = int-to-double(output)
1724 __ cvtsi2sd(temp, output);
1725 // if input >= temp goto done
1726 __ comisd(input, temp);
1727 __ j(kAboveEqual, &done);
1728 // if input == NaN goto nan
1729 __ j(kUnordered, &nan);
1730 // output = double-to-int-truncate(input)
1731 __ cvttsd2si(output, input);
1732 __ jmp(&done);
1733 __ Bind(&nan);
1734 // output = 0
1735 __ xorl(output, output);
1736 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001737 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001738 }
Roland Levillain946e1432014-11-11 17:35:19 +00001739
1740 default:
1741 LOG(FATAL) << "Unexpected type conversion from " << input_type
1742 << " to " << result_type;
1743 }
1744 break;
1745
Roland Levillaindff1f282014-11-05 14:15:05 +00001746 case Primitive::kPrimLong:
1747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001748 case Primitive::kPrimBoolean:
1749 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimByte:
1751 case Primitive::kPrimShort:
1752 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001753 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001754 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001755 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1756 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 __ cdq();
1759 break;
1760
1761 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001762 // Processing a Dex `float-to-long' instruction.
1763 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001764 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1765 break;
1766
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001768 // Processing a Dex `double-to-long' instruction.
1769 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1770 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 break;
1772
1773 default:
1774 LOG(FATAL) << "Unexpected type conversion from " << input_type
1775 << " to " << result_type;
1776 }
1777 break;
1778
Roland Levillain981e4542014-11-14 11:47:14 +00001779 case Primitive::kPrimChar:
1780 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001781 case Primitive::kPrimBoolean:
1782 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimByte:
1784 case Primitive::kPrimShort:
1785 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001786 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1787 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001789 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001790 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001791 } else {
1792 DCHECK(in.GetConstant()->IsIntConstant());
1793 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 }
1796 break;
1797
1798 default:
1799 LOG(FATAL) << "Unexpected type conversion from " << input_type
1800 << " to " << result_type;
1801 }
1802 break;
1803
Roland Levillaindff1f282014-11-05 14:15:05 +00001804 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001805 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001806 case Primitive::kPrimBoolean:
1807 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001808 case Primitive::kPrimByte:
1809 case Primitive::kPrimShort:
1810 case Primitive::kPrimInt:
1811 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001812 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001813 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001814 break;
1815
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 case Primitive::kPrimLong: {
1817 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001818 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819
Roland Levillain232ade02015-04-20 15:14:36 +01001820 // Create stack space for the call to
1821 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1822 // TODO: enhance register allocator to ask for stack temporaries.
1823 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1824 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1825 __ subl(ESP, Immediate(adjustment));
1826 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001827
Roland Levillain232ade02015-04-20 15:14:36 +01001828 // Load the value to the FP stack, using temporaries if needed.
1829 PushOntoFPStack(in, 0, adjustment, false, true);
1830
1831 if (out.IsStackSlot()) {
1832 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1833 } else {
1834 __ fstps(Address(ESP, 0));
1835 Location stack_temp = Location::StackSlot(0);
1836 codegen_->Move32(out, stack_temp);
1837 }
1838
1839 // Remove the temporary stack space we allocated.
1840 if (adjustment != 0) {
1841 __ addl(ESP, Immediate(adjustment));
1842 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001843 break;
1844 }
1845
Roland Levillaincff13742014-11-17 14:32:17 +00001846 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001847 // Processing a Dex `double-to-float' instruction.
1848 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001849 break;
1850
1851 default:
1852 LOG(FATAL) << "Unexpected type conversion from " << input_type
1853 << " to " << result_type;
1854 };
1855 break;
1856
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001859 case Primitive::kPrimBoolean:
1860 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001861 case Primitive::kPrimByte:
1862 case Primitive::kPrimShort:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001865 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001867 break;
1868
Roland Levillain647b9ed2014-11-27 12:06:00 +00001869 case Primitive::kPrimLong: {
1870 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001871 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872
Roland Levillain232ade02015-04-20 15:14:36 +01001873 // Create stack space for the call to
1874 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1875 // TODO: enhance register allocator to ask for stack temporaries.
1876 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1877 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1878 __ subl(ESP, Immediate(adjustment));
1879 }
1880
1881 // Load the value to the FP stack, using temporaries if needed.
1882 PushOntoFPStack(in, 0, adjustment, false, true);
1883
1884 if (out.IsDoubleStackSlot()) {
1885 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1886 } else {
1887 __ fstpl(Address(ESP, 0));
1888 Location stack_temp = Location::DoubleStackSlot(0);
1889 codegen_->Move64(out, stack_temp);
1890 }
1891
1892 // Remove the temporary stack space we allocated.
1893 if (adjustment != 0) {
1894 __ addl(ESP, Immediate(adjustment));
1895 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001896 break;
1897 }
1898
Roland Levillaincff13742014-11-17 14:32:17 +00001899 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001900 // Processing a Dex `float-to-double' instruction.
1901 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
1914}
1915
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001916void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001917 LocationSummary* locations =
1918 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001919 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001920 case Primitive::kPrimInt: {
1921 locations->SetInAt(0, Location::RequiresRegister());
1922 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1924 break;
1925 }
1926
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetInAt(1, Location::Any());
1930 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932 }
1933
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 case Primitive::kPrimFloat:
1935 case Primitive::kPrimDouble: {
1936 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001937 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001942 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1944 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946}
1947
1948void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1949 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 Location first = locations->InAt(0);
1951 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001952 Location out = locations->Out();
1953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001954 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001957 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1958 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1959 } else {
1960 __ leal(out.AsRegister<Register>(), Address(
1961 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1962 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001964 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1965 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1966 __ addl(out.AsRegister<Register>(), Immediate(value));
1967 } else {
1968 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1969 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001970 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001971 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001973 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001974 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 }
1976
1977 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001978 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001979 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1980 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001981 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1983 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001985 } else {
1986 DCHECK(second.IsConstant()) << second;
1987 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1988 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1989 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 break;
1992 }
1993
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 case Primitive::kPrimFloat: {
1995 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 }
2000
2001 case Primitive::kPrimDouble: {
2002 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002003 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002004 }
2005 break;
2006 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002008 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002010 }
2011}
2012
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002013void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002014 LocationSummary* locations =
2015 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002016 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002017 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002018 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002019 locations->SetInAt(0, Location::RequiresRegister());
2020 locations->SetInAt(1, Location::Any());
2021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 break;
2023 }
Calin Juravle11351682014-10-23 15:38:15 +01002024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble: {
2026 locations->SetInAt(0, Location::RequiresFpuRegister());
2027 locations->SetInAt(1, Location::RequiresFpuRegister());
2028 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002029 break;
Calin Juravle11351682014-10-23 15:38:15 +01002030 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002031
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002032 default:
Calin Juravle11351682014-10-23 15:38:15 +01002033 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002034 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035}
2036
2037void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2038 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 Location first = locations->InAt(0);
2040 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002041 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002047 __ subl(first.AsRegister<Register>(),
2048 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002049 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002051 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002052 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002053 }
2054
2055 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002056 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002057 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2058 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002059 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002060 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 __ sbbl(first.AsRegisterPairHigh<Register>(),
2062 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002063 } else {
2064 DCHECK(second.IsConstant()) << second;
2065 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2066 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2067 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069 break;
2070 }
2071
Calin Juravle11351682014-10-23 15:38:15 +01002072 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074 break;
Calin Juravle11351682014-10-23 15:38:15 +01002075 }
2076
2077 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002079 break;
2080 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002082 default:
Calin Juravle11351682014-10-23 15:38:15 +01002083 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002084 }
2085}
2086
Calin Juravle34bacdf2014-10-07 20:23:36 +01002087void LocationsBuilderX86::VisitMul(HMul* mul) {
2088 LocationSummary* locations =
2089 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2090 switch (mul->GetResultType()) {
2091 case Primitive::kPrimInt:
2092 locations->SetInAt(0, Location::RequiresRegister());
2093 locations->SetInAt(1, Location::Any());
2094 locations->SetOut(Location::SameAsFirstInput());
2095 break;
2096 case Primitive::kPrimLong: {
2097 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098 locations->SetInAt(1, Location::Any());
2099 locations->SetOut(Location::SameAsFirstInput());
2100 // Needed for imul on 32bits with 64bits output.
2101 locations->AddTemp(Location::RegisterLocation(EAX));
2102 locations->AddTemp(Location::RegisterLocation(EDX));
2103 break;
2104 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002105 case Primitive::kPrimFloat:
2106 case Primitive::kPrimDouble: {
2107 locations->SetInAt(0, Location::RequiresFpuRegister());
2108 locations->SetInAt(1, Location::RequiresFpuRegister());
2109 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002111 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002112
2113 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002114 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115 }
2116}
2117
2118void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2119 LocationSummary* locations = mul->GetLocations();
2120 Location first = locations->InAt(0);
2121 Location second = locations->InAt(1);
2122 DCHECK(first.Equals(locations->Out()));
2123
2124 switch (mul->GetResultType()) {
2125 case Primitive::kPrimInt: {
2126 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002128 } else if (second.IsConstant()) {
2129 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 } else {
2132 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 }
2135 break;
2136 }
2137
2138 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139 Register in1_hi = first.AsRegisterPairHigh<Register>();
2140 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 Register eax = locations->GetTemp(0).AsRegister<Register>();
2142 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143
2144 DCHECK_EQ(EAX, eax);
2145 DCHECK_EQ(EDX, edx);
2146
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002147 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002148 // output: in1
2149 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2150 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2151 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002152 if (second.IsConstant()) {
2153 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002154
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002155 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2156 int32_t low_value = Low32Bits(value);
2157 int32_t high_value = High32Bits(value);
2158 Immediate low(low_value);
2159 Immediate high(high_value);
2160
2161 __ movl(eax, high);
2162 // eax <- in1.lo * in2.hi
2163 __ imull(eax, in1_lo);
2164 // in1.hi <- in1.hi * in2.lo
2165 __ imull(in1_hi, low);
2166 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2167 __ addl(in1_hi, eax);
2168 // move in2_lo to eax to prepare for double precision
2169 __ movl(eax, low);
2170 // edx:eax <- in1.lo * in2.lo
2171 __ mull(in1_lo);
2172 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2173 __ addl(in1_hi, edx);
2174 // in1.lo <- (in1.lo * in2.lo)[31:0];
2175 __ movl(in1_lo, eax);
2176 } else if (second.IsRegisterPair()) {
2177 Register in2_hi = second.AsRegisterPairHigh<Register>();
2178 Register in2_lo = second.AsRegisterPairLow<Register>();
2179
2180 __ movl(eax, in2_hi);
2181 // eax <- in1.lo * in2.hi
2182 __ imull(eax, in1_lo);
2183 // in1.hi <- in1.hi * in2.lo
2184 __ imull(in1_hi, in2_lo);
2185 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2186 __ addl(in1_hi, eax);
2187 // move in1_lo to eax to prepare for double precision
2188 __ movl(eax, in1_lo);
2189 // edx:eax <- in1.lo * in2.lo
2190 __ mull(in2_lo);
2191 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2192 __ addl(in1_hi, edx);
2193 // in1.lo <- (in1.lo * in2.lo)[31:0];
2194 __ movl(in1_lo, eax);
2195 } else {
2196 DCHECK(second.IsDoubleStackSlot()) << second;
2197 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2198 Address in2_lo(ESP, second.GetStackIndex());
2199
2200 __ movl(eax, in2_hi);
2201 // eax <- in1.lo * in2.hi
2202 __ imull(eax, in1_lo);
2203 // in1.hi <- in1.hi * in2.lo
2204 __ imull(in1_hi, in2_lo);
2205 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2206 __ addl(in1_hi, eax);
2207 // move in1_lo to eax to prepare for double precision
2208 __ movl(eax, in1_lo);
2209 // edx:eax <- in1.lo * in2.lo
2210 __ mull(in2_lo);
2211 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2212 __ addl(in1_hi, edx);
2213 // in1.lo <- (in1.lo * in2.lo)[31:0];
2214 __ movl(in1_lo, eax);
2215 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002216
2217 break;
2218 }
2219
Calin Juravleb5bfa962014-10-21 18:02:24 +01002220 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002221 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 }
2224
2225 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002226 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 break;
2228 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002229
2230 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002232 }
2233}
2234
Roland Levillain232ade02015-04-20 15:14:36 +01002235void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2236 uint32_t temp_offset,
2237 uint32_t stack_adjustment,
2238 bool is_fp,
2239 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002240 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002241 DCHECK(!is_wide);
2242 if (is_fp) {
2243 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2244 } else {
2245 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2246 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002247 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002248 DCHECK(is_wide);
2249 if (is_fp) {
2250 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2251 } else {
2252 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2253 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002254 } else {
2255 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002256 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002257 Location stack_temp = Location::StackSlot(temp_offset);
2258 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002259 if (is_fp) {
2260 __ flds(Address(ESP, temp_offset));
2261 } else {
2262 __ filds(Address(ESP, temp_offset));
2263 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002264 } else {
2265 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2266 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002267 if (is_fp) {
2268 __ fldl(Address(ESP, temp_offset));
2269 } else {
2270 __ fildl(Address(ESP, temp_offset));
2271 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002272 }
2273 }
2274}
2275
2276void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2277 Primitive::Type type = rem->GetResultType();
2278 bool is_float = type == Primitive::kPrimFloat;
2279 size_t elem_size = Primitive::ComponentSize(type);
2280 LocationSummary* locations = rem->GetLocations();
2281 Location first = locations->InAt(0);
2282 Location second = locations->InAt(1);
2283 Location out = locations->Out();
2284
2285 // Create stack space for 2 elements.
2286 // TODO: enhance register allocator to ask for stack temporaries.
2287 __ subl(ESP, Immediate(2 * elem_size));
2288
2289 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002290 const bool is_wide = !is_float;
2291 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2292 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002293
2294 // Loop doing FPREM until we stabilize.
2295 Label retry;
2296 __ Bind(&retry);
2297 __ fprem();
2298
2299 // Move FP status to AX.
2300 __ fstsw();
2301
2302 // And see if the argument reduction is complete. This is signaled by the
2303 // C2 FPU flag bit set to 0.
2304 __ andl(EAX, Immediate(kC2ConditionMask));
2305 __ j(kNotEqual, &retry);
2306
2307 // We have settled on the final value. Retrieve it into an XMM register.
2308 // Store FP top of stack to real stack.
2309 if (is_float) {
2310 __ fsts(Address(ESP, 0));
2311 } else {
2312 __ fstl(Address(ESP, 0));
2313 }
2314
2315 // Pop the 2 items from the FP stack.
2316 __ fucompp();
2317
2318 // Load the value from the stack into an XMM register.
2319 DCHECK(out.IsFpuRegister()) << out;
2320 if (is_float) {
2321 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2322 } else {
2323 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2324 }
2325
2326 // And remove the temporary stack space we allocated.
2327 __ addl(ESP, Immediate(2 * elem_size));
2328}
2329
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002330
2331void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2332 DCHECK(instruction->IsDiv() || instruction->IsRem());
2333
2334 LocationSummary* locations = instruction->GetLocations();
2335 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002336 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002337
2338 Register out_register = locations->Out().AsRegister<Register>();
2339 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002340 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002341
2342 DCHECK(imm == 1 || imm == -1);
2343
2344 if (instruction->IsRem()) {
2345 __ xorl(out_register, out_register);
2346 } else {
2347 __ movl(out_register, input_register);
2348 if (imm == -1) {
2349 __ negl(out_register);
2350 }
2351 }
2352}
2353
2354
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002355void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002356 LocationSummary* locations = instruction->GetLocations();
2357
2358 Register out_register = locations->Out().AsRegister<Register>();
2359 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002360 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002361
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002362 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002363 Register num = locations->GetTemp(0).AsRegister<Register>();
2364
2365 __ leal(num, Address(input_register, std::abs(imm) - 1));
2366 __ testl(input_register, input_register);
2367 __ cmovl(kGreaterEqual, num, input_register);
2368 int shift = CTZ(imm);
2369 __ sarl(num, Immediate(shift));
2370
2371 if (imm < 0) {
2372 __ negl(num);
2373 }
2374
2375 __ movl(out_register, num);
2376}
2377
2378void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2379 DCHECK(instruction->IsDiv() || instruction->IsRem());
2380
2381 LocationSummary* locations = instruction->GetLocations();
2382 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2383
2384 Register eax = locations->InAt(0).AsRegister<Register>();
2385 Register out = locations->Out().AsRegister<Register>();
2386 Register num;
2387 Register edx;
2388
2389 if (instruction->IsDiv()) {
2390 edx = locations->GetTemp(0).AsRegister<Register>();
2391 num = locations->GetTemp(1).AsRegister<Register>();
2392 } else {
2393 edx = locations->Out().AsRegister<Register>();
2394 num = locations->GetTemp(0).AsRegister<Register>();
2395 }
2396
2397 DCHECK_EQ(EAX, eax);
2398 DCHECK_EQ(EDX, edx);
2399 if (instruction->IsDiv()) {
2400 DCHECK_EQ(EAX, out);
2401 } else {
2402 DCHECK_EQ(EDX, out);
2403 }
2404
2405 int64_t magic;
2406 int shift;
2407 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2408
2409 Label ndiv;
2410 Label end;
2411 // If numerator is 0, the result is 0, no computation needed.
2412 __ testl(eax, eax);
2413 __ j(kNotEqual, &ndiv);
2414
2415 __ xorl(out, out);
2416 __ jmp(&end);
2417
2418 __ Bind(&ndiv);
2419
2420 // Save the numerator.
2421 __ movl(num, eax);
2422
2423 // EAX = magic
2424 __ movl(eax, Immediate(magic));
2425
2426 // EDX:EAX = magic * numerator
2427 __ imull(num);
2428
2429 if (imm > 0 && magic < 0) {
2430 // EDX += num
2431 __ addl(edx, num);
2432 } else if (imm < 0 && magic > 0) {
2433 __ subl(edx, num);
2434 }
2435
2436 // Shift if needed.
2437 if (shift != 0) {
2438 __ sarl(edx, Immediate(shift));
2439 }
2440
2441 // EDX += 1 if EDX < 0
2442 __ movl(eax, edx);
2443 __ shrl(edx, Immediate(31));
2444 __ addl(edx, eax);
2445
2446 if (instruction->IsRem()) {
2447 __ movl(eax, num);
2448 __ imull(edx, Immediate(imm));
2449 __ subl(eax, edx);
2450 __ movl(edx, eax);
2451 } else {
2452 __ movl(eax, edx);
2453 }
2454 __ Bind(&end);
2455}
2456
Calin Juravlebacfec32014-11-14 15:54:36 +00002457void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2458 DCHECK(instruction->IsDiv() || instruction->IsRem());
2459
2460 LocationSummary* locations = instruction->GetLocations();
2461 Location out = locations->Out();
2462 Location first = locations->InAt(0);
2463 Location second = locations->InAt(1);
2464 bool is_div = instruction->IsDiv();
2465
2466 switch (instruction->GetResultType()) {
2467 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(EAX, first.AsRegister<Register>());
2469 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002470
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002471 if (instruction->InputAt(1)->IsIntConstant()) {
2472 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002473
2474 if (imm == 0) {
2475 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2476 } else if (imm == 1 || imm == -1) {
2477 DivRemOneOrMinusOne(instruction);
2478 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002479 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002480 } else {
2481 DCHECK(imm <= -2 || imm >= 2);
2482 GenerateDivRemWithAnyConstant(instruction);
2483 }
2484 } else {
2485 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002486 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002487 is_div);
2488 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002489
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002490 Register second_reg = second.AsRegister<Register>();
2491 // 0x80000000/-1 triggers an arithmetic exception!
2492 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2493 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495 __ cmpl(second_reg, Immediate(-1));
2496 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 // edx:eax <- sign-extended of eax
2499 __ cdq();
2500 // eax = quotient, edx = remainder
2501 __ idivl(second_reg);
2502 __ Bind(slow_path->GetExitLabel());
2503 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002504 break;
2505 }
2506
2507 case Primitive::kPrimLong: {
2508 InvokeRuntimeCallingConvention calling_convention;
2509 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2510 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2511 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2512 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2513 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2514 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2515
2516 if (is_div) {
2517 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2518 } else {
2519 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2520 }
2521 uint32_t dex_pc = is_div
2522 ? instruction->AsDiv()->GetDexPc()
2523 : instruction->AsRem()->GetDexPc();
2524 codegen_->RecordPcInfo(instruction, dex_pc);
2525
2526 break;
2527 }
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2531 }
2532}
2533
Calin Juravle7c4954d2014-10-28 16:57:40 +00002534void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002535 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002536 ? LocationSummary::kCall
2537 : LocationSummary::kNoCall;
2538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2539
Calin Juravle7c4954d2014-10-28 16:57:40 +00002540 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002541 case Primitive::kPrimInt: {
2542 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002543 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002544 locations->SetOut(Location::SameAsFirstInput());
2545 // Intel uses edx:eax as the dividend.
2546 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2548 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2549 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002550 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 locations->AddTemp(Location::RequiresRegister());
2552 }
Calin Juravled0d48522014-11-04 16:40:20 +00002553 break;
2554 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002555 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002556 InvokeRuntimeCallingConvention calling_convention;
2557 locations->SetInAt(0, Location::RegisterPairLocation(
2558 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2559 locations->SetInAt(1, Location::RegisterPairLocation(
2560 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2561 // Runtime helper puts the result in EAX, EDX.
2562 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002563 break;
2564 }
2565 case Primitive::kPrimFloat:
2566 case Primitive::kPrimDouble: {
2567 locations->SetInAt(0, Location::RequiresFpuRegister());
2568 locations->SetInAt(1, Location::RequiresFpuRegister());
2569 locations->SetOut(Location::SameAsFirstInput());
2570 break;
2571 }
2572
2573 default:
2574 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2575 }
2576}
2577
2578void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2579 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002580 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002581 Location first = locations->InAt(0);
2582 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002583
2584 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002585 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002586 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002587 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002588 break;
2589 }
2590
2591 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002592 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 break;
2595 }
2596
2597 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002598 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002600 break;
2601 }
2602
2603 default:
2604 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2605 }
2606}
2607
Calin Juravlebacfec32014-11-14 15:54:36 +00002608void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002609 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002610
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002611 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2612 ? LocationSummary::kCall
2613 : LocationSummary::kNoCall;
2614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002615
Calin Juravled2ec87d2014-12-08 14:24:46 +00002616 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002617 case Primitive::kPrimInt: {
2618 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002619 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002620 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002621 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2622 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2623 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002624 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 locations->AddTemp(Location::RequiresRegister());
2626 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002627 break;
2628 }
2629 case Primitive::kPrimLong: {
2630 InvokeRuntimeCallingConvention calling_convention;
2631 locations->SetInAt(0, Location::RegisterPairLocation(
2632 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2633 locations->SetInAt(1, Location::RegisterPairLocation(
2634 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2635 // Runtime helper puts the result in EAX, EDX.
2636 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2637 break;
2638 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002639 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002640 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002641 locations->SetInAt(0, Location::Any());
2642 locations->SetInAt(1, Location::Any());
2643 locations->SetOut(Location::RequiresFpuRegister());
2644 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002645 break;
2646 }
2647
2648 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002649 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002650 }
2651}
2652
2653void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2654 Primitive::Type type = rem->GetResultType();
2655 switch (type) {
2656 case Primitive::kPrimInt:
2657 case Primitive::kPrimLong: {
2658 GenerateDivRemIntegral(rem);
2659 break;
2660 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002661 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002662 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002663 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002664 break;
2665 }
2666 default:
2667 LOG(FATAL) << "Unexpected rem type " << type;
2668 }
2669}
2670
Calin Juravled0d48522014-11-04 16:40:20 +00002671void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2672 LocationSummary* locations =
2673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002674 switch (instruction->GetType()) {
2675 case Primitive::kPrimInt: {
2676 locations->SetInAt(0, Location::Any());
2677 break;
2678 }
2679 case Primitive::kPrimLong: {
2680 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2681 if (!instruction->IsConstant()) {
2682 locations->AddTemp(Location::RequiresRegister());
2683 }
2684 break;
2685 }
2686 default:
2687 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2688 }
Calin Juravled0d48522014-11-04 16:40:20 +00002689 if (instruction->HasUses()) {
2690 locations->SetOut(Location::SameAsFirstInput());
2691 }
2692}
2693
2694void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2695 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2696 codegen_->AddSlowPath(slow_path);
2697
2698 LocationSummary* locations = instruction->GetLocations();
2699 Location value = locations->InAt(0);
2700
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002701 switch (instruction->GetType()) {
2702 case Primitive::kPrimInt: {
2703 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002704 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002705 __ j(kEqual, slow_path->GetEntryLabel());
2706 } else if (value.IsStackSlot()) {
2707 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2708 __ j(kEqual, slow_path->GetEntryLabel());
2709 } else {
2710 DCHECK(value.IsConstant()) << value;
2711 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2712 __ jmp(slow_path->GetEntryLabel());
2713 }
2714 }
2715 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002716 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002717 case Primitive::kPrimLong: {
2718 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002720 __ movl(temp, value.AsRegisterPairLow<Register>());
2721 __ orl(temp, value.AsRegisterPairHigh<Register>());
2722 __ j(kEqual, slow_path->GetEntryLabel());
2723 } else {
2724 DCHECK(value.IsConstant()) << value;
2725 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2726 __ jmp(slow_path->GetEntryLabel());
2727 }
2728 }
2729 break;
2730 }
2731 default:
2732 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002733 }
Calin Juravled0d48522014-11-04 16:40:20 +00002734}
2735
Calin Juravle9aec02f2014-11-18 23:06:35 +00002736void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2737 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2738
2739 LocationSummary* locations =
2740 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2741
2742 switch (op->GetResultType()) {
2743 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002744 locations->SetInAt(0, Location::RequiresRegister());
2745 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002746 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2747 locations->SetOut(Location::SameAsFirstInput());
2748 break;
2749 }
2750 case Primitive::kPrimLong: {
2751 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002752 // The shift count needs to be in CL.
2753 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002754 locations->SetOut(Location::SameAsFirstInput());
2755 break;
2756 }
2757 default:
2758 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2759 }
2760}
2761
2762void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2763 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2764
2765 LocationSummary* locations = op->GetLocations();
2766 Location first = locations->InAt(0);
2767 Location second = locations->InAt(1);
2768 DCHECK(first.Equals(locations->Out()));
2769
2770 switch (op->GetResultType()) {
2771 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002772 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002773 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002775 DCHECK_EQ(ECX, second_reg);
2776 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002777 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002778 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002779 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002780 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 }
2783 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002784 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2785 if (op->IsShl()) {
2786 __ shll(first_reg, imm);
2787 } else if (op->IsShr()) {
2788 __ sarl(first_reg, imm);
2789 } else {
2790 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002791 }
2792 }
2793 break;
2794 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002795 case Primitive::kPrimLong: {
2796 Register second_reg = second.AsRegister<Register>();
2797 DCHECK_EQ(ECX, second_reg);
2798 if (op->IsShl()) {
2799 GenerateShlLong(first, second_reg);
2800 } else if (op->IsShr()) {
2801 GenerateShrLong(first, second_reg);
2802 } else {
2803 GenerateUShrLong(first, second_reg);
2804 }
2805 break;
2806 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002807 default:
2808 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2809 }
2810}
2811
2812void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2813 Label done;
2814 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2815 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2816 __ testl(shifter, Immediate(32));
2817 __ j(kEqual, &done);
2818 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2819 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2820 __ Bind(&done);
2821}
2822
2823void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2824 Label done;
2825 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2826 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2827 __ testl(shifter, Immediate(32));
2828 __ j(kEqual, &done);
2829 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2830 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2831 __ Bind(&done);
2832}
2833
2834void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2835 Label done;
2836 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2837 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2838 __ testl(shifter, Immediate(32));
2839 __ j(kEqual, &done);
2840 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2841 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2842 __ Bind(&done);
2843}
2844
2845void LocationsBuilderX86::VisitShl(HShl* shl) {
2846 HandleShift(shl);
2847}
2848
2849void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2850 HandleShift(shl);
2851}
2852
2853void LocationsBuilderX86::VisitShr(HShr* shr) {
2854 HandleShift(shr);
2855}
2856
2857void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2858 HandleShift(shr);
2859}
2860
2861void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2862 HandleShift(ushr);
2863}
2864
2865void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2866 HandleShift(ushr);
2867}
2868
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002869void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002870 LocationSummary* locations =
2871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002873 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002874 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2875 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002876}
2877
2878void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2879 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002880 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002881 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002882
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002883 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002884
Nicolas Geoffray39468442014-09-02 15:17:15 +01002885 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002886 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002887}
2888
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002889void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2890 LocationSummary* locations =
2891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2892 locations->SetOut(Location::RegisterLocation(EAX));
2893 InvokeRuntimeCallingConvention calling_convention;
2894 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002895 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2896 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002897}
2898
2899void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2900 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002901 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002902 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2903
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002904 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002905
2906 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2907 DCHECK(!codegen_->IsLeafMethod());
2908}
2909
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002910void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002911 LocationSummary* locations =
2912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002913 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2914 if (location.IsStackSlot()) {
2915 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2916 } else if (location.IsDoubleStackSlot()) {
2917 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002918 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002919 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920}
2921
2922void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002923 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924}
2925
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002926void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002927 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002928 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002931}
2932
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002933void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2934 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002935 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002936 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002937 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002938 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002939 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002941 break;
2942
2943 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002944 __ notl(out.AsRegisterPairLow<Register>());
2945 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002946 break;
2947
2948 default:
2949 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2950 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002951}
2952
David Brazdil66d126e2015-04-03 16:02:44 +01002953void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2954 LocationSummary* locations =
2955 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2956 locations->SetInAt(0, Location::RequiresRegister());
2957 locations->SetOut(Location::SameAsFirstInput());
2958}
2959
2960void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002961 LocationSummary* locations = bool_not->GetLocations();
2962 Location in = locations->InAt(0);
2963 Location out = locations->Out();
2964 DCHECK(in.Equals(out));
2965 __ xorl(out.AsRegister<Register>(), Immediate(1));
2966}
2967
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002968void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002969 LocationSummary* locations =
2970 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002971 switch (compare->InputAt(0)->GetType()) {
2972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002974 locations->SetInAt(1, Location::Any());
2975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2976 break;
2977 }
2978 case Primitive::kPrimFloat:
2979 case Primitive::kPrimDouble: {
2980 locations->SetInAt(0, Location::RequiresFpuRegister());
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
2982 locations->SetOut(Location::RequiresRegister());
2983 break;
2984 }
2985 default:
2986 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2987 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002988}
2989
2990void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002991 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002993 Location left = locations->InAt(0);
2994 Location right = locations->InAt(1);
2995
2996 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002997 switch (compare->InputAt(0)->GetType()) {
2998 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002999 Register left_low = left.AsRegisterPairLow<Register>();
3000 Register left_high = left.AsRegisterPairHigh<Register>();
3001 int32_t val_low = 0;
3002 int32_t val_high = 0;
3003 bool right_is_const = false;
3004
3005 if (right.IsConstant()) {
3006 DCHECK(right.GetConstant()->IsLongConstant());
3007 right_is_const = true;
3008 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3009 val_low = Low32Bits(val);
3010 val_high = High32Bits(val);
3011 }
3012
Calin Juravleddb7df22014-11-25 20:56:51 +00003013 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003014 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003015 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003016 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003017 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003018 DCHECK(right_is_const) << right;
3019 if (val_high == 0) {
3020 __ testl(left_high, left_high);
3021 } else {
3022 __ cmpl(left_high, Immediate(val_high));
3023 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003024 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003025 __ j(kLess, &less); // Signed compare.
3026 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003027 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003028 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003029 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003030 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003032 DCHECK(right_is_const) << right;
3033 if (val_low == 0) {
3034 __ testl(left_low, left_low);
3035 } else {
3036 __ cmpl(left_low, Immediate(val_low));
3037 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003038 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003039 break;
3040 }
3041 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003043 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3044 break;
3045 }
3046 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003048 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003049 break;
3050 }
3051 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003053 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003054 __ movl(out, Immediate(0));
3055 __ j(kEqual, &done);
3056 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3057
3058 __ Bind(&greater);
3059 __ movl(out, Immediate(1));
3060 __ jmp(&done);
3061
3062 __ Bind(&less);
3063 __ movl(out, Immediate(-1));
3064
3065 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003066}
3067
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003068void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003069 LocationSummary* locations =
3070 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003071 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3072 locations->SetInAt(i, Location::Any());
3073 }
3074 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003075}
3076
3077void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003078 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003079 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003080}
3081
Calin Juravle52c48962014-12-16 17:02:57 +00003082void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3083 /*
3084 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3085 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3086 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3087 */
3088 switch (kind) {
3089 case MemBarrierKind::kAnyAny: {
3090 __ mfence();
3091 break;
3092 }
3093 case MemBarrierKind::kAnyStore:
3094 case MemBarrierKind::kLoadAny:
3095 case MemBarrierKind::kStoreStore: {
3096 // nop
3097 break;
3098 }
3099 default:
3100 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003101 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003102}
3103
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003104
Mark Mendell09ed1a32015-03-25 08:30:06 -04003105void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3106 Register temp) {
3107 // TODO: Implement all kinds of calls:
3108 // 1) boot -> boot
3109 // 2) app -> boot
3110 // 3) app -> app
3111 //
3112 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3113 // temp = method;
3114 LoadCurrentMethod(temp);
3115 if (!invoke->IsRecursive()) {
3116 // temp = temp->dex_cache_resolved_methods_;
3117 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3118 // temp = temp[index_in_cache]
3119 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3120 // (temp + offset_of_quick_compiled_code)()
3121 __ call(Address(
3122 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3123 } else {
3124 __ call(GetFrameEntryLabel());
3125 }
3126
3127 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003128}
3129
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003130void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003131 Label is_null;
3132 __ testl(value, value);
3133 __ j(kEqual, &is_null);
3134 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3135 __ movl(temp, object);
3136 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003137 __ movb(Address(temp, card, TIMES_1, 0),
3138 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003139 __ Bind(&is_null);
3140}
3141
Calin Juravle52c48962014-12-16 17:02:57 +00003142void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3143 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003144 LocationSummary* locations =
3145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003146 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003147
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003148 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3149 locations->SetOut(Location::RequiresFpuRegister());
3150 } else {
3151 // The output overlaps in case of long: we don't want the low move to overwrite
3152 // the object's location.
3153 locations->SetOut(Location::RequiresRegister(),
3154 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3155 : Location::kNoOutputOverlap);
3156 }
Calin Juravle52c48962014-12-16 17:02:57 +00003157
3158 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3159 // Long values can be loaded atomically into an XMM using movsd.
3160 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3161 // and then copy the XMM into the output 32bits at a time).
3162 locations->AddTemp(Location::RequiresFpuRegister());
3163 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003164}
3165
Calin Juravle52c48962014-12-16 17:02:57 +00003166void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3167 const FieldInfo& field_info) {
3168 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003169
Calin Juravle52c48962014-12-16 17:02:57 +00003170 LocationSummary* locations = instruction->GetLocations();
3171 Register base = locations->InAt(0).AsRegister<Register>();
3172 Location out = locations->Out();
3173 bool is_volatile = field_info.IsVolatile();
3174 Primitive::Type field_type = field_info.GetFieldType();
3175 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3176
3177 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003178 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003179 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003180 break;
3181 }
3182
3183 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003184 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003185 break;
3186 }
3187
3188 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003189 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003190 break;
3191 }
3192
3193 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003194 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003195 break;
3196 }
3197
3198 case Primitive::kPrimInt:
3199 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003200 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003201 break;
3202 }
3203
3204 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003205 if (is_volatile) {
3206 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3207 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003208 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003209 __ movd(out.AsRegisterPairLow<Register>(), temp);
3210 __ psrlq(temp, Immediate(32));
3211 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3212 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003213 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003214 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003215 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003216 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3217 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003218 break;
3219 }
3220
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003221 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003222 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003223 break;
3224 }
3225
3226 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003227 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003228 break;
3229 }
3230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003231 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003232 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003233 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003234 }
Calin Juravle52c48962014-12-16 17:02:57 +00003235
Calin Juravle77520bc2015-01-12 18:45:46 +00003236 // Longs are handled in the switch.
3237 if (field_type != Primitive::kPrimLong) {
3238 codegen_->MaybeRecordImplicitNullCheck(instruction);
3239 }
3240
Calin Juravle52c48962014-12-16 17:02:57 +00003241 if (is_volatile) {
3242 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3243 }
3244}
3245
3246void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3247 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3248
3249 LocationSummary* locations =
3250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3251 locations->SetInAt(0, Location::RequiresRegister());
3252 bool is_volatile = field_info.IsVolatile();
3253 Primitive::Type field_type = field_info.GetFieldType();
3254 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3255 || (field_type == Primitive::kPrimByte);
3256
3257 // The register allocator does not support multiple
3258 // inputs that die at entry with one in a specific register.
3259 if (is_byte_type) {
3260 // Ensure the value is in a byte register.
3261 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003262 } else if (Primitive::IsFloatingPointType(field_type)) {
3263 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003264 } else {
3265 locations->SetInAt(1, Location::RequiresRegister());
3266 }
3267 // Temporary registers for the write barrier.
3268 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3269 locations->AddTemp(Location::RequiresRegister());
3270 // Ensure the card is in a byte register.
3271 locations->AddTemp(Location::RegisterLocation(ECX));
3272 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3273 // 64bits value can be atomically written to an address with movsd and an XMM register.
3274 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3275 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3276 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3277 // isolated cases when we need this it isn't worth adding the extra complexity.
3278 locations->AddTemp(Location::RequiresFpuRegister());
3279 locations->AddTemp(Location::RequiresFpuRegister());
3280 }
3281}
3282
3283void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3284 const FieldInfo& field_info) {
3285 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3286
3287 LocationSummary* locations = instruction->GetLocations();
3288 Register base = locations->InAt(0).AsRegister<Register>();
3289 Location value = locations->InAt(1);
3290 bool is_volatile = field_info.IsVolatile();
3291 Primitive::Type field_type = field_info.GetFieldType();
3292 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3293
3294 if (is_volatile) {
3295 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3296 }
3297
3298 switch (field_type) {
3299 case Primitive::kPrimBoolean:
3300 case Primitive::kPrimByte: {
3301 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3302 break;
3303 }
3304
3305 case Primitive::kPrimShort:
3306 case Primitive::kPrimChar: {
3307 __ movw(Address(base, offset), value.AsRegister<Register>());
3308 break;
3309 }
3310
3311 case Primitive::kPrimInt:
3312 case Primitive::kPrimNot: {
3313 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003314 break;
3315 }
3316
3317 case Primitive::kPrimLong: {
3318 if (is_volatile) {
3319 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3320 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3321 __ movd(temp1, value.AsRegisterPairLow<Register>());
3322 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3323 __ punpckldq(temp1, temp2);
3324 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003326 } else {
3327 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003328 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003329 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3330 }
3331 break;
3332 }
3333
3334 case Primitive::kPrimFloat: {
3335 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3336 break;
3337 }
3338
3339 case Primitive::kPrimDouble: {
3340 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3341 break;
3342 }
3343
3344 case Primitive::kPrimVoid:
3345 LOG(FATAL) << "Unreachable type " << field_type;
3346 UNREACHABLE();
3347 }
3348
Calin Juravle77520bc2015-01-12 18:45:46 +00003349 // Longs are handled in the switch.
3350 if (field_type != Primitive::kPrimLong) {
3351 codegen_->MaybeRecordImplicitNullCheck(instruction);
3352 }
3353
3354 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3355 Register temp = locations->GetTemp(0).AsRegister<Register>();
3356 Register card = locations->GetTemp(1).AsRegister<Register>();
3357 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3358 }
3359
Calin Juravle52c48962014-12-16 17:02:57 +00003360 if (is_volatile) {
3361 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3362 }
3363}
3364
3365void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3366 HandleFieldGet(instruction, instruction->GetFieldInfo());
3367}
3368
3369void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3370 HandleFieldGet(instruction, instruction->GetFieldInfo());
3371}
3372
3373void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3374 HandleFieldSet(instruction, instruction->GetFieldInfo());
3375}
3376
3377void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3378 HandleFieldSet(instruction, instruction->GetFieldInfo());
3379}
3380
3381void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3382 HandleFieldSet(instruction, instruction->GetFieldInfo());
3383}
3384
3385void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3386 HandleFieldSet(instruction, instruction->GetFieldInfo());
3387}
3388
3389void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3390 HandleFieldGet(instruction, instruction->GetFieldInfo());
3391}
3392
3393void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3394 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003395}
3396
3397void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003398 LocationSummary* locations =
3399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003400 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3401 ? Location::RequiresRegister()
3402 : Location::Any();
3403 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003404 if (instruction->HasUses()) {
3405 locations->SetOut(Location::SameAsFirstInput());
3406 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003407}
3408
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003409void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003410 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3411 return;
3412 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003413 LocationSummary* locations = instruction->GetLocations();
3414 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003415
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003416 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3417 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3418}
3419
3420void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003421 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003422 codegen_->AddSlowPath(slow_path);
3423
3424 LocationSummary* locations = instruction->GetLocations();
3425 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426
3427 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003428 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003429 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003431 } else {
3432 DCHECK(obj.IsConstant()) << obj;
3433 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3434 __ jmp(slow_path->GetEntryLabel());
3435 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003436 }
3437 __ j(kEqual, slow_path->GetEntryLabel());
3438}
3439
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003440void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3441 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3442 GenerateImplicitNullCheck(instruction);
3443 } else {
3444 GenerateExplicitNullCheck(instruction);
3445 }
3446}
3447
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003449 LocationSummary* locations =
3450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003451 locations->SetInAt(0, Location::RequiresRegister());
3452 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003453 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3454 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3455 } else {
3456 // The output overlaps in case of long: we don't want the low move to overwrite
3457 // the array's location.
3458 locations->SetOut(Location::RequiresRegister(),
3459 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3460 : Location::kNoOutputOverlap);
3461 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003462}
3463
3464void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3465 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003467 Location index = locations->InAt(1);
3468
Calin Juravle77520bc2015-01-12 18:45:46 +00003469 Primitive::Type type = instruction->GetType();
3470 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003471 case Primitive::kPrimBoolean: {
3472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003474 if (index.IsConstant()) {
3475 __ movzxb(out, Address(obj,
3476 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3477 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003478 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003479 }
3480 break;
3481 }
3482
3483 case Primitive::kPrimByte: {
3484 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003486 if (index.IsConstant()) {
3487 __ movsxb(out, Address(obj,
3488 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3489 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003491 }
3492 break;
3493 }
3494
3495 case Primitive::kPrimShort: {
3496 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003498 if (index.IsConstant()) {
3499 __ movsxw(out, Address(obj,
3500 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3501 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003502 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003503 }
3504 break;
3505 }
3506
3507 case Primitive::kPrimChar: {
3508 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003509 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510 if (index.IsConstant()) {
3511 __ movzxw(out, Address(obj,
3512 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3513 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003515 }
3516 break;
3517 }
3518
3519 case Primitive::kPrimInt:
3520 case Primitive::kPrimNot: {
3521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 if (index.IsConstant()) {
3524 __ movl(out, Address(obj,
3525 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3526 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 }
3529 break;
3530 }
3531
3532 case Primitive::kPrimLong: {
3533 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003534 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003535 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 if (index.IsConstant()) {
3537 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003538 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003539 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003540 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003542 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003545 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003546 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003547 }
3548 break;
3549 }
3550
Mark Mendell7c8d0092015-01-26 11:21:33 -05003551 case Primitive::kPrimFloat: {
3552 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3553 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3554 if (index.IsConstant()) {
3555 __ movss(out, Address(obj,
3556 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3557 } else {
3558 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3559 }
3560 break;
3561 }
3562
3563 case Primitive::kPrimDouble: {
3564 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3565 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3566 if (index.IsConstant()) {
3567 __ movsd(out, Address(obj,
3568 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3569 } else {
3570 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3571 }
3572 break;
3573 }
3574
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003576 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003577 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003579
3580 if (type != Primitive::kPrimLong) {
3581 codegen_->MaybeRecordImplicitNullCheck(instruction);
3582 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003583}
3584
3585void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003586 // This location builder might end up asking to up to four registers, which is
3587 // not currently possible for baseline. The situation in which we need four
3588 // registers cannot be met by baseline though, because it has not run any
3589 // optimization.
3590
Nicolas Geoffray39468442014-09-02 15:17:15 +01003591 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003592 bool needs_write_barrier =
3593 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3594
Mark Mendell5f874182015-03-04 15:42:45 -05003595 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003596
Nicolas Geoffray39468442014-09-02 15:17:15 +01003597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3598 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003599 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003600
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003601 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003603 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3604 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3605 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003607 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3608 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003609 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003610 // In case of a byte operation, the register allocator does not support multiple
3611 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003612 locations->SetInAt(0, Location::RequiresRegister());
3613 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003614 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003615 // Ensure the value is in a byte register.
3616 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003617 } else if (Primitive::IsFloatingPointType(value_type)) {
3618 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003620 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003622 // Temporary registers for the write barrier.
3623 if (needs_write_barrier) {
3624 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003625 // Ensure the card is in a byte register.
3626 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003627 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629}
3630
3631void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3632 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003635 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003636 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003637 bool needs_runtime_call = locations->WillCall();
3638 bool needs_write_barrier =
3639 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640
3641 switch (value_type) {
3642 case Primitive::kPrimBoolean:
3643 case Primitive::kPrimByte: {
3644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 if (index.IsConstant()) {
3646 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003647 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003648 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003649 } else {
3650 __ movb(Address(obj, offset),
3651 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003654 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003656 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003657 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003659 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3660 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 break;
3664 }
3665
3666 case Primitive::kPrimShort:
3667 case Primitive::kPrimChar: {
3668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003669 if (index.IsConstant()) {
3670 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003671 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003673 } else {
3674 __ movw(Address(obj, offset),
3675 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3676 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003677 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003678 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003679 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3680 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003681 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003683 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3684 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003685 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003686 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003687 break;
3688 }
3689
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003690 case Primitive::kPrimInt:
3691 case Primitive::kPrimNot: {
3692 if (!needs_runtime_call) {
3693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3694 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003695 size_t offset =
3696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003697 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 } else {
3700 DCHECK(value.IsConstant()) << value;
3701 __ movl(Address(obj, offset),
3702 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3703 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003704 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003705 DCHECK(index.IsRegister()) << index;
3706 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003707 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3708 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 } else {
3710 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003712 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3713 }
3714 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003715 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003716
3717 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 Register temp = locations->GetTemp(0).AsRegister<Register>();
3719 Register card = locations->GetTemp(1).AsRegister<Register>();
3720 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003723 DCHECK_EQ(value_type, Primitive::kPrimNot);
3724 DCHECK(!codegen_->IsLeafMethod());
3725 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3726 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 }
3728 break;
3729 }
3730
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 case Primitive::kPrimLong: {
3732 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733 if (index.IsConstant()) {
3734 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003735 if (value.IsRegisterPair()) {
3736 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003737 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003738 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003739 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003740 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003741 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3742 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003743 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003744 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003747 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003749 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003750 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003752 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003753 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003754 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003755 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003758 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003760 Immediate(High32Bits(val)));
3761 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 }
3763 break;
3764 }
3765
Mark Mendell7c8d0092015-01-26 11:21:33 -05003766 case Primitive::kPrimFloat: {
3767 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3768 DCHECK(value.IsFpuRegister());
3769 if (index.IsConstant()) {
3770 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3771 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3772 } else {
3773 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3774 value.AsFpuRegister<XmmRegister>());
3775 }
3776 break;
3777 }
3778
3779 case Primitive::kPrimDouble: {
3780 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3781 DCHECK(value.IsFpuRegister());
3782 if (index.IsConstant()) {
3783 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3784 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3785 } else {
3786 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3787 value.AsFpuRegister<XmmRegister>());
3788 }
3789 break;
3790 }
3791
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 case Primitive::kPrimVoid:
3793 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003794 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003795 }
3796}
3797
3798void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3799 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003800 locations->SetInAt(0, Location::RequiresRegister());
3801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 instruction->SetLocations(locations);
3803}
3804
3805void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3806 LocationSummary* locations = instruction->GetLocations();
3807 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 Register obj = locations->InAt(0).AsRegister<Register>();
3809 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003810 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812}
3813
3814void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003815 LocationSummary* locations =
3816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003817 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003818 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003819 if (instruction->HasUses()) {
3820 locations->SetOut(Location::SameAsFirstInput());
3821 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822}
3823
3824void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3825 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003826 Location index_loc = locations->InAt(0);
3827 Location length_loc = locations->InAt(1);
3828 SlowPathCodeX86* slow_path =
3829 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830
Mark Mendell99dbd682015-04-22 16:18:52 -04003831 if (length_loc.IsConstant()) {
3832 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3833 if (index_loc.IsConstant()) {
3834 // BCE will remove the bounds check if we are guarenteed to pass.
3835 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3836 if (index < 0 || index >= length) {
3837 codegen_->AddSlowPath(slow_path);
3838 __ jmp(slow_path->GetEntryLabel());
3839 } else {
3840 // Some optimization after BCE may have generated this, and we should not
3841 // generate a bounds check if it is a valid range.
3842 }
3843 return;
3844 }
3845
3846 // We have to reverse the jump condition because the length is the constant.
3847 Register index_reg = index_loc.AsRegister<Register>();
3848 __ cmpl(index_reg, Immediate(length));
3849 codegen_->AddSlowPath(slow_path);
3850 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003851 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003852 Register length = length_loc.AsRegister<Register>();
3853 if (index_loc.IsConstant()) {
3854 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3855 __ cmpl(length, Immediate(value));
3856 } else {
3857 __ cmpl(length, index_loc.AsRegister<Register>());
3858 }
3859 codegen_->AddSlowPath(slow_path);
3860 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003861 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003862}
3863
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003864void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3865 temp->SetLocations(nullptr);
3866}
3867
3868void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3869 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003870 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003871}
3872
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003873void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003874 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003875 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003876}
3877
3878void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003879 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3880}
3881
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003882void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3884}
3885
3886void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003887 HBasicBlock* block = instruction->GetBlock();
3888 if (block->GetLoopInformation() != nullptr) {
3889 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3890 // The back edge will generate the suspend check.
3891 return;
3892 }
3893 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3894 // The goto will generate the suspend check.
3895 return;
3896 }
3897 GenerateSuspendCheck(instruction, nullptr);
3898}
3899
3900void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3901 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003902 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003903 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003904 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003905 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003906 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003907 if (successor == nullptr) {
3908 __ j(kNotEqual, slow_path->GetEntryLabel());
3909 __ Bind(slow_path->GetReturnLabel());
3910 } else {
3911 __ j(kEqual, codegen_->GetLabelOf(successor));
3912 __ jmp(slow_path->GetEntryLabel());
3913 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003914}
3915
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003916X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3917 return codegen_->GetAssembler();
3918}
3919
Mark Mendell7c8d0092015-01-26 11:21:33 -05003920void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003921 ScratchRegisterScope ensure_scratch(
3922 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3923 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3924 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3925 __ movl(temp_reg, Address(ESP, src + stack_offset));
3926 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003927}
3928
3929void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003930 ScratchRegisterScope ensure_scratch(
3931 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3932 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3933 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3934 __ movl(temp_reg, Address(ESP, src + stack_offset));
3935 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3936 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3937 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003938}
3939
3940void ParallelMoveResolverX86::EmitMove(size_t index) {
3941 MoveOperands* move = moves_.Get(index);
3942 Location source = move->GetSource();
3943 Location destination = move->GetDestination();
3944
3945 if (source.IsRegister()) {
3946 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003947 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003948 } else {
3949 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003950 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003951 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003952 } else if (source.IsFpuRegister()) {
3953 if (destination.IsFpuRegister()) {
3954 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3955 } else if (destination.IsStackSlot()) {
3956 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3957 } else {
3958 DCHECK(destination.IsDoubleStackSlot());
3959 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3960 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003961 } else if (source.IsStackSlot()) {
3962 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003964 } else if (destination.IsFpuRegister()) {
3965 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003966 } else {
3967 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003968 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3969 }
3970 } else if (source.IsDoubleStackSlot()) {
3971 if (destination.IsFpuRegister()) {
3972 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3973 } else {
3974 DCHECK(destination.IsDoubleStackSlot()) << destination;
3975 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003976 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003977 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003978 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003979 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003980 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003981 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003982 if (value == 0) {
3983 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3984 } else {
3985 __ movl(destination.AsRegister<Register>(), Immediate(value));
3986 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003987 } else {
3988 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003989 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003990 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003991 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003992 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003993 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003994 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003995 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003996 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3997 if (value == 0) {
3998 // Easy handling of 0.0.
3999 __ xorps(dest, dest);
4000 } else {
4001 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004002 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4003 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4004 __ movl(temp, Immediate(value));
4005 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004006 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004007 } else {
4008 DCHECK(destination.IsStackSlot()) << destination;
4009 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4010 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004011 } else if (constant->IsLongConstant()) {
4012 int64_t value = constant->AsLongConstant()->GetValue();
4013 int32_t low_value = Low32Bits(value);
4014 int32_t high_value = High32Bits(value);
4015 Immediate low(low_value);
4016 Immediate high(high_value);
4017 if (destination.IsDoubleStackSlot()) {
4018 __ movl(Address(ESP, destination.GetStackIndex()), low);
4019 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4020 } else {
4021 __ movl(destination.AsRegisterPairLow<Register>(), low);
4022 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4023 }
4024 } else {
4025 DCHECK(constant->IsDoubleConstant());
4026 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004027 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004028 int32_t low_value = Low32Bits(value);
4029 int32_t high_value = High32Bits(value);
4030 Immediate low(low_value);
4031 Immediate high(high_value);
4032 if (destination.IsFpuRegister()) {
4033 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4034 if (value == 0) {
4035 // Easy handling of 0.0.
4036 __ xorpd(dest, dest);
4037 } else {
4038 __ pushl(high);
4039 __ pushl(low);
4040 __ movsd(dest, Address(ESP, 0));
4041 __ addl(ESP, Immediate(8));
4042 }
4043 } else {
4044 DCHECK(destination.IsDoubleStackSlot()) << destination;
4045 __ movl(Address(ESP, destination.GetStackIndex()), low);
4046 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4047 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004048 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004049 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004050 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004051 }
4052}
4053
Mark Mendella5c19ce2015-04-01 12:51:05 -04004054void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004055 Register suggested_scratch = reg == EAX ? EBX : EAX;
4056 ScratchRegisterScope ensure_scratch(
4057 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4058
4059 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4060 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4061 __ movl(Address(ESP, mem + stack_offset), reg);
4062 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004063}
4064
Mark Mendell7c8d0092015-01-26 11:21:33 -05004065void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004066 ScratchRegisterScope ensure_scratch(
4067 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4068
4069 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4070 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4071 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4072 __ movss(Address(ESP, mem + stack_offset), reg);
4073 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004074}
4075
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004076void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004077 ScratchRegisterScope ensure_scratch1(
4078 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004079
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004080 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4081 ScratchRegisterScope ensure_scratch2(
4082 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004083
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004084 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4085 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4086 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4087 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4088 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4089 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004090}
4091
4092void ParallelMoveResolverX86::EmitSwap(size_t index) {
4093 MoveOperands* move = moves_.Get(index);
4094 Location source = move->GetSource();
4095 Location destination = move->GetDestination();
4096
4097 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004098 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004099 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004100 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004101 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004103 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4104 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004105 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4106 // Use XOR Swap algorithm to avoid a temporary.
4107 DCHECK_NE(source.reg(), destination.reg());
4108 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4109 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4110 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4111 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4112 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4113 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4114 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004115 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4116 // Take advantage of the 16 bytes in the XMM register.
4117 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4118 Address stack(ESP, destination.GetStackIndex());
4119 // Load the double into the high doubleword.
4120 __ movhpd(reg, stack);
4121
4122 // Store the low double into the destination.
4123 __ movsd(stack, reg);
4124
4125 // Move the high double to the low double.
4126 __ psrldq(reg, Immediate(8));
4127 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4128 // Take advantage of the 16 bytes in the XMM register.
4129 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4130 Address stack(ESP, source.GetStackIndex());
4131 // Load the double into the high doubleword.
4132 __ movhpd(reg, stack);
4133
4134 // Store the low double into the destination.
4135 __ movsd(stack, reg);
4136
4137 // Move the high double to the low double.
4138 __ psrldq(reg, Immediate(8));
4139 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4140 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4141 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004142 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004143 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004144 }
4145}
4146
4147void ParallelMoveResolverX86::SpillScratch(int reg) {
4148 __ pushl(static_cast<Register>(reg));
4149}
4150
4151void ParallelMoveResolverX86::RestoreScratch(int reg) {
4152 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004153}
4154
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004155void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004156 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4157 ? LocationSummary::kCallOnSlowPath
4158 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004159 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004160 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004161 locations->SetOut(Location::RequiresRegister());
4162}
4163
4164void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004165 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004166 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004167 DCHECK(!cls->CanCallRuntime());
4168 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004169 codegen_->LoadCurrentMethod(out);
4170 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4171 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004172 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004173 codegen_->LoadCurrentMethod(out);
4174 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4175 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004176
4177 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4178 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4179 codegen_->AddSlowPath(slow_path);
4180 __ testl(out, out);
4181 __ j(kEqual, slow_path->GetEntryLabel());
4182 if (cls->MustGenerateClinitCheck()) {
4183 GenerateClassInitializationCheck(slow_path, out);
4184 } else {
4185 __ Bind(slow_path->GetExitLabel());
4186 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004187 }
4188}
4189
4190void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4191 LocationSummary* locations =
4192 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4193 locations->SetInAt(0, Location::RequiresRegister());
4194 if (check->HasUses()) {
4195 locations->SetOut(Location::SameAsFirstInput());
4196 }
4197}
4198
4199void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004200 // We assume the class to not be null.
4201 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4202 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004203 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004204 GenerateClassInitializationCheck(slow_path,
4205 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004206}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004207
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004208void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4209 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004210 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4211 Immediate(mirror::Class::kStatusInitialized));
4212 __ j(kLess, slow_path->GetEntryLabel());
4213 __ Bind(slow_path->GetExitLabel());
4214 // No need for memory fence, thanks to the X86 memory model.
4215}
4216
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004217void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4218 LocationSummary* locations =
4219 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4220 locations->SetOut(Location::RequiresRegister());
4221}
4222
4223void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4224 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4225 codegen_->AddSlowPath(slow_path);
4226
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004228 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004229 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4230 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004231 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4232 __ testl(out, out);
4233 __ j(kEqual, slow_path->GetEntryLabel());
4234 __ Bind(slow_path->GetExitLabel());
4235}
4236
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004237void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4238 LocationSummary* locations =
4239 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4240 locations->SetOut(Location::RequiresRegister());
4241}
4242
4243void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4244 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004246 __ fs()->movl(address, Immediate(0));
4247}
4248
4249void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4250 LocationSummary* locations =
4251 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4252 InvokeRuntimeCallingConvention calling_convention;
4253 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4254}
4255
4256void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4257 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4258 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4259}
4260
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004261void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004262 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4263 ? LocationSummary::kNoCall
4264 : LocationSummary::kCallOnSlowPath;
4265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4266 locations->SetInAt(0, Location::RequiresRegister());
4267 locations->SetInAt(1, Location::Any());
4268 locations->SetOut(Location::RequiresRegister());
4269}
4270
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004271void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004272 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004274 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004275 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004276 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4277 Label done, zero;
4278 SlowPathCodeX86* slow_path = nullptr;
4279
4280 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004281 // Avoid null check if we know obj is not null.
4282 if (instruction->MustDoNullCheck()) {
4283 __ testl(obj, obj);
4284 __ j(kEqual, &zero);
4285 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004286 __ movl(out, Address(obj, class_offset));
4287 // Compare the class of `obj` with `cls`.
4288 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004289 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004290 } else {
4291 DCHECK(cls.IsStackSlot()) << cls;
4292 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4293 }
4294
4295 if (instruction->IsClassFinal()) {
4296 // Classes must be equal for the instanceof to succeed.
4297 __ j(kNotEqual, &zero);
4298 __ movl(out, Immediate(1));
4299 __ jmp(&done);
4300 } else {
4301 // If the classes are not equal, we go into a slow path.
4302 DCHECK(locations->OnlyCallsOnSlowPath());
4303 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004304 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004305 codegen_->AddSlowPath(slow_path);
4306 __ j(kNotEqual, slow_path->GetEntryLabel());
4307 __ movl(out, Immediate(1));
4308 __ jmp(&done);
4309 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004310
4311 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4312 __ Bind(&zero);
4313 __ movl(out, Immediate(0));
4314 }
4315
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004316 if (slow_path != nullptr) {
4317 __ Bind(slow_path->GetExitLabel());
4318 }
4319 __ Bind(&done);
4320}
4321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004322void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4323 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4324 instruction, LocationSummary::kCallOnSlowPath);
4325 locations->SetInAt(0, Location::RequiresRegister());
4326 locations->SetInAt(1, Location::Any());
4327 locations->AddTemp(Location::RequiresRegister());
4328}
4329
4330void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4331 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004332 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004333 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004335 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4336 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4337 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4338 codegen_->AddSlowPath(slow_path);
4339
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004340 // Avoid null check if we know obj is not null.
4341 if (instruction->MustDoNullCheck()) {
4342 __ testl(obj, obj);
4343 __ j(kEqual, slow_path->GetExitLabel());
4344 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004345
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004346 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004347 // Compare the class of `obj` with `cls`.
4348 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004349 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004350 } else {
4351 DCHECK(cls.IsStackSlot()) << cls;
4352 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4353 }
4354
4355 __ j(kNotEqual, slow_path->GetEntryLabel());
4356 __ Bind(slow_path->GetExitLabel());
4357}
4358
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004359void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4360 LocationSummary* locations =
4361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4362 InvokeRuntimeCallingConvention calling_convention;
4363 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4364}
4365
4366void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4367 __ fs()->call(Address::Absolute(instruction->IsEnter()
4368 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4369 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4370 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4371}
4372
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004373void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4374void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4375void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4376
4377void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4378 LocationSummary* locations =
4379 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4380 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4381 || instruction->GetResultType() == Primitive::kPrimLong);
4382 locations->SetInAt(0, Location::RequiresRegister());
4383 locations->SetInAt(1, Location::Any());
4384 locations->SetOut(Location::SameAsFirstInput());
4385}
4386
4387void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4388 HandleBitwiseOperation(instruction);
4389}
4390
4391void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4392 HandleBitwiseOperation(instruction);
4393}
4394
4395void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4396 HandleBitwiseOperation(instruction);
4397}
4398
4399void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4400 LocationSummary* locations = instruction->GetLocations();
4401 Location first = locations->InAt(0);
4402 Location second = locations->InAt(1);
4403 DCHECK(first.Equals(locations->Out()));
4404
4405 if (instruction->GetResultType() == Primitive::kPrimInt) {
4406 if (second.IsRegister()) {
4407 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004409 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004411 } else {
4412 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004414 }
4415 } else if (second.IsConstant()) {
4416 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004417 __ andl(first.AsRegister<Register>(),
4418 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004419 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004420 __ orl(first.AsRegister<Register>(),
4421 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004422 } else {
4423 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004424 __ xorl(first.AsRegister<Register>(),
4425 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004426 }
4427 } else {
4428 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004429 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004430 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004432 } else {
4433 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004434 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004435 }
4436 }
4437 } else {
4438 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4439 if (second.IsRegisterPair()) {
4440 if (instruction->IsAnd()) {
4441 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4442 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4443 } else if (instruction->IsOr()) {
4444 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4445 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4446 } else {
4447 DCHECK(instruction->IsXor());
4448 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4449 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4450 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004451 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004452 if (instruction->IsAnd()) {
4453 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4454 __ andl(first.AsRegisterPairHigh<Register>(),
4455 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4456 } else if (instruction->IsOr()) {
4457 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4458 __ orl(first.AsRegisterPairHigh<Register>(),
4459 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4460 } else {
4461 DCHECK(instruction->IsXor());
4462 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4463 __ xorl(first.AsRegisterPairHigh<Register>(),
4464 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4465 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004466 } else {
4467 DCHECK(second.IsConstant()) << second;
4468 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004469 int32_t low_value = Low32Bits(value);
4470 int32_t high_value = High32Bits(value);
4471 Immediate low(low_value);
4472 Immediate high(high_value);
4473 Register first_low = first.AsRegisterPairLow<Register>();
4474 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004475 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004476 if (low_value == 0) {
4477 __ xorl(first_low, first_low);
4478 } else if (low_value != -1) {
4479 __ andl(first_low, low);
4480 }
4481 if (high_value == 0) {
4482 __ xorl(first_high, first_high);
4483 } else if (high_value != -1) {
4484 __ andl(first_high, high);
4485 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004486 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004487 if (low_value != 0) {
4488 __ orl(first_low, low);
4489 }
4490 if (high_value != 0) {
4491 __ orl(first_high, high);
4492 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004493 } else {
4494 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004495 if (low_value != 0) {
4496 __ xorl(first_low, low);
4497 }
4498 if (high_value != 0) {
4499 __ xorl(first_high, high);
4500 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004501 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004502 }
4503 }
4504}
4505
Calin Juravleb1498f62015-02-16 13:13:29 +00004506void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4507 // Nothing to do, this should be removed during prepare for register allocator.
4508 UNUSED(instruction);
4509 LOG(FATAL) << "Unreachable";
4510}
4511
4512void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4513 // Nothing to do, this should be removed during prepare for register allocator.
4514 UNUSED(instruction);
4515 LOG(FATAL) << "Unreachable";
4516}
4517
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004518} // namespace x86
4519} // namespace art