blob: a037040ae4283cf91bd37faf7c2f7cea40ff9100 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
156 private:
157 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100158 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 Label return_label_;
160
161 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
162};
163
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000164class LoadStringSlowPathX86 : public SlowPathCodeX86 {
165 public:
166 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
167
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169 LocationSummary* locations = instruction_->GetLocations();
170 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
171
172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000175
176 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800177 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000178 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000181 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000182
183 __ jmp(GetExitLabel());
184 }
185
186 private:
187 HLoadString* const instruction_;
188
189 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
190};
191
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000192class LoadClassSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 LoadClassSlowPathX86(HLoadClass* cls,
195 HInstruction* at,
196 uint32_t dex_pc,
197 bool do_clinit)
198 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
199 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
200 }
201
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000203 LocationSummary* locations = at_->GetLocations();
204 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207
208 InvokeRuntimeCallingConvention calling_convention;
209 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 __ fs()->call(Address::Absolute(do_clinit_
211 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
212 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214
215 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000216 Location out = locations->Out();
217 if (out.IsValid()) {
218 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
219 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000222 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 __ jmp(GetExitLabel());
224 }
225
226 private:
227 // The class this slow path will load.
228 HLoadClass* const cls_;
229
230 // The instruction where this slow path is happening.
231 // (Might be the load class or an initialization check).
232 HInstruction* const at_;
233
234 // The dex PC of `at_`.
235 const uint32_t dex_pc_;
236
237 // Whether to initialize the class.
238 const bool do_clinit_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
241};
242
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000243class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
244 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000245 TypeCheckSlowPathX86(HInstruction* instruction,
246 Location class_to_check,
247 Location object_class,
248 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 class_to_check_(class_to_check),
251 object_class_(object_class),
252 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000254 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000256 DCHECK(instruction_->IsCheckCast()
257 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
259 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
260 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000261 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000266 x86_codegen->EmitParallelMoves(
267 class_to_check_,
268 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100269 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000270 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100271 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
272 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000275 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
276 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000277 } else {
278 DCHECK(instruction_->IsCheckCast());
279 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
280 }
281
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000282 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 if (instruction_->IsInstanceOf()) {
284 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
285 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
288 __ jmp(GetExitLabel());
289 }
290
291 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 HInstruction* const instruction_;
293 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000295 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
298};
299
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700300class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
301 public:
302 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
303 : instruction_(instruction) {}
304
305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
309 // No need to restore live registers.
310 DCHECK(instruction_->IsDeoptimize());
311 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
312 uint32_t dex_pc = deoptimize->GetDexPc();
313 codegen->RecordPcInfo(instruction_, dex_pc, this);
314 }
315
316 private:
317 HInstruction* const instruction_;
318 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
319};
320
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100321#undef __
322#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
323
Dave Allison20dfc792014-06-16 20:44:29 -0700324inline Condition X86Condition(IfCondition cond) {
325 switch (cond) {
326 case kCondEQ: return kEqual;
327 case kCondNE: return kNotEqual;
328 case kCondLT: return kLess;
329 case kCondLE: return kLessEqual;
330 case kCondGT: return kGreater;
331 case kCondGE: return kGreaterEqual;
332 default:
333 LOG(FATAL) << "Unknown if condition";
334 }
335 return kEqual;
336}
337
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
339 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
340}
341
342void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
343 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
344}
345
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100346size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
347 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
348 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Mark Mendell7c8d0092015-01-26 11:21:33 -0500356size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
357 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
358 return GetFloatingPointSpillSlotSize();
359}
360
361size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
363 return GetFloatingPointSpillSlotSize();
364}
365
Mark Mendellfb8d2792015-03-31 22:16:59 -0400366CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
367 const X86InstructionSetFeatures& isa_features,
368 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500369 : CodeGenerator(graph,
370 kNumberOfCpuRegisters,
371 kNumberOfXmmRegisters,
372 kNumberOfRegisterPairs,
373 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
374 arraysize(kCoreCalleeSaves))
375 | (1 << kFakeReturnRegister),
376 0,
377 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100378 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100380 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400381 move_resolver_(graph->GetArena(), this),
382 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000383 // Use a fake return address register to mimic Quick.
384 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 switch (type) {
389 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 X86ManagedRegister pair =
392 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
411 X86ManagedRegister current =
412 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
413 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 }
416 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
420 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 return Location::FpuRegisterLocation(
423 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 case Primitive::kPrimVoid:
427 LOG(FATAL) << "Unreachable type " << type;
428 }
429
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431}
432
Mark Mendell5f874182015-03-04 15:42:45 -0500433void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
Mark Mendell5f874182015-03-04 15:42:45 -0500440 if (is_baseline) {
441 blocked_core_registers_[EBP] = true;
442 blocked_core_registers_[ESI] = true;
443 blocked_core_registers_[EDI] = true;
444 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100445
446 UpdateBlockedPairRegisters();
447}
448
449void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
450 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
451 X86ManagedRegister current =
452 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
453 if (blocked_core_registers_[current.AsRegisterPairLow()]
454 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
455 blocked_register_pairs_[i] = true;
456 }
457 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458}
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
461 : HGraphVisitor(graph),
462 assembler_(codegen->GetAssembler()),
463 codegen_(codegen) {}
464
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100465static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100466 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467}
468
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000469void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000471 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000472 bool skip_overflow_check =
473 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000474 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000475
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000476 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100477 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100478 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100479 }
480
Mark Mendell5f874182015-03-04 15:42:45 -0500481 if (HasEmptyFrame()) {
482 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000483 }
Mark Mendell5f874182015-03-04 15:42:45 -0500484
485 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
486 Register reg = kCoreCalleeSaves[i];
487 if (allocated_registers_.ContainsCoreRegister(reg)) {
488 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100489 __ cfi().AdjustCFAOffset(kX86WordSize);
490 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500491 }
492 }
493
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 int adjust = GetFrameSize() - FrameEntrySpillSize();
495 __ subl(ESP, Immediate(adjust));
496 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500497 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000498}
499
500void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100501 __ cfi().RememberState();
502 if (!HasEmptyFrame()) {
503 int adjust = GetFrameSize() - FrameEntrySpillSize();
504 __ addl(ESP, Immediate(adjust));
505 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500506
David Srbeckyc34dc932015-04-12 09:27:43 +0100507 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
508 Register reg = kCoreCalleeSaves[i];
509 if (allocated_registers_.ContainsCoreRegister(reg)) {
510 __ popl(reg);
511 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
512 __ cfi().Restore(DWARFReg(reg));
513 }
Mark Mendell5f874182015-03-04 15:42:45 -0500514 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000515 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100516 __ ret();
517 __ cfi().RestoreState();
518 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000519}
520
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100521void CodeGeneratorX86::Bind(HBasicBlock* block) {
522 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000523}
524
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100525void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000526 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100527 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000528}
529
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
531 switch (load->GetType()) {
532 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
536 case Primitive::kPrimInt:
537 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimBoolean:
542 case Primitive::kPrimByte:
543 case Primitive::kPrimChar:
544 case Primitive::kPrimShort:
545 case Primitive::kPrimVoid:
546 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700547 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548 }
549
550 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552}
553
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
555 switch (type) {
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimByte:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot: {
562 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000563 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100564 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000567 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100568 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100569 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000571 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 uint32_t index = gp_index_;
573 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000574 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100576 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
577 calling_convention.GetRegisterPairAt(index));
578 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100579 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000580 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
581 }
582 }
583
584 case Primitive::kPrimFloat: {
585 uint32_t index = fp_index_++;
586 stack_index_++;
587 if (index < calling_convention.GetNumberOfFpuRegisters()) {
588 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
589 } else {
590 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
591 }
592 }
593
594 case Primitive::kPrimDouble: {
595 uint32_t index = fp_index_++;
596 stack_index_ += 2;
597 if (index < calling_convention.GetNumberOfFpuRegisters()) {
598 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
599 } else {
600 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100601 }
602 }
603
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100604 case Primitive::kPrimVoid:
605 LOG(FATAL) << "Unexpected parameter type " << type;
606 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100607 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 return Location();
609}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100610
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611void CodeGeneratorX86::Move32(Location destination, Location source) {
612 if (source.Equals(destination)) {
613 return;
614 }
615 if (destination.IsRegister()) {
616 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 } else {
621 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else if (destination.IsFpuRegister()) {
625 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000628 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else {
630 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000634 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000638 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500639 } else if (source.IsConstant()) {
640 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000641 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500642 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
644 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100645 __ pushl(Address(ESP, source.GetStackIndex()));
646 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 }
648 }
649}
650
651void CodeGeneratorX86::Move64(Location destination, Location source) {
652 if (source.Equals(destination)) {
653 return;
654 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 if (destination.IsRegisterPair()) {
656 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000657 EmitParallelMoves(
658 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
659 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100660 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100662 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
663 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
665 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000667 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
670 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
672 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500674 if (source.IsFpuRegister()) {
675 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
676 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 } else {
679 LOG(FATAL) << "Unimplemented";
680 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000682 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100683 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000684 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000689 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000690 } else if (source.IsConstant()) {
691 HConstant* constant = source.GetConstant();
692 int64_t value;
693 if (constant->IsLongConstant()) {
694 value = constant->AsLongConstant()->GetValue();
695 } else {
696 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000697 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000698 }
699 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
700 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000702 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000703 EmitParallelMoves(
704 Location::StackSlot(source.GetStackIndex()),
705 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100706 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
709 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 }
711 }
712}
713
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100714void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 LocationSummary* locations = instruction->GetLocations();
716 if (locations != nullptr && locations->Out().Equals(location)) {
717 return;
718 }
719
720 if (locations != nullptr && locations->Out().IsConstant()) {
721 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000722 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
723 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 } else if (location.IsStackSlot()) {
727 __ movl(Address(ESP, location.GetStackIndex()), imm);
728 } else {
729 DCHECK(location.IsConstant());
730 DCHECK_EQ(location.GetConstant(), const_to_move);
731 }
732 } else if (const_to_move->IsLongConstant()) {
733 int64_t value = const_to_move->AsLongConstant()->GetValue();
734 if (location.IsRegisterPair()) {
735 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
736 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
737 } else if (location.IsDoubleStackSlot()) {
738 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000739 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
740 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000741 } else {
742 DCHECK(location.IsConstant());
743 DCHECK_EQ(location.GetConstant(), instruction);
744 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000746 } else if (instruction->IsTemporary()) {
747 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000748 if (temp_location.IsStackSlot()) {
749 Move32(location, temp_location);
750 } else {
751 DCHECK(temp_location.IsDoubleStackSlot());
752 Move64(location, temp_location);
753 }
Roland Levillain476df552014-10-09 17:51:36 +0100754 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 switch (instruction->GetType()) {
757 case Primitive::kPrimBoolean:
758 case Primitive::kPrimByte:
759 case Primitive::kPrimChar:
760 case Primitive::kPrimShort:
761 case Primitive::kPrimInt:
762 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 case Primitive::kPrimFloat:
764 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 break;
766
767 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 case Primitive::kPrimDouble:
769 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 break;
771
772 default:
773 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
774 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100776 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 switch (instruction->GetType()) {
778 case Primitive::kPrimBoolean:
779 case Primitive::kPrimByte:
780 case Primitive::kPrimChar:
781 case Primitive::kPrimShort:
782 case Primitive::kPrimInt:
783 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000785 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 break;
787
788 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000790 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 break;
792
793 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796 }
797}
798
799void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801}
802
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000803void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000804 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100805 DCHECK(!successor->IsExitBlock());
806
807 HBasicBlock* block = got->GetBlock();
808 HInstruction* previous = got->GetPrevious();
809
810 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000811 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
813 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
814 return;
815 }
816
817 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
818 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
819 }
820 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000822 }
823}
824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000825void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000827}
828
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700830 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000831}
832
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700833void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
834 Label* true_target,
835 Label* false_target,
836 Label* always_true_target) {
837 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 if (cond->IsIntConstant()) {
839 // Constant condition, statically compared against 1.
840 int32_t cond_value = cond->AsIntConstant()->GetValue();
841 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700842 if (always_true_target != nullptr) {
843 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100844 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100846 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 DCHECK_EQ(cond_value, 0);
848 }
849 } else {
850 bool materialized =
851 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
852 // Moves do not affect the eflags register, so if the condition is
853 // evaluated just before the if, we don't need to evaluate it
854 // again.
855 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700856 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100857 if (materialized) {
858 if (!eflags_set) {
859 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500862 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 } else {
864 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
865 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700866 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 }
870 } else {
871 Location lhs = cond->GetLocations()->InAt(0);
872 Location rhs = cond->GetLocations()->InAt(1);
873 // LHS is guaranteed to be in a register (see
874 // LocationsBuilderX86::VisitCondition).
875 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100878 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500879 if (constant == 0) {
880 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
881 } else {
882 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
883 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100886 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700888 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100889 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700890 if (false_target != nullptr) {
891 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892 }
893}
894
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700895void LocationsBuilderX86::VisitIf(HIf* if_instr) {
896 LocationSummary* locations =
897 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
898 HInstruction* cond = if_instr->InputAt(0);
899 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
900 locations->SetInAt(0, Location::Any());
901 }
902}
903
904void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
905 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
906 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
907 Label* always_true_target = true_target;
908 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
909 if_instr->IfTrueSuccessor())) {
910 always_true_target = nullptr;
911 }
912 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
913 if_instr->IfFalseSuccessor())) {
914 false_target = nullptr;
915 }
916 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
917}
918
919void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
920 LocationSummary* locations = new (GetGraph()->GetArena())
921 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
922 HInstruction* cond = deoptimize->InputAt(0);
923 DCHECK(cond->IsCondition());
924 if (cond->AsCondition()->NeedsMaterialization()) {
925 locations->SetInAt(0, Location::Any());
926 }
927}
928
929void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
930 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
931 DeoptimizationSlowPathX86(deoptimize);
932 codegen_->AddSlowPath(slow_path);
933 Label* slow_path_entry = slow_path->GetEntryLabel();
934 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
935}
936
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000937void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000938 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000939}
940
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
942 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000943}
944
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000945void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100946 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000947}
948
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000949void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100950 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700951 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000952}
953
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100955 LocationSummary* locations =
956 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 switch (store->InputAt(1)->GetType()) {
958 case Primitive::kPrimBoolean:
959 case Primitive::kPrimByte:
960 case Primitive::kPrimChar:
961 case Primitive::kPrimShort:
962 case Primitive::kPrimInt:
963 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100965 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
966 break;
967
968 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100969 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
971 break;
972
973 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 }
976 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977}
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700980 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981}
982
Dave Allison20dfc792014-06-16 20:44:29 -0700983void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100984 LocationSummary* locations =
985 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100986 locations->SetInAt(0, Location::RequiresRegister());
987 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100988 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500989 // We need a byte register.
990 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000992}
993
Dave Allison20dfc792014-06-16 20:44:29 -0700994void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
995 if (comp->NeedsMaterialization()) {
996 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100998 // Clear register: setcc only sets the low byte.
999 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001000 Location lhs = locations->InAt(0);
1001 Location rhs = locations->InAt(1);
1002 if (rhs.IsRegister()) {
1003 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1004 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001005 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001006 if (constant == 0) {
1007 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1008 } else {
1009 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1010 }
Dave Allison20dfc792014-06-16 20:44:29 -07001011 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001012 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001013 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001014 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001015 }
Dave Allison20dfc792014-06-16 20:44:29 -07001016}
1017
1018void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1047 VisitCondition(comp);
1048}
1049
1050void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1051 VisitCondition(comp);
1052}
1053
1054void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1055 VisitCondition(comp);
1056}
1057
1058void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1059 VisitCondition(comp);
1060}
1061
1062void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1063 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064}
1065
1066void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 LocationSummary* locations =
1068 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001069 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001070}
1071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001073 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001074 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001075}
1076
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001077void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1078 LocationSummary* locations =
1079 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1080 locations->SetOut(Location::ConstantLocation(constant));
1081}
1082
1083void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1084 // Will be generated at use site.
1085 UNUSED(constant);
1086}
1087
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001089 LocationSummary* locations =
1090 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001091 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092}
1093
1094void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1095 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097}
1098
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001099void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1100 LocationSummary* locations =
1101 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1102 locations->SetOut(Location::ConstantLocation(constant));
1103}
1104
1105void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1106 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001107 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001108}
1109
1110void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1113 locations->SetOut(Location::ConstantLocation(constant));
1114}
1115
1116void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1117 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001119}
1120
Calin Juravle27df7582015-04-17 19:12:31 +01001121void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1122 memory_barrier->SetLocations(nullptr);
1123}
1124
1125void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1126 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1127}
1128
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001129void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131}
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001135 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001136}
1137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001138void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001139 LocationSummary* locations =
1140 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 switch (ret->InputAt(0)->GetType()) {
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimByte:
1144 case Primitive::kPrimChar:
1145 case Primitive::kPrimShort:
1146 case Primitive::kPrimInt:
1147 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 break;
1150
1151 case Primitive::kPrimLong:
1152 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 break;
1155
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 case Primitive::kPrimFloat:
1157 case Primitive::kPrimDouble:
1158 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 break;
1161
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001165}
1166
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001167void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 if (kIsDebugBuild) {
1169 switch (ret->InputAt(0)->GetType()) {
1170 case Primitive::kPrimBoolean:
1171 case Primitive::kPrimByte:
1172 case Primitive::kPrimChar:
1173 case Primitive::kPrimShort:
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001176 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 break;
1178
1179 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1181 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 break;
1183
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimFloat:
1185 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001186 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 break;
1188
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001189 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 }
1192 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001194}
1195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001196void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001197 // Explicit clinit checks triggered by static invokes must have been
Roland Levillain0379f822015-04-24 23:01:24 +01001198 // pruned by art::PrepareForRegisterAllocation, but this step is not
1199 // run in baseline. So we remove them manually here if we find them.
1200 // TODO: Instead of this local workaround, address this properly.
1201 if (invoke->IsStaticWithExplicitClinitCheck()) {
1202 invoke->RemoveClinitCheckOrLoadClassAsLastInput();
1203 }
Roland Levillain4c0eb422015-04-24 16:43:49 +01001204
Mark Mendellfb8d2792015-03-31 22:16:59 -04001205 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001206 if (intrinsic.TryDispatch(invoke)) {
1207 return;
1208 }
1209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 HandleInvoke(invoke);
1211}
1212
Mark Mendell09ed1a32015-03-25 08:30:06 -04001213static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1214 if (invoke->GetLocations()->Intrinsified()) {
1215 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1216 intrinsic.Dispatch(invoke);
1217 return true;
1218 }
1219 return false;
1220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01001223 // Explicit clinit checks triggered by static invokes must have been
1224 // pruned by art::PrepareForRegisterAllocation.
1225 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
1226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1228 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001229 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230
Mark Mendell09ed1a32015-03-25 08:30:06 -04001231 codegen_->GenerateStaticOrDirectCall(
1232 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001233 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234}
1235
1236void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1237 HandleInvoke(invoke);
1238}
1239
1240void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001243 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001244
1245 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001247 HInstruction* input = invoke->InputAt(i);
1248 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1249 }
1250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 switch (invoke->GetType()) {
1252 case Primitive::kPrimBoolean:
1253 case Primitive::kPrimByte:
1254 case Primitive::kPrimChar:
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001262 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 break;
1264
1265 case Primitive::kPrimVoid:
1266 break;
1267
1268 case Primitive::kPrimDouble:
1269 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001270 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 break;
1272 }
1273
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001274 invoke->SetLocations(locations);
1275}
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1280 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
1286 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
1288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 // temp = temp->GetMethodAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001295 __ call(Address(
1296 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001300}
1301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1303 HandleInvoke(invoke);
1304 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001305 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001306}
1307
1308void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001311 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1312 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1313 LocationSummary* locations = invoke->GetLocations();
1314 Location receiver = locations->InAt(0);
1315 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1316
1317 // Set the hidden argument.
1318 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
1323 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
1325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 // temp = temp->GetImtEntryAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001332 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001334
1335 DCHECK(!codegen_->IsLeafMethod());
1336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1337}
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001344 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 break;
1348
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001350 locations->SetInAt(0, Location::RequiresFpuRegister());
1351 locations->SetOut(Location::SameAsFirstInput());
1352 locations->AddTemp(Location::RequiresRegister());
1353 locations->AddTemp(Location::RequiresFpuRegister());
1354 break;
1355
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001357 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001358 locations->SetOut(Location::SameAsFirstInput());
1359 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1364 }
1365}
1366
1367void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1368 LocationSummary* locations = neg->GetLocations();
1369 Location out = locations->Out();
1370 Location in = locations->InAt(0);
1371 switch (neg->GetResultType()) {
1372 case Primitive::kPrimInt:
1373 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001374 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001376 break;
1377
1378 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001379 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 __ negl(out.AsRegisterPairLow<Register>());
1382 // Negation is similar to subtraction from zero. The least
1383 // significant byte triggers a borrow when it is different from
1384 // zero; to take it into account, add 1 to the most significant
1385 // byte if the carry flag (CF) is set to 1 after the first NEGL
1386 // operation.
1387 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1388 __ negl(out.AsRegisterPairHigh<Register>());
1389 break;
1390
Roland Levillain5368c212014-11-27 15:03:41 +00001391 case Primitive::kPrimFloat: {
1392 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 Register constant = locations->GetTemp(0).AsRegister<Register>();
1394 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001395 // Implement float negation with an exclusive or with value
1396 // 0x80000000 (mask for bit 31, representing the sign of a
1397 // single-precision floating-point number).
1398 __ movl(constant, Immediate(INT32_C(0x80000000)));
1399 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001402 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403
Roland Levillain5368c212014-11-27 15:03:41 +00001404 case Primitive::kPrimDouble: {
1405 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001407 // Implement double negation with an exclusive or with value
1408 // 0x8000000000000000 (mask for bit 63, representing the sign of
1409 // a double-precision floating-point number).
1410 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001411 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001412 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001413 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001414
1415 default:
1416 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1417 }
1418}
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001421 Primitive::Type result_type = conversion->GetResultType();
1422 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001423 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001424
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001425 // The float-to-long and double-to-long type conversions rely on a
1426 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001427 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1429 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001430 ? LocationSummary::kCall
1431 : LocationSummary::kNoCall;
1432 LocationSummary* locations =
1433 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1434
David Brazdilb2bd1c52015-03-25 11:17:37 +00001435 // The Java language does not allow treating boolean as an integral type but
1436 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001437
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimByte:
1440 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001441 case Primitive::kPrimBoolean:
1442 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1448 // Make the output overlap to please the register allocator. This greatly simplifies
1449 // the validation of the linear scan implementation
1450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unexpected type conversion from " << input_type
1455 << " to " << result_type;
1456 }
1457 break;
1458
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimShort:
1460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001461 case Primitive::kPrimBoolean:
1462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001463 case Primitive::kPrimByte:
1464 case Primitive::kPrimInt:
1465 case Primitive::kPrimChar:
1466 // Processing a Dex `int-to-short' instruction.
1467 locations->SetInAt(0, Location::Any());
1468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillain946e1432014-11-11 17:35:19 +00001477 case Primitive::kPrimInt:
1478 switch (input_type) {
1479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001481 locations->SetInAt(0, Location::Any());
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 break;
1484
1485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001486 // Processing a Dex `float-to-int' instruction.
1487 locations->SetInAt(0, Location::RequiresFpuRegister());
1488 locations->SetOut(Location::RequiresRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 break;
1491
Roland Levillain946e1432014-11-11 17:35:19 +00001492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001493 // Processing a Dex `double-to-int' instruction.
1494 locations->SetInAt(0, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::RequiresRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503 break;
1504
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimLong:
1506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001507 case Primitive::kPrimBoolean:
1508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 case Primitive::kPrimByte:
1510 case Primitive::kPrimShort:
1511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 locations->SetInAt(0, Location::RegisterLocation(EAX));
1515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1516 break;
1517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1524
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 // The runtime helper puts the result in EAX, EDX.
1526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
1534 break;
1535
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimChar:
1537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001538 case Primitive::kPrimBoolean:
1539 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001540 case Primitive::kPrimByte:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001543 // Processing a Dex `int-to-char' instruction.
1544 locations->SetInAt(0, Location::Any());
1545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1546 break;
1547
1548 default:
1549 LOG(FATAL) << "Unexpected type conversion from " << input_type
1550 << " to " << result_type;
1551 }
1552 break;
1553
Roland Levillaindff1f282014-11-05 14:15:05 +00001554 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001555 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001556 case Primitive::kPrimBoolean:
1557 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001558 case Primitive::kPrimByte:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimChar:
1562 // Processing a Dex `int-to-float' instruction.
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetOut(Location::RequiresFpuRegister());
1565 break;
1566
1567 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001568 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001569 locations->SetInAt(0, Location::Any());
1570 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001571 break;
1572
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001574 // Processing a Dex `double-to-float' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 };
1583 break;
1584
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001587 case Primitive::kPrimBoolean:
1588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimByte:
1590 case Primitive::kPrimShort:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-double' instruction.
1594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetOut(Location::RequiresFpuRegister());
1596 break;
1597
1598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001600 locations->SetInAt(0, Location::Any());
1601 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001602 break;
1603
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001605 // Processing a Dex `float-to-double' instruction.
1606 locations->SetInAt(0, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620}
1621
1622void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1623 LocationSummary* locations = conversion->GetLocations();
1624 Location out = locations->Out();
1625 Location in = locations->InAt(0);
1626 Primitive::Type result_type = conversion->GetResultType();
1627 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001628 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimByte:
1631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001632 case Primitive::kPrimBoolean:
1633 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001637 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001638 if (in.IsRegister()) {
1639 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 } else {
1641 DCHECK(in.GetConstant()->IsIntConstant());
1642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1644 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001645 break;
1646
1647 default:
1648 LOG(FATAL) << "Unexpected type conversion from " << input_type
1649 << " to " << result_type;
1650 }
1651 break;
1652
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimShort:
1654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001655 case Primitive::kPrimBoolean:
1656 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001657 case Primitive::kPrimByte:
1658 case Primitive::kPrimInt:
1659 case Primitive::kPrimChar:
1660 // Processing a Dex `int-to-short' instruction.
1661 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else {
1666 DCHECK(in.GetConstant()->IsIntConstant());
1667 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001669 }
1670 break;
1671
1672 default:
1673 LOG(FATAL) << "Unexpected type conversion from " << input_type
1674 << " to " << result_type;
1675 }
1676 break;
1677
Roland Levillain946e1432014-11-11 17:35:19 +00001678 case Primitive::kPrimInt:
1679 switch (input_type) {
1680 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001681 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001682 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001684 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else {
1687 DCHECK(in.IsConstant());
1688 DCHECK(in.GetConstant()->IsLongConstant());
1689 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001691 }
1692 break;
1693
Roland Levillain3f8f9362014-12-02 17:45:01 +00001694 case Primitive::kPrimFloat: {
1695 // Processing a Dex `float-to-int' instruction.
1696 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1697 Register output = out.AsRegister<Register>();
1698 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1699 Label done, nan;
1700
1701 __ movl(output, Immediate(kPrimIntMax));
1702 // temp = int-to-float(output)
1703 __ cvtsi2ss(temp, output);
1704 // if input >= temp goto done
1705 __ comiss(input, temp);
1706 __ j(kAboveEqual, &done);
1707 // if input == NaN goto nan
1708 __ j(kUnordered, &nan);
1709 // output = float-to-int-truncate(input)
1710 __ cvttss2si(output, input);
1711 __ jmp(&done);
1712 __ Bind(&nan);
1713 // output = 0
1714 __ xorl(output, output);
1715 __ Bind(&done);
1716 break;
1717 }
1718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 case Primitive::kPrimDouble: {
1720 // Processing a Dex `double-to-int' instruction.
1721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1722 Register output = out.AsRegister<Register>();
1723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1724 Label done, nan;
1725
1726 __ movl(output, Immediate(kPrimIntMax));
1727 // temp = int-to-double(output)
1728 __ cvtsi2sd(temp, output);
1729 // if input >= temp goto done
1730 __ comisd(input, temp);
1731 __ j(kAboveEqual, &done);
1732 // if input == NaN goto nan
1733 __ j(kUnordered, &nan);
1734 // output = double-to-int-truncate(input)
1735 __ cvttsd2si(output, input);
1736 __ jmp(&done);
1737 __ Bind(&nan);
1738 // output = 0
1739 __ xorl(output, output);
1740 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001741 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001742 }
Roland Levillain946e1432014-11-11 17:35:19 +00001743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimLong:
1751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001752 case Primitive::kPrimBoolean:
1753 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Primitive::kPrimByte:
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001757 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001758 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001759 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1760 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 __ cdq();
1763 break;
1764
1765 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001766 // Processing a Dex `float-to-long' instruction.
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001768 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1769 break;
1770
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001772 // Processing a Dex `double-to-long' instruction.
1773 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1774 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 break;
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type conversion from " << input_type
1779 << " to " << result_type;
1780 }
1781 break;
1782
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimChar:
1784 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001785 case Primitive::kPrimBoolean:
1786 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimByte:
1788 case Primitive::kPrimShort:
1789 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1791 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001792 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001793 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else {
1796 DCHECK(in.GetConstant()->IsIntConstant());
1797 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001799 }
1800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
Roland Levillain6d0e4832014-11-27 18:31:21 +00001820 case Primitive::kPrimLong: {
1821 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001822 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001823
Roland Levillain232ade02015-04-20 15:14:36 +01001824 // Create stack space for the call to
1825 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1826 // TODO: enhance register allocator to ask for stack temporaries.
1827 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1828 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1829 __ subl(ESP, Immediate(adjustment));
1830 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831
Roland Levillain232ade02015-04-20 15:14:36 +01001832 // Load the value to the FP stack, using temporaries if needed.
1833 PushOntoFPStack(in, 0, adjustment, false, true);
1834
1835 if (out.IsStackSlot()) {
1836 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1837 } else {
1838 __ fstps(Address(ESP, 0));
1839 Location stack_temp = Location::StackSlot(0);
1840 codegen_->Move32(out, stack_temp);
1841 }
1842
1843 // Remove the temporary stack space we allocated.
1844 if (adjustment != 0) {
1845 __ addl(ESP, Immediate(adjustment));
1846 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 break;
1848 }
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 };
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001869 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001871 break;
1872
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 case Primitive::kPrimLong: {
1874 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001875 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876
Roland Levillain232ade02015-04-20 15:14:36 +01001877 // Create stack space for the call to
1878 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1879 // TODO: enhance register allocator to ask for stack temporaries.
1880 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1881 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1882 __ subl(ESP, Immediate(adjustment));
1883 }
1884
1885 // Load the value to the FP stack, using temporaries if needed.
1886 PushOntoFPStack(in, 0, adjustment, false, true);
1887
1888 if (out.IsDoubleStackSlot()) {
1889 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1890 } else {
1891 __ fstpl(Address(ESP, 0));
1892 Location stack_temp = Location::DoubleStackSlot(0);
1893 codegen_->Move64(out, stack_temp);
1894 }
1895
1896 // Remove the temporary stack space we allocated.
1897 if (adjustment != 0) {
1898 __ addl(ESP, Immediate(adjustment));
1899 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001900 break;
1901 }
1902
Roland Levillaincff13742014-11-17 14:32:17 +00001903 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001904 // Processing a Dex `float-to-double' instruction.
1905 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918}
1919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001923 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001924 case Primitive::kPrimInt: {
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1928 break;
1929 }
1930
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetInAt(1, Location::Any());
1934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936 }
1937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 case Primitive::kPrimFloat:
1939 case Primitive::kPrimDouble: {
1940 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001941 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1948 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950}
1951
1952void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1953 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 Location first = locations->InAt(0);
1955 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001956 Location out = locations->Out();
1957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001958 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1962 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1963 } else {
1964 __ leal(out.AsRegister<Register>(), Address(
1965 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1966 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001968 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1969 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1970 __ addl(out.AsRegister<Register>(), Immediate(value));
1971 } else {
1972 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1973 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001975 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001976 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001977 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001978 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001979 }
1980
1981 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001982 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1984 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001985 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1987 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001989 } else {
1990 DCHECK(second.IsConstant()) << second;
1991 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1992 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1993 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001994 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001995 break;
1996 }
1997
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 case Primitive::kPrimFloat: {
1999 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002002 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 }
2004
2005 case Primitive::kPrimDouble: {
2006 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002008 }
2009 break;
2010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002013 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 }
2015}
2016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002020 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002021 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::Any());
2025 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026 break;
2027 }
Calin Juravle11351682014-10-23 15:38:15 +01002028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble: {
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 break;
Calin Juravle11351682014-10-23 15:38:15 +01002034 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036 default:
Calin Juravle11351682014-10-23 15:38:15 +01002037 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002039}
2040
2041void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2042 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002043 Location first = locations->InAt(0);
2044 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002045 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002051 __ subl(first.AsRegister<Register>(),
2052 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002055 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002056 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 }
2058
2059 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002060 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002061 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2062 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002063 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002064 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 __ sbbl(first.AsRegisterPairHigh<Register>(),
2066 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002067 } else {
2068 DCHECK(second.IsConstant()) << second;
2069 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2070 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2071 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002073 break;
2074 }
2075
Calin Juravle11351682014-10-23 15:38:15 +01002076 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 break;
Calin Juravle11351682014-10-23 15:38:15 +01002079 }
2080
2081 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002083 break;
2084 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002086 default:
Calin Juravle11351682014-10-23 15:38:15 +01002087 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002088 }
2089}
2090
Calin Juravle34bacdf2014-10-07 20:23:36 +01002091void LocationsBuilderX86::VisitMul(HMul* mul) {
2092 LocationSummary* locations =
2093 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2094 switch (mul->GetResultType()) {
2095 case Primitive::kPrimInt:
2096 locations->SetInAt(0, Location::RequiresRegister());
2097 locations->SetInAt(1, Location::Any());
2098 locations->SetOut(Location::SameAsFirstInput());
2099 break;
2100 case Primitive::kPrimLong: {
2101 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102 locations->SetInAt(1, Location::Any());
2103 locations->SetOut(Location::SameAsFirstInput());
2104 // Needed for imul on 32bits with 64bits output.
2105 locations->AddTemp(Location::RegisterLocation(EAX));
2106 locations->AddTemp(Location::RegisterLocation(EDX));
2107 break;
2108 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002109 case Primitive::kPrimFloat:
2110 case Primitive::kPrimDouble: {
2111 locations->SetInAt(0, Location::RequiresFpuRegister());
2112 locations->SetInAt(1, Location::RequiresFpuRegister());
2113 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116
2117 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 }
2120}
2121
2122void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2123 LocationSummary* locations = mul->GetLocations();
2124 Location first = locations->InAt(0);
2125 Location second = locations->InAt(1);
2126 DCHECK(first.Equals(locations->Out()));
2127
2128 switch (mul->GetResultType()) {
2129 case Primitive::kPrimInt: {
2130 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 } else if (second.IsConstant()) {
2133 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 } else {
2136 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 }
2139 break;
2140 }
2141
2142 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143 Register in1_hi = first.AsRegisterPairHigh<Register>();
2144 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 Register eax = locations->GetTemp(0).AsRegister<Register>();
2146 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147
2148 DCHECK_EQ(EAX, eax);
2149 DCHECK_EQ(EDX, edx);
2150
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002151 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002152 // output: in1
2153 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2154 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2155 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002156 if (second.IsConstant()) {
2157 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002158
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002159 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2160 int32_t low_value = Low32Bits(value);
2161 int32_t high_value = High32Bits(value);
2162 Immediate low(low_value);
2163 Immediate high(high_value);
2164
2165 __ movl(eax, high);
2166 // eax <- in1.lo * in2.hi
2167 __ imull(eax, in1_lo);
2168 // in1.hi <- in1.hi * in2.lo
2169 __ imull(in1_hi, low);
2170 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2171 __ addl(in1_hi, eax);
2172 // move in2_lo to eax to prepare for double precision
2173 __ movl(eax, low);
2174 // edx:eax <- in1.lo * in2.lo
2175 __ mull(in1_lo);
2176 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2177 __ addl(in1_hi, edx);
2178 // in1.lo <- (in1.lo * in2.lo)[31:0];
2179 __ movl(in1_lo, eax);
2180 } else if (second.IsRegisterPair()) {
2181 Register in2_hi = second.AsRegisterPairHigh<Register>();
2182 Register in2_lo = second.AsRegisterPairLow<Register>();
2183
2184 __ movl(eax, in2_hi);
2185 // eax <- in1.lo * in2.hi
2186 __ imull(eax, in1_lo);
2187 // in1.hi <- in1.hi * in2.lo
2188 __ imull(in1_hi, in2_lo);
2189 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2190 __ addl(in1_hi, eax);
2191 // move in1_lo to eax to prepare for double precision
2192 __ movl(eax, in1_lo);
2193 // edx:eax <- in1.lo * in2.lo
2194 __ mull(in2_lo);
2195 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2196 __ addl(in1_hi, edx);
2197 // in1.lo <- (in1.lo * in2.lo)[31:0];
2198 __ movl(in1_lo, eax);
2199 } else {
2200 DCHECK(second.IsDoubleStackSlot()) << second;
2201 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2202 Address in2_lo(ESP, second.GetStackIndex());
2203
2204 __ movl(eax, in2_hi);
2205 // eax <- in1.lo * in2.hi
2206 __ imull(eax, in1_lo);
2207 // in1.hi <- in1.hi * in2.lo
2208 __ imull(in1_hi, in2_lo);
2209 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2210 __ addl(in1_hi, eax);
2211 // move in1_lo to eax to prepare for double precision
2212 __ movl(eax, in1_lo);
2213 // edx:eax <- in1.lo * in2.lo
2214 __ mull(in2_lo);
2215 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2216 __ addl(in1_hi, edx);
2217 // in1.lo <- (in1.lo * in2.lo)[31:0];
2218 __ movl(in1_lo, eax);
2219 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002220
2221 break;
2222 }
2223
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002225 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002226 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 }
2228
2229 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 break;
2232 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233
2234 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002236 }
2237}
2238
Roland Levillain232ade02015-04-20 15:14:36 +01002239void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2240 uint32_t temp_offset,
2241 uint32_t stack_adjustment,
2242 bool is_fp,
2243 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002244 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002245 DCHECK(!is_wide);
2246 if (is_fp) {
2247 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2248 } else {
2249 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2250 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002251 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002252 DCHECK(is_wide);
2253 if (is_fp) {
2254 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2255 } else {
2256 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2257 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 } else {
2259 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002260 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 Location stack_temp = Location::StackSlot(temp_offset);
2262 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002263 if (is_fp) {
2264 __ flds(Address(ESP, temp_offset));
2265 } else {
2266 __ filds(Address(ESP, temp_offset));
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 } else {
2269 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2270 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002271 if (is_fp) {
2272 __ fldl(Address(ESP, temp_offset));
2273 } else {
2274 __ fildl(Address(ESP, temp_offset));
2275 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002276 }
2277 }
2278}
2279
2280void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2281 Primitive::Type type = rem->GetResultType();
2282 bool is_float = type == Primitive::kPrimFloat;
2283 size_t elem_size = Primitive::ComponentSize(type);
2284 LocationSummary* locations = rem->GetLocations();
2285 Location first = locations->InAt(0);
2286 Location second = locations->InAt(1);
2287 Location out = locations->Out();
2288
2289 // Create stack space for 2 elements.
2290 // TODO: enhance register allocator to ask for stack temporaries.
2291 __ subl(ESP, Immediate(2 * elem_size));
2292
2293 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 const bool is_wide = !is_float;
2295 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2296 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002297
2298 // Loop doing FPREM until we stabilize.
2299 Label retry;
2300 __ Bind(&retry);
2301 __ fprem();
2302
2303 // Move FP status to AX.
2304 __ fstsw();
2305
2306 // And see if the argument reduction is complete. This is signaled by the
2307 // C2 FPU flag bit set to 0.
2308 __ andl(EAX, Immediate(kC2ConditionMask));
2309 __ j(kNotEqual, &retry);
2310
2311 // We have settled on the final value. Retrieve it into an XMM register.
2312 // Store FP top of stack to real stack.
2313 if (is_float) {
2314 __ fsts(Address(ESP, 0));
2315 } else {
2316 __ fstl(Address(ESP, 0));
2317 }
2318
2319 // Pop the 2 items from the FP stack.
2320 __ fucompp();
2321
2322 // Load the value from the stack into an XMM register.
2323 DCHECK(out.IsFpuRegister()) << out;
2324 if (is_float) {
2325 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2326 } else {
2327 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2328 }
2329
2330 // And remove the temporary stack space we allocated.
2331 __ addl(ESP, Immediate(2 * elem_size));
2332}
2333
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002334
2335void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2336 DCHECK(instruction->IsDiv() || instruction->IsRem());
2337
2338 LocationSummary* locations = instruction->GetLocations();
2339 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002340 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002341
2342 Register out_register = locations->Out().AsRegister<Register>();
2343 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002344 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002345
2346 DCHECK(imm == 1 || imm == -1);
2347
2348 if (instruction->IsRem()) {
2349 __ xorl(out_register, out_register);
2350 } else {
2351 __ movl(out_register, input_register);
2352 if (imm == -1) {
2353 __ negl(out_register);
2354 }
2355 }
2356}
2357
2358
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002359void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002360 LocationSummary* locations = instruction->GetLocations();
2361
2362 Register out_register = locations->Out().AsRegister<Register>();
2363 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002364 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002365
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002366 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002367 Register num = locations->GetTemp(0).AsRegister<Register>();
2368
2369 __ leal(num, Address(input_register, std::abs(imm) - 1));
2370 __ testl(input_register, input_register);
2371 __ cmovl(kGreaterEqual, num, input_register);
2372 int shift = CTZ(imm);
2373 __ sarl(num, Immediate(shift));
2374
2375 if (imm < 0) {
2376 __ negl(num);
2377 }
2378
2379 __ movl(out_register, num);
2380}
2381
2382void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2383 DCHECK(instruction->IsDiv() || instruction->IsRem());
2384
2385 LocationSummary* locations = instruction->GetLocations();
2386 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2387
2388 Register eax = locations->InAt(0).AsRegister<Register>();
2389 Register out = locations->Out().AsRegister<Register>();
2390 Register num;
2391 Register edx;
2392
2393 if (instruction->IsDiv()) {
2394 edx = locations->GetTemp(0).AsRegister<Register>();
2395 num = locations->GetTemp(1).AsRegister<Register>();
2396 } else {
2397 edx = locations->Out().AsRegister<Register>();
2398 num = locations->GetTemp(0).AsRegister<Register>();
2399 }
2400
2401 DCHECK_EQ(EAX, eax);
2402 DCHECK_EQ(EDX, edx);
2403 if (instruction->IsDiv()) {
2404 DCHECK_EQ(EAX, out);
2405 } else {
2406 DCHECK_EQ(EDX, out);
2407 }
2408
2409 int64_t magic;
2410 int shift;
2411 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2412
2413 Label ndiv;
2414 Label end;
2415 // If numerator is 0, the result is 0, no computation needed.
2416 __ testl(eax, eax);
2417 __ j(kNotEqual, &ndiv);
2418
2419 __ xorl(out, out);
2420 __ jmp(&end);
2421
2422 __ Bind(&ndiv);
2423
2424 // Save the numerator.
2425 __ movl(num, eax);
2426
2427 // EAX = magic
2428 __ movl(eax, Immediate(magic));
2429
2430 // EDX:EAX = magic * numerator
2431 __ imull(num);
2432
2433 if (imm > 0 && magic < 0) {
2434 // EDX += num
2435 __ addl(edx, num);
2436 } else if (imm < 0 && magic > 0) {
2437 __ subl(edx, num);
2438 }
2439
2440 // Shift if needed.
2441 if (shift != 0) {
2442 __ sarl(edx, Immediate(shift));
2443 }
2444
2445 // EDX += 1 if EDX < 0
2446 __ movl(eax, edx);
2447 __ shrl(edx, Immediate(31));
2448 __ addl(edx, eax);
2449
2450 if (instruction->IsRem()) {
2451 __ movl(eax, num);
2452 __ imull(edx, Immediate(imm));
2453 __ subl(eax, edx);
2454 __ movl(edx, eax);
2455 } else {
2456 __ movl(eax, edx);
2457 }
2458 __ Bind(&end);
2459}
2460
Calin Juravlebacfec32014-11-14 15:54:36 +00002461void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2462 DCHECK(instruction->IsDiv() || instruction->IsRem());
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location out = locations->Out();
2466 Location first = locations->InAt(0);
2467 Location second = locations->InAt(1);
2468 bool is_div = instruction->IsDiv();
2469
2470 switch (instruction->GetResultType()) {
2471 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 DCHECK_EQ(EAX, first.AsRegister<Register>());
2473 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002474
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002475 if (instruction->InputAt(1)->IsIntConstant()) {
2476 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477
2478 if (imm == 0) {
2479 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2480 } else if (imm == 1 || imm == -1) {
2481 DivRemOneOrMinusOne(instruction);
2482 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002483 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 } else {
2485 DCHECK(imm <= -2 || imm >= 2);
2486 GenerateDivRemWithAnyConstant(instruction);
2487 }
2488 } else {
2489 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002490 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002491 is_div);
2492 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002493
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494 Register second_reg = second.AsRegister<Register>();
2495 // 0x80000000/-1 triggers an arithmetic exception!
2496 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2497 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002498
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002499 __ cmpl(second_reg, Immediate(-1));
2500 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002501
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002502 // edx:eax <- sign-extended of eax
2503 __ cdq();
2504 // eax = quotient, edx = remainder
2505 __ idivl(second_reg);
2506 __ Bind(slow_path->GetExitLabel());
2507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002508 break;
2509 }
2510
2511 case Primitive::kPrimLong: {
2512 InvokeRuntimeCallingConvention calling_convention;
2513 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2514 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2516 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2517 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2518 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2519
2520 if (is_div) {
2521 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2522 } else {
2523 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2524 }
2525 uint32_t dex_pc = is_div
2526 ? instruction->AsDiv()->GetDexPc()
2527 : instruction->AsRem()->GetDexPc();
2528 codegen_->RecordPcInfo(instruction, dex_pc);
2529
2530 break;
2531 }
2532
2533 default:
2534 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2535 }
2536}
2537
Calin Juravle7c4954d2014-10-28 16:57:40 +00002538void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002539 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002540 ? LocationSummary::kCall
2541 : LocationSummary::kNoCall;
2542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2543
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002545 case Primitive::kPrimInt: {
2546 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002548 locations->SetOut(Location::SameAsFirstInput());
2549 // Intel uses edx:eax as the dividend.
2550 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2552 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2553 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002554 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 locations->AddTemp(Location::RequiresRegister());
2556 }
Calin Juravled0d48522014-11-04 16:40:20 +00002557 break;
2558 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002560 InvokeRuntimeCallingConvention calling_convention;
2561 locations->SetInAt(0, Location::RegisterPairLocation(
2562 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2563 locations->SetInAt(1, Location::RegisterPairLocation(
2564 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2565 // Runtime helper puts the result in EAX, EDX.
2566 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 break;
2568 }
2569 case Primitive::kPrimFloat:
2570 case Primitive::kPrimDouble: {
2571 locations->SetInAt(0, Location::RequiresFpuRegister());
2572 locations->SetInAt(1, Location::RequiresFpuRegister());
2573 locations->SetOut(Location::SameAsFirstInput());
2574 break;
2575 }
2576
2577 default:
2578 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2579 }
2580}
2581
2582void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2583 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002584 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002585 Location first = locations->InAt(0);
2586 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587
2588 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002589 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 break;
2593 }
2594
2595 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 break;
2599 }
2600
2601 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002602 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604 break;
2605 }
2606
2607 default:
2608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2609 }
2610}
2611
Calin Juravlebacfec32014-11-14 15:54:36 +00002612void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002613 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002614
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002615 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2616 ? LocationSummary::kCall
2617 : LocationSummary::kNoCall;
2618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002619
Calin Juravled2ec87d2014-12-08 14:24:46 +00002620 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 case Primitive::kPrimInt: {
2622 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002623 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002624 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2626 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2627 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002628 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002629 locations->AddTemp(Location::RequiresRegister());
2630 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002631 break;
2632 }
2633 case Primitive::kPrimLong: {
2634 InvokeRuntimeCallingConvention calling_convention;
2635 locations->SetInAt(0, Location::RegisterPairLocation(
2636 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2637 locations->SetInAt(1, Location::RegisterPairLocation(
2638 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2639 // Runtime helper puts the result in EAX, EDX.
2640 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2641 break;
2642 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002643 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002644 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 locations->SetInAt(0, Location::Any());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::RequiresFpuRegister());
2648 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002649 break;
2650 }
2651
2652 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002653 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002654 }
2655}
2656
2657void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2658 Primitive::Type type = rem->GetResultType();
2659 switch (type) {
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimLong: {
2662 GenerateDivRemIntegral(rem);
2663 break;
2664 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002665 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002666 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002667 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002668 break;
2669 }
2670 default:
2671 LOG(FATAL) << "Unexpected rem type " << type;
2672 }
2673}
2674
Calin Juravled0d48522014-11-04 16:40:20 +00002675void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 LocationSummary* locations =
2677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002678 switch (instruction->GetType()) {
2679 case Primitive::kPrimInt: {
2680 locations->SetInAt(0, Location::Any());
2681 break;
2682 }
2683 case Primitive::kPrimLong: {
2684 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2685 if (!instruction->IsConstant()) {
2686 locations->AddTemp(Location::RequiresRegister());
2687 }
2688 break;
2689 }
2690 default:
2691 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2692 }
Calin Juravled0d48522014-11-04 16:40:20 +00002693 if (instruction->HasUses()) {
2694 locations->SetOut(Location::SameAsFirstInput());
2695 }
2696}
2697
2698void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2699 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2700 codegen_->AddSlowPath(slow_path);
2701
2702 LocationSummary* locations = instruction->GetLocations();
2703 Location value = locations->InAt(0);
2704
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002705 switch (instruction->GetType()) {
2706 case Primitive::kPrimInt: {
2707 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002709 __ j(kEqual, slow_path->GetEntryLabel());
2710 } else if (value.IsStackSlot()) {
2711 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2712 __ j(kEqual, slow_path->GetEntryLabel());
2713 } else {
2714 DCHECK(value.IsConstant()) << value;
2715 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2716 __ jmp(slow_path->GetEntryLabel());
2717 }
2718 }
2719 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002720 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002721 case Primitive::kPrimLong: {
2722 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002724 __ movl(temp, value.AsRegisterPairLow<Register>());
2725 __ orl(temp, value.AsRegisterPairHigh<Register>());
2726 __ j(kEqual, slow_path->GetEntryLabel());
2727 } else {
2728 DCHECK(value.IsConstant()) << value;
2729 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2730 __ jmp(slow_path->GetEntryLabel());
2731 }
2732 }
2733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002737 }
Calin Juravled0d48522014-11-04 16:40:20 +00002738}
2739
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2741 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2742
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2745
2746 switch (op->GetResultType()) {
2747 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002748 locations->SetInAt(0, Location::RequiresRegister());
2749 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002750 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2751 locations->SetOut(Location::SameAsFirstInput());
2752 break;
2753 }
2754 case Primitive::kPrimLong: {
2755 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002756 // The shift count needs to be in CL.
2757 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002758 locations->SetOut(Location::SameAsFirstInput());
2759 break;
2760 }
2761 default:
2762 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2763 }
2764}
2765
2766void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2767 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2768
2769 LocationSummary* locations = op->GetLocations();
2770 Location first = locations->InAt(0);
2771 Location second = locations->InAt(1);
2772 DCHECK(first.Equals(locations->Out()));
2773
2774 switch (op->GetResultType()) {
2775 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 DCHECK_EQ(ECX, second_reg);
2780 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002783 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002784 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002786 }
2787 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002788 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2789 if (op->IsShl()) {
2790 __ shll(first_reg, imm);
2791 } else if (op->IsShr()) {
2792 __ sarl(first_reg, imm);
2793 } else {
2794 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795 }
2796 }
2797 break;
2798 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002799 case Primitive::kPrimLong: {
2800 Register second_reg = second.AsRegister<Register>();
2801 DCHECK_EQ(ECX, second_reg);
2802 if (op->IsShl()) {
2803 GenerateShlLong(first, second_reg);
2804 } else if (op->IsShr()) {
2805 GenerateShrLong(first, second_reg);
2806 } else {
2807 GenerateUShrLong(first, second_reg);
2808 }
2809 break;
2810 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002811 default:
2812 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2813 }
2814}
2815
2816void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2817 Label done;
2818 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2819 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2820 __ testl(shifter, Immediate(32));
2821 __ j(kEqual, &done);
2822 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2823 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2824 __ Bind(&done);
2825}
2826
2827void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2828 Label done;
2829 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2830 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2831 __ testl(shifter, Immediate(32));
2832 __ j(kEqual, &done);
2833 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2834 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2835 __ Bind(&done);
2836}
2837
2838void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2839 Label done;
2840 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2841 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2842 __ testl(shifter, Immediate(32));
2843 __ j(kEqual, &done);
2844 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2845 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2846 __ Bind(&done);
2847}
2848
2849void LocationsBuilderX86::VisitShl(HShl* shl) {
2850 HandleShift(shl);
2851}
2852
2853void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2854 HandleShift(shl);
2855}
2856
2857void LocationsBuilderX86::VisitShr(HShr* shr) {
2858 HandleShift(shr);
2859}
2860
2861void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2862 HandleShift(shr);
2863}
2864
2865void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2866 HandleShift(ushr);
2867}
2868
2869void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2870 HandleShift(ushr);
2871}
2872
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002873void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002876 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002877 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2879 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002880}
2881
2882void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2883 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002884 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002885 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002886
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002887 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002888
Nicolas Geoffray39468442014-09-02 15:17:15 +01002889 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002890 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002891}
2892
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002893void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2894 LocationSummary* locations =
2895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2896 locations->SetOut(Location::RegisterLocation(EAX));
2897 InvokeRuntimeCallingConvention calling_convention;
2898 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002899 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2900 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002901}
2902
2903void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2904 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002905 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002906 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2907
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002908 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002909
2910 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2911 DCHECK(!codegen_->IsLeafMethod());
2912}
2913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002915 LocationSummary* locations =
2916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002917 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2918 if (location.IsStackSlot()) {
2919 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2920 } else if (location.IsDoubleStackSlot()) {
2921 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002923 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924}
2925
2926void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002927 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002928}
2929
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002930void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002931 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002932 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002935}
2936
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002937void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2938 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002939 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002940 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002941 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002942 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002943 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002945 break;
2946
2947 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002948 __ notl(out.AsRegisterPairLow<Register>());
2949 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2954 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002955}
2956
David Brazdil66d126e2015-04-03 16:02:44 +01002957void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
2958 LocationSummary* locations =
2959 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
2960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetOut(Location::SameAsFirstInput());
2962}
2963
2964void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01002965 LocationSummary* locations = bool_not->GetLocations();
2966 Location in = locations->InAt(0);
2967 Location out = locations->Out();
2968 DCHECK(in.Equals(out));
2969 __ xorl(out.AsRegister<Register>(), Immediate(1));
2970}
2971
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002972void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002975 switch (compare->InputAt(0)->GetType()) {
2976 case Primitive::kPrimLong: {
2977 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002978 locations->SetInAt(1, Location::Any());
2979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2980 break;
2981 }
2982 case Primitive::kPrimFloat:
2983 case Primitive::kPrimDouble: {
2984 locations->SetInAt(0, Location::RequiresFpuRegister());
2985 locations->SetInAt(1, Location::RequiresFpuRegister());
2986 locations->SetOut(Location::RequiresRegister());
2987 break;
2988 }
2989 default:
2990 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2991 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002992}
2993
2994void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002995 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002997 Location left = locations->InAt(0);
2998 Location right = locations->InAt(1);
2999
3000 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003001 switch (compare->InputAt(0)->GetType()) {
3002 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003003 Register left_low = left.AsRegisterPairLow<Register>();
3004 Register left_high = left.AsRegisterPairHigh<Register>();
3005 int32_t val_low = 0;
3006 int32_t val_high = 0;
3007 bool right_is_const = false;
3008
3009 if (right.IsConstant()) {
3010 DCHECK(right.GetConstant()->IsLongConstant());
3011 right_is_const = true;
3012 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3013 val_low = Low32Bits(val);
3014 val_high = High32Bits(val);
3015 }
3016
Calin Juravleddb7df22014-11-25 20:56:51 +00003017 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003018 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003020 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003022 DCHECK(right_is_const) << right;
3023 if (val_high == 0) {
3024 __ testl(left_high, left_high);
3025 } else {
3026 __ cmpl(left_high, Immediate(val_high));
3027 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003029 __ j(kLess, &less); // Signed compare.
3030 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003031 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003032 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003033 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003034 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003035 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003036 DCHECK(right_is_const) << right;
3037 if (val_low == 0) {
3038 __ testl(left_low, left_low);
3039 } else {
3040 __ cmpl(left_low, Immediate(val_low));
3041 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003042 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003043 break;
3044 }
3045 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003047 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3048 break;
3049 }
3050 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003052 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003053 break;
3054 }
3055 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003056 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003057 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003058 __ movl(out, Immediate(0));
3059 __ j(kEqual, &done);
3060 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3061
3062 __ Bind(&greater);
3063 __ movl(out, Immediate(1));
3064 __ jmp(&done);
3065
3066 __ Bind(&less);
3067 __ movl(out, Immediate(-1));
3068
3069 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003070}
3071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003072void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003075 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3076 locations->SetInAt(i, Location::Any());
3077 }
3078 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003079}
3080
3081void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003082 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003083 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003084}
3085
Calin Juravle52c48962014-12-16 17:02:57 +00003086void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3087 /*
3088 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3089 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3090 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3091 */
3092 switch (kind) {
3093 case MemBarrierKind::kAnyAny: {
3094 __ mfence();
3095 break;
3096 }
3097 case MemBarrierKind::kAnyStore:
3098 case MemBarrierKind::kLoadAny:
3099 case MemBarrierKind::kStoreStore: {
3100 // nop
3101 break;
3102 }
3103 default:
3104 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003105 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003106}
3107
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108
Mark Mendell09ed1a32015-03-25 08:30:06 -04003109void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3110 Register temp) {
3111 // TODO: Implement all kinds of calls:
3112 // 1) boot -> boot
3113 // 2) app -> boot
3114 // 3) app -> app
3115 //
3116 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003117
3118 if (invoke->IsStringInit()) {
3119 // temp = thread->string_init_entrypoint
3120 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003121 // (temp + offset_of_quick_compiled_code)()
3122 __ call(Address(
3123 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3124 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003125 // temp = method;
3126 LoadCurrentMethod(temp);
3127 if (!invoke->IsRecursive()) {
3128 // temp = temp->dex_cache_resolved_methods_;
3129 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3130 // temp = temp[index_in_cache]
3131 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3132 // (temp + offset_of_quick_compiled_code)()
3133 __ call(Address(temp,
3134 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3135 } else {
3136 __ call(GetFrameEntryLabel());
3137 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003138 }
3139
3140 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003141}
3142
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003143void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003144 Label is_null;
3145 __ testl(value, value);
3146 __ j(kEqual, &is_null);
3147 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3148 __ movl(temp, object);
3149 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003150 __ movb(Address(temp, card, TIMES_1, 0),
3151 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003152 __ Bind(&is_null);
3153}
3154
Calin Juravle52c48962014-12-16 17:02:57 +00003155void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3156 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003159 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003160
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003161 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3162 locations->SetOut(Location::RequiresFpuRegister());
3163 } else {
3164 // The output overlaps in case of long: we don't want the low move to overwrite
3165 // the object's location.
3166 locations->SetOut(Location::RequiresRegister(),
3167 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3168 : Location::kNoOutputOverlap);
3169 }
Calin Juravle52c48962014-12-16 17:02:57 +00003170
3171 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3172 // Long values can be loaded atomically into an XMM using movsd.
3173 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3174 // and then copy the XMM into the output 32bits at a time).
3175 locations->AddTemp(Location::RequiresFpuRegister());
3176 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177}
3178
Calin Juravle52c48962014-12-16 17:02:57 +00003179void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3180 const FieldInfo& field_info) {
3181 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003182
Calin Juravle52c48962014-12-16 17:02:57 +00003183 LocationSummary* locations = instruction->GetLocations();
3184 Register base = locations->InAt(0).AsRegister<Register>();
3185 Location out = locations->Out();
3186 bool is_volatile = field_info.IsVolatile();
3187 Primitive::Type field_type = field_info.GetFieldType();
3188 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3189
3190 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003191 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003192 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003193 break;
3194 }
3195
3196 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003197 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003198 break;
3199 }
3200
3201 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003202 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203 break;
3204 }
3205
3206 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003207 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003208 break;
3209 }
3210
3211 case Primitive::kPrimInt:
3212 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003213 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003214 break;
3215 }
3216
3217 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003218 if (is_volatile) {
3219 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3220 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003222 __ movd(out.AsRegisterPairLow<Register>(), temp);
3223 __ psrlq(temp, Immediate(32));
3224 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3225 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003226 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003227 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003229 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3230 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003231 break;
3232 }
3233
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003234 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003235 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003236 break;
3237 }
3238
3239 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003240 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003241 break;
3242 }
3243
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003244 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003245 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003246 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003247 }
Calin Juravle52c48962014-12-16 17:02:57 +00003248
Calin Juravle77520bc2015-01-12 18:45:46 +00003249 // Longs are handled in the switch.
3250 if (field_type != Primitive::kPrimLong) {
3251 codegen_->MaybeRecordImplicitNullCheck(instruction);
3252 }
3253
Calin Juravle52c48962014-12-16 17:02:57 +00003254 if (is_volatile) {
3255 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3256 }
3257}
3258
3259void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3260 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3261
3262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3264 locations->SetInAt(0, Location::RequiresRegister());
3265 bool is_volatile = field_info.IsVolatile();
3266 Primitive::Type field_type = field_info.GetFieldType();
3267 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3268 || (field_type == Primitive::kPrimByte);
3269
3270 // The register allocator does not support multiple
3271 // inputs that die at entry with one in a specific register.
3272 if (is_byte_type) {
3273 // Ensure the value is in a byte register.
3274 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003275 } else if (Primitive::IsFloatingPointType(field_type)) {
3276 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003277 } else {
3278 locations->SetInAt(1, Location::RequiresRegister());
3279 }
3280 // Temporary registers for the write barrier.
3281 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3282 locations->AddTemp(Location::RequiresRegister());
3283 // Ensure the card is in a byte register.
3284 locations->AddTemp(Location::RegisterLocation(ECX));
3285 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3286 // 64bits value can be atomically written to an address with movsd and an XMM register.
3287 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3288 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3289 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3290 // isolated cases when we need this it isn't worth adding the extra complexity.
3291 locations->AddTemp(Location::RequiresFpuRegister());
3292 locations->AddTemp(Location::RequiresFpuRegister());
3293 }
3294}
3295
3296void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3297 const FieldInfo& field_info) {
3298 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3299
3300 LocationSummary* locations = instruction->GetLocations();
3301 Register base = locations->InAt(0).AsRegister<Register>();
3302 Location value = locations->InAt(1);
3303 bool is_volatile = field_info.IsVolatile();
3304 Primitive::Type field_type = field_info.GetFieldType();
3305 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3306
3307 if (is_volatile) {
3308 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3309 }
3310
3311 switch (field_type) {
3312 case Primitive::kPrimBoolean:
3313 case Primitive::kPrimByte: {
3314 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3315 break;
3316 }
3317
3318 case Primitive::kPrimShort:
3319 case Primitive::kPrimChar: {
3320 __ movw(Address(base, offset), value.AsRegister<Register>());
3321 break;
3322 }
3323
3324 case Primitive::kPrimInt:
3325 case Primitive::kPrimNot: {
3326 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003327 break;
3328 }
3329
3330 case Primitive::kPrimLong: {
3331 if (is_volatile) {
3332 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3333 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3334 __ movd(temp1, value.AsRegisterPairLow<Register>());
3335 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3336 __ punpckldq(temp1, temp2);
3337 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003338 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003339 } else {
3340 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003341 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003342 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3343 }
3344 break;
3345 }
3346
3347 case Primitive::kPrimFloat: {
3348 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3349 break;
3350 }
3351
3352 case Primitive::kPrimDouble: {
3353 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3354 break;
3355 }
3356
3357 case Primitive::kPrimVoid:
3358 LOG(FATAL) << "Unreachable type " << field_type;
3359 UNREACHABLE();
3360 }
3361
Calin Juravle77520bc2015-01-12 18:45:46 +00003362 // Longs are handled in the switch.
3363 if (field_type != Primitive::kPrimLong) {
3364 codegen_->MaybeRecordImplicitNullCheck(instruction);
3365 }
3366
3367 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3368 Register temp = locations->GetTemp(0).AsRegister<Register>();
3369 Register card = locations->GetTemp(1).AsRegister<Register>();
3370 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3371 }
3372
Calin Juravle52c48962014-12-16 17:02:57 +00003373 if (is_volatile) {
3374 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3375 }
3376}
3377
3378void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3379 HandleFieldGet(instruction, instruction->GetFieldInfo());
3380}
3381
3382void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3383 HandleFieldGet(instruction, instruction->GetFieldInfo());
3384}
3385
3386void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3387 HandleFieldSet(instruction, instruction->GetFieldInfo());
3388}
3389
3390void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3391 HandleFieldSet(instruction, instruction->GetFieldInfo());
3392}
3393
3394void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3395 HandleFieldSet(instruction, instruction->GetFieldInfo());
3396}
3397
3398void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3399 HandleFieldSet(instruction, instruction->GetFieldInfo());
3400}
3401
3402void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3403 HandleFieldGet(instruction, instruction->GetFieldInfo());
3404}
3405
3406void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3407 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003408}
3409
3410void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003411 LocationSummary* locations =
3412 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003413 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3414 ? Location::RequiresRegister()
3415 : Location::Any();
3416 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003417 if (instruction->HasUses()) {
3418 locations->SetOut(Location::SameAsFirstInput());
3419 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420}
3421
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003422void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003423 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3424 return;
3425 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003426 LocationSummary* locations = instruction->GetLocations();
3427 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003428
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003429 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3430 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3431}
3432
3433void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003434 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003435 codegen_->AddSlowPath(slow_path);
3436
3437 LocationSummary* locations = instruction->GetLocations();
3438 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003439
3440 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003441 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003442 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003443 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003444 } else {
3445 DCHECK(obj.IsConstant()) << obj;
3446 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3447 __ jmp(slow_path->GetEntryLabel());
3448 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003449 }
3450 __ j(kEqual, slow_path->GetEntryLabel());
3451}
3452
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003453void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3454 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3455 GenerateImplicitNullCheck(instruction);
3456 } else {
3457 GenerateExplicitNullCheck(instruction);
3458 }
3459}
3460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003461void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003464 locations->SetInAt(0, Location::RequiresRegister());
3465 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003466 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3468 } else {
3469 // The output overlaps in case of long: we don't want the low move to overwrite
3470 // the array's location.
3471 locations->SetOut(Location::RequiresRegister(),
3472 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3473 : Location::kNoOutputOverlap);
3474 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475}
3476
3477void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3478 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 Location index = locations->InAt(1);
3481
Calin Juravle77520bc2015-01-12 18:45:46 +00003482 Primitive::Type type = instruction->GetType();
3483 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003484 case Primitive::kPrimBoolean: {
3485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003487 if (index.IsConstant()) {
3488 __ movzxb(out, Address(obj,
3489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3490 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003492 }
3493 break;
3494 }
3495
3496 case Primitive::kPrimByte: {
3497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003498 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003499 if (index.IsConstant()) {
3500 __ movsxb(out, Address(obj,
3501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3502 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003504 }
3505 break;
3506 }
3507
3508 case Primitive::kPrimShort: {
3509 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003510 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003511 if (index.IsConstant()) {
3512 __ movsxw(out, Address(obj,
3513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3514 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003515 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516 }
3517 break;
3518 }
3519
3520 case Primitive::kPrimChar: {
3521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
3525 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3526 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 }
3529 break;
3530 }
3531
3532 case Primitive::kPrimInt:
3533 case Primitive::kPrimNot: {
3534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 if (index.IsConstant()) {
3537 __ movl(out, Address(obj,
3538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3539 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003541 }
3542 break;
3543 }
3544
3545 case Primitive::kPrimLong: {
3546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003547 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003548 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003549 if (index.IsConstant()) {
3550 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003551 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003553 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003554 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003555 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003556 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003557 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003558 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 }
3561 break;
3562 }
3563
Mark Mendell7c8d0092015-01-26 11:21:33 -05003564 case Primitive::kPrimFloat: {
3565 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3566 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3567 if (index.IsConstant()) {
3568 __ movss(out, Address(obj,
3569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3570 } else {
3571 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3572 }
3573 break;
3574 }
3575
3576 case Primitive::kPrimDouble: {
3577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3578 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3579 if (index.IsConstant()) {
3580 __ movsd(out, Address(obj,
3581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3582 } else {
3583 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3584 }
3585 break;
3586 }
3587
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003588 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003589 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003590 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003591 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003592
3593 if (type != Primitive::kPrimLong) {
3594 codegen_->MaybeRecordImplicitNullCheck(instruction);
3595 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596}
3597
3598void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003599 // This location builder might end up asking to up to four registers, which is
3600 // not currently possible for baseline. The situation in which we need four
3601 // registers cannot be met by baseline though, because it has not run any
3602 // optimization.
3603
Nicolas Geoffray39468442014-09-02 15:17:15 +01003604 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003605 bool needs_write_barrier =
3606 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3607
Mark Mendell5f874182015-03-04 15:42:45 -05003608 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003609
Nicolas Geoffray39468442014-09-02 15:17:15 +01003610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3611 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003612 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003613
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003614 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003616 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3617 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3618 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003620 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3621 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003622 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003623 // In case of a byte operation, the register allocator does not support multiple
3624 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003625 locations->SetInAt(0, Location::RequiresRegister());
3626 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003627 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003628 // Ensure the value is in a byte register.
3629 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003630 } else if (Primitive::IsFloatingPointType(value_type)) {
3631 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003633 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003635 // Temporary registers for the write barrier.
3636 if (needs_write_barrier) {
3637 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003638 // Ensure the card is in a byte register.
3639 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003640 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003641 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003642}
3643
3644void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3645 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003648 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003649 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003650 bool needs_runtime_call = locations->WillCall();
3651 bool needs_write_barrier =
3652 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653
3654 switch (value_type) {
3655 case Primitive::kPrimBoolean:
3656 case Primitive::kPrimByte: {
3657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 if (index.IsConstant()) {
3659 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003660 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003661 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003662 } else {
3663 __ movb(Address(obj, offset),
3664 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3665 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003667 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003669 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003670 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003672 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3673 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003674 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003676 break;
3677 }
3678
3679 case Primitive::kPrimShort:
3680 case Primitive::kPrimChar: {
3681 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003682 if (index.IsConstant()) {
3683 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003684 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003686 } else {
3687 __ movw(Address(obj, offset),
3688 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3689 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003690 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003691 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3693 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003694 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003696 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3697 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003699 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700 break;
3701 }
3702
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003703 case Primitive::kPrimInt:
3704 case Primitive::kPrimNot: {
3705 if (!needs_runtime_call) {
3706 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3707 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003708 size_t offset =
3709 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003710 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003712 } else {
3713 DCHECK(value.IsConstant()) << value;
3714 __ movl(Address(obj, offset),
3715 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3716 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003717 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718 DCHECK(index.IsRegister()) << index;
3719 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003720 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3721 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003722 } else {
3723 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003725 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3726 }
3727 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003728 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003729
3730 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 Register temp = locations->GetTemp(0).AsRegister<Register>();
3732 Register card = locations->GetTemp(1).AsRegister<Register>();
3733 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003734 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003736 DCHECK_EQ(value_type, Primitive::kPrimNot);
3737 DCHECK(!codegen_->IsLeafMethod());
3738 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3739 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 }
3741 break;
3742 }
3743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 case Primitive::kPrimLong: {
3745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 if (index.IsConstant()) {
3747 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003748 if (value.IsRegisterPair()) {
3749 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003750 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003751 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003752 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003753 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003754 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3755 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3758 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003760 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003761 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003762 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003763 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003764 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003765 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003766 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003767 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003768 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003770 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003771 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003773 Immediate(High32Bits(val)));
3774 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 }
3776 break;
3777 }
3778
Mark Mendell7c8d0092015-01-26 11:21:33 -05003779 case Primitive::kPrimFloat: {
3780 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3781 DCHECK(value.IsFpuRegister());
3782 if (index.IsConstant()) {
3783 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3784 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3785 } else {
3786 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3787 value.AsFpuRegister<XmmRegister>());
3788 }
3789 break;
3790 }
3791
3792 case Primitive::kPrimDouble: {
3793 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3794 DCHECK(value.IsFpuRegister());
3795 if (index.IsConstant()) {
3796 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3797 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3798 } else {
3799 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3800 value.AsFpuRegister<XmmRegister>());
3801 }
3802 break;
3803 }
3804
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 case Primitive::kPrimVoid:
3806 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003807 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808 }
3809}
3810
3811void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3812 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003813 locations->SetInAt(0, Location::RequiresRegister());
3814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815 instruction->SetLocations(locations);
3816}
3817
3818void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3819 LocationSummary* locations = instruction->GetLocations();
3820 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003821 Register obj = locations->InAt(0).AsRegister<Register>();
3822 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003825}
3826
3827void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003828 LocationSummary* locations =
3829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003830 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003831 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003832 if (instruction->HasUses()) {
3833 locations->SetOut(Location::SameAsFirstInput());
3834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003835}
3836
3837void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3838 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003839 Location index_loc = locations->InAt(0);
3840 Location length_loc = locations->InAt(1);
3841 SlowPathCodeX86* slow_path =
3842 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843
Mark Mendell99dbd682015-04-22 16:18:52 -04003844 if (length_loc.IsConstant()) {
3845 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3846 if (index_loc.IsConstant()) {
3847 // BCE will remove the bounds check if we are guarenteed to pass.
3848 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3849 if (index < 0 || index >= length) {
3850 codegen_->AddSlowPath(slow_path);
3851 __ jmp(slow_path->GetEntryLabel());
3852 } else {
3853 // Some optimization after BCE may have generated this, and we should not
3854 // generate a bounds check if it is a valid range.
3855 }
3856 return;
3857 }
3858
3859 // We have to reverse the jump condition because the length is the constant.
3860 Register index_reg = index_loc.AsRegister<Register>();
3861 __ cmpl(index_reg, Immediate(length));
3862 codegen_->AddSlowPath(slow_path);
3863 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003864 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003865 Register length = length_loc.AsRegister<Register>();
3866 if (index_loc.IsConstant()) {
3867 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3868 __ cmpl(length, Immediate(value));
3869 } else {
3870 __ cmpl(length, index_loc.AsRegister<Register>());
3871 }
3872 codegen_->AddSlowPath(slow_path);
3873 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003874 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003875}
3876
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003877void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3878 temp->SetLocations(nullptr);
3879}
3880
3881void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3882 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003883 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003884}
3885
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003886void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003887 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003888 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003889}
3890
3891void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003892 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3893}
3894
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003895void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3897}
3898
3899void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003900 HBasicBlock* block = instruction->GetBlock();
3901 if (block->GetLoopInformation() != nullptr) {
3902 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3903 // The back edge will generate the suspend check.
3904 return;
3905 }
3906 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3907 // The goto will generate the suspend check.
3908 return;
3909 }
3910 GenerateSuspendCheck(instruction, nullptr);
3911}
3912
3913void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3914 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003915 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003916 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003917 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003918 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003919 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003920 if (successor == nullptr) {
3921 __ j(kNotEqual, slow_path->GetEntryLabel());
3922 __ Bind(slow_path->GetReturnLabel());
3923 } else {
3924 __ j(kEqual, codegen_->GetLabelOf(successor));
3925 __ jmp(slow_path->GetEntryLabel());
3926 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003927}
3928
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003929X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3930 return codegen_->GetAssembler();
3931}
3932
Mark Mendell7c8d0092015-01-26 11:21:33 -05003933void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003934 ScratchRegisterScope ensure_scratch(
3935 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3936 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3937 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3938 __ movl(temp_reg, Address(ESP, src + stack_offset));
3939 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003940}
3941
3942void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00003943 ScratchRegisterScope ensure_scratch(
3944 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3945 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3946 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3947 __ movl(temp_reg, Address(ESP, src + stack_offset));
3948 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3949 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3950 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003951}
3952
3953void ParallelMoveResolverX86::EmitMove(size_t index) {
3954 MoveOperands* move = moves_.Get(index);
3955 Location source = move->GetSource();
3956 Location destination = move->GetDestination();
3957
3958 if (source.IsRegister()) {
3959 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003960 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003961 } else {
3962 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003963 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003964 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003965 } else if (source.IsFpuRegister()) {
3966 if (destination.IsFpuRegister()) {
3967 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3968 } else if (destination.IsStackSlot()) {
3969 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3970 } else {
3971 DCHECK(destination.IsDoubleStackSlot());
3972 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3973 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003974 } else if (source.IsStackSlot()) {
3975 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003976 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003977 } else if (destination.IsFpuRegister()) {
3978 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003979 } else {
3980 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003981 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3982 }
3983 } else if (source.IsDoubleStackSlot()) {
3984 if (destination.IsFpuRegister()) {
3985 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3986 } else {
3987 DCHECK(destination.IsDoubleStackSlot()) << destination;
3988 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003989 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003990 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003991 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003992 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003993 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003994 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003995 if (value == 0) {
3996 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3997 } else {
3998 __ movl(destination.AsRegister<Register>(), Immediate(value));
3999 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004000 } else {
4001 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004002 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004003 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004004 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004005 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004006 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004007 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004008 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004009 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4010 if (value == 0) {
4011 // Easy handling of 0.0.
4012 __ xorps(dest, dest);
4013 } else {
4014 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004015 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4016 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4017 __ movl(temp, Immediate(value));
4018 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004019 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004020 } else {
4021 DCHECK(destination.IsStackSlot()) << destination;
4022 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4023 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004024 } else if (constant->IsLongConstant()) {
4025 int64_t value = constant->AsLongConstant()->GetValue();
4026 int32_t low_value = Low32Bits(value);
4027 int32_t high_value = High32Bits(value);
4028 Immediate low(low_value);
4029 Immediate high(high_value);
4030 if (destination.IsDoubleStackSlot()) {
4031 __ movl(Address(ESP, destination.GetStackIndex()), low);
4032 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4033 } else {
4034 __ movl(destination.AsRegisterPairLow<Register>(), low);
4035 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4036 }
4037 } else {
4038 DCHECK(constant->IsDoubleConstant());
4039 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004040 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004041 int32_t low_value = Low32Bits(value);
4042 int32_t high_value = High32Bits(value);
4043 Immediate low(low_value);
4044 Immediate high(high_value);
4045 if (destination.IsFpuRegister()) {
4046 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4047 if (value == 0) {
4048 // Easy handling of 0.0.
4049 __ xorpd(dest, dest);
4050 } else {
4051 __ pushl(high);
4052 __ pushl(low);
4053 __ movsd(dest, Address(ESP, 0));
4054 __ addl(ESP, Immediate(8));
4055 }
4056 } else {
4057 DCHECK(destination.IsDoubleStackSlot()) << destination;
4058 __ movl(Address(ESP, destination.GetStackIndex()), low);
4059 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4060 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004061 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004062 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004063 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004064 }
4065}
4066
Mark Mendella5c19ce2015-04-01 12:51:05 -04004067void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004068 Register suggested_scratch = reg == EAX ? EBX : EAX;
4069 ScratchRegisterScope ensure_scratch(
4070 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4071
4072 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4073 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4074 __ movl(Address(ESP, mem + stack_offset), reg);
4075 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004076}
4077
Mark Mendell7c8d0092015-01-26 11:21:33 -05004078void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004079 ScratchRegisterScope ensure_scratch(
4080 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4081
4082 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4083 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4084 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4085 __ movss(Address(ESP, mem + stack_offset), reg);
4086 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004087}
4088
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004089void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004090 ScratchRegisterScope ensure_scratch1(
4091 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004092
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004093 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4094 ScratchRegisterScope ensure_scratch2(
4095 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004096
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004097 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4098 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4099 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4100 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4101 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4102 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004103}
4104
4105void ParallelMoveResolverX86::EmitSwap(size_t index) {
4106 MoveOperands* move = moves_.Get(index);
4107 Location source = move->GetSource();
4108 Location destination = move->GetDestination();
4109
4110 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004111 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004112 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004113 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004114 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004115 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004116 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4117 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4119 // Use XOR Swap algorithm to avoid a temporary.
4120 DCHECK_NE(source.reg(), destination.reg());
4121 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4122 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4123 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4124 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4125 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4126 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4127 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004128 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4129 // Take advantage of the 16 bytes in the XMM register.
4130 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4131 Address stack(ESP, destination.GetStackIndex());
4132 // Load the double into the high doubleword.
4133 __ movhpd(reg, stack);
4134
4135 // Store the low double into the destination.
4136 __ movsd(stack, reg);
4137
4138 // Move the high double to the low double.
4139 __ psrldq(reg, Immediate(8));
4140 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4141 // Take advantage of the 16 bytes in the XMM register.
4142 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4143 Address stack(ESP, source.GetStackIndex());
4144 // Load the double into the high doubleword.
4145 __ movhpd(reg, stack);
4146
4147 // Store the low double into the destination.
4148 __ movsd(stack, reg);
4149
4150 // Move the high double to the low double.
4151 __ psrldq(reg, Immediate(8));
4152 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4153 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4154 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004155 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004156 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004157 }
4158}
4159
4160void ParallelMoveResolverX86::SpillScratch(int reg) {
4161 __ pushl(static_cast<Register>(reg));
4162}
4163
4164void ParallelMoveResolverX86::RestoreScratch(int reg) {
4165 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004166}
4167
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004168void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004169 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4170 ? LocationSummary::kCallOnSlowPath
4171 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004172 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004173 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004174 locations->SetOut(Location::RequiresRegister());
4175}
4176
4177void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004178 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004179 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004180 DCHECK(!cls->CanCallRuntime());
4181 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004182 codegen_->LoadCurrentMethod(out);
4183 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4184 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004185 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004186 codegen_->LoadCurrentMethod(out);
4187 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4188 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004189
4190 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4191 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4192 codegen_->AddSlowPath(slow_path);
4193 __ testl(out, out);
4194 __ j(kEqual, slow_path->GetEntryLabel());
4195 if (cls->MustGenerateClinitCheck()) {
4196 GenerateClassInitializationCheck(slow_path, out);
4197 } else {
4198 __ Bind(slow_path->GetExitLabel());
4199 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004200 }
4201}
4202
4203void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4206 locations->SetInAt(0, Location::RequiresRegister());
4207 if (check->HasUses()) {
4208 locations->SetOut(Location::SameAsFirstInput());
4209 }
4210}
4211
4212void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004213 // We assume the class to not be null.
4214 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4215 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004216 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004217 GenerateClassInitializationCheck(slow_path,
4218 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004219}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004220
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004221void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4222 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004223 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4224 Immediate(mirror::Class::kStatusInitialized));
4225 __ j(kLess, slow_path->GetEntryLabel());
4226 __ Bind(slow_path->GetExitLabel());
4227 // No need for memory fence, thanks to the X86 memory model.
4228}
4229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004230void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4231 LocationSummary* locations =
4232 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4233 locations->SetOut(Location::RequiresRegister());
4234}
4235
4236void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4237 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4238 codegen_->AddSlowPath(slow_path);
4239
Roland Levillain271ab9c2014-11-27 15:23:57 +00004240 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004241 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004242 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4243 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004244 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4245 __ testl(out, out);
4246 __ j(kEqual, slow_path->GetEntryLabel());
4247 __ Bind(slow_path->GetExitLabel());
4248}
4249
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004250void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4251 LocationSummary* locations =
4252 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4253 locations->SetOut(Location::RequiresRegister());
4254}
4255
4256void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4257 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004258 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004259 __ fs()->movl(address, Immediate(0));
4260}
4261
4262void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4263 LocationSummary* locations =
4264 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4265 InvokeRuntimeCallingConvention calling_convention;
4266 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4267}
4268
4269void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4270 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4271 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4272}
4273
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004274void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004275 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4276 ? LocationSummary::kNoCall
4277 : LocationSummary::kCallOnSlowPath;
4278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4279 locations->SetInAt(0, Location::RequiresRegister());
4280 locations->SetInAt(1, Location::Any());
4281 locations->SetOut(Location::RequiresRegister());
4282}
4283
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004284void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004285 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004287 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004289 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4290 Label done, zero;
4291 SlowPathCodeX86* slow_path = nullptr;
4292
4293 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004294 // Avoid null check if we know obj is not null.
4295 if (instruction->MustDoNullCheck()) {
4296 __ testl(obj, obj);
4297 __ j(kEqual, &zero);
4298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004299 __ movl(out, Address(obj, class_offset));
4300 // Compare the class of `obj` with `cls`.
4301 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004302 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004303 } else {
4304 DCHECK(cls.IsStackSlot()) << cls;
4305 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4306 }
4307
4308 if (instruction->IsClassFinal()) {
4309 // Classes must be equal for the instanceof to succeed.
4310 __ j(kNotEqual, &zero);
4311 __ movl(out, Immediate(1));
4312 __ jmp(&done);
4313 } else {
4314 // If the classes are not equal, we go into a slow path.
4315 DCHECK(locations->OnlyCallsOnSlowPath());
4316 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004317 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004318 codegen_->AddSlowPath(slow_path);
4319 __ j(kNotEqual, slow_path->GetEntryLabel());
4320 __ movl(out, Immediate(1));
4321 __ jmp(&done);
4322 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004323
4324 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4325 __ Bind(&zero);
4326 __ movl(out, Immediate(0));
4327 }
4328
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004329 if (slow_path != nullptr) {
4330 __ Bind(slow_path->GetExitLabel());
4331 }
4332 __ Bind(&done);
4333}
4334
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004335void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4337 instruction, LocationSummary::kCallOnSlowPath);
4338 locations->SetInAt(0, Location::RequiresRegister());
4339 locations->SetInAt(1, Location::Any());
4340 locations->AddTemp(Location::RequiresRegister());
4341}
4342
4343void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4344 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004345 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004346 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004347 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004348 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4349 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4350 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4351 codegen_->AddSlowPath(slow_path);
4352
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004353 // Avoid null check if we know obj is not null.
4354 if (instruction->MustDoNullCheck()) {
4355 __ testl(obj, obj);
4356 __ j(kEqual, slow_path->GetExitLabel());
4357 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004358
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004359 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004360 // Compare the class of `obj` with `cls`.
4361 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004363 } else {
4364 DCHECK(cls.IsStackSlot()) << cls;
4365 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4366 }
4367
4368 __ j(kNotEqual, slow_path->GetEntryLabel());
4369 __ Bind(slow_path->GetExitLabel());
4370}
4371
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004372void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4373 LocationSummary* locations =
4374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4375 InvokeRuntimeCallingConvention calling_convention;
4376 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4377}
4378
4379void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4380 __ fs()->call(Address::Absolute(instruction->IsEnter()
4381 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4382 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4383 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4384}
4385
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004386void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4387void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4388void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4389
4390void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4391 LocationSummary* locations =
4392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4393 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4394 || instruction->GetResultType() == Primitive::kPrimLong);
4395 locations->SetInAt(0, Location::RequiresRegister());
4396 locations->SetInAt(1, Location::Any());
4397 locations->SetOut(Location::SameAsFirstInput());
4398}
4399
4400void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4401 HandleBitwiseOperation(instruction);
4402}
4403
4404void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4405 HandleBitwiseOperation(instruction);
4406}
4407
4408void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4409 HandleBitwiseOperation(instruction);
4410}
4411
4412void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4413 LocationSummary* locations = instruction->GetLocations();
4414 Location first = locations->InAt(0);
4415 Location second = locations->InAt(1);
4416 DCHECK(first.Equals(locations->Out()));
4417
4418 if (instruction->GetResultType() == Primitive::kPrimInt) {
4419 if (second.IsRegister()) {
4420 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004422 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004424 } else {
4425 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004426 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004427 }
4428 } else if (second.IsConstant()) {
4429 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004430 __ andl(first.AsRegister<Register>(),
4431 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004432 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004433 __ orl(first.AsRegister<Register>(),
4434 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004435 } else {
4436 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004437 __ xorl(first.AsRegister<Register>(),
4438 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004439 }
4440 } else {
4441 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004443 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004444 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004445 } else {
4446 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004448 }
4449 }
4450 } else {
4451 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4452 if (second.IsRegisterPair()) {
4453 if (instruction->IsAnd()) {
4454 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4455 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4456 } else if (instruction->IsOr()) {
4457 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4458 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4459 } else {
4460 DCHECK(instruction->IsXor());
4461 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4462 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4463 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004464 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004465 if (instruction->IsAnd()) {
4466 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4467 __ andl(first.AsRegisterPairHigh<Register>(),
4468 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4469 } else if (instruction->IsOr()) {
4470 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4471 __ orl(first.AsRegisterPairHigh<Register>(),
4472 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4473 } else {
4474 DCHECK(instruction->IsXor());
4475 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4476 __ xorl(first.AsRegisterPairHigh<Register>(),
4477 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4478 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004479 } else {
4480 DCHECK(second.IsConstant()) << second;
4481 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004482 int32_t low_value = Low32Bits(value);
4483 int32_t high_value = High32Bits(value);
4484 Immediate low(low_value);
4485 Immediate high(high_value);
4486 Register first_low = first.AsRegisterPairLow<Register>();
4487 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004488 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004489 if (low_value == 0) {
4490 __ xorl(first_low, first_low);
4491 } else if (low_value != -1) {
4492 __ andl(first_low, low);
4493 }
4494 if (high_value == 0) {
4495 __ xorl(first_high, first_high);
4496 } else if (high_value != -1) {
4497 __ andl(first_high, high);
4498 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004499 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004500 if (low_value != 0) {
4501 __ orl(first_low, low);
4502 }
4503 if (high_value != 0) {
4504 __ orl(first_high, high);
4505 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004506 } else {
4507 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004508 if (low_value != 0) {
4509 __ xorl(first_low, low);
4510 }
4511 if (high_value != 0) {
4512 __ xorl(first_high, high);
4513 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004514 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004515 }
4516 }
4517}
4518
Calin Juravleb1498f62015-02-16 13:13:29 +00004519void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4520 // Nothing to do, this should be removed during prepare for register allocator.
4521 UNUSED(instruction);
4522 LOG(FATAL) << "Unreachable";
4523}
4524
4525void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4526 // Nothing to do, this should be removed during prepare for register allocator.
4527 UNUSED(instruction);
4528 LOG(FATAL) << "Unreachable";
4529}
4530
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004531} // namespace x86
4532} // namespace art