blob: 07d88deffa84f879daf9733d24fc758abdd3c3ac [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
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Mark P Mendell966c3ae2015-01-27 15:45:27 +000041static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
42static constexpr size_t kRuntimeParameterFpuRegistersLength =
43 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86 : public SlowPathCode {
64 public:
65 SlowPathCodeX86() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
75};
76
77class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
Alexandre Rames2ed20af2015-03-06 13:55:35 +000081 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 __ Bind(GetEntryLabel());
83 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 }
86
87 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
90};
91
Calin Juravled0d48522014-11-04 16:40:20 +000092class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
93 public:
94 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
95
Alexandre Rames2ed20af2015-03-06 13:55:35 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000097 __ Bind(GetEntryLabel());
98 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
99 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
100 }
101
102 private:
103 HDivZeroCheck* const instruction_;
104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Calin Juravlebacfec32014-11-14 15:54:36 +0000107class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
121 private:
122 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 bool is_div_;
124 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000125};
126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100129 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
130 Location index_location,
131 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000132 : instruction_(instruction),
133 index_location_(index_location),
134 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100138 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000139 // We're moving two locations to locations that could overlap, so we need a parallel
140 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000142 x86_codegen->EmitParallelMoves(
143 index_location_,
144 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
145 length_location_,
146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 }
150
151 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 const Location index_location_;
154 const Location length_location_;
155
156 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
157};
158
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
162 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000164 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100167 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
169 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100170 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 if (successor_ == nullptr) {
172 __ jmp(GetReturnLabel());
173 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 }
177
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 Label* GetReturnLabel() {
179 DCHECK(successor_ == nullptr);
180 return &return_label_;
181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182
183 private:
184 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 Label return_label_;
187
188 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
189};
190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000191class LoadStringSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
194
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000196 LocationSummary* locations = instruction_->GetLocations();
197 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
198
199 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
200 __ Bind(GetEntryLabel());
201 codegen->SaveLiveRegisters(locations);
202
203 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800204 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
205 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000206 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
207 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
208 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
209 codegen->RestoreLiveRegisters(locations);
210
211 __ jmp(GetExitLabel());
212 }
213
214 private:
215 HLoadString* const instruction_;
216
217 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
218};
219
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220class LoadClassSlowPathX86 : public SlowPathCodeX86 {
221 public:
222 LoadClassSlowPathX86(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
226 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
233 __ Bind(GetEntryLabel());
234 codegen->SaveLiveRegisters(locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
237 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
238 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
239 __ fs()->call(Address::Absolute(do_clinit_
240 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
241 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
242 codegen->RecordPcInfo(at_, dex_pc_);
243
244 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245 Location out = locations->Out();
246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
248 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 codegen->RestoreLiveRegisters(locations);
252 __ jmp(GetExitLabel());
253 }
254
255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
273 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 TypeCheckSlowPathX86(HInstruction* instruction,
275 Location class_to_check,
276 Location object_class,
277 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 class_to_check_(class_to_check),
280 object_class_(object_class),
281 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 DCHECK(instruction_->IsCheckCast()
286 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
288 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
289 __ Bind(GetEntryLabel());
290 codegen->SaveLiveRegisters(locations);
291
292 // We're moving two locations to locations that could overlap, so we need a parallel
293 // move resolver.
294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000295 x86_codegen->EmitParallelMoves(
296 class_to_check_,
297 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
298 object_class_,
299 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000302 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
303 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 } else {
305 DCHECK(instruction_->IsCheckCast());
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
307 }
308
309 codegen->RecordPcInfo(instruction_, dex_pc_);
310 if (instruction_->IsInstanceOf()) {
311 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
312 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 codegen->RestoreLiveRegisters(locations);
314
315 __ jmp(GetExitLabel());
316 }
317
318 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 HInstruction* const instruction_;
320 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
324 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
325};
326
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100327#undef __
328#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
329
Dave Allison20dfc792014-06-16 20:44:29 -0700330inline Condition X86Condition(IfCondition cond) {
331 switch (cond) {
332 case kCondEQ: return kEqual;
333 case kCondNE: return kNotEqual;
334 case kCondLT: return kLess;
335 case kCondLE: return kLessEqual;
336 case kCondGT: return kGreater;
337 case kCondGE: return kGreaterEqual;
338 default:
339 LOG(FATAL) << "Unknown if condition";
340 }
341 return kEqual;
342}
343
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
346}
347
348void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
349 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
350}
351
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100352size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
353 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
354 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100355}
356
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100357size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
358 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
359 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100360}
361
Mark Mendell7c8d0092015-01-26 11:21:33 -0500362size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
363 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
364 return GetFloatingPointSpillSlotSize();
365}
366
367size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
368 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
369 return GetFloatingPointSpillSlotSize();
370}
371
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000372CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500373 : CodeGenerator(graph,
374 kNumberOfCpuRegisters,
375 kNumberOfXmmRegisters,
376 kNumberOfRegisterPairs,
377 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
378 arraysize(kCoreCalleeSaves))
379 | (1 << kFakeReturnRegister),
380 0,
381 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100382 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100384 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 move_resolver_(graph->GetArena(), this) {
386 // Use a fake return address register to mimic Quick.
387 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100388}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 switch (type) {
392 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 X86ManagedRegister pair =
395 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100398 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
399 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100400 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100401 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100402 }
403
404 case Primitive::kPrimByte:
405 case Primitive::kPrimBoolean:
406 case Primitive::kPrimChar:
407 case Primitive::kPrimShort:
408 case Primitive::kPrimInt:
409 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
414 X86ManagedRegister current =
415 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
416 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421 }
422
423 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100425 return Location::FpuRegisterLocation(
426 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100427 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428
429 case Primitive::kPrimVoid:
430 LOG(FATAL) << "Unreachable type " << type;
431 }
432
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100433 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434}
435
Mark Mendell5f874182015-03-04 15:42:45 -0500436void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100437 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
440 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442
Mark Mendell5f874182015-03-04 15:42:45 -0500443 if (is_baseline) {
444 blocked_core_registers_[EBP] = true;
445 blocked_core_registers_[ESI] = true;
446 blocked_core_registers_[EDI] = true;
447 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100448
449 UpdateBlockedPairRegisters();
450}
451
452void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
453 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
454 X86ManagedRegister current =
455 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
456 if (blocked_core_registers_[current.AsRegisterPairLow()]
457 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
458 blocked_register_pairs_[i] = true;
459 }
460 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100461}
462
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100463InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
464 : HGraphVisitor(graph),
465 assembler_(codegen->GetAssembler()),
466 codegen_(codegen) {}
467
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000468void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000469 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000470 bool skip_overflow_check =
471 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000472 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000473
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000474 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100476 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100477 }
478
Mark Mendell5f874182015-03-04 15:42:45 -0500479 if (HasEmptyFrame()) {
480 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000481 }
Mark Mendell5f874182015-03-04 15:42:45 -0500482
483 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
484 Register reg = kCoreCalleeSaves[i];
485 if (allocated_registers_.ContainsCoreRegister(reg)) {
486 __ pushl(reg);
487 }
488 }
489
490 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
491 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000492}
493
494void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500495 if (HasEmptyFrame()) {
496 return;
497 }
498
499 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
500
501 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
502 Register reg = kCoreCalleeSaves[i];
503 if (allocated_registers_.ContainsCoreRegister(reg)) {
504 __ popl(reg);
505 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000506 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000507}
508
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100509void CodeGeneratorX86::Bind(HBasicBlock* block) {
510 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000511}
512
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000514 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100515 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000516}
517
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100518Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
519 switch (load->GetType()) {
520 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
523 break;
524
525 case Primitive::kPrimInt:
526 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100527 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100528 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100529
530 case Primitive::kPrimBoolean:
531 case Primitive::kPrimByte:
532 case Primitive::kPrimChar:
533 case Primitive::kPrimShort:
534 case Primitive::kPrimVoid:
535 LOG(FATAL) << "Unexpected type " << load->GetType();
536 }
537
538 LOG(FATAL) << "Unreachable";
539 return Location();
540}
541
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100542Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
543 switch (type) {
544 case Primitive::kPrimBoolean:
545 case Primitive::kPrimByte:
546 case Primitive::kPrimChar:
547 case Primitive::kPrimShort:
548 case Primitive::kPrimInt:
549 case Primitive::kPrimNot: {
550 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000551 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000555 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100556 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100557 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100558
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000559 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 uint32_t index = gp_index_;
561 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000562 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100563 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100564 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
565 calling_convention.GetRegisterPairAt(index));
566 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000568 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
569 }
570 }
571
572 case Primitive::kPrimFloat: {
573 uint32_t index = fp_index_++;
574 stack_index_++;
575 if (index < calling_convention.GetNumberOfFpuRegisters()) {
576 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
577 } else {
578 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
579 }
580 }
581
582 case Primitive::kPrimDouble: {
583 uint32_t index = fp_index_++;
584 stack_index_ += 2;
585 if (index < calling_convention.GetNumberOfFpuRegisters()) {
586 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
587 } else {
588 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100589 }
590 }
591
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100592 case Primitive::kPrimVoid:
593 LOG(FATAL) << "Unexpected parameter type " << type;
594 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100595 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100596 return Location();
597}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100598
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599void CodeGeneratorX86::Move32(Location destination, Location source) {
600 if (source.Equals(destination)) {
601 return;
602 }
603 if (destination.IsRegister()) {
604 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000605 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 } else {
609 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 } else if (destination.IsFpuRegister()) {
613 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000616 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 } else {
618 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000622 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500627 } else if (source.IsConstant()) {
628 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000629 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500630 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 } else {
632 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100633 __ pushl(Address(ESP, source.GetStackIndex()));
634 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 }
636 }
637}
638
639void CodeGeneratorX86::Move64(Location destination, Location source) {
640 if (source.Equals(destination)) {
641 return;
642 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 if (destination.IsRegisterPair()) {
644 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000645 EmitParallelMoves(
646 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
647 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
648 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
649 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (source.IsFpuRegister()) {
651 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
656 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
658 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500660 if (source.IsFpuRegister()) {
661 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
662 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else {
665 LOG(FATAL) << "Unimplemented";
666 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000668 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000670 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 } else {
Nicolas Geoffray154552e2015-03-06 16:10:14 +0000677 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000678 EmitParallelMoves(
679 Location::StackSlot(source.GetStackIndex()),
680 Location::StackSlot(destination.GetStackIndex()),
681 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
682 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 }
684 }
685}
686
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100687void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000688 LocationSummary* locations = instruction->GetLocations();
689 if (locations != nullptr && locations->Out().Equals(location)) {
690 return;
691 }
692
693 if (locations != nullptr && locations->Out().IsConstant()) {
694 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000695 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
696 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000697 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000698 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000699 } else if (location.IsStackSlot()) {
700 __ movl(Address(ESP, location.GetStackIndex()), imm);
701 } else {
702 DCHECK(location.IsConstant());
703 DCHECK_EQ(location.GetConstant(), const_to_move);
704 }
705 } else if (const_to_move->IsLongConstant()) {
706 int64_t value = const_to_move->AsLongConstant()->GetValue();
707 if (location.IsRegisterPair()) {
708 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
709 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
710 } else if (location.IsDoubleStackSlot()) {
711 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000712 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
713 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000714 } else {
715 DCHECK(location.IsConstant());
716 DCHECK_EQ(location.GetConstant(), instruction);
717 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000719 } else if (instruction->IsTemporary()) {
720 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000721 if (temp_location.IsStackSlot()) {
722 Move32(location, temp_location);
723 } else {
724 DCHECK(temp_location.IsDoubleStackSlot());
725 Move64(location, temp_location);
726 }
Roland Levillain476df552014-10-09 17:51:36 +0100727 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 switch (instruction->GetType()) {
730 case Primitive::kPrimBoolean:
731 case Primitive::kPrimByte:
732 case Primitive::kPrimChar:
733 case Primitive::kPrimShort:
734 case Primitive::kPrimInt:
735 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 case Primitive::kPrimFloat:
737 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 break;
739
740 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 case Primitive::kPrimDouble:
742 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 break;
744
745 default:
746 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
747 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000748 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100749 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 switch (instruction->GetType()) {
751 case Primitive::kPrimBoolean:
752 case Primitive::kPrimByte:
753 case Primitive::kPrimChar:
754 case Primitive::kPrimShort:
755 case Primitive::kPrimInt:
756 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000758 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 break;
760
761 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000763 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 break;
765
766 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000769 }
770}
771
772void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000773 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774}
775
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000776void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000777 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100778 DCHECK(!successor->IsExitBlock());
779
780 HBasicBlock* block = got->GetBlock();
781 HInstruction* previous = got->GetPrevious();
782
783 HLoopInformation* info = block->GetLoopInformation();
784 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
785 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
786 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
787 return;
788 }
789
790 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
791 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
792 }
793 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000794 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000795 }
796}
797
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000799 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800}
801
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700803 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000804 if (kIsDebugBuild) {
805 __ Comment("Unreachable");
806 __ int3();
807 }
808}
809
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000810void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100811 LocationSummary* locations =
812 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100813 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100814 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100815 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100816 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817}
818
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000819void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700820 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100821 if (cond->IsIntConstant()) {
822 // Constant condition, statically compared against 1.
823 int32_t cond_value = cond->AsIntConstant()->GetValue();
824 if (cond_value == 1) {
825 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
826 if_instr->IfTrueSuccessor())) {
827 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100828 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100829 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100830 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100831 DCHECK_EQ(cond_value, 0);
832 }
833 } else {
834 bool materialized =
835 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
836 // Moves do not affect the eflags register, so if the condition is
837 // evaluated just before the if, we don't need to evaluate it
838 // again.
839 bool eflags_set = cond->IsCondition()
840 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
841 if (materialized) {
842 if (!eflags_set) {
843 // Materialized condition, compare against 0.
844 Location lhs = if_instr->GetLocations()->InAt(0);
845 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500846 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 } else {
848 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
849 }
850 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
851 } else {
852 __ j(X86Condition(cond->AsCondition()->GetCondition()),
853 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
854 }
855 } else {
856 Location lhs = cond->GetLocations()->InAt(0);
857 Location rhs = cond->GetLocations()->InAt(1);
858 // LHS is guaranteed to be in a register (see
859 // LocationsBuilderX86::VisitCondition).
860 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000861 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100862 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500863 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
864 if (constant == 0) {
865 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
866 } else {
867 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
868 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100872 __ j(X86Condition(cond->AsCondition()->GetCondition()),
873 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700874 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100875 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100876 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
877 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700878 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000879 }
880}
881
882void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000884}
885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
887 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888}
889
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100891 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892}
893
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700896 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000897}
898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 switch (store->InputAt(1)->GetType()) {
903 case Primitive::kPrimBoolean:
904 case Primitive::kPrimByte:
905 case Primitive::kPrimChar:
906 case Primitive::kPrimShort:
907 case Primitive::kPrimInt:
908 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100910 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
911 break;
912
913 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
916 break;
917
918 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100919 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100920 }
921 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922}
923
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000924void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700925 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000926}
927
Dave Allison20dfc792014-06-16 20:44:29 -0700928void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 LocationSummary* locations =
930 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100931 locations->SetInAt(0, Location::RequiresRegister());
932 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100933 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500934 // We need a byte register.
935 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100936 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937}
938
Dave Allison20dfc792014-06-16 20:44:29 -0700939void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
940 if (comp->NeedsMaterialization()) {
941 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000942 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100943 // Clear register: setcc only sets the low byte.
944 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500945 Location lhs = locations->InAt(0);
946 Location rhs = locations->InAt(1);
947 if (rhs.IsRegister()) {
948 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
949 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800950 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500951 if (constant == 0) {
952 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
953 } else {
954 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
955 }
Dave Allison20dfc792014-06-16 20:44:29 -0700956 } else {
Mark Mendell09b84632015-02-13 17:48:38 -0500957 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700958 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000959 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100960 }
Dave Allison20dfc792014-06-16 20:44:29 -0700961}
962
963void LocationsBuilderX86::VisitEqual(HEqual* comp) {
964 VisitCondition(comp);
965}
966
967void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
968 VisitCondition(comp);
969}
970
971void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
972 VisitCondition(comp);
973}
974
975void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
976 VisitCondition(comp);
977}
978
979void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
980 VisitCondition(comp);
981}
982
983void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
984 VisitCondition(comp);
985}
986
987void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
988 VisitCondition(comp);
989}
990
991void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
992 VisitCondition(comp);
993}
994
995void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
996 VisitCondition(comp);
997}
998
999void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1008 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001009}
1010
1011void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001014 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001015}
1016
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001017void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001018 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001020}
1021
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001022void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1023 LocationSummary* locations =
1024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1025 locations->SetOut(Location::ConstantLocation(constant));
1026}
1027
1028void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1029 // Will be generated at use site.
1030 UNUSED(constant);
1031}
1032
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001034 LocationSummary* locations =
1035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001036 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037}
1038
1039void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1040 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001041 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042}
1043
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001044void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1045 LocationSummary* locations =
1046 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1047 locations->SetOut(Location::ConstantLocation(constant));
1048}
1049
1050void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1051 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001052 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001053}
1054
1055void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1056 LocationSummary* locations =
1057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1058 locations->SetOut(Location::ConstantLocation(constant));
1059}
1060
1061void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1062 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001063 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001064}
1065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001067 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001068}
1069
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001070void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001071 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001073 __ ret();
1074}
1075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001076void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001077 LocationSummary* locations =
1078 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 switch (ret->InputAt(0)->GetType()) {
1080 case Primitive::kPrimBoolean:
1081 case Primitive::kPrimByte:
1082 case Primitive::kPrimChar:
1083 case Primitive::kPrimShort:
1084 case Primitive::kPrimInt:
1085 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001086 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 break;
1088
1089 case Primitive::kPrimLong:
1090 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 break;
1093
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 case Primitive::kPrimFloat:
1095 case Primitive::kPrimDouble:
1096 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 break;
1099
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001103}
1104
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 if (kIsDebugBuild) {
1107 switch (ret->InputAt(0)->GetType()) {
1108 case Primitive::kPrimBoolean:
1109 case Primitive::kPrimByte:
1110 case Primitive::kPrimChar:
1111 case Primitive::kPrimShort:
1112 case Primitive::kPrimInt:
1113 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001114 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 break;
1116
1117 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1119 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 break;
1121
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 case Primitive::kPrimFloat:
1123 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 break;
1126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001128 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001129 }
1130 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001131 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132 __ ret();
1133}
1134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001135void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001136 HandleInvoke(invoke);
1137}
1138
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001139void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001140 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001141
1142 // TODO: Implement all kinds of calls:
1143 // 1) boot -> boot
1144 // 2) app -> boot
1145 // 3) app -> app
1146 //
1147 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1148
1149 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001150 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001151 if (!invoke->IsRecursive()) {
1152 // temp = temp->dex_cache_resolved_methods_;
1153 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1154 // temp = temp[index_in_cache]
1155 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1156 // (temp + offset_of_quick_compiled_code)()
1157 __ call(Address(
1158 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1159 } else {
1160 __ call(codegen_->GetFrameEntryLabel());
1161 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001162
1163 DCHECK(!codegen_->IsLeafMethod());
1164 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1165}
1166
1167void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1168 HandleInvoke(invoke);
1169}
1170
1171void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001172 LocationSummary* locations =
1173 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001175
1176 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 HInstruction* input = invoke->InputAt(i);
1179 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1180 }
1181
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 switch (invoke->GetType()) {
1183 case Primitive::kPrimBoolean:
1184 case Primitive::kPrimByte:
1185 case Primitive::kPrimChar:
1186 case Primitive::kPrimShort:
1187 case Primitive::kPrimInt:
1188 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001189 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 break;
1191
1192 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001193 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 break;
1195
1196 case Primitive::kPrimVoid:
1197 break;
1198
1199 case Primitive::kPrimDouble:
1200 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001201 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 break;
1203 }
1204
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001205 invoke->SetLocations(locations);
1206}
1207
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001208void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1211 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1212 LocationSummary* locations = invoke->GetLocations();
1213 Location receiver = locations->InAt(0);
1214 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1215 // temp = object->GetClass();
1216 if (receiver.IsStackSlot()) {
1217 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1218 __ movl(temp, Address(temp, class_offset));
1219 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001221 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001222 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001223 // temp = temp->GetMethodAt(method_offset);
1224 __ movl(temp, Address(temp, method_offset));
1225 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001226 __ call(Address(
1227 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001228
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001229 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001230 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001231}
1232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1234 HandleInvoke(invoke);
1235 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001236 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237}
1238
1239void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1240 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1243 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1244 LocationSummary* locations = invoke->GetLocations();
1245 Location receiver = locations->InAt(0);
1246 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1247
1248 // Set the hidden argument.
1249 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001250 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001251
1252 // temp = object->GetClass();
1253 if (receiver.IsStackSlot()) {
1254 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1255 __ movl(temp, Address(temp, class_offset));
1256 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001257 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001258 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001259 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001260 // temp = temp->GetImtEntryAt(method_offset);
1261 __ movl(temp, Address(temp, method_offset));
1262 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001263 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001264 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001265
1266 DCHECK(!codegen_->IsLeafMethod());
1267 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1268}
1269
Roland Levillain88cb1752014-10-20 16:36:47 +01001270void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1271 LocationSummary* locations =
1272 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1273 switch (neg->GetResultType()) {
1274 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001275 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001276 locations->SetInAt(0, Location::RequiresRegister());
1277 locations->SetOut(Location::SameAsFirstInput());
1278 break;
1279
Roland Levillain88cb1752014-10-20 16:36:47 +01001280 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001281 locations->SetInAt(0, Location::RequiresFpuRegister());
1282 locations->SetOut(Location::SameAsFirstInput());
1283 locations->AddTemp(Location::RequiresRegister());
1284 locations->AddTemp(Location::RequiresFpuRegister());
1285 break;
1286
Roland Levillain88cb1752014-10-20 16:36:47 +01001287 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001288 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001289 locations->SetOut(Location::SameAsFirstInput());
1290 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1295 }
1296}
1297
1298void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1299 LocationSummary* locations = neg->GetLocations();
1300 Location out = locations->Out();
1301 Location in = locations->InAt(0);
1302 switch (neg->GetResultType()) {
1303 case Primitive::kPrimInt:
1304 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001307 break;
1308
1309 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001310 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001311 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001312 __ negl(out.AsRegisterPairLow<Register>());
1313 // Negation is similar to subtraction from zero. The least
1314 // significant byte triggers a borrow when it is different from
1315 // zero; to take it into account, add 1 to the most significant
1316 // byte if the carry flag (CF) is set to 1 after the first NEGL
1317 // operation.
1318 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1319 __ negl(out.AsRegisterPairHigh<Register>());
1320 break;
1321
Roland Levillain5368c212014-11-27 15:03:41 +00001322 case Primitive::kPrimFloat: {
1323 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001324 Register constant = locations->GetTemp(0).AsRegister<Register>();
1325 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001326 // Implement float negation with an exclusive or with value
1327 // 0x80000000 (mask for bit 31, representing the sign of a
1328 // single-precision floating-point number).
1329 __ movl(constant, Immediate(INT32_C(0x80000000)));
1330 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001331 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001332 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001333 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001334
Roland Levillain5368c212014-11-27 15:03:41 +00001335 case Primitive::kPrimDouble: {
1336 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001337 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001338 // Implement double negation with an exclusive or with value
1339 // 0x8000000000000000 (mask for bit 63, representing the sign of
1340 // a double-precision floating-point number).
1341 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001342 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001343 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001344 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001345
1346 default:
1347 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1348 }
1349}
1350
Roland Levillaindff1f282014-11-05 14:15:05 +00001351void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001352 Primitive::Type result_type = conversion->GetResultType();
1353 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001354 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001355
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001356 // The float-to-long and double-to-long type conversions rely on a
1357 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001358 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001359 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1360 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001361 ? LocationSummary::kCall
1362 : LocationSummary::kNoCall;
1363 LocationSummary* locations =
1364 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1365
Roland Levillaindff1f282014-11-05 14:15:05 +00001366 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001367 case Primitive::kPrimByte:
1368 switch (input_type) {
1369 case Primitive::kPrimShort:
1370 case Primitive::kPrimInt:
1371 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001372 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001373 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1374 // Make the output overlap to please the register allocator. This greatly simplifies
1375 // the validation of the linear scan implementation
1376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001377 break;
1378
1379 default:
1380 LOG(FATAL) << "Unexpected type conversion from " << input_type
1381 << " to " << result_type;
1382 }
1383 break;
1384
Roland Levillain01a8d712014-11-14 16:27:39 +00001385 case Primitive::kPrimShort:
1386 switch (input_type) {
1387 case Primitive::kPrimByte:
1388 case Primitive::kPrimInt:
1389 case Primitive::kPrimChar:
1390 // Processing a Dex `int-to-short' instruction.
1391 locations->SetInAt(0, Location::Any());
1392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1393 break;
1394
1395 default:
1396 LOG(FATAL) << "Unexpected type conversion from " << input_type
1397 << " to " << result_type;
1398 }
1399 break;
1400
Roland Levillain946e1432014-11-11 17:35:19 +00001401 case Primitive::kPrimInt:
1402 switch (input_type) {
1403 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001404 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001405 locations->SetInAt(0, Location::Any());
1406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1407 break;
1408
1409 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001410 // Processing a Dex `float-to-int' instruction.
1411 locations->SetInAt(0, Location::RequiresFpuRegister());
1412 locations->SetOut(Location::RequiresRegister());
1413 locations->AddTemp(Location::RequiresFpuRegister());
1414 break;
1415
Roland Levillain946e1432014-11-11 17:35:19 +00001416 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001417 // Processing a Dex `double-to-int' instruction.
1418 locations->SetInAt(0, Location::RequiresFpuRegister());
1419 locations->SetOut(Location::RequiresRegister());
1420 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001421 break;
1422
1423 default:
1424 LOG(FATAL) << "Unexpected type conversion from " << input_type
1425 << " to " << result_type;
1426 }
1427 break;
1428
Roland Levillaindff1f282014-11-05 14:15:05 +00001429 case Primitive::kPrimLong:
1430 switch (input_type) {
1431 case Primitive::kPrimByte:
1432 case Primitive::kPrimShort:
1433 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001434 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001435 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001436 locations->SetInAt(0, Location::RegisterLocation(EAX));
1437 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1438 break;
1439
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001440 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001441 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001442 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001443 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001444 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1445 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1446
Vladimir Marko949c91f2015-01-27 10:48:44 +00001447 // The runtime helper puts the result in EAX, EDX.
1448 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001449 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001450 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001451
1452 default:
1453 LOG(FATAL) << "Unexpected type conversion from " << input_type
1454 << " to " << result_type;
1455 }
1456 break;
1457
Roland Levillain981e4542014-11-14 11:47:14 +00001458 case Primitive::kPrimChar:
1459 switch (input_type) {
1460 case Primitive::kPrimByte:
1461 case Primitive::kPrimShort:
1462 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001463 // Processing a Dex `int-to-char' instruction.
1464 locations->SetInAt(0, Location::Any());
1465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1466 break;
1467
1468 default:
1469 LOG(FATAL) << "Unexpected type conversion from " << input_type
1470 << " to " << result_type;
1471 }
1472 break;
1473
Roland Levillaindff1f282014-11-05 14:15:05 +00001474 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001475 switch (input_type) {
1476 case Primitive::kPrimByte:
1477 case Primitive::kPrimShort:
1478 case Primitive::kPrimInt:
1479 case Primitive::kPrimChar:
1480 // Processing a Dex `int-to-float' instruction.
1481 locations->SetInAt(0, Location::RequiresRegister());
1482 locations->SetOut(Location::RequiresFpuRegister());
1483 break;
1484
1485 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001486 // Processing a Dex `long-to-float' instruction.
1487 locations->SetInAt(0, Location::RequiresRegister());
1488 locations->SetOut(Location::RequiresFpuRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 locations->AddTemp(Location::RequiresFpuRegister());
1491 break;
1492
Roland Levillaincff13742014-11-17 14:32:17 +00001493 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001494 // Processing a Dex `double-to-float' instruction.
1495 locations->SetInAt(0, Location::RequiresFpuRegister());
1496 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +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::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001506 switch (input_type) {
1507 case Primitive::kPrimByte:
1508 case Primitive::kPrimShort:
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimChar:
1511 // Processing a Dex `int-to-double' instruction.
1512 locations->SetInAt(0, Location::RequiresRegister());
1513 locations->SetOut(Location::RequiresFpuRegister());
1514 break;
1515
1516 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001517 // Processing a Dex `long-to-double' instruction.
1518 locations->SetInAt(0, Location::RequiresRegister());
1519 locations->SetOut(Location::RequiresFpuRegister());
1520 locations->AddTemp(Location::RequiresFpuRegister());
1521 locations->AddTemp(Location::RequiresFpuRegister());
1522 break;
1523
Roland Levillaincff13742014-11-17 14:32:17 +00001524 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001525 // Processing a Dex `float-to-double' instruction.
1526 locations->SetInAt(0, Location::RequiresFpuRegister());
1527 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001528 break;
1529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540}
1541
1542void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1543 LocationSummary* locations = conversion->GetLocations();
1544 Location out = locations->Out();
1545 Location in = locations->InAt(0);
1546 Primitive::Type result_type = conversion->GetResultType();
1547 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001548 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001549 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001550 case Primitive::kPrimByte:
1551 switch (input_type) {
1552 case Primitive::kPrimShort:
1553 case Primitive::kPrimInt:
1554 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001555 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001556 if (in.IsRegister()) {
1557 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00001558 } else if (in.IsStackSlot()) {
1559 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001560 } else {
1561 DCHECK(in.GetConstant()->IsIntConstant());
1562 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1563 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1564 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001565 break;
1566
1567 default:
1568 LOG(FATAL) << "Unexpected type conversion from " << input_type
1569 << " to " << result_type;
1570 }
1571 break;
1572
Roland Levillain01a8d712014-11-14 16:27:39 +00001573 case Primitive::kPrimShort:
1574 switch (input_type) {
1575 case Primitive::kPrimByte:
1576 case Primitive::kPrimInt:
1577 case Primitive::kPrimChar:
1578 // Processing a Dex `int-to-short' instruction.
1579 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001580 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001581 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001582 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001583 } else {
1584 DCHECK(in.GetConstant()->IsIntConstant());
1585 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001586 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001587 }
1588 break;
1589
1590 default:
1591 LOG(FATAL) << "Unexpected type conversion from " << input_type
1592 << " to " << result_type;
1593 }
1594 break;
1595
Roland Levillain946e1432014-11-11 17:35:19 +00001596 case Primitive::kPrimInt:
1597 switch (input_type) {
1598 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001599 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001600 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001601 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001602 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001603 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001604 } else {
1605 DCHECK(in.IsConstant());
1606 DCHECK(in.GetConstant()->IsLongConstant());
1607 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001608 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001609 }
1610 break;
1611
Roland Levillain3f8f9362014-12-02 17:45:01 +00001612 case Primitive::kPrimFloat: {
1613 // Processing a Dex `float-to-int' instruction.
1614 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1615 Register output = out.AsRegister<Register>();
1616 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1617 Label done, nan;
1618
1619 __ movl(output, Immediate(kPrimIntMax));
1620 // temp = int-to-float(output)
1621 __ cvtsi2ss(temp, output);
1622 // if input >= temp goto done
1623 __ comiss(input, temp);
1624 __ j(kAboveEqual, &done);
1625 // if input == NaN goto nan
1626 __ j(kUnordered, &nan);
1627 // output = float-to-int-truncate(input)
1628 __ cvttss2si(output, input);
1629 __ jmp(&done);
1630 __ Bind(&nan);
1631 // output = 0
1632 __ xorl(output, output);
1633 __ Bind(&done);
1634 break;
1635 }
1636
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001637 case Primitive::kPrimDouble: {
1638 // Processing a Dex `double-to-int' instruction.
1639 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1640 Register output = out.AsRegister<Register>();
1641 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1642 Label done, nan;
1643
1644 __ movl(output, Immediate(kPrimIntMax));
1645 // temp = int-to-double(output)
1646 __ cvtsi2sd(temp, output);
1647 // if input >= temp goto done
1648 __ comisd(input, temp);
1649 __ j(kAboveEqual, &done);
1650 // if input == NaN goto nan
1651 __ j(kUnordered, &nan);
1652 // output = double-to-int-truncate(input)
1653 __ cvttsd2si(output, input);
1654 __ jmp(&done);
1655 __ Bind(&nan);
1656 // output = 0
1657 __ xorl(output, output);
1658 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001659 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001660 }
Roland Levillain946e1432014-11-11 17:35:19 +00001661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 }
1666 break;
1667
Roland Levillaindff1f282014-11-05 14:15:05 +00001668 case Primitive::kPrimLong:
1669 switch (input_type) {
1670 case Primitive::kPrimByte:
1671 case Primitive::kPrimShort:
1672 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001673 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001674 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001675 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1676 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001678 __ cdq();
1679 break;
1680
1681 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001682 // Processing a Dex `float-to-long' instruction.
1683 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001684 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1685 break;
1686
Roland Levillaindff1f282014-11-05 14:15:05 +00001687 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001688 // Processing a Dex `double-to-long' instruction.
1689 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1690 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001691 break;
1692
1693 default:
1694 LOG(FATAL) << "Unexpected type conversion from " << input_type
1695 << " to " << result_type;
1696 }
1697 break;
1698
Roland Levillain981e4542014-11-14 11:47:14 +00001699 case Primitive::kPrimChar:
1700 switch (input_type) {
1701 case Primitive::kPrimByte:
1702 case Primitive::kPrimShort:
1703 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001704 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1705 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001706 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001707 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001709 } else {
1710 DCHECK(in.GetConstant()->IsIntConstant());
1711 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001712 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001713 }
1714 break;
1715
1716 default:
1717 LOG(FATAL) << "Unexpected type conversion from " << input_type
1718 << " to " << result_type;
1719 }
1720 break;
1721
Roland Levillaindff1f282014-11-05 14:15:05 +00001722 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001723 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001724 case Primitive::kPrimByte:
1725 case Primitive::kPrimShort:
1726 case Primitive::kPrimInt:
1727 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001728 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001729 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001730 break;
1731
Roland Levillain6d0e4832014-11-27 18:31:21 +00001732 case Primitive::kPrimLong: {
1733 // Processing a Dex `long-to-float' instruction.
1734 Register low = in.AsRegisterPairLow<Register>();
1735 Register high = in.AsRegisterPairHigh<Register>();
1736 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1737 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1738 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1739
1740 // Operations use doubles for precision reasons (each 32-bit
1741 // half of a long fits in the 53-bit mantissa of a double,
1742 // but not in the 24-bit mantissa of a float). This is
1743 // especially important for the low bits. The result is
1744 // eventually converted to float.
1745
1746 // low = low - 2^31 (to prevent bit 31 of `low` to be
1747 // interpreted as a sign bit)
1748 __ subl(low, Immediate(0x80000000));
1749 // temp = int-to-double(high)
1750 __ cvtsi2sd(temp, high);
1751 // temp = temp * 2^32
1752 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1753 __ mulsd(temp, constant);
1754 // result = int-to-double(low)
1755 __ cvtsi2sd(result, low);
1756 // result = result + 2^31 (restore the original value of `low`)
1757 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1758 __ addsd(result, constant);
1759 // result = result + temp
1760 __ addsd(result, temp);
1761 // result = double-to-float(result)
1762 __ cvtsd2ss(result, result);
1763 break;
1764 }
1765
Roland Levillaincff13742014-11-17 14:32:17 +00001766 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001767 // Processing a Dex `double-to-float' instruction.
1768 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 };
1775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001778 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001779 case Primitive::kPrimByte:
1780 case Primitive::kPrimShort:
1781 case Primitive::kPrimInt:
1782 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001783 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001784 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001785 break;
1786
Roland Levillain647b9ed2014-11-27 12:06:00 +00001787 case Primitive::kPrimLong: {
1788 // Processing a Dex `long-to-double' instruction.
1789 Register low = in.AsRegisterPairLow<Register>();
1790 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1792 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1793 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001794
Roland Levillain647b9ed2014-11-27 12:06:00 +00001795 // low = low - 2^31 (to prevent bit 31 of `low` to be
1796 // interpreted as a sign bit)
1797 __ subl(low, Immediate(0x80000000));
1798 // temp = int-to-double(high)
1799 __ cvtsi2sd(temp, high);
1800 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001801 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001802 __ mulsd(temp, constant);
1803 // result = int-to-double(low)
1804 __ cvtsi2sd(result, low);
1805 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001807 __ addsd(result, constant);
1808 // result = result + temp
1809 __ addsd(result, temp);
1810 break;
1811 }
1812
Roland Levillaincff13742014-11-17 14:32:17 +00001813 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001814 // Processing a Dex `float-to-double' instruction.
1815 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001816 break;
1817
1818 default:
1819 LOG(FATAL) << "Unexpected type conversion from " << input_type
1820 << " to " << result_type;
1821 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001822 break;
1823
1824 default:
1825 LOG(FATAL) << "Unexpected type conversion from " << input_type
1826 << " to " << result_type;
1827 }
1828}
1829
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001830void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001833 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001834 case Primitive::kPrimInt: {
1835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1838 break;
1839 }
1840
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetInAt(1, Location::Any());
1844 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 break;
1846 }
1847
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 case Primitive::kPrimFloat:
1849 case Primitive::kPrimDouble: {
1850 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001851 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001853 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1858 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001859 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001860}
1861
1862void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1863 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 Location first = locations->InAt(0);
1865 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001866 Location out = locations->Out();
1867
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001868 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001871 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1872 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1873 } else {
1874 __ leal(out.AsRegister<Register>(), Address(
1875 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1876 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001877 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001878 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1879 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1880 __ addl(out.AsRegister<Register>(), Immediate(value));
1881 } else {
1882 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1883 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001884 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001885 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001886 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001887 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001888 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 }
1890
1891 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001892 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1894 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00001895 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001896 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1897 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001898 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001899 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 break;
1901 }
1902
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903 case Primitive::kPrimFloat: {
1904 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001905 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001906 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001908 }
1909
1910 case Primitive::kPrimDouble: {
1911 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 }
1914 break;
1915 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001918 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001919 }
1920}
1921
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001923 LocationSummary* locations =
1924 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001925 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001926 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetInAt(1, Location::Any());
1930 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932 }
Calin Juravle11351682014-10-23 15:38:15 +01001933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble: {
1935 locations->SetInAt(0, Location::RequiresFpuRegister());
1936 locations->SetInAt(1, Location::RequiresFpuRegister());
1937 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 break;
Calin Juravle11351682014-10-23 15:38:15 +01001939 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001941 default:
Calin Juravle11351682014-10-23 15:38:15 +01001942 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001943 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001944}
1945
1946void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1947 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 Location first = locations->InAt(0);
1949 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001950 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001951 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001953 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001956 __ subl(first.AsRegister<Register>(),
1957 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001958 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001959 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001960 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001961 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 }
1963
1964 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001965 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001966 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1967 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00001968 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001969 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001970 __ sbbl(first.AsRegisterPairHigh<Register>(),
1971 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001972 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001973 break;
1974 }
1975
Calin Juravle11351682014-10-23 15:38:15 +01001976 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001978 break;
Calin Juravle11351682014-10-23 15:38:15 +01001979 }
1980
1981 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001983 break;
1984 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001985
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001986 default:
Calin Juravle11351682014-10-23 15:38:15 +01001987 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001988 }
1989}
1990
Calin Juravle34bacdf2014-10-07 20:23:36 +01001991void LocationsBuilderX86::VisitMul(HMul* mul) {
1992 LocationSummary* locations =
1993 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1994 switch (mul->GetResultType()) {
1995 case Primitive::kPrimInt:
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 locations->SetInAt(1, Location::Any());
1998 locations->SetOut(Location::SameAsFirstInput());
1999 break;
2000 case Primitive::kPrimLong: {
2001 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002002 // TODO: Currently this handles only stack operands:
2003 // - we don't have enough registers because we currently use Quick ABI.
2004 // - by the time we have a working register allocator we will probably change the ABI
2005 // and fix the above.
2006 // - we don't have a way yet to request operands on stack but the base line compiler
2007 // will leave the operands on the stack with Any().
Calin Juravle34bacdf2014-10-07 20:23:36 +01002008 locations->SetInAt(1, Location::Any());
2009 locations->SetOut(Location::SameAsFirstInput());
2010 // Needed for imul on 32bits with 64bits output.
2011 locations->AddTemp(Location::RegisterLocation(EAX));
2012 locations->AddTemp(Location::RegisterLocation(EDX));
2013 break;
2014 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble: {
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetInAt(1, Location::RequiresFpuRegister());
2019 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002020 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002021 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002022
2023 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002024 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002025 }
2026}
2027
2028void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2029 LocationSummary* locations = mul->GetLocations();
2030 Location first = locations->InAt(0);
2031 Location second = locations->InAt(1);
2032 DCHECK(first.Equals(locations->Out()));
2033
2034 switch (mul->GetResultType()) {
2035 case Primitive::kPrimInt: {
2036 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002038 } else if (second.IsConstant()) {
2039 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002041 } else {
2042 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002043 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimLong: {
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002049 DCHECK(second.IsDoubleStackSlot());
2050
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 Register in1_hi = first.AsRegisterPairHigh<Register>();
2052 Register in1_lo = first.AsRegisterPairLow<Register>();
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002053 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2054 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 Register eax = locations->GetTemp(0).AsRegister<Register>();
2056 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002057
2058 DCHECK_EQ(EAX, eax);
2059 DCHECK_EQ(EDX, edx);
2060
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002061 // input: in1 - 64 bits, in2 - 64 bits
Calin Juravle34bacdf2014-10-07 20:23:36 +01002062 // output: in1
2063 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2064 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2065 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2066
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002067 __ movl(eax, in2_hi);
2068 // eax <- in1.lo * in2.hi
2069 __ imull(eax, in1_lo);
2070 // in1.hi <- in1.hi * in2.lo
2071 __ imull(in1_hi, in2_lo);
2072 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2073 __ addl(in1_hi, eax);
2074 // move in1_lo to eax to prepare for double precision
2075 __ movl(eax, in1_lo);
2076 // edx:eax <- in1.lo * in2.lo
2077 __ mull(in2_lo);
2078 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2079 __ addl(in1_hi, edx);
2080 // in1.lo <- (in1.lo * in2.lo)[31:0];
2081 __ movl(in1_lo, eax);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002082
2083 break;
2084 }
2085
Calin Juravleb5bfa962014-10-21 18:02:24 +01002086 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002088 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002089 }
2090
2091 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002093 break;
2094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002095
2096 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002098 }
2099}
2100
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002101void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2102 uint32_t stack_adjustment, bool is_float) {
2103 if (source.IsStackSlot()) {
2104 DCHECK(is_float);
2105 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2106 } else if (source.IsDoubleStackSlot()) {
2107 DCHECK(!is_float);
2108 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2109 } else {
2110 // Write the value to the temporary location on the stack and load to FP stack.
2111 if (is_float) {
2112 Location stack_temp = Location::StackSlot(temp_offset);
2113 codegen_->Move32(stack_temp, source);
2114 __ flds(Address(ESP, temp_offset));
2115 } else {
2116 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2117 codegen_->Move64(stack_temp, source);
2118 __ fldl(Address(ESP, temp_offset));
2119 }
2120 }
2121}
2122
2123void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2124 Primitive::Type type = rem->GetResultType();
2125 bool is_float = type == Primitive::kPrimFloat;
2126 size_t elem_size = Primitive::ComponentSize(type);
2127 LocationSummary* locations = rem->GetLocations();
2128 Location first = locations->InAt(0);
2129 Location second = locations->InAt(1);
2130 Location out = locations->Out();
2131
2132 // Create stack space for 2 elements.
2133 // TODO: enhance register allocator to ask for stack temporaries.
2134 __ subl(ESP, Immediate(2 * elem_size));
2135
2136 // Load the values to the FP stack in reverse order, using temporaries if needed.
2137 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2138 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2139
2140 // Loop doing FPREM until we stabilize.
2141 Label retry;
2142 __ Bind(&retry);
2143 __ fprem();
2144
2145 // Move FP status to AX.
2146 __ fstsw();
2147
2148 // And see if the argument reduction is complete. This is signaled by the
2149 // C2 FPU flag bit set to 0.
2150 __ andl(EAX, Immediate(kC2ConditionMask));
2151 __ j(kNotEqual, &retry);
2152
2153 // We have settled on the final value. Retrieve it into an XMM register.
2154 // Store FP top of stack to real stack.
2155 if (is_float) {
2156 __ fsts(Address(ESP, 0));
2157 } else {
2158 __ fstl(Address(ESP, 0));
2159 }
2160
2161 // Pop the 2 items from the FP stack.
2162 __ fucompp();
2163
2164 // Load the value from the stack into an XMM register.
2165 DCHECK(out.IsFpuRegister()) << out;
2166 if (is_float) {
2167 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2168 } else {
2169 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2170 }
2171
2172 // And remove the temporary stack space we allocated.
2173 __ addl(ESP, Immediate(2 * elem_size));
2174}
2175
Calin Juravlebacfec32014-11-14 15:54:36 +00002176void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2177 DCHECK(instruction->IsDiv() || instruction->IsRem());
2178
2179 LocationSummary* locations = instruction->GetLocations();
2180 Location out = locations->Out();
2181 Location first = locations->InAt(0);
2182 Location second = locations->InAt(1);
2183 bool is_div = instruction->IsDiv();
2184
2185 switch (instruction->GetResultType()) {
2186 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002187 Register second_reg = second.AsRegister<Register>();
2188 DCHECK_EQ(EAX, first.AsRegister<Register>());
2189 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002190
2191 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002192 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2193 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002194 codegen_->AddSlowPath(slow_path);
2195
2196 // 0x80000000/-1 triggers an arithmetic exception!
2197 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2198 // it's safe to just use negl instead of more complex comparisons.
2199
2200 __ cmpl(second_reg, Immediate(-1));
2201 __ j(kEqual, slow_path->GetEntryLabel());
2202
2203 // edx:eax <- sign-extended of eax
2204 __ cdq();
2205 // eax = quotient, edx = remainder
2206 __ idivl(second_reg);
2207
2208 __ Bind(slow_path->GetExitLabel());
2209 break;
2210 }
2211
2212 case Primitive::kPrimLong: {
2213 InvokeRuntimeCallingConvention calling_convention;
2214 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2215 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2216 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2217 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2218 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2219 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2220
2221 if (is_div) {
2222 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2223 } else {
2224 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2225 }
2226 uint32_t dex_pc = is_div
2227 ? instruction->AsDiv()->GetDexPc()
2228 : instruction->AsRem()->GetDexPc();
2229 codegen_->RecordPcInfo(instruction, dex_pc);
2230
2231 break;
2232 }
2233
2234 default:
2235 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2236 }
2237}
2238
Calin Juravle7c4954d2014-10-28 16:57:40 +00002239void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002240 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2241 ? LocationSummary::kCall
2242 : LocationSummary::kNoCall;
2243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2244
Calin Juravle7c4954d2014-10-28 16:57:40 +00002245 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002246 case Primitive::kPrimInt: {
2247 locations->SetInAt(0, Location::RegisterLocation(EAX));
2248 locations->SetInAt(1, Location::RequiresRegister());
2249 locations->SetOut(Location::SameAsFirstInput());
2250 // Intel uses edx:eax as the dividend.
2251 locations->AddTemp(Location::RegisterLocation(EDX));
2252 break;
2253 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002254 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002255 InvokeRuntimeCallingConvention calling_convention;
2256 locations->SetInAt(0, Location::RegisterPairLocation(
2257 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2258 locations->SetInAt(1, Location::RegisterPairLocation(
2259 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2260 // Runtime helper puts the result in EAX, EDX.
2261 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002262 break;
2263 }
2264 case Primitive::kPrimFloat:
2265 case Primitive::kPrimDouble: {
2266 locations->SetInAt(0, Location::RequiresFpuRegister());
2267 locations->SetInAt(1, Location::RequiresFpuRegister());
2268 locations->SetOut(Location::SameAsFirstInput());
2269 break;
2270 }
2271
2272 default:
2273 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2274 }
2275}
2276
2277void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2278 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002279 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002280 Location first = locations->InAt(0);
2281 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002282
2283 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002284 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002285 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002286 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002287 break;
2288 }
2289
2290 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002291 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002293 break;
2294 }
2295
2296 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002297 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002299 break;
2300 }
2301
2302 default:
2303 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2304 }
2305}
2306
Calin Juravlebacfec32014-11-14 15:54:36 +00002307void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002308 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002309 LocationSummary* locations =
2310 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002311
Calin Juravled2ec87d2014-12-08 14:24:46 +00002312 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002313 case Primitive::kPrimInt: {
2314 locations->SetInAt(0, Location::RegisterLocation(EAX));
2315 locations->SetInAt(1, Location::RequiresRegister());
2316 locations->SetOut(Location::RegisterLocation(EDX));
2317 break;
2318 }
2319 case Primitive::kPrimLong: {
2320 InvokeRuntimeCallingConvention calling_convention;
2321 locations->SetInAt(0, Location::RegisterPairLocation(
2322 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2323 locations->SetInAt(1, Location::RegisterPairLocation(
2324 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2325 // Runtime helper puts the result in EAX, EDX.
2326 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2327 break;
2328 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002329 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002330 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002331 locations->SetInAt(0, Location::Any());
2332 locations->SetInAt(1, Location::Any());
2333 locations->SetOut(Location::RequiresFpuRegister());
2334 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002335 break;
2336 }
2337
2338 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002339 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002340 }
2341}
2342
2343void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2344 Primitive::Type type = rem->GetResultType();
2345 switch (type) {
2346 case Primitive::kPrimInt:
2347 case Primitive::kPrimLong: {
2348 GenerateDivRemIntegral(rem);
2349 break;
2350 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002351 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002352 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002353 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002354 break;
2355 }
2356 default:
2357 LOG(FATAL) << "Unexpected rem type " << type;
2358 }
2359}
2360
Calin Juravled0d48522014-11-04 16:40:20 +00002361void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2362 LocationSummary* locations =
2363 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002364 switch (instruction->GetType()) {
2365 case Primitive::kPrimInt: {
2366 locations->SetInAt(0, Location::Any());
2367 break;
2368 }
2369 case Primitive::kPrimLong: {
2370 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2371 if (!instruction->IsConstant()) {
2372 locations->AddTemp(Location::RequiresRegister());
2373 }
2374 break;
2375 }
2376 default:
2377 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2378 }
Calin Juravled0d48522014-11-04 16:40:20 +00002379 if (instruction->HasUses()) {
2380 locations->SetOut(Location::SameAsFirstInput());
2381 }
2382}
2383
2384void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2385 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2386 codegen_->AddSlowPath(slow_path);
2387
2388 LocationSummary* locations = instruction->GetLocations();
2389 Location value = locations->InAt(0);
2390
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002391 switch (instruction->GetType()) {
2392 case Primitive::kPrimInt: {
2393 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002394 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002395 __ j(kEqual, slow_path->GetEntryLabel());
2396 } else if (value.IsStackSlot()) {
2397 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2398 __ j(kEqual, slow_path->GetEntryLabel());
2399 } else {
2400 DCHECK(value.IsConstant()) << value;
2401 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2402 __ jmp(slow_path->GetEntryLabel());
2403 }
2404 }
2405 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002406 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002407 case Primitive::kPrimLong: {
2408 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002409 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002410 __ movl(temp, value.AsRegisterPairLow<Register>());
2411 __ orl(temp, value.AsRegisterPairHigh<Register>());
2412 __ j(kEqual, slow_path->GetEntryLabel());
2413 } else {
2414 DCHECK(value.IsConstant()) << value;
2415 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2416 __ jmp(slow_path->GetEntryLabel());
2417 }
2418 }
2419 break;
2420 }
2421 default:
2422 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002423 }
Calin Juravled0d48522014-11-04 16:40:20 +00002424}
2425
Calin Juravle9aec02f2014-11-18 23:06:35 +00002426void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2427 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2428
2429 LocationSummary* locations =
2430 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2431
2432 switch (op->GetResultType()) {
2433 case Primitive::kPrimInt: {
2434 locations->SetInAt(0, Location::RequiresRegister());
2435 // The shift count needs to be in CL.
2436 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2437 locations->SetOut(Location::SameAsFirstInput());
2438 break;
2439 }
2440 case Primitive::kPrimLong: {
2441 locations->SetInAt(0, Location::RequiresRegister());
2442 // The shift count needs to be in CL.
2443 locations->SetInAt(1, Location::RegisterLocation(ECX));
2444 locations->SetOut(Location::SameAsFirstInput());
2445 break;
2446 }
2447 default:
2448 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2449 }
2450}
2451
2452void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2453 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2454
2455 LocationSummary* locations = op->GetLocations();
2456 Location first = locations->InAt(0);
2457 Location second = locations->InAt(1);
2458 DCHECK(first.Equals(locations->Out()));
2459
2460 switch (op->GetResultType()) {
2461 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002463 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002465 DCHECK_EQ(ECX, second_reg);
2466 if (op->IsShl()) {
2467 __ shll(first_reg, second_reg);
2468 } else if (op->IsShr()) {
2469 __ sarl(first_reg, second_reg);
2470 } else {
2471 __ shrl(first_reg, second_reg);
2472 }
2473 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002474 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002475 if (op->IsShl()) {
2476 __ shll(first_reg, imm);
2477 } else if (op->IsShr()) {
2478 __ sarl(first_reg, imm);
2479 } else {
2480 __ shrl(first_reg, imm);
2481 }
2482 }
2483 break;
2484 }
2485 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002487 DCHECK_EQ(ECX, second_reg);
2488 if (op->IsShl()) {
2489 GenerateShlLong(first, second_reg);
2490 } else if (op->IsShr()) {
2491 GenerateShrLong(first, second_reg);
2492 } else {
2493 GenerateUShrLong(first, second_reg);
2494 }
2495 break;
2496 }
2497 default:
2498 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2499 }
2500}
2501
2502void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2503 Label done;
2504 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2505 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2506 __ testl(shifter, Immediate(32));
2507 __ j(kEqual, &done);
2508 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2509 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2510 __ Bind(&done);
2511}
2512
2513void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2514 Label done;
2515 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2516 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2517 __ testl(shifter, Immediate(32));
2518 __ j(kEqual, &done);
2519 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2520 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2521 __ Bind(&done);
2522}
2523
2524void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2525 Label done;
2526 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2527 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2528 __ testl(shifter, Immediate(32));
2529 __ j(kEqual, &done);
2530 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2531 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2532 __ Bind(&done);
2533}
2534
2535void LocationsBuilderX86::VisitShl(HShl* shl) {
2536 HandleShift(shl);
2537}
2538
2539void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2540 HandleShift(shl);
2541}
2542
2543void LocationsBuilderX86::VisitShr(HShr* shr) {
2544 HandleShift(shr);
2545}
2546
2547void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2548 HandleShift(shr);
2549}
2550
2551void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2552 HandleShift(ushr);
2553}
2554
2555void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2556 HandleShift(ushr);
2557}
2558
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002559void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002560 LocationSummary* locations =
2561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002562 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002563 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002564 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2565 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002566}
2567
2568void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2569 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002570 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002571 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002572
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002573 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002574
Nicolas Geoffray39468442014-09-02 15:17:15 +01002575 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002576 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002577}
2578
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002579void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2582 locations->SetOut(Location::RegisterLocation(EAX));
2583 InvokeRuntimeCallingConvention calling_convention;
2584 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002585 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2586 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002587}
2588
2589void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2590 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002591 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002592 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2593
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002594 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002595
2596 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2597 DCHECK(!codegen_->IsLeafMethod());
2598}
2599
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002600void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002603 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2604 if (location.IsStackSlot()) {
2605 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2606 } else if (location.IsDoubleStackSlot()) {
2607 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002608 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002609 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002610}
2611
2612void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002613 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002614}
2615
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002616void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002617 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002618 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002619 locations->SetInAt(0, Location::RequiresRegister());
2620 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002621}
2622
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002623void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2624 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002625 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002626 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002627 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002628 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002629 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002630 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002631 break;
2632
2633 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002634 __ notl(out.AsRegisterPairLow<Register>());
2635 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002636 break;
2637
2638 default:
2639 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2640 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002641}
2642
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002643void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002644 LocationSummary* locations =
2645 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002646 switch (compare->InputAt(0)->GetType()) {
2647 case Primitive::kPrimLong: {
2648 locations->SetInAt(0, Location::RequiresRegister());
2649 // TODO: we set any here but we don't handle constants
2650 locations->SetInAt(1, Location::Any());
2651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2652 break;
2653 }
2654 case Primitive::kPrimFloat:
2655 case Primitive::kPrimDouble: {
2656 locations->SetInAt(0, Location::RequiresFpuRegister());
2657 locations->SetInAt(1, Location::RequiresFpuRegister());
2658 locations->SetOut(Location::RequiresRegister());
2659 break;
2660 }
2661 default:
2662 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2663 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002664}
2665
2666void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002667 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002669 Location left = locations->InAt(0);
2670 Location right = locations->InAt(1);
2671
2672 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002673 switch (compare->InputAt(0)->GetType()) {
2674 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002675 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002676 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002677 } else {
2678 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002679 __ cmpl(left.AsRegisterPairHigh<Register>(),
2680 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002682 __ j(kLess, &less); // Signed compare.
2683 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002684 if (right.IsRegisterPair()) {
2685 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffrayb4ba3542015-03-05 11:28:58 +00002686 } else {
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002687 DCHECK(right.IsDoubleStackSlot());
2688 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002689 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002690 break;
2691 }
2692 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002694 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2695 break;
2696 }
2697 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002699 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002700 break;
2701 }
2702 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002703 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002704 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002705 __ movl(out, Immediate(0));
2706 __ j(kEqual, &done);
2707 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2708
2709 __ Bind(&greater);
2710 __ movl(out, Immediate(1));
2711 __ jmp(&done);
2712
2713 __ Bind(&less);
2714 __ movl(out, Immediate(-1));
2715
2716 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002717}
2718
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002719void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002720 LocationSummary* locations =
2721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002722 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2723 locations->SetInAt(i, Location::Any());
2724 }
2725 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002726}
2727
2728void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002729 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002730 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002731}
2732
Calin Juravle52c48962014-12-16 17:02:57 +00002733void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2734 /*
2735 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2736 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2737 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2738 */
2739 switch (kind) {
2740 case MemBarrierKind::kAnyAny: {
2741 __ mfence();
2742 break;
2743 }
2744 case MemBarrierKind::kAnyStore:
2745 case MemBarrierKind::kLoadAny:
2746 case MemBarrierKind::kStoreStore: {
2747 // nop
2748 break;
2749 }
2750 default:
2751 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002752 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002753}
2754
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002755
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002756void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002757 Label is_null;
2758 __ testl(value, value);
2759 __ j(kEqual, &is_null);
2760 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2761 __ movl(temp, object);
2762 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002763 __ movb(Address(temp, card, TIMES_1, 0),
2764 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002765 __ Bind(&is_null);
2766}
2767
Calin Juravle52c48962014-12-16 17:02:57 +00002768void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2769 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002770 LocationSummary* locations =
2771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002772 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002774
2775 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2776 // Long values can be loaded atomically into an XMM using movsd.
2777 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2778 // and then copy the XMM into the output 32bits at a time).
2779 locations->AddTemp(Location::RequiresFpuRegister());
2780 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781}
2782
Calin Juravle52c48962014-12-16 17:02:57 +00002783void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2784 const FieldInfo& field_info) {
2785 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002786
Calin Juravle52c48962014-12-16 17:02:57 +00002787 LocationSummary* locations = instruction->GetLocations();
2788 Register base = locations->InAt(0).AsRegister<Register>();
2789 Location out = locations->Out();
2790 bool is_volatile = field_info.IsVolatile();
2791 Primitive::Type field_type = field_info.GetFieldType();
2792 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2793
2794 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002795 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002796 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797 break;
2798 }
2799
2800 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002801 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002802 break;
2803 }
2804
2805 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002806 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002807 break;
2808 }
2809
2810 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002811 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812 break;
2813 }
2814
2815 case Primitive::kPrimInt:
2816 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002817 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002818 break;
2819 }
2820
2821 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002822 if (is_volatile) {
2823 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2824 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002826 __ movd(out.AsRegisterPairLow<Register>(), temp);
2827 __ psrlq(temp, Immediate(32));
2828 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2829 } else {
2830 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002832 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002834 break;
2835 }
2836
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002837 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002838 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002839 break;
2840 }
2841
2842 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002843 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002844 break;
2845 }
2846
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002847 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002848 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002849 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002850 }
Calin Juravle52c48962014-12-16 17:02:57 +00002851
Calin Juravle77520bc2015-01-12 18:45:46 +00002852 // Longs are handled in the switch.
2853 if (field_type != Primitive::kPrimLong) {
2854 codegen_->MaybeRecordImplicitNullCheck(instruction);
2855 }
2856
Calin Juravle52c48962014-12-16 17:02:57 +00002857 if (is_volatile) {
2858 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2859 }
2860}
2861
2862void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2863 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2864
2865 LocationSummary* locations =
2866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2867 locations->SetInAt(0, Location::RequiresRegister());
2868 bool is_volatile = field_info.IsVolatile();
2869 Primitive::Type field_type = field_info.GetFieldType();
2870 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2871 || (field_type == Primitive::kPrimByte);
2872
2873 // The register allocator does not support multiple
2874 // inputs that die at entry with one in a specific register.
2875 if (is_byte_type) {
2876 // Ensure the value is in a byte register.
2877 locations->SetInAt(1, Location::RegisterLocation(EAX));
2878 } else {
2879 locations->SetInAt(1, Location::RequiresRegister());
2880 }
2881 // Temporary registers for the write barrier.
2882 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2883 locations->AddTemp(Location::RequiresRegister());
2884 // Ensure the card is in a byte register.
2885 locations->AddTemp(Location::RegisterLocation(ECX));
2886 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2887 // 64bits value can be atomically written to an address with movsd and an XMM register.
2888 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2889 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2890 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2891 // isolated cases when we need this it isn't worth adding the extra complexity.
2892 locations->AddTemp(Location::RequiresFpuRegister());
2893 locations->AddTemp(Location::RequiresFpuRegister());
2894 }
2895}
2896
2897void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2898 const FieldInfo& field_info) {
2899 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2900
2901 LocationSummary* locations = instruction->GetLocations();
2902 Register base = locations->InAt(0).AsRegister<Register>();
2903 Location value = locations->InAt(1);
2904 bool is_volatile = field_info.IsVolatile();
2905 Primitive::Type field_type = field_info.GetFieldType();
2906 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2907
2908 if (is_volatile) {
2909 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2910 }
2911
2912 switch (field_type) {
2913 case Primitive::kPrimBoolean:
2914 case Primitive::kPrimByte: {
2915 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2916 break;
2917 }
2918
2919 case Primitive::kPrimShort:
2920 case Primitive::kPrimChar: {
2921 __ movw(Address(base, offset), value.AsRegister<Register>());
2922 break;
2923 }
2924
2925 case Primitive::kPrimInt:
2926 case Primitive::kPrimNot: {
2927 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002928 break;
2929 }
2930
2931 case Primitive::kPrimLong: {
2932 if (is_volatile) {
2933 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2934 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2935 __ movd(temp1, value.AsRegisterPairLow<Register>());
2936 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2937 __ punpckldq(temp1, temp2);
2938 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002939 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002940 } else {
2941 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002942 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002943 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2944 }
2945 break;
2946 }
2947
2948 case Primitive::kPrimFloat: {
2949 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2950 break;
2951 }
2952
2953 case Primitive::kPrimDouble: {
2954 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2955 break;
2956 }
2957
2958 case Primitive::kPrimVoid:
2959 LOG(FATAL) << "Unreachable type " << field_type;
2960 UNREACHABLE();
2961 }
2962
Calin Juravle77520bc2015-01-12 18:45:46 +00002963 // Longs are handled in the switch.
2964 if (field_type != Primitive::kPrimLong) {
2965 codegen_->MaybeRecordImplicitNullCheck(instruction);
2966 }
2967
2968 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2969 Register temp = locations->GetTemp(0).AsRegister<Register>();
2970 Register card = locations->GetTemp(1).AsRegister<Register>();
2971 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2972 }
2973
Calin Juravle52c48962014-12-16 17:02:57 +00002974 if (is_volatile) {
2975 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2976 }
2977}
2978
2979void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2980 HandleFieldGet(instruction, instruction->GetFieldInfo());
2981}
2982
2983void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2984 HandleFieldGet(instruction, instruction->GetFieldInfo());
2985}
2986
2987void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2988 HandleFieldSet(instruction, instruction->GetFieldInfo());
2989}
2990
2991void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2992 HandleFieldSet(instruction, instruction->GetFieldInfo());
2993}
2994
2995void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2996 HandleFieldSet(instruction, instruction->GetFieldInfo());
2997}
2998
2999void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3000 HandleFieldSet(instruction, instruction->GetFieldInfo());
3001}
3002
3003void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3004 HandleFieldGet(instruction, instruction->GetFieldInfo());
3005}
3006
3007void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3008 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009}
3010
3011void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003014 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3015 ? Location::RequiresRegister()
3016 : Location::Any();
3017 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003018 if (instruction->HasUses()) {
3019 locations->SetOut(Location::SameAsFirstInput());
3020 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003021}
3022
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003023void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003024 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3025 return;
3026 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003027 LocationSummary* locations = instruction->GetLocations();
3028 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003029
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003030 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3031 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3032}
3033
3034void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003035 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003036 codegen_->AddSlowPath(slow_path);
3037
3038 LocationSummary* locations = instruction->GetLocations();
3039 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003040
3041 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003043 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003044 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003045 } else {
3046 DCHECK(obj.IsConstant()) << obj;
3047 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3048 __ jmp(slow_path->GetEntryLabel());
3049 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003050 }
3051 __ j(kEqual, slow_path->GetEntryLabel());
3052}
3053
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003054void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3055 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3056 GenerateImplicitNullCheck(instruction);
3057 } else {
3058 GenerateExplicitNullCheck(instruction);
3059 }
3060}
3061
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003062void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
3064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003065 locations->SetInAt(0, Location::RequiresRegister());
3066 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray154552e2015-03-06 16:10:14 +00003067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003068}
3069
3070void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3071 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 Location index = locations->InAt(1);
3074
Calin Juravle77520bc2015-01-12 18:45:46 +00003075 Primitive::Type type = instruction->GetType();
3076 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003077 case Primitive::kPrimBoolean: {
3078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003079 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003080 if (index.IsConstant()) {
3081 __ movzxb(out, Address(obj,
3082 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3083 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003084 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003085 }
3086 break;
3087 }
3088
3089 case Primitive::kPrimByte: {
3090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 if (index.IsConstant()) {
3093 __ movsxb(out, Address(obj,
3094 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3095 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003096 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 }
3098 break;
3099 }
3100
3101 case Primitive::kPrimShort: {
3102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003103 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 if (index.IsConstant()) {
3105 __ movsxw(out, Address(obj,
3106 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109 }
3110 break;
3111 }
3112
3113 case Primitive::kPrimChar: {
3114 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003115 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003116 if (index.IsConstant()) {
3117 __ movzxw(out, Address(obj,
3118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003120 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003121 }
3122 break;
3123 }
3124
3125 case Primitive::kPrimInt:
3126 case Primitive::kPrimNot: {
3127 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 if (index.IsConstant()) {
3130 __ movl(out, Address(obj,
3131 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3132 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 }
3135 break;
3136 }
3137
3138 case Primitive::kPrimLong: {
3139 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003140 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003141 if (index.IsConstant()) {
3142 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003143 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003144 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003145 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003147 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003149 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003150 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003151 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003152 }
3153 break;
3154 }
3155
Mark Mendell7c8d0092015-01-26 11:21:33 -05003156 case Primitive::kPrimFloat: {
3157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3158 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3159 if (index.IsConstant()) {
3160 __ movss(out, Address(obj,
3161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3162 } else {
3163 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3164 }
3165 break;
3166 }
3167
3168 case Primitive::kPrimDouble: {
3169 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3170 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3171 if (index.IsConstant()) {
3172 __ movsd(out, Address(obj,
3173 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3174 } else {
3175 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3176 }
3177 break;
3178 }
3179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003181 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003182 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003184
3185 if (type != Primitive::kPrimLong) {
3186 codegen_->MaybeRecordImplicitNullCheck(instruction);
3187 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003188}
3189
3190void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003191 // This location builder might end up asking to up to four registers, which is
3192 // not currently possible for baseline. The situation in which we need four
3193 // registers cannot be met by baseline though, because it has not run any
3194 // optimization.
3195
Nicolas Geoffray39468442014-09-02 15:17:15 +01003196 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003197 bool needs_write_barrier =
3198 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3199
Mark Mendell5f874182015-03-04 15:42:45 -05003200 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003201
Nicolas Geoffray39468442014-09-02 15:17:15 +01003202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3203 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003204 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003205
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003206 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003208 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3209 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3210 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003211 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003212 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3213 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003214 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003215 // In case of a byte operation, the register allocator does not support multiple
3216 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003217 locations->SetInAt(0, Location::RequiresRegister());
3218 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003219 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003220 // Ensure the value is in a byte register.
3221 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003222 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003223 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003224 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003225 // Temporary registers for the write barrier.
3226 if (needs_write_barrier) {
3227 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003228 // Ensure the card is in a byte register.
3229 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232}
3233
3234void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3235 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003237 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003238 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003239 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003240 bool needs_runtime_call = locations->WillCall();
3241 bool needs_write_barrier =
3242 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243
3244 switch (value_type) {
3245 case Primitive::kPrimBoolean:
3246 case Primitive::kPrimByte: {
3247 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 if (index.IsConstant()) {
3249 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003250 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003251 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003252 } else {
3253 __ movb(Address(obj, offset),
3254 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3255 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003256 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003257 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003259 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003260 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003262 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003265 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003266 break;
3267 }
3268
3269 case Primitive::kPrimShort:
3270 case Primitive::kPrimChar: {
3271 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 if (index.IsConstant()) {
3273 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003274 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003275 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003276 } else {
3277 __ movw(Address(obj, offset),
3278 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3279 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003280 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003281 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3283 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003285 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003286 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3287 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003288 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003290 break;
3291 }
3292
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003293 case Primitive::kPrimInt:
3294 case Primitive::kPrimNot: {
3295 if (!needs_runtime_call) {
3296 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3297 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003298 size_t offset =
3299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003300 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003301 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003302 } else {
3303 DCHECK(value.IsConstant()) << value;
3304 __ movl(Address(obj, offset),
3305 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3306 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003307 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003308 DCHECK(index.IsRegister()) << index;
3309 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003310 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3311 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003312 } else {
3313 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003314 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003315 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3316 }
3317 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003319
3320 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 Register temp = locations->GetTemp(0).AsRegister<Register>();
3322 Register card = locations->GetTemp(1).AsRegister<Register>();
3323 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003325 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003326 DCHECK_EQ(value_type, Primitive::kPrimNot);
3327 DCHECK(!codegen_->IsLeafMethod());
3328 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3329 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003330 }
3331 break;
3332 }
3333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003334 case Primitive::kPrimLong: {
3335 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003336 if (index.IsConstant()) {
3337 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003338 if (value.IsRegisterPair()) {
3339 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003341 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003342 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003343 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003344 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3345 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003346 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003347 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3348 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003349 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003350 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003352 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003353 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003354 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003355 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003356 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003357 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003358 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003360 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003361 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003363 Immediate(High32Bits(val)));
3364 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365 }
3366 break;
3367 }
3368
Mark Mendell7c8d0092015-01-26 11:21:33 -05003369 case Primitive::kPrimFloat: {
3370 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3371 DCHECK(value.IsFpuRegister());
3372 if (index.IsConstant()) {
3373 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3374 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3375 } else {
3376 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3377 value.AsFpuRegister<XmmRegister>());
3378 }
3379 break;
3380 }
3381
3382 case Primitive::kPrimDouble: {
3383 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3384 DCHECK(value.IsFpuRegister());
3385 if (index.IsConstant()) {
3386 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3387 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3388 } else {
3389 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3390 value.AsFpuRegister<XmmRegister>());
3391 }
3392 break;
3393 }
3394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003395 case Primitive::kPrimVoid:
3396 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003397 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 }
3399}
3400
3401void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003403 locations->SetInAt(0, Location::RequiresRegister());
3404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 instruction->SetLocations(locations);
3406}
3407
3408void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3409 LocationSummary* locations = instruction->GetLocations();
3410 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003411 Register obj = locations->InAt(0).AsRegister<Register>();
3412 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003413 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003414 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003415}
3416
3417void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003418 LocationSummary* locations =
3419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003420 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003421 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003422 if (instruction->HasUses()) {
3423 locations->SetOut(Location::SameAsFirstInput());
3424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003425}
3426
3427void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3428 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003429 Location index_loc = locations->InAt(0);
3430 Location length_loc = locations->InAt(1);
3431 SlowPathCodeX86* slow_path =
3432 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003433 codegen_->AddSlowPath(slow_path);
3434
Mark Mendellf60c90b2015-03-04 15:12:59 -05003435 Register length = length_loc.AsRegister<Register>();
3436 if (index_loc.IsConstant()) {
3437 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3438 __ cmpl(length, Immediate(value));
3439 } else {
3440 __ cmpl(length, index_loc.AsRegister<Register>());
3441 }
3442 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003443}
3444
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003445void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3446 temp->SetLocations(nullptr);
3447}
3448
3449void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3450 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003451 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003452}
3453
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003454void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003455 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003456 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003457}
3458
3459void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003460 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3461}
3462
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003463void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3465}
3466
3467void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003468 HBasicBlock* block = instruction->GetBlock();
3469 if (block->GetLoopInformation() != nullptr) {
3470 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3471 // The back edge will generate the suspend check.
3472 return;
3473 }
3474 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3475 // The goto will generate the suspend check.
3476 return;
3477 }
3478 GenerateSuspendCheck(instruction, nullptr);
3479}
3480
3481void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3482 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003483 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003484 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003485 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003486 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003487 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003488 if (successor == nullptr) {
3489 __ j(kNotEqual, slow_path->GetEntryLabel());
3490 __ Bind(slow_path->GetReturnLabel());
3491 } else {
3492 __ j(kEqual, codegen_->GetLabelOf(successor));
3493 __ jmp(slow_path->GetEntryLabel());
3494 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003495}
3496
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003497X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3498 return codegen_->GetAssembler();
3499}
3500
Mark Mendell7c8d0092015-01-26 11:21:33 -05003501void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003502 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003503 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003504 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003505 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003506 __ movl(temp_reg, Address(ESP, src + stack_offset));
3507 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3508}
3509
3510void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3511 ScratchRegisterScope ensure_scratch(
3512 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3513 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3514 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3515 __ movl(temp_reg, Address(ESP, src + stack_offset));
3516 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3517 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3518 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003519}
3520
3521void ParallelMoveResolverX86::EmitMove(size_t index) {
3522 MoveOperands* move = moves_.Get(index);
3523 Location source = move->GetSource();
3524 Location destination = move->GetDestination();
3525
3526 if (source.IsRegister()) {
3527 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003529 } else {
3530 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003532 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003533 } else if (source.IsFpuRegister()) {
3534 if (destination.IsFpuRegister()) {
3535 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3536 } else if (destination.IsStackSlot()) {
3537 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3538 } else {
3539 DCHECK(destination.IsDoubleStackSlot());
3540 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3541 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003542 } else if (source.IsStackSlot()) {
3543 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003545 } else if (destination.IsFpuRegister()) {
3546 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003547 } else {
3548 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003549 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3550 }
3551 } else if (source.IsDoubleStackSlot()) {
3552 if (destination.IsFpuRegister()) {
3553 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3554 } else {
3555 DCHECK(destination.IsDoubleStackSlot()) << destination;
3556 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003557 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003558 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003559 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003560 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003561 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003562 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003563 if (value == 0) {
3564 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3565 } else {
3566 __ movl(destination.AsRegister<Register>(), Immediate(value));
3567 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003568 } else {
3569 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003570 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003571 }
Nicolas Geoffray154552e2015-03-06 16:10:14 +00003572 } else {
3573 DCHECK(constant->IsFloatConstant());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003574 float value = constant->AsFloatConstant()->GetValue();
3575 Immediate imm(bit_cast<float, int32_t>(value));
3576 if (destination.IsFpuRegister()) {
3577 ScratchRegisterScope ensure_scratch(
3578 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3579 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3580 __ movl(temp, imm);
3581 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3582 } else {
3583 DCHECK(destination.IsStackSlot()) << destination;
3584 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3585 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003586 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003587 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003588 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003589 }
3590}
3591
3592void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003593 Register suggested_scratch = reg == EAX ? EBX : EAX;
3594 ScratchRegisterScope ensure_scratch(
3595 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3596
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003597 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3598 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3599 __ movl(Address(ESP, mem + stack_offset), reg);
3600 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3601}
3602
Mark Mendell7c8d0092015-01-26 11:21:33 -05003603void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3604 ScratchRegisterScope ensure_scratch(
3605 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3606
3607 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3608 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3609 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3610 __ movss(Address(ESP, mem + stack_offset), reg);
3611 __ movd(reg, temp_reg);
3612}
3613
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003614void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3615 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003616 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3617
3618 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003619 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003620 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3621
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003622 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3623 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3624 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3625 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3626 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3627 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3628}
3629
3630void ParallelMoveResolverX86::EmitSwap(size_t index) {
3631 MoveOperands* move = moves_.Get(index);
3632 Location source = move->GetSource();
3633 Location destination = move->GetDestination();
3634
3635 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003637 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003638 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003639 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003641 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3642 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003643 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3644 // Use XOR Swap algorithm to avoid a temporary.
3645 DCHECK_NE(source.reg(), destination.reg());
3646 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3647 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3648 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3649 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3650 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3651 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3652 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003653 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003654 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003655 }
3656}
3657
3658void ParallelMoveResolverX86::SpillScratch(int reg) {
3659 __ pushl(static_cast<Register>(reg));
3660}
3661
3662void ParallelMoveResolverX86::RestoreScratch(int reg) {
3663 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003664}
3665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003666void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003667 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3668 ? LocationSummary::kCallOnSlowPath
3669 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003670 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003671 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003672 locations->SetOut(Location::RequiresRegister());
3673}
3674
3675void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003677 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003678 DCHECK(!cls->CanCallRuntime());
3679 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003680 codegen_->LoadCurrentMethod(out);
3681 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3682 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003683 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003684 codegen_->LoadCurrentMethod(out);
3685 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3686 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003687
3688 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3689 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3690 codegen_->AddSlowPath(slow_path);
3691 __ testl(out, out);
3692 __ j(kEqual, slow_path->GetEntryLabel());
3693 if (cls->MustGenerateClinitCheck()) {
3694 GenerateClassInitializationCheck(slow_path, out);
3695 } else {
3696 __ Bind(slow_path->GetExitLabel());
3697 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003698 }
3699}
3700
3701void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3702 LocationSummary* locations =
3703 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3704 locations->SetInAt(0, Location::RequiresRegister());
3705 if (check->HasUses()) {
3706 locations->SetOut(Location::SameAsFirstInput());
3707 }
3708}
3709
3710void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003711 // We assume the class to not be null.
3712 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3713 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003714 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003715 GenerateClassInitializationCheck(slow_path,
3716 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003717}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003718
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003719void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3720 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003721 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3722 Immediate(mirror::Class::kStatusInitialized));
3723 __ j(kLess, slow_path->GetEntryLabel());
3724 __ Bind(slow_path->GetExitLabel());
3725 // No need for memory fence, thanks to the X86 memory model.
3726}
3727
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003728void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3729 LocationSummary* locations =
3730 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3731 locations->SetOut(Location::RequiresRegister());
3732}
3733
3734void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3735 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3736 codegen_->AddSlowPath(slow_path);
3737
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003739 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003740 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3741 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003742 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3743 __ testl(out, out);
3744 __ j(kEqual, slow_path->GetEntryLabel());
3745 __ Bind(slow_path->GetExitLabel());
3746}
3747
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003748void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3749 LocationSummary* locations =
3750 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3751 locations->SetOut(Location::RequiresRegister());
3752}
3753
3754void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3755 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003757 __ fs()->movl(address, Immediate(0));
3758}
3759
3760void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3761 LocationSummary* locations =
3762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3763 InvokeRuntimeCallingConvention calling_convention;
3764 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3765}
3766
3767void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3768 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3769 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3770}
3771
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003772void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003773 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3774 ? LocationSummary::kNoCall
3775 : LocationSummary::kCallOnSlowPath;
3776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3777 locations->SetInAt(0, Location::RequiresRegister());
3778 locations->SetInAt(1, Location::Any());
3779 locations->SetOut(Location::RequiresRegister());
3780}
3781
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003782void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003783 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003785 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003787 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3788 Label done, zero;
3789 SlowPathCodeX86* slow_path = nullptr;
3790
3791 // Return 0 if `obj` is null.
3792 // TODO: avoid this check if we know obj is not null.
3793 __ testl(obj, obj);
3794 __ j(kEqual, &zero);
3795 __ movl(out, Address(obj, class_offset));
3796 // Compare the class of `obj` with `cls`.
3797 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003799 } else {
3800 DCHECK(cls.IsStackSlot()) << cls;
3801 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3802 }
3803
3804 if (instruction->IsClassFinal()) {
3805 // Classes must be equal for the instanceof to succeed.
3806 __ j(kNotEqual, &zero);
3807 __ movl(out, Immediate(1));
3808 __ jmp(&done);
3809 } else {
3810 // If the classes are not equal, we go into a slow path.
3811 DCHECK(locations->OnlyCallsOnSlowPath());
3812 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003813 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003814 codegen_->AddSlowPath(slow_path);
3815 __ j(kNotEqual, slow_path->GetEntryLabel());
3816 __ movl(out, Immediate(1));
3817 __ jmp(&done);
3818 }
3819 __ Bind(&zero);
3820 __ movl(out, Immediate(0));
3821 if (slow_path != nullptr) {
3822 __ Bind(slow_path->GetExitLabel());
3823 }
3824 __ Bind(&done);
3825}
3826
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003827void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3829 instruction, LocationSummary::kCallOnSlowPath);
3830 locations->SetInAt(0, Location::RequiresRegister());
3831 locations->SetInAt(1, Location::Any());
3832 locations->AddTemp(Location::RequiresRegister());
3833}
3834
3835void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3836 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003837 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003838 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003839 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003840 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3841 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3842 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3843 codegen_->AddSlowPath(slow_path);
3844
3845 // TODO: avoid this check if we know obj is not null.
3846 __ testl(obj, obj);
3847 __ j(kEqual, slow_path->GetExitLabel());
3848 __ movl(temp, Address(obj, class_offset));
3849
3850 // Compare the class of `obj` with `cls`.
3851 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003853 } else {
3854 DCHECK(cls.IsStackSlot()) << cls;
3855 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3856 }
3857
3858 __ j(kNotEqual, slow_path->GetEntryLabel());
3859 __ Bind(slow_path->GetExitLabel());
3860}
3861
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003862void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3863 LocationSummary* locations =
3864 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3865 InvokeRuntimeCallingConvention calling_convention;
3866 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3867}
3868
3869void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3870 __ fs()->call(Address::Absolute(instruction->IsEnter()
3871 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3872 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3873 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3874}
3875
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003876void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3877void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3878void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3879
3880void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3881 LocationSummary* locations =
3882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3883 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3884 || instruction->GetResultType() == Primitive::kPrimLong);
3885 locations->SetInAt(0, Location::RequiresRegister());
3886 locations->SetInAt(1, Location::Any());
3887 locations->SetOut(Location::SameAsFirstInput());
3888}
3889
3890void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3891 HandleBitwiseOperation(instruction);
3892}
3893
3894void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3895 HandleBitwiseOperation(instruction);
3896}
3897
3898void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3899 HandleBitwiseOperation(instruction);
3900}
3901
3902void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3903 LocationSummary* locations = instruction->GetLocations();
3904 Location first = locations->InAt(0);
3905 Location second = locations->InAt(1);
3906 DCHECK(first.Equals(locations->Out()));
3907
3908 if (instruction->GetResultType() == Primitive::kPrimInt) {
3909 if (second.IsRegister()) {
3910 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003911 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003912 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003914 } else {
3915 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003916 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003917 }
3918 } else if (second.IsConstant()) {
3919 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003920 __ andl(first.AsRegister<Register>(),
3921 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003922 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003923 __ orl(first.AsRegister<Register>(),
3924 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003925 } else {
3926 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003927 __ xorl(first.AsRegister<Register>(),
3928 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003929 }
3930 } else {
3931 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003933 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003934 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003935 } else {
3936 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003938 }
3939 }
3940 } else {
3941 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3942 if (second.IsRegisterPair()) {
3943 if (instruction->IsAnd()) {
3944 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3945 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3946 } else if (instruction->IsOr()) {
3947 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3948 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3949 } else {
3950 DCHECK(instruction->IsXor());
3951 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3952 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3953 }
Nicolas Geoffray154552e2015-03-06 16:10:14 +00003954 } else {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003955 if (instruction->IsAnd()) {
3956 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3957 __ andl(first.AsRegisterPairHigh<Register>(),
3958 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3959 } else if (instruction->IsOr()) {
3960 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3961 __ orl(first.AsRegisterPairHigh<Register>(),
3962 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3963 } else {
3964 DCHECK(instruction->IsXor());
3965 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3966 __ xorl(first.AsRegisterPairHigh<Register>(),
3967 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3968 }
3969 }
3970 }
3971}
3972
Calin Juravleb1498f62015-02-16 13:13:29 +00003973void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
3974 // Nothing to do, this should be removed during prepare for register allocator.
3975 UNUSED(instruction);
3976 LOG(FATAL) << "Unreachable";
3977}
3978
3979void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
3980 // Nothing to do, this should be removed during prepare for register allocator.
3981 UNUSED(instruction);
3982 LOG(FATAL) << "Unreachable";
3983}
3984
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003985} // namespace x86
3986} // namespace art