blob: 224be0f25bffac92588cee62eaacec32387fe0b5 [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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040022#include "intrinsics.h"
23#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070024#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010027#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010029#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace x86 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037static constexpr int kCurrentMethodStackOffset = 0;
38
Mark Mendell5f874182015-03-04 15:42:45 -050039static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040
Mark Mendell24f2dfa2015-01-14 19:51:45 -050041static constexpr int kC2ConditionMask = 0x400;
42
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000043static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000044
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
46
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010047class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010049 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050
Alexandre Rames2ed20af2015-03-06 13:55:35 +000051 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 __ Bind(GetEntryLabel());
53 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 }
56
57 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010058 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
60};
61
Calin Juravled0d48522014-11-04 16:40:20 +000062class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
63 public:
64 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
65
Alexandre Rames2ed20af2015-03-06 13:55:35 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000067 __ Bind(GetEntryLabel());
68 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
69 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
70 }
71
72 private:
73 HDivZeroCheck* const instruction_;
74 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
75};
76
Calin Juravlebacfec32014-11-14 15:54:36 +000077class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000078 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000079 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000080
Alexandre Rames2ed20af2015-03-06 13:55:35 +000081 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000082 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000083 if (is_div_) {
84 __ negl(reg_);
85 } else {
86 __ movl(reg_, Immediate(0));
87 }
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ jmp(GetExitLabel());
89 }
90
91 private:
92 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000093 bool is_div_;
94 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000095};
96
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010097class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010098 public:
Roland Levillain5799fc02014-09-25 12:15:20 +010099 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
100 Location index_location,
101 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000102 : instruction_(instruction),
103 index_location_(index_location),
104 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100105
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000106 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100107 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100108 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000109 // We're moving two locations to locations that could overlap, so we need a parallel
110 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100111 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000112 x86_codegen->EmitParallelMoves(
113 index_location_,
114 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
115 length_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100117 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100118 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100119 }
120
121 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100122 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 const Location index_location_;
124 const Location length_location_;
125
126 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
127};
128
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100129class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000131 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000137 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
139 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100141 if (successor_ == nullptr) {
142 __ jmp(GetReturnLabel());
143 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100144 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 }
147
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 Label* GetReturnLabel() {
149 DCHECK(successor_ == nullptr);
150 return &return_label_;
151 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152
153 private:
154 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100155 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 Label return_label_;
157
158 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
159};
160
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000161class LoadStringSlowPathX86 : public SlowPathCodeX86 {
162 public:
163 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
164
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000165 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000166 LocationSummary* locations = instruction_->GetLocations();
167 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
168
169 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
170 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000171 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000172
173 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800174 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
175 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000176 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000178 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 __ jmp(GetExitLabel());
182 }
183
184 private:
185 HLoadString* const instruction_;
186
187 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
188};
189
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000190class LoadClassSlowPathX86 : public SlowPathCodeX86 {
191 public:
192 LoadClassSlowPathX86(HLoadClass* cls,
193 HInstruction* at,
194 uint32_t dex_pc,
195 bool do_clinit)
196 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
197 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
198 }
199
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000200 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000201 LocationSummary* locations = at_->GetLocations();
202 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
203 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000205
206 InvokeRuntimeCallingConvention calling_convention;
207 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
208 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
209 __ fs()->call(Address::Absolute(do_clinit_
210 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
211 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000212 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213
214 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000215 Location out = locations->Out();
216 if (out.IsValid()) {
217 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
218 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000221 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ jmp(GetExitLabel());
223 }
224
225 private:
226 // The class this slow path will load.
227 HLoadClass* const cls_;
228
229 // The instruction where this slow path is happening.
230 // (Might be the load class or an initialization check).
231 HInstruction* const at_;
232
233 // The dex PC of `at_`.
234 const uint32_t dex_pc_;
235
236 // Whether to initialize the class.
237 const bool do_clinit_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
240};
241
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000242class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
243 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000244 TypeCheckSlowPathX86(HInstruction* instruction,
245 Location class_to_check,
246 Location object_class,
247 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000249 class_to_check_(class_to_check),
250 object_class_(object_class),
251 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 DCHECK(instruction_->IsCheckCast()
256 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257
258 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
259 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000261
262 // We're moving two locations to locations that could overlap, so we need a parallel
263 // move resolver.
264 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000265 x86_codegen->EmitParallelMoves(
266 class_to_check_,
267 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
268 object_class_,
269 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000271 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000272 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
273 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 } else {
275 DCHECK(instruction_->IsCheckCast());
276 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
277 }
278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 if (instruction_->IsInstanceOf()) {
281 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
282 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000283 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
285 __ jmp(GetExitLabel());
286 }
287
288 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000289 HInstruction* const instruction_;
290 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
295};
296
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100297#undef __
298#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
299
Dave Allison20dfc792014-06-16 20:44:29 -0700300inline Condition X86Condition(IfCondition cond) {
301 switch (cond) {
302 case kCondEQ: return kEqual;
303 case kCondNE: return kNotEqual;
304 case kCondLT: return kLess;
305 case kCondLE: return kLessEqual;
306 case kCondGT: return kGreater;
307 case kCondGE: return kGreaterEqual;
308 default:
309 LOG(FATAL) << "Unknown if condition";
310 }
311 return kEqual;
312}
313
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
315 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
316}
317
318void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
319 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
320}
321
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100322size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
323 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
324 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100325}
326
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100327size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
328 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
329 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100330}
331
Mark Mendell7c8d0092015-01-26 11:21:33 -0500332size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
333 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
334 return GetFloatingPointSpillSlotSize();
335}
336
337size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
338 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
339 return GetFloatingPointSpillSlotSize();
340}
341
Mark Mendellfb8d2792015-03-31 22:16:59 -0400342CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
343 const X86InstructionSetFeatures& isa_features,
344 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500345 : CodeGenerator(graph,
346 kNumberOfCpuRegisters,
347 kNumberOfXmmRegisters,
348 kNumberOfRegisterPairs,
349 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
350 arraysize(kCoreCalleeSaves))
351 | (1 << kFakeReturnRegister),
352 0,
353 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100354 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100355 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100356 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400357 move_resolver_(graph->GetArena(), this),
358 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000359 // Use a fake return address register to mimic Quick.
360 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100361}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100362
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100363Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100364 switch (type) {
365 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100366 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100367 X86ManagedRegister pair =
368 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100369 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
370 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100371 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
372 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100373 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100374 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375 }
376
377 case Primitive::kPrimByte:
378 case Primitive::kPrimBoolean:
379 case Primitive::kPrimChar:
380 case Primitive::kPrimShort:
381 case Primitive::kPrimInt:
382 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100383 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100385 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100386 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
387 X86ManagedRegister current =
388 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
389 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100391 }
392 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100393 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 }
395
396 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100397 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100398 return Location::FpuRegisterLocation(
399 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100400 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401
402 case Primitive::kPrimVoid:
403 LOG(FATAL) << "Unreachable type " << type;
404 }
405
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100406 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100407}
408
Mark Mendell5f874182015-03-04 15:42:45 -0500409void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100410 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100412
413 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415
Mark Mendell5f874182015-03-04 15:42:45 -0500416 if (is_baseline) {
417 blocked_core_registers_[EBP] = true;
418 blocked_core_registers_[ESI] = true;
419 blocked_core_registers_[EDI] = true;
420 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100421
422 UpdateBlockedPairRegisters();
423}
424
425void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
426 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
427 X86ManagedRegister current =
428 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
429 if (blocked_core_registers_[current.AsRegisterPairLow()]
430 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
431 blocked_register_pairs_[i] = true;
432 }
433 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434}
435
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100436InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
437 : HGraphVisitor(graph),
438 assembler_(codegen->GetAssembler()),
439 codegen_(codegen) {}
440
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000441void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000442 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000443 bool skip_overflow_check =
444 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000445 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000446
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000447 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100448 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100449 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100450 }
451
Mark Mendell5f874182015-03-04 15:42:45 -0500452 if (HasEmptyFrame()) {
453 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000454 }
Mark Mendell5f874182015-03-04 15:42:45 -0500455
456 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
457 Register reg = kCoreCalleeSaves[i];
458 if (allocated_registers_.ContainsCoreRegister(reg)) {
459 __ pushl(reg);
460 }
461 }
462
463 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
464 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000465}
466
467void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500468 if (HasEmptyFrame()) {
469 return;
470 }
471
472 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
473
474 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
475 Register reg = kCoreCalleeSaves[i];
476 if (allocated_registers_.ContainsCoreRegister(reg)) {
477 __ popl(reg);
478 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000479 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000480}
481
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100482void CodeGeneratorX86::Bind(HBasicBlock* block) {
483 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000484}
485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100486void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000487 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100488 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000489}
490
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100491Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
492 switch (load->GetType()) {
493 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100494 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100495 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
496 break;
497
498 case Primitive::kPrimInt:
499 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100500 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100502
503 case Primitive::kPrimBoolean:
504 case Primitive::kPrimByte:
505 case Primitive::kPrimChar:
506 case Primitive::kPrimShort:
507 case Primitive::kPrimVoid:
508 LOG(FATAL) << "Unexpected type " << load->GetType();
509 }
510
511 LOG(FATAL) << "Unreachable";
512 return Location();
513}
514
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100515Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
516 switch (type) {
517 case Primitive::kPrimBoolean:
518 case Primitive::kPrimByte:
519 case Primitive::kPrimChar:
520 case Primitive::kPrimShort:
521 case Primitive::kPrimInt:
522 case Primitive::kPrimNot: {
523 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000524 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100525 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100526 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000528 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100529 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100530 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000532 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100533 uint32_t index = gp_index_;
534 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000535 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100537 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
538 calling_convention.GetRegisterPairAt(index));
539 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100540 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000541 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
542 }
543 }
544
545 case Primitive::kPrimFloat: {
546 uint32_t index = fp_index_++;
547 stack_index_++;
548 if (index < calling_convention.GetNumberOfFpuRegisters()) {
549 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
550 } else {
551 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
552 }
553 }
554
555 case Primitive::kPrimDouble: {
556 uint32_t index = fp_index_++;
557 stack_index_ += 2;
558 if (index < calling_convention.GetNumberOfFpuRegisters()) {
559 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
560 } else {
561 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100562 }
563 }
564
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 case Primitive::kPrimVoid:
566 LOG(FATAL) << "Unexpected parameter type " << type;
567 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100568 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100569 return Location();
570}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100572void CodeGeneratorX86::Move32(Location destination, Location source) {
573 if (source.Equals(destination)) {
574 return;
575 }
576 if (destination.IsRegister()) {
577 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000580 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100581 } else {
582 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (destination.IsFpuRegister()) {
586 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000589 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 } else {
591 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100594 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000595 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000597 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100598 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000599 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500600 } else if (source.IsConstant()) {
601 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000602 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500603 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100604 } else {
605 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100606 __ pushl(Address(ESP, source.GetStackIndex()));
607 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 }
609 }
610}
611
612void CodeGeneratorX86::Move64(Location destination, Location source) {
613 if (source.Equals(destination)) {
614 return;
615 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100616 if (destination.IsRegisterPair()) {
617 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000618 EmitParallelMoves(
619 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
620 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
621 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
622 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
624 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000626 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100628 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
629 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
631 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500633 if (source.IsFpuRegister()) {
634 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
635 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else {
638 LOG(FATAL) << "Unimplemented";
639 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000641 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100642 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000643 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100644 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100646 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100647 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000648 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000649 } else if (source.IsConstant()) {
650 HConstant* constant = source.GetConstant();
651 int64_t value;
652 if (constant->IsLongConstant()) {
653 value = constant->AsLongConstant()->GetValue();
654 } else {
655 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000656 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000657 }
658 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
659 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000661 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000662 EmitParallelMoves(
663 Location::StackSlot(source.GetStackIndex()),
664 Location::StackSlot(destination.GetStackIndex()),
665 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
666 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 }
668 }
669}
670
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100671void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000672 LocationSummary* locations = instruction->GetLocations();
673 if (locations != nullptr && locations->Out().Equals(location)) {
674 return;
675 }
676
677 if (locations != nullptr && locations->Out().IsConstant()) {
678 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000679 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
680 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000681 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000683 } else if (location.IsStackSlot()) {
684 __ movl(Address(ESP, location.GetStackIndex()), imm);
685 } else {
686 DCHECK(location.IsConstant());
687 DCHECK_EQ(location.GetConstant(), const_to_move);
688 }
689 } else if (const_to_move->IsLongConstant()) {
690 int64_t value = const_to_move->AsLongConstant()->GetValue();
691 if (location.IsRegisterPair()) {
692 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
693 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
694 } else if (location.IsDoubleStackSlot()) {
695 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000696 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
697 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000698 } else {
699 DCHECK(location.IsConstant());
700 DCHECK_EQ(location.GetConstant(), instruction);
701 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000703 } else if (instruction->IsTemporary()) {
704 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000705 if (temp_location.IsStackSlot()) {
706 Move32(location, temp_location);
707 } else {
708 DCHECK(temp_location.IsDoubleStackSlot());
709 Move64(location, temp_location);
710 }
Roland Levillain476df552014-10-09 17:51:36 +0100711 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 switch (instruction->GetType()) {
714 case Primitive::kPrimBoolean:
715 case Primitive::kPrimByte:
716 case Primitive::kPrimChar:
717 case Primitive::kPrimShort:
718 case Primitive::kPrimInt:
719 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 case Primitive::kPrimFloat:
721 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100722 break;
723
724 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 case Primitive::kPrimDouble:
726 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 break;
728
729 default:
730 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
731 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100733 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 switch (instruction->GetType()) {
735 case Primitive::kPrimBoolean:
736 case Primitive::kPrimByte:
737 case Primitive::kPrimChar:
738 case Primitive::kPrimShort:
739 case Primitive::kPrimInt:
740 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000742 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 break;
744
745 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000747 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 break;
749
750 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100751 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000753 }
754}
755
756void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000757 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000758}
759
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000760void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000761 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100762 DCHECK(!successor->IsExitBlock());
763
764 HBasicBlock* block = got->GetBlock();
765 HInstruction* previous = got->GetPrevious();
766
767 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000768 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
770 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
771 return;
772 }
773
774 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
775 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
776 }
777 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000779 }
780}
781
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000782void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000783 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000784}
785
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000786void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700787 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000788}
789
Andreas Gampe0ba62732015-03-24 02:39:46 +0000790void LocationsBuilderX86::VisitIf(HIf* if_instr) {
791 LocationSummary* locations =
792 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
793 HInstruction* cond = if_instr->InputAt(0);
794 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
795 locations->SetInAt(0, Location::Any());
796 }
797}
798
799void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
800 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100801 if (cond->IsIntConstant()) {
802 // Constant condition, statically compared against 1.
803 int32_t cond_value = cond->AsIntConstant()->GetValue();
804 if (cond_value == 1) {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000805 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
806 if_instr->IfTrueSuccessor())) {
807 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100808 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100809 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100810 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100811 DCHECK_EQ(cond_value, 0);
812 }
813 } else {
814 bool materialized =
815 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
816 // Moves do not affect the eflags register, so if the condition is
817 // evaluated just before the if, we don't need to evaluate it
818 // again.
819 bool eflags_set = cond->IsCondition()
Andreas Gampe0ba62732015-03-24 02:39:46 +0000820 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100821 if (materialized) {
822 if (!eflags_set) {
823 // Materialized condition, compare against 0.
Andreas Gampe0ba62732015-03-24 02:39:46 +0000824 Location lhs = if_instr->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500826 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 } else {
828 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
829 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000830 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100831 } else {
Andreas Gampe0ba62732015-03-24 02:39:46 +0000832 __ j(X86Condition(cond->AsCondition()->GetCondition()),
833 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 }
835 } else {
836 Location lhs = cond->GetLocations()->InAt(0);
837 Location rhs = cond->GetLocations()->InAt(1);
838 // LHS is guaranteed to be in a register (see
839 // LocationsBuilderX86::VisitCondition).
840 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000841 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500843 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
844 if (constant == 0) {
845 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
846 } else {
847 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000850 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100851 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000852 __ j(X86Condition(cond->AsCondition()->GetCondition()),
853 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700854 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100855 }
Andreas Gampe0ba62732015-03-24 02:39:46 +0000856 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
857 if_instr->IfFalseSuccessor())) {
858 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000859 }
860}
861
862void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000863 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000864}
865
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
867 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868}
869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100871 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872}
873
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000874void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100875 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700876 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877}
878
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100880 LocationSummary* locations =
881 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 switch (store->InputAt(1)->GetType()) {
883 case Primitive::kPrimBoolean:
884 case Primitive::kPrimByte:
885 case Primitive::kPrimChar:
886 case Primitive::kPrimShort:
887 case Primitive::kPrimInt:
888 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
891 break;
892
893 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100894 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100895 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
896 break;
897
898 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100899 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100900 }
901 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000902}
903
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700905 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000906}
907
Dave Allison20dfc792014-06-16 20:44:29 -0700908void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100909 LocationSummary* locations =
910 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100911 locations->SetInAt(0, Location::RequiresRegister());
912 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100913 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500914 // We need a byte register.
915 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100916 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000917}
918
Dave Allison20dfc792014-06-16 20:44:29 -0700919void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
920 if (comp->NeedsMaterialization()) {
921 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000922 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100923 // Clear register: setcc only sets the low byte.
924 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500925 Location lhs = locations->InAt(0);
926 Location rhs = locations->InAt(1);
927 if (rhs.IsRegister()) {
928 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
929 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800930 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500931 if (constant == 0) {
932 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
933 } else {
934 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
935 }
Dave Allison20dfc792014-06-16 20:44:29 -0700936 } else {
Mark Mendell09b84632015-02-13 17:48:38 -0500937 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700938 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000939 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100940 }
Dave Allison20dfc792014-06-16 20:44:29 -0700941}
942
943void LocationsBuilderX86::VisitEqual(HEqual* comp) {
944 VisitCondition(comp);
945}
946
947void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
948 VisitCondition(comp);
949}
950
951void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
952 VisitCondition(comp);
953}
954
955void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
956 VisitCondition(comp);
957}
958
959void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
960 VisitCondition(comp);
961}
962
963void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
964 VisitCondition(comp);
965}
966
967void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
968 VisitCondition(comp);
969}
970
971void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
972 VisitCondition(comp);
973}
974
975void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
976 VisitCondition(comp);
977}
978
979void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
980 VisitCondition(comp);
981}
982
983void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
988 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989}
990
991void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100992 LocationSummary* locations =
993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100994 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995}
996
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000997void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100998 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700999 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001000}
1001
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001002void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1003 LocationSummary* locations =
1004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1005 locations->SetOut(Location::ConstantLocation(constant));
1006}
1007
1008void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1009 // Will be generated at use site.
1010 UNUSED(constant);
1011}
1012
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001016 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017}
1018
1019void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1020 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022}
1023
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001024void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1025 LocationSummary* locations =
1026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1027 locations->SetOut(Location::ConstantLocation(constant));
1028}
1029
1030void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1031 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001033}
1034
1035void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1036 LocationSummary* locations =
1037 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1038 locations->SetOut(Location::ConstantLocation(constant));
1039}
1040
1041void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1042 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001043 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001044}
1045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001048}
1049
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001051 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001053 __ ret();
1054}
1055
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001057 LocationSummary* locations =
1058 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 switch (ret->InputAt(0)->GetType()) {
1060 case Primitive::kPrimBoolean:
1061 case Primitive::kPrimByte:
1062 case Primitive::kPrimChar:
1063 case Primitive::kPrimShort:
1064 case Primitive::kPrimInt:
1065 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 break;
1068
1069 case Primitive::kPrimLong:
1070 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 break;
1073
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 case Primitive::kPrimFloat:
1075 case Primitive::kPrimDouble:
1076 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001077 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 break;
1079
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001083}
1084
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001085void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 if (kIsDebugBuild) {
1087 switch (ret->InputAt(0)->GetType()) {
1088 case Primitive::kPrimBoolean:
1089 case Primitive::kPrimByte:
1090 case Primitive::kPrimChar:
1091 case Primitive::kPrimShort:
1092 case Primitive::kPrimInt:
1093 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 break;
1096
1097 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1099 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 break;
1101
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 case Primitive::kPrimFloat:
1103 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 break;
1106
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
1110 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001111 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112 __ ret();
1113}
1114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001115void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001116 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001117 if (intrinsic.TryDispatch(invoke)) {
1118 return;
1119 }
1120
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001121 HandleInvoke(invoke);
1122}
1123
Mark Mendell09ed1a32015-03-25 08:30:06 -04001124static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1125 if (invoke->GetLocations()->Intrinsified()) {
1126 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1127 intrinsic.Dispatch(invoke);
1128 return true;
1129 }
1130 return false;
1131}
1132
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001133void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001134 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1135 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001136 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001137
Mark Mendell09ed1a32015-03-25 08:30:06 -04001138 codegen_->GenerateStaticOrDirectCall(
1139 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001140}
1141
1142void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1143 HandleInvoke(invoke);
1144}
1145
1146void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001150
1151 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001153 HInstruction* input = invoke->InputAt(i);
1154 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1155 }
1156
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 switch (invoke->GetType()) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001164 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 break;
1166
1167 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001168 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 break;
1170
1171 case Primitive::kPrimVoid:
1172 break;
1173
1174 case Primitive::kPrimDouble:
1175 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001176 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 break;
1178 }
1179
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001180 invoke->SetLocations(locations);
1181}
1182
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001183void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001184 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001185 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1186 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1187 LocationSummary* locations = invoke->GetLocations();
1188 Location receiver = locations->InAt(0);
1189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1190 // temp = object->GetClass();
1191 if (receiver.IsStackSlot()) {
1192 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1193 __ movl(temp, Address(temp, class_offset));
1194 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001196 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001197 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001198 // temp = temp->GetMethodAt(method_offset);
1199 __ movl(temp, Address(temp, method_offset));
1200 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001201 __ call(Address(
1202 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001203
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001204 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001205 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206}
1207
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001208void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1209 HandleInvoke(invoke);
1210 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001211 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212}
1213
1214void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1215 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001217 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1218 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1219 LocationSummary* locations = invoke->GetLocations();
1220 Location receiver = locations->InAt(0);
1221 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1222
1223 // Set the hidden argument.
1224 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226
1227 // temp = object->GetClass();
1228 if (receiver.IsStackSlot()) {
1229 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1230 __ movl(temp, Address(temp, class_offset));
1231 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001234 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001235 // temp = temp->GetImtEntryAt(method_offset);
1236 __ movl(temp, Address(temp, method_offset));
1237 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001238 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001239 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240
1241 DCHECK(!codegen_->IsLeafMethod());
1242 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1243}
1244
Roland Levillain88cb1752014-10-20 16:36:47 +01001245void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1246 LocationSummary* locations =
1247 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1248 switch (neg->GetResultType()) {
1249 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001250 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetOut(Location::SameAsFirstInput());
1253 break;
1254
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001256 locations->SetInAt(0, Location::RequiresFpuRegister());
1257 locations->SetOut(Location::SameAsFirstInput());
1258 locations->AddTemp(Location::RequiresRegister());
1259 locations->AddTemp(Location::RequiresFpuRegister());
1260 break;
1261
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001263 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001264 locations->SetOut(Location::SameAsFirstInput());
1265 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 break;
1267
1268 default:
1269 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1270 }
1271}
1272
1273void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1274 LocationSummary* locations = neg->GetLocations();
1275 Location out = locations->Out();
1276 Location in = locations->InAt(0);
1277 switch (neg->GetResultType()) {
1278 case Primitive::kPrimInt:
1279 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001280 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001282 break;
1283
1284 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001285 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001286 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001287 __ negl(out.AsRegisterPairLow<Register>());
1288 // Negation is similar to subtraction from zero. The least
1289 // significant byte triggers a borrow when it is different from
1290 // zero; to take it into account, add 1 to the most significant
1291 // byte if the carry flag (CF) is set to 1 after the first NEGL
1292 // operation.
1293 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1294 __ negl(out.AsRegisterPairHigh<Register>());
1295 break;
1296
Roland Levillain5368c212014-11-27 15:03:41 +00001297 case Primitive::kPrimFloat: {
1298 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 Register constant = locations->GetTemp(0).AsRegister<Register>();
1300 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001301 // Implement float negation with an exclusive or with value
1302 // 0x80000000 (mask for bit 31, representing the sign of a
1303 // single-precision floating-point number).
1304 __ movl(constant, Immediate(INT32_C(0x80000000)));
1305 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001308 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001309
Roland Levillain5368c212014-11-27 15:03:41 +00001310 case Primitive::kPrimDouble: {
1311 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001313 // Implement double negation with an exclusive or with value
1314 // 0x8000000000000000 (mask for bit 63, representing the sign of
1315 // a double-precision floating-point number).
1316 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001318 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001319 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001320
1321 default:
1322 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1323 }
1324}
1325
Roland Levillaindff1f282014-11-05 14:15:05 +00001326void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001327 Primitive::Type result_type = conversion->GetResultType();
1328 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001329 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001330
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001331 // The float-to-long and double-to-long type conversions rely on a
1332 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001333 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001334 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1335 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001336 ? LocationSummary::kCall
1337 : LocationSummary::kNoCall;
1338 LocationSummary* locations =
1339 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1340
David Brazdilb2bd1c52015-03-25 11:17:37 +00001341 // The Java language does not allow treating boolean as an integral type but
1342 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001343
Roland Levillaindff1f282014-11-05 14:15:05 +00001344 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001345 case Primitive::kPrimByte:
1346 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001347 case Primitive::kPrimBoolean:
1348 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001349 case Primitive::kPrimShort:
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001352 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001353 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1354 // Make the output overlap to please the register allocator. This greatly simplifies
1355 // the validation of the linear scan implementation
1356 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001357 break;
1358
1359 default:
1360 LOG(FATAL) << "Unexpected type conversion from " << input_type
1361 << " to " << result_type;
1362 }
1363 break;
1364
Roland Levillain01a8d712014-11-14 16:27:39 +00001365 case Primitive::kPrimShort:
1366 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001367 case Primitive::kPrimBoolean:
1368 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001369 case Primitive::kPrimByte:
1370 case Primitive::kPrimInt:
1371 case Primitive::kPrimChar:
1372 // Processing a Dex `int-to-short' instruction.
1373 locations->SetInAt(0, Location::Any());
1374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1375 break;
1376
1377 default:
1378 LOG(FATAL) << "Unexpected type conversion from " << input_type
1379 << " to " << result_type;
1380 }
1381 break;
1382
Roland Levillain946e1432014-11-11 17:35:19 +00001383 case Primitive::kPrimInt:
1384 switch (input_type) {
1385 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001386 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001387 locations->SetInAt(0, Location::Any());
1388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1389 break;
1390
1391 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001392 // Processing a Dex `float-to-int' instruction.
1393 locations->SetInAt(0, Location::RequiresFpuRegister());
1394 locations->SetOut(Location::RequiresRegister());
1395 locations->AddTemp(Location::RequiresFpuRegister());
1396 break;
1397
Roland Levillain946e1432014-11-11 17:35:19 +00001398 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001399 // Processing a Dex `double-to-int' instruction.
1400 locations->SetInAt(0, Location::RequiresFpuRegister());
1401 locations->SetOut(Location::RequiresRegister());
1402 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001403 break;
1404
1405 default:
1406 LOG(FATAL) << "Unexpected type conversion from " << input_type
1407 << " to " << result_type;
1408 }
1409 break;
1410
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 case Primitive::kPrimLong:
1412 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001413 case Primitive::kPrimBoolean:
1414 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001415 case Primitive::kPrimByte:
1416 case Primitive::kPrimShort:
1417 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001418 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001419 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 locations->SetInAt(0, Location::RegisterLocation(EAX));
1421 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1422 break;
1423
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001424 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001425 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001426 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001427 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001428 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1429 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1430
Vladimir Marko949c91f2015-01-27 10:48:44 +00001431 // The runtime helper puts the result in EAX, EDX.
1432 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001433 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001434 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001435
1436 default:
1437 LOG(FATAL) << "Unexpected type conversion from " << input_type
1438 << " to " << result_type;
1439 }
1440 break;
1441
Roland Levillain981e4542014-11-14 11:47:14 +00001442 case Primitive::kPrimChar:
1443 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001444 case Primitive::kPrimBoolean:
1445 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001446 case Primitive::kPrimByte:
1447 case Primitive::kPrimShort:
1448 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001449 // Processing a Dex `int-to-char' instruction.
1450 locations->SetInAt(0, Location::Any());
1451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1452 break;
1453
1454 default:
1455 LOG(FATAL) << "Unexpected type conversion from " << input_type
1456 << " to " << result_type;
1457 }
1458 break;
1459
Roland Levillaindff1f282014-11-05 14:15:05 +00001460 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001461 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001462 case Primitive::kPrimBoolean:
1463 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001464 case Primitive::kPrimByte:
1465 case Primitive::kPrimShort:
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimChar:
1468 // Processing a Dex `int-to-float' instruction.
1469 locations->SetInAt(0, Location::RequiresRegister());
1470 locations->SetOut(Location::RequiresFpuRegister());
1471 break;
1472
1473 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001474 // Processing a Dex `long-to-float' instruction.
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetOut(Location::RequiresFpuRegister());
1477 locations->AddTemp(Location::RequiresFpuRegister());
1478 locations->AddTemp(Location::RequiresFpuRegister());
1479 break;
1480
Roland Levillaincff13742014-11-17 14:32:17 +00001481 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001482 // Processing a Dex `double-to-float' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001485 break;
1486
1487 default:
1488 LOG(FATAL) << "Unexpected type conversion from " << input_type
1489 << " to " << result_type;
1490 };
1491 break;
1492
Roland Levillaindff1f282014-11-05 14:15:05 +00001493 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001494 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001495 case Primitive::kPrimBoolean:
1496 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001497 case Primitive::kPrimByte:
1498 case Primitive::kPrimShort:
1499 case Primitive::kPrimInt:
1500 case Primitive::kPrimChar:
1501 // Processing a Dex `int-to-double' instruction.
1502 locations->SetInAt(0, Location::RequiresRegister());
1503 locations->SetOut(Location::RequiresFpuRegister());
1504 break;
1505
1506 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001507 // Processing a Dex `long-to-double' instruction.
1508 locations->SetInAt(0, Location::RequiresRegister());
1509 locations->SetOut(Location::RequiresFpuRegister());
1510 locations->AddTemp(Location::RequiresFpuRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
1512 break;
1513
Roland Levillaincff13742014-11-17 14:32:17 +00001514 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001515 // Processing a Dex `float-to-double' instruction.
1516 locations->SetInAt(0, Location::RequiresFpuRegister());
1517 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001518 break;
1519
1520 default:
1521 LOG(FATAL) << "Unexpected type conversion from " << input_type
1522 << " to " << result_type;
1523 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001524 break;
1525
1526 default:
1527 LOG(FATAL) << "Unexpected type conversion from " << input_type
1528 << " to " << result_type;
1529 }
1530}
1531
1532void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1533 LocationSummary* locations = conversion->GetLocations();
1534 Location out = locations->Out();
1535 Location in = locations->InAt(0);
1536 Primitive::Type result_type = conversion->GetResultType();
1537 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001538 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001539 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001540 case Primitive::kPrimByte:
1541 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001542 case Primitive::kPrimBoolean:
1543 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001544 case Primitive::kPrimShort:
1545 case Primitive::kPrimInt:
1546 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001547 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001548 if (in.IsRegister()) {
1549 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001550 } else {
1551 DCHECK(in.GetConstant()->IsIntConstant());
1552 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1553 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1554 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561 break;
1562
Roland Levillain01a8d712014-11-14 16:27:39 +00001563 case Primitive::kPrimShort:
1564 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001565 case Primitive::kPrimBoolean:
1566 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001567 case Primitive::kPrimByte:
1568 case Primitive::kPrimInt:
1569 case Primitive::kPrimChar:
1570 // Processing a Dex `int-to-short' instruction.
1571 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001573 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001575 } else {
1576 DCHECK(in.GetConstant()->IsIntConstant());
1577 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001578 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001579 }
1580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected type conversion from " << input_type
1584 << " to " << result_type;
1585 }
1586 break;
1587
Roland Levillain946e1432014-11-11 17:35:19 +00001588 case Primitive::kPrimInt:
1589 switch (input_type) {
1590 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001591 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001592 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001593 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001594 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001595 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001596 } else {
1597 DCHECK(in.IsConstant());
1598 DCHECK(in.GetConstant()->IsLongConstant());
1599 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001600 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001601 }
1602 break;
1603
Roland Levillain3f8f9362014-12-02 17:45:01 +00001604 case Primitive::kPrimFloat: {
1605 // Processing a Dex `float-to-int' instruction.
1606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1607 Register output = out.AsRegister<Register>();
1608 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1609 Label done, nan;
1610
1611 __ movl(output, Immediate(kPrimIntMax));
1612 // temp = int-to-float(output)
1613 __ cvtsi2ss(temp, output);
1614 // if input >= temp goto done
1615 __ comiss(input, temp);
1616 __ j(kAboveEqual, &done);
1617 // if input == NaN goto nan
1618 __ j(kUnordered, &nan);
1619 // output = float-to-int-truncate(input)
1620 __ cvttss2si(output, input);
1621 __ jmp(&done);
1622 __ Bind(&nan);
1623 // output = 0
1624 __ xorl(output, output);
1625 __ Bind(&done);
1626 break;
1627 }
1628
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001629 case Primitive::kPrimDouble: {
1630 // Processing a Dex `double-to-int' instruction.
1631 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1632 Register output = out.AsRegister<Register>();
1633 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1634 Label done, nan;
1635
1636 __ movl(output, Immediate(kPrimIntMax));
1637 // temp = int-to-double(output)
1638 __ cvtsi2sd(temp, output);
1639 // if input >= temp goto done
1640 __ comisd(input, temp);
1641 __ j(kAboveEqual, &done);
1642 // if input == NaN goto nan
1643 __ j(kUnordered, &nan);
1644 // output = double-to-int-truncate(input)
1645 __ cvttsd2si(output, input);
1646 __ jmp(&done);
1647 __ Bind(&nan);
1648 // output = 0
1649 __ xorl(output, output);
1650 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001651 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001652 }
Roland Levillain946e1432014-11-11 17:35:19 +00001653
1654 default:
1655 LOG(FATAL) << "Unexpected type conversion from " << input_type
1656 << " to " << result_type;
1657 }
1658 break;
1659
Roland Levillaindff1f282014-11-05 14:15:05 +00001660 case Primitive::kPrimLong:
1661 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001662 case Primitive::kPrimBoolean:
1663 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001664 case Primitive::kPrimByte:
1665 case Primitive::kPrimShort:
1666 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001667 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001668 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001669 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1670 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001671 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001672 __ cdq();
1673 break;
1674
1675 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001676 // Processing a Dex `float-to-long' instruction.
1677 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001678 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1679 break;
1680
Roland Levillaindff1f282014-11-05 14:15:05 +00001681 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001682 // Processing a Dex `double-to-long' instruction.
1683 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1684 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001685 break;
1686
1687 default:
1688 LOG(FATAL) << "Unexpected type conversion from " << input_type
1689 << " to " << result_type;
1690 }
1691 break;
1692
Roland Levillain981e4542014-11-14 11:47:14 +00001693 case Primitive::kPrimChar:
1694 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001695 case Primitive::kPrimBoolean:
1696 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001697 case Primitive::kPrimByte:
1698 case Primitive::kPrimShort:
1699 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001700 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1701 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001702 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001703 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001704 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001705 } else {
1706 DCHECK(in.GetConstant()->IsIntConstant());
1707 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001708 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001709 }
1710 break;
1711
1712 default:
1713 LOG(FATAL) << "Unexpected type conversion from " << input_type
1714 << " to " << result_type;
1715 }
1716 break;
1717
Roland Levillaindff1f282014-11-05 14:15:05 +00001718 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001720 case Primitive::kPrimBoolean:
1721 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001722 case Primitive::kPrimByte:
1723 case Primitive::kPrimShort:
1724 case Primitive::kPrimInt:
1725 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001726 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001727 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001728 break;
1729
Roland Levillain6d0e4832014-11-27 18:31:21 +00001730 case Primitive::kPrimLong: {
1731 // Processing a Dex `long-to-float' instruction.
1732 Register low = in.AsRegisterPairLow<Register>();
1733 Register high = in.AsRegisterPairHigh<Register>();
1734 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1735 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1736 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1737
1738 // Operations use doubles for precision reasons (each 32-bit
1739 // half of a long fits in the 53-bit mantissa of a double,
1740 // but not in the 24-bit mantissa of a float). This is
1741 // especially important for the low bits. The result is
1742 // eventually converted to float.
1743
1744 // low = low - 2^31 (to prevent bit 31 of `low` to be
1745 // interpreted as a sign bit)
1746 __ subl(low, Immediate(0x80000000));
1747 // temp = int-to-double(high)
1748 __ cvtsi2sd(temp, high);
1749 // temp = temp * 2^32
1750 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1751 __ mulsd(temp, constant);
1752 // result = int-to-double(low)
1753 __ cvtsi2sd(result, low);
1754 // result = result + 2^31 (restore the original value of `low`)
1755 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1756 __ addsd(result, constant);
1757 // result = result + temp
1758 __ addsd(result, temp);
1759 // result = double-to-float(result)
1760 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001761 // Restore low.
1762 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001763 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) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
1784 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001785 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001786 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001787 break;
1788
Roland Levillain647b9ed2014-11-27 12:06:00 +00001789 case Primitive::kPrimLong: {
1790 // Processing a Dex `long-to-double' instruction.
1791 Register low = in.AsRegisterPairLow<Register>();
1792 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1794 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1795 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001796
Roland Levillain647b9ed2014-11-27 12:06:00 +00001797 // low = low - 2^31 (to prevent bit 31 of `low` to be
1798 // interpreted as a sign bit)
1799 __ subl(low, Immediate(0x80000000));
1800 // temp = int-to-double(high)
1801 __ cvtsi2sd(temp, high);
1802 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001803 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001804 __ mulsd(temp, constant);
1805 // result = int-to-double(low)
1806 __ cvtsi2sd(result, low);
1807 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001808 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001809 __ addsd(result, constant);
1810 // result = result + temp
1811 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001812 // Restore low.
1813 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001814 break;
1815 }
1816
Roland Levillaincff13742014-11-17 14:32:17 +00001817 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001818 // Processing a Dex `float-to-double' instruction.
1819 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001820 break;
1821
1822 default:
1823 LOG(FATAL) << "Unexpected type conversion from " << input_type
1824 << " to " << result_type;
1825 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 break;
1827
1828 default:
1829 LOG(FATAL) << "Unexpected type conversion from " << input_type
1830 << " to " << result_type;
1831 }
1832}
1833
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001834void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001837 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001838 case Primitive::kPrimInt: {
1839 locations->SetInAt(0, Location::RequiresRegister());
1840 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1841 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1842 break;
1843 }
1844
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001846 locations->SetInAt(0, Location::RequiresRegister());
1847 locations->SetInAt(1, Location::Any());
1848 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849 break;
1850 }
1851
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 case Primitive::kPrimFloat:
1853 case Primitive::kPrimDouble: {
1854 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001855 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001860 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001861 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1862 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001863 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001864}
1865
1866void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1867 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868 Location first = locations->InAt(0);
1869 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001870 Location out = locations->Out();
1871
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001872 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001874 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001875 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1876 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1877 } else {
1878 __ leal(out.AsRegister<Register>(), Address(
1879 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1880 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001882 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1883 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1884 __ addl(out.AsRegister<Register>(), Immediate(value));
1885 } else {
1886 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1887 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001888 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001889 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001890 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001891 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001892 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 }
1894
1895 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001896 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1898 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001899 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1901 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001902 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001903 } else {
1904 DCHECK(second.IsConstant()) << second;
1905 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1906 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1907 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001908 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 break;
1910 }
1911
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001912 case Primitive::kPrimFloat: {
1913 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001914 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001915 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001917 }
1918
1919 case Primitive::kPrimDouble: {
1920 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001922 }
1923 break;
1924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001926 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001928 }
1929}
1930
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001931void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001932 LocationSummary* locations =
1933 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001934 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001935 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001936 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001937 locations->SetInAt(0, Location::RequiresRegister());
1938 locations->SetInAt(1, Location::Any());
1939 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 break;
1941 }
Calin Juravle11351682014-10-23 15:38:15 +01001942 case Primitive::kPrimFloat:
1943 case Primitive::kPrimDouble: {
1944 locations->SetInAt(0, Location::RequiresFpuRegister());
1945 locations->SetInAt(1, Location::RequiresFpuRegister());
1946 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 break;
Calin Juravle11351682014-10-23 15:38:15 +01001948 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001950 default:
Calin Juravle11351682014-10-23 15:38:15 +01001951 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001952 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001953}
1954
1955void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1956 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001957 Location first = locations->InAt(0);
1958 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001959 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001960 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001962 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001965 __ subl(first.AsRegister<Register>(),
1966 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001967 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001969 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001970 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971 }
1972
1973 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001974 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001975 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1976 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001977 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01001978 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001979 __ sbbl(first.AsRegisterPairHigh<Register>(),
1980 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001981 } else {
1982 DCHECK(second.IsConstant()) << second;
1983 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1984 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1985 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001987 break;
1988 }
1989
Calin Juravle11351682014-10-23 15:38:15 +01001990 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001991 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
Calin Juravle11351682014-10-23 15:38:15 +01001993 }
1994
1995 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001997 break;
1998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002000 default:
Calin Juravle11351682014-10-23 15:38:15 +01002001 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002002 }
2003}
2004
Calin Juravle34bacdf2014-10-07 20:23:36 +01002005void LocationsBuilderX86::VisitMul(HMul* mul) {
2006 LocationSummary* locations =
2007 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2008 switch (mul->GetResultType()) {
2009 case Primitive::kPrimInt:
2010 locations->SetInAt(0, Location::RequiresRegister());
2011 locations->SetInAt(1, Location::Any());
2012 locations->SetOut(Location::SameAsFirstInput());
2013 break;
2014 case Primitive::kPrimLong: {
2015 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002016 locations->SetInAt(1, Location::Any());
2017 locations->SetOut(Location::SameAsFirstInput());
2018 // Needed for imul on 32bits with 64bits output.
2019 locations->AddTemp(Location::RegisterLocation(EAX));
2020 locations->AddTemp(Location::RegisterLocation(EDX));
2021 break;
2022 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002023 case Primitive::kPrimFloat:
2024 case Primitive::kPrimDouble: {
2025 locations->SetInAt(0, Location::RequiresFpuRegister());
2026 locations->SetInAt(1, Location::RequiresFpuRegister());
2027 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002028 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002029 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002030
2031 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002032 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033 }
2034}
2035
2036void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2037 LocationSummary* locations = mul->GetLocations();
2038 Location first = locations->InAt(0);
2039 Location second = locations->InAt(1);
2040 DCHECK(first.Equals(locations->Out()));
2041
2042 switch (mul->GetResultType()) {
2043 case Primitive::kPrimInt: {
2044 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002046 } else if (second.IsConstant()) {
2047 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002049 } else {
2050 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002052 }
2053 break;
2054 }
2055
2056 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002057 Register in1_hi = first.AsRegisterPairHigh<Register>();
2058 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002059 Register eax = locations->GetTemp(0).AsRegister<Register>();
2060 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002061
2062 DCHECK_EQ(EAX, eax);
2063 DCHECK_EQ(EDX, edx);
2064
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002065 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002066 // output: in1
2067 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2068 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2069 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002070 if (second.IsConstant()) {
2071 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002072
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002073 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2074 int32_t low_value = Low32Bits(value);
2075 int32_t high_value = High32Bits(value);
2076 Immediate low(low_value);
2077 Immediate high(high_value);
2078
2079 __ movl(eax, high);
2080 // eax <- in1.lo * in2.hi
2081 __ imull(eax, in1_lo);
2082 // in1.hi <- in1.hi * in2.lo
2083 __ imull(in1_hi, low);
2084 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2085 __ addl(in1_hi, eax);
2086 // move in2_lo to eax to prepare for double precision
2087 __ movl(eax, low);
2088 // edx:eax <- in1.lo * in2.lo
2089 __ mull(in1_lo);
2090 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2091 __ addl(in1_hi, edx);
2092 // in1.lo <- (in1.lo * in2.lo)[31:0];
2093 __ movl(in1_lo, eax);
2094 } else if (second.IsRegisterPair()) {
2095 Register in2_hi = second.AsRegisterPairHigh<Register>();
2096 Register in2_lo = second.AsRegisterPairLow<Register>();
2097
2098 __ movl(eax, in2_hi);
2099 // eax <- in1.lo * in2.hi
2100 __ imull(eax, in1_lo);
2101 // in1.hi <- in1.hi * in2.lo
2102 __ imull(in1_hi, in2_lo);
2103 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2104 __ addl(in1_hi, eax);
2105 // move in1_lo to eax to prepare for double precision
2106 __ movl(eax, in1_lo);
2107 // edx:eax <- in1.lo * in2.lo
2108 __ mull(in2_lo);
2109 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2110 __ addl(in1_hi, edx);
2111 // in1.lo <- (in1.lo * in2.lo)[31:0];
2112 __ movl(in1_lo, eax);
2113 } else {
2114 DCHECK(second.IsDoubleStackSlot()) << second;
2115 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2116 Address in2_lo(ESP, second.GetStackIndex());
2117
2118 __ movl(eax, in2_hi);
2119 // eax <- in1.lo * in2.hi
2120 __ imull(eax, in1_lo);
2121 // in1.hi <- in1.hi * in2.lo
2122 __ imull(in1_hi, in2_lo);
2123 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2124 __ addl(in1_hi, eax);
2125 // move in1_lo to eax to prepare for double precision
2126 __ movl(eax, in1_lo);
2127 // edx:eax <- in1.lo * in2.lo
2128 __ mull(in2_lo);
2129 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2130 __ addl(in1_hi, edx);
2131 // in1.lo <- (in1.lo * in2.lo)[31:0];
2132 __ movl(in1_lo, eax);
2133 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134
2135 break;
2136 }
2137
Calin Juravleb5bfa962014-10-21 18:02:24 +01002138 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002141 }
2142
2143 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002145 break;
2146 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147
2148 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002149 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002150 }
2151}
2152
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002153void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2154 uint32_t stack_adjustment, bool is_float) {
2155 if (source.IsStackSlot()) {
2156 DCHECK(is_float);
2157 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2158 } else if (source.IsDoubleStackSlot()) {
2159 DCHECK(!is_float);
2160 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2161 } else {
2162 // Write the value to the temporary location on the stack and load to FP stack.
2163 if (is_float) {
2164 Location stack_temp = Location::StackSlot(temp_offset);
2165 codegen_->Move32(stack_temp, source);
2166 __ flds(Address(ESP, temp_offset));
2167 } else {
2168 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2169 codegen_->Move64(stack_temp, source);
2170 __ fldl(Address(ESP, temp_offset));
2171 }
2172 }
2173}
2174
2175void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2176 Primitive::Type type = rem->GetResultType();
2177 bool is_float = type == Primitive::kPrimFloat;
2178 size_t elem_size = Primitive::ComponentSize(type);
2179 LocationSummary* locations = rem->GetLocations();
2180 Location first = locations->InAt(0);
2181 Location second = locations->InAt(1);
2182 Location out = locations->Out();
2183
2184 // Create stack space for 2 elements.
2185 // TODO: enhance register allocator to ask for stack temporaries.
2186 __ subl(ESP, Immediate(2 * elem_size));
2187
2188 // Load the values to the FP stack in reverse order, using temporaries if needed.
2189 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2190 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2191
2192 // Loop doing FPREM until we stabilize.
2193 Label retry;
2194 __ Bind(&retry);
2195 __ fprem();
2196
2197 // Move FP status to AX.
2198 __ fstsw();
2199
2200 // And see if the argument reduction is complete. This is signaled by the
2201 // C2 FPU flag bit set to 0.
2202 __ andl(EAX, Immediate(kC2ConditionMask));
2203 __ j(kNotEqual, &retry);
2204
2205 // We have settled on the final value. Retrieve it into an XMM register.
2206 // Store FP top of stack to real stack.
2207 if (is_float) {
2208 __ fsts(Address(ESP, 0));
2209 } else {
2210 __ fstl(Address(ESP, 0));
2211 }
2212
2213 // Pop the 2 items from the FP stack.
2214 __ fucompp();
2215
2216 // Load the value from the stack into an XMM register.
2217 DCHECK(out.IsFpuRegister()) << out;
2218 if (is_float) {
2219 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2220 } else {
2221 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2222 }
2223
2224 // And remove the temporary stack space we allocated.
2225 __ addl(ESP, Immediate(2 * elem_size));
2226}
2227
Calin Juravlebacfec32014-11-14 15:54:36 +00002228void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2229 DCHECK(instruction->IsDiv() || instruction->IsRem());
2230
2231 LocationSummary* locations = instruction->GetLocations();
2232 Location out = locations->Out();
2233 Location first = locations->InAt(0);
2234 Location second = locations->InAt(1);
2235 bool is_div = instruction->IsDiv();
2236
2237 switch (instruction->GetResultType()) {
2238 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002239 Register second_reg = second.AsRegister<Register>();
2240 DCHECK_EQ(EAX, first.AsRegister<Register>());
2241 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002242
2243 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002244 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2245 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002246 codegen_->AddSlowPath(slow_path);
2247
2248 // 0x80000000/-1 triggers an arithmetic exception!
2249 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2250 // it's safe to just use negl instead of more complex comparisons.
2251
2252 __ cmpl(second_reg, Immediate(-1));
2253 __ j(kEqual, slow_path->GetEntryLabel());
2254
2255 // edx:eax <- sign-extended of eax
2256 __ cdq();
2257 // eax = quotient, edx = remainder
2258 __ idivl(second_reg);
2259
2260 __ Bind(slow_path->GetExitLabel());
2261 break;
2262 }
2263
2264 case Primitive::kPrimLong: {
2265 InvokeRuntimeCallingConvention calling_convention;
2266 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2267 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2268 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2269 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2270 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2271 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2272
2273 if (is_div) {
2274 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2275 } else {
2276 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2277 }
2278 uint32_t dex_pc = is_div
2279 ? instruction->AsDiv()->GetDexPc()
2280 : instruction->AsRem()->GetDexPc();
2281 codegen_->RecordPcInfo(instruction, dex_pc);
2282
2283 break;
2284 }
2285
2286 default:
2287 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2288 }
2289}
2290
Calin Juravle7c4954d2014-10-28 16:57:40 +00002291void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002292 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002293 ? LocationSummary::kCall
2294 : LocationSummary::kNoCall;
2295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2296
Calin Juravle7c4954d2014-10-28 16:57:40 +00002297 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002298 case Primitive::kPrimInt: {
2299 locations->SetInAt(0, Location::RegisterLocation(EAX));
2300 locations->SetInAt(1, Location::RequiresRegister());
2301 locations->SetOut(Location::SameAsFirstInput());
2302 // Intel uses edx:eax as the dividend.
2303 locations->AddTemp(Location::RegisterLocation(EDX));
2304 break;
2305 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002306 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002307 InvokeRuntimeCallingConvention calling_convention;
2308 locations->SetInAt(0, Location::RegisterPairLocation(
2309 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2310 locations->SetInAt(1, Location::RegisterPairLocation(
2311 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2312 // Runtime helper puts the result in EAX, EDX.
2313 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002314 break;
2315 }
2316 case Primitive::kPrimFloat:
2317 case Primitive::kPrimDouble: {
2318 locations->SetInAt(0, Location::RequiresFpuRegister());
2319 locations->SetInAt(1, Location::RequiresFpuRegister());
2320 locations->SetOut(Location::SameAsFirstInput());
2321 break;
2322 }
2323
2324 default:
2325 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2330 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002331 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002332 Location first = locations->InAt(0);
2333 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002334
2335 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002336 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002337 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002338 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002339 break;
2340 }
2341
2342 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002343 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002344 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002345 break;
2346 }
2347
2348 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002349 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002351 break;
2352 }
2353
2354 default:
2355 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2356 }
2357}
2358
Calin Juravlebacfec32014-11-14 15:54:36 +00002359void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002360 Primitive::Type type = rem->GetResultType();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002361 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2362 ? LocationSummary::kCall
2363 : LocationSummary::kNoCall;
2364 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002365
Calin Juravled2ec87d2014-12-08 14:24:46 +00002366 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002367 case Primitive::kPrimInt: {
2368 locations->SetInAt(0, Location::RegisterLocation(EAX));
2369 locations->SetInAt(1, Location::RequiresRegister());
2370 locations->SetOut(Location::RegisterLocation(EDX));
2371 break;
2372 }
2373 case Primitive::kPrimLong: {
2374 InvokeRuntimeCallingConvention calling_convention;
2375 locations->SetInAt(0, Location::RegisterPairLocation(
2376 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2377 locations->SetInAt(1, Location::RegisterPairLocation(
2378 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2379 // Runtime helper puts the result in EAX, EDX.
2380 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2381 break;
2382 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002383 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002384 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002385 locations->SetInAt(0, Location::Any());
2386 locations->SetInAt(1, Location::Any());
2387 locations->SetOut(Location::RequiresFpuRegister());
2388 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 break;
2390 }
2391
2392 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002393 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002394 }
2395}
2396
2397void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2398 Primitive::Type type = rem->GetResultType();
2399 switch (type) {
2400 case Primitive::kPrimInt:
2401 case Primitive::kPrimLong: {
2402 GenerateDivRemIntegral(rem);
2403 break;
2404 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002405 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002406 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002407 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002408 break;
2409 }
2410 default:
2411 LOG(FATAL) << "Unexpected rem type " << type;
2412 }
2413}
2414
Calin Juravled0d48522014-11-04 16:40:20 +00002415void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2416 LocationSummary* locations =
2417 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002418 switch (instruction->GetType()) {
2419 case Primitive::kPrimInt: {
2420 locations->SetInAt(0, Location::Any());
2421 break;
2422 }
2423 case Primitive::kPrimLong: {
2424 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2425 if (!instruction->IsConstant()) {
2426 locations->AddTemp(Location::RequiresRegister());
2427 }
2428 break;
2429 }
2430 default:
2431 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2432 }
Calin Juravled0d48522014-11-04 16:40:20 +00002433 if (instruction->HasUses()) {
2434 locations->SetOut(Location::SameAsFirstInput());
2435 }
2436}
2437
2438void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2439 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2440 codegen_->AddSlowPath(slow_path);
2441
2442 LocationSummary* locations = instruction->GetLocations();
2443 Location value = locations->InAt(0);
2444
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002445 switch (instruction->GetType()) {
2446 case Primitive::kPrimInt: {
2447 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002449 __ j(kEqual, slow_path->GetEntryLabel());
2450 } else if (value.IsStackSlot()) {
2451 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2452 __ j(kEqual, slow_path->GetEntryLabel());
2453 } else {
2454 DCHECK(value.IsConstant()) << value;
2455 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2456 __ jmp(slow_path->GetEntryLabel());
2457 }
2458 }
2459 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002460 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002461 case Primitive::kPrimLong: {
2462 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002463 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002464 __ movl(temp, value.AsRegisterPairLow<Register>());
2465 __ orl(temp, value.AsRegisterPairHigh<Register>());
2466 __ j(kEqual, slow_path->GetEntryLabel());
2467 } else {
2468 DCHECK(value.IsConstant()) << value;
2469 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2470 __ jmp(slow_path->GetEntryLabel());
2471 }
2472 }
2473 break;
2474 }
2475 default:
2476 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002477 }
Calin Juravled0d48522014-11-04 16:40:20 +00002478}
2479
Calin Juravle9aec02f2014-11-18 23:06:35 +00002480void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2481 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2482
2483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2485
2486 switch (op->GetResultType()) {
2487 case Primitive::kPrimInt: {
2488 locations->SetInAt(0, Location::RequiresRegister());
2489 // The shift count needs to be in CL.
2490 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2491 locations->SetOut(Location::SameAsFirstInput());
2492 break;
2493 }
2494 case Primitive::kPrimLong: {
2495 locations->SetInAt(0, Location::RequiresRegister());
2496 // The shift count needs to be in CL.
2497 locations->SetInAt(1, Location::RegisterLocation(ECX));
2498 locations->SetOut(Location::SameAsFirstInput());
2499 break;
2500 }
2501 default:
2502 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2503 }
2504}
2505
2506void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2507 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2508
2509 LocationSummary* locations = op->GetLocations();
2510 Location first = locations->InAt(0);
2511 Location second = locations->InAt(1);
2512 DCHECK(first.Equals(locations->Out()));
2513
2514 switch (op->GetResultType()) {
2515 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002517 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002519 DCHECK_EQ(ECX, second_reg);
2520 if (op->IsShl()) {
2521 __ shll(first_reg, second_reg);
2522 } else if (op->IsShr()) {
2523 __ sarl(first_reg, second_reg);
2524 } else {
2525 __ shrl(first_reg, second_reg);
2526 }
2527 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002528 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002529 if (op->IsShl()) {
2530 __ shll(first_reg, imm);
2531 } else if (op->IsShr()) {
2532 __ sarl(first_reg, imm);
2533 } else {
2534 __ shrl(first_reg, imm);
2535 }
2536 }
2537 break;
2538 }
2539 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002541 DCHECK_EQ(ECX, second_reg);
2542 if (op->IsShl()) {
2543 GenerateShlLong(first, second_reg);
2544 } else if (op->IsShr()) {
2545 GenerateShrLong(first, second_reg);
2546 } else {
2547 GenerateUShrLong(first, second_reg);
2548 }
2549 break;
2550 }
2551 default:
2552 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2553 }
2554}
2555
2556void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2557 Label done;
2558 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2559 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2560 __ testl(shifter, Immediate(32));
2561 __ j(kEqual, &done);
2562 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2563 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2564 __ Bind(&done);
2565}
2566
2567void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2568 Label done;
2569 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2570 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2571 __ testl(shifter, Immediate(32));
2572 __ j(kEqual, &done);
2573 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2574 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2575 __ Bind(&done);
2576}
2577
2578void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2579 Label done;
2580 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2581 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2582 __ testl(shifter, Immediate(32));
2583 __ j(kEqual, &done);
2584 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2585 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2586 __ Bind(&done);
2587}
2588
2589void LocationsBuilderX86::VisitShl(HShl* shl) {
2590 HandleShift(shl);
2591}
2592
2593void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2594 HandleShift(shl);
2595}
2596
2597void LocationsBuilderX86::VisitShr(HShr* shr) {
2598 HandleShift(shr);
2599}
2600
2601void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2602 HandleShift(shr);
2603}
2604
2605void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2606 HandleShift(ushr);
2607}
2608
2609void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2610 HandleShift(ushr);
2611}
2612
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002613void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002614 LocationSummary* locations =
2615 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002616 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002617 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002618 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2619 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002620}
2621
2622void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2623 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002625 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002626
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002627 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002628
Nicolas Geoffray39468442014-09-02 15:17:15 +01002629 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002630 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002631}
2632
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002633void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2636 locations->SetOut(Location::RegisterLocation(EAX));
2637 InvokeRuntimeCallingConvention calling_convention;
2638 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002639 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2640 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002641}
2642
2643void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2644 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002645 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002646 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2647
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002648 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002649
2650 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2651 DCHECK(!codegen_->IsLeafMethod());
2652}
2653
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002654void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002655 LocationSummary* locations =
2656 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002657 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2658 if (location.IsStackSlot()) {
2659 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2660 } else if (location.IsDoubleStackSlot()) {
2661 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002662 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002663 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002664}
2665
2666void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002667 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002668}
2669
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002670void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002671 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002672 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002673 locations->SetInAt(0, Location::RequiresRegister());
2674 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002675}
2676
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002677void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2678 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002679 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002680 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002681 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002682 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002683 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002685 break;
2686
2687 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002688 __ notl(out.AsRegisterPairLow<Register>());
2689 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002690 break;
2691
2692 default:
2693 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2694 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002695}
2696
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002697void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002700 switch (compare->InputAt(0)->GetType()) {
2701 case Primitive::kPrimLong: {
2702 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002703 locations->SetInAt(1, Location::Any());
2704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2705 break;
2706 }
2707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble: {
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
2710 locations->SetInAt(1, Location::RequiresFpuRegister());
2711 locations->SetOut(Location::RequiresRegister());
2712 break;
2713 }
2714 default:
2715 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2716 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002717}
2718
2719void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002720 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002722 Location left = locations->InAt(0);
2723 Location right = locations->InAt(1);
2724
2725 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002726 switch (compare->InputAt(0)->GetType()) {
2727 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002728 Register left_low = left.AsRegisterPairLow<Register>();
2729 Register left_high = left.AsRegisterPairHigh<Register>();
2730 int32_t val_low = 0;
2731 int32_t val_high = 0;
2732 bool right_is_const = false;
2733
2734 if (right.IsConstant()) {
2735 DCHECK(right.GetConstant()->IsLongConstant());
2736 right_is_const = true;
2737 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2738 val_low = Low32Bits(val);
2739 val_high = High32Bits(val);
2740 }
2741
Calin Juravleddb7df22014-11-25 20:56:51 +00002742 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002743 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002744 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002745 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002746 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002747 DCHECK(right_is_const) << right;
2748 if (val_high == 0) {
2749 __ testl(left_high, left_high);
2750 } else {
2751 __ cmpl(left_high, Immediate(val_high));
2752 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002753 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002754 __ j(kLess, &less); // Signed compare.
2755 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002756 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002757 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002758 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002759 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002760 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002761 DCHECK(right_is_const) << right;
2762 if (val_low == 0) {
2763 __ testl(left_low, left_low);
2764 } else {
2765 __ cmpl(left_low, Immediate(val_low));
2766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002768 break;
2769 }
2770 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002772 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2773 break;
2774 }
2775 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002777 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002778 break;
2779 }
2780 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002781 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002782 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002783 __ movl(out, Immediate(0));
2784 __ j(kEqual, &done);
2785 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2786
2787 __ Bind(&greater);
2788 __ movl(out, Immediate(1));
2789 __ jmp(&done);
2790
2791 __ Bind(&less);
2792 __ movl(out, Immediate(-1));
2793
2794 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002795}
2796
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002797void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002800 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2801 locations->SetInAt(i, Location::Any());
2802 }
2803 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002804}
2805
2806void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002807 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002808 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002809}
2810
Calin Juravle52c48962014-12-16 17:02:57 +00002811void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2812 /*
2813 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2814 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2815 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2816 */
2817 switch (kind) {
2818 case MemBarrierKind::kAnyAny: {
2819 __ mfence();
2820 break;
2821 }
2822 case MemBarrierKind::kAnyStore:
2823 case MemBarrierKind::kLoadAny:
2824 case MemBarrierKind::kStoreStore: {
2825 // nop
2826 break;
2827 }
2828 default:
2829 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002830 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002831}
2832
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002833
Mark Mendell09ed1a32015-03-25 08:30:06 -04002834void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
2835 Register temp) {
2836 // TODO: Implement all kinds of calls:
2837 // 1) boot -> boot
2838 // 2) app -> boot
2839 // 3) app -> app
2840 //
2841 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2842 // temp = method;
2843 LoadCurrentMethod(temp);
2844 if (!invoke->IsRecursive()) {
2845 // temp = temp->dex_cache_resolved_methods_;
2846 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2847 // temp = temp[index_in_cache]
2848 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
2849 // (temp + offset_of_quick_compiled_code)()
2850 __ call(Address(
2851 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
2852 } else {
2853 __ call(GetFrameEntryLabel());
2854 }
2855
2856 DCHECK(!IsLeafMethod());
2857 RecordPcInfo(invoke, invoke->GetDexPc());
2858}
2859
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002860void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 Label is_null;
2862 __ testl(value, value);
2863 __ j(kEqual, &is_null);
2864 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2865 __ movl(temp, object);
2866 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002867 __ movb(Address(temp, card, TIMES_1, 0),
2868 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002869 __ Bind(&is_null);
2870}
2871
Calin Juravle52c48962014-12-16 17:02:57 +00002872void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2873 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002876 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002877
2878 // The output overlaps in case of long: we don't want the low move to overwrite
2879 // the object's location.
2880 locations->SetOut(Location::RequiresRegister(),
2881 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
2882 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002883
2884 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2885 // Long values can be loaded atomically into an XMM using movsd.
2886 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2887 // and then copy the XMM into the output 32bits at a time).
2888 locations->AddTemp(Location::RequiresFpuRegister());
2889 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002890}
2891
Calin Juravle52c48962014-12-16 17:02:57 +00002892void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2893 const FieldInfo& field_info) {
2894 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002895
Calin Juravle52c48962014-12-16 17:02:57 +00002896 LocationSummary* locations = instruction->GetLocations();
2897 Register base = locations->InAt(0).AsRegister<Register>();
2898 Location out = locations->Out();
2899 bool is_volatile = field_info.IsVolatile();
2900 Primitive::Type field_type = field_info.GetFieldType();
2901 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2902
2903 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002904 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002905 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002906 break;
2907 }
2908
2909 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002910 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002911 break;
2912 }
2913
2914 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002915 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002916 break;
2917 }
2918
2919 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002920 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002921 break;
2922 }
2923
2924 case Primitive::kPrimInt:
2925 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002926 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002927 break;
2928 }
2929
2930 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002931 if (is_volatile) {
2932 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2933 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002934 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002935 __ movd(out.AsRegisterPairLow<Register>(), temp);
2936 __ psrlq(temp, Immediate(32));
2937 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2938 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002939 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002940 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002941 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002942 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2943 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002944 break;
2945 }
2946
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002947 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002948 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002949 break;
2950 }
2951
2952 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002953 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002954 break;
2955 }
2956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002957 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002958 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002959 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002960 }
Calin Juravle52c48962014-12-16 17:02:57 +00002961
Calin Juravle77520bc2015-01-12 18:45:46 +00002962 // Longs are handled in the switch.
2963 if (field_type != Primitive::kPrimLong) {
2964 codegen_->MaybeRecordImplicitNullCheck(instruction);
2965 }
2966
Calin Juravle52c48962014-12-16 17:02:57 +00002967 if (is_volatile) {
2968 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2969 }
2970}
2971
2972void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2973 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2974
2975 LocationSummary* locations =
2976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2977 locations->SetInAt(0, Location::RequiresRegister());
2978 bool is_volatile = field_info.IsVolatile();
2979 Primitive::Type field_type = field_info.GetFieldType();
2980 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2981 || (field_type == Primitive::kPrimByte);
2982
2983 // The register allocator does not support multiple
2984 // inputs that die at entry with one in a specific register.
2985 if (is_byte_type) {
2986 // Ensure the value is in a byte register.
2987 locations->SetInAt(1, Location::RegisterLocation(EAX));
2988 } else {
2989 locations->SetInAt(1, Location::RequiresRegister());
2990 }
2991 // Temporary registers for the write barrier.
2992 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2993 locations->AddTemp(Location::RequiresRegister());
2994 // Ensure the card is in a byte register.
2995 locations->AddTemp(Location::RegisterLocation(ECX));
2996 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2997 // 64bits value can be atomically written to an address with movsd and an XMM register.
2998 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2999 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3000 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3001 // isolated cases when we need this it isn't worth adding the extra complexity.
3002 locations->AddTemp(Location::RequiresFpuRegister());
3003 locations->AddTemp(Location::RequiresFpuRegister());
3004 }
3005}
3006
3007void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3008 const FieldInfo& field_info) {
3009 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3010
3011 LocationSummary* locations = instruction->GetLocations();
3012 Register base = locations->InAt(0).AsRegister<Register>();
3013 Location value = locations->InAt(1);
3014 bool is_volatile = field_info.IsVolatile();
3015 Primitive::Type field_type = field_info.GetFieldType();
3016 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3017
3018 if (is_volatile) {
3019 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3020 }
3021
3022 switch (field_type) {
3023 case Primitive::kPrimBoolean:
3024 case Primitive::kPrimByte: {
3025 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3026 break;
3027 }
3028
3029 case Primitive::kPrimShort:
3030 case Primitive::kPrimChar: {
3031 __ movw(Address(base, offset), value.AsRegister<Register>());
3032 break;
3033 }
3034
3035 case Primitive::kPrimInt:
3036 case Primitive::kPrimNot: {
3037 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003038 break;
3039 }
3040
3041 case Primitive::kPrimLong: {
3042 if (is_volatile) {
3043 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3044 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3045 __ movd(temp1, value.AsRegisterPairLow<Register>());
3046 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3047 __ punpckldq(temp1, temp2);
3048 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003049 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003050 } else {
3051 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003052 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003053 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3054 }
3055 break;
3056 }
3057
3058 case Primitive::kPrimFloat: {
3059 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3060 break;
3061 }
3062
3063 case Primitive::kPrimDouble: {
3064 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3065 break;
3066 }
3067
3068 case Primitive::kPrimVoid:
3069 LOG(FATAL) << "Unreachable type " << field_type;
3070 UNREACHABLE();
3071 }
3072
Calin Juravle77520bc2015-01-12 18:45:46 +00003073 // Longs are handled in the switch.
3074 if (field_type != Primitive::kPrimLong) {
3075 codegen_->MaybeRecordImplicitNullCheck(instruction);
3076 }
3077
3078 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3079 Register temp = locations->GetTemp(0).AsRegister<Register>();
3080 Register card = locations->GetTemp(1).AsRegister<Register>();
3081 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3082 }
3083
Calin Juravle52c48962014-12-16 17:02:57 +00003084 if (is_volatile) {
3085 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3086 }
3087}
3088
3089void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3090 HandleFieldGet(instruction, instruction->GetFieldInfo());
3091}
3092
3093void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3094 HandleFieldGet(instruction, instruction->GetFieldInfo());
3095}
3096
3097void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3098 HandleFieldSet(instruction, instruction->GetFieldInfo());
3099}
3100
3101void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3102 HandleFieldSet(instruction, instruction->GetFieldInfo());
3103}
3104
3105void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3106 HandleFieldSet(instruction, instruction->GetFieldInfo());
3107}
3108
3109void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3110 HandleFieldSet(instruction, instruction->GetFieldInfo());
3111}
3112
3113void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3114 HandleFieldGet(instruction, instruction->GetFieldInfo());
3115}
3116
3117void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3118 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003119}
3120
3121void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003122 LocationSummary* locations =
3123 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003124 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3125 ? Location::RequiresRegister()
3126 : Location::Any();
3127 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003128 if (instruction->HasUses()) {
3129 locations->SetOut(Location::SameAsFirstInput());
3130 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003131}
3132
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003133void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003134 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3135 return;
3136 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003137 LocationSummary* locations = instruction->GetLocations();
3138 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003139
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003140 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3141 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3142}
3143
3144void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003145 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146 codegen_->AddSlowPath(slow_path);
3147
3148 LocationSummary* locations = instruction->GetLocations();
3149 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003150
3151 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003152 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003153 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003154 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003155 } else {
3156 DCHECK(obj.IsConstant()) << obj;
3157 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3158 __ jmp(slow_path->GetEntryLabel());
3159 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160 }
3161 __ j(kEqual, slow_path->GetEntryLabel());
3162}
3163
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003164void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3165 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3166 GenerateImplicitNullCheck(instruction);
3167 } else {
3168 GenerateExplicitNullCheck(instruction);
3169 }
3170}
3171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003172void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003173 LocationSummary* locations =
3174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003175 locations->SetInAt(0, Location::RequiresRegister());
3176 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003177 // The output overlaps in case of long: we don't want the low move to overwrite
3178 // the array's location.
3179 locations->SetOut(Location::RequiresRegister(),
3180 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3181 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003182}
3183
3184void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3185 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003187 Location index = locations->InAt(1);
3188
Calin Juravle77520bc2015-01-12 18:45:46 +00003189 Primitive::Type type = instruction->GetType();
3190 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 case Primitive::kPrimBoolean: {
3192 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003194 if (index.IsConstant()) {
3195 __ movzxb(out, Address(obj,
3196 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3197 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003198 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003199 }
3200 break;
3201 }
3202
3203 case Primitive::kPrimByte: {
3204 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003206 if (index.IsConstant()) {
3207 __ movsxb(out, Address(obj,
3208 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3209 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003211 }
3212 break;
3213 }
3214
3215 case Primitive::kPrimShort: {
3216 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003218 if (index.IsConstant()) {
3219 __ movsxw(out, Address(obj,
3220 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3221 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223 }
3224 break;
3225 }
3226
3227 case Primitive::kPrimChar: {
3228 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003230 if (index.IsConstant()) {
3231 __ movzxw(out, Address(obj,
3232 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3233 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003235 }
3236 break;
3237 }
3238
3239 case Primitive::kPrimInt:
3240 case Primitive::kPrimNot: {
3241 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243 if (index.IsConstant()) {
3244 __ movl(out, Address(obj,
3245 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3246 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 }
3249 break;
3250 }
3251
3252 case Primitive::kPrimLong: {
3253 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003254 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003255 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003256 if (index.IsConstant()) {
3257 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003258 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003259 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003260 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003261 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003262 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003264 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003265 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003266 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003267 }
3268 break;
3269 }
3270
Mark Mendell7c8d0092015-01-26 11:21:33 -05003271 case Primitive::kPrimFloat: {
3272 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3273 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3274 if (index.IsConstant()) {
3275 __ movss(out, Address(obj,
3276 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3277 } else {
3278 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3279 }
3280 break;
3281 }
3282
3283 case Primitive::kPrimDouble: {
3284 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3285 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3286 if (index.IsConstant()) {
3287 __ movsd(out, Address(obj,
3288 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3289 } else {
3290 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3291 }
3292 break;
3293 }
3294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003296 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003297 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003298 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003299
3300 if (type != Primitive::kPrimLong) {
3301 codegen_->MaybeRecordImplicitNullCheck(instruction);
3302 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003303}
3304
3305void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003306 // This location builder might end up asking to up to four registers, which is
3307 // not currently possible for baseline. The situation in which we need four
3308 // registers cannot be met by baseline though, because it has not run any
3309 // optimization.
3310
Nicolas Geoffray39468442014-09-02 15:17:15 +01003311 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003312 bool needs_write_barrier =
3313 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3314
Mark Mendell5f874182015-03-04 15:42:45 -05003315 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003316
Nicolas Geoffray39468442014-09-02 15:17:15 +01003317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3318 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003319 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003320
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003321 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003322 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003323 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3324 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3325 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003326 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003327 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3328 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003329 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003330 // In case of a byte operation, the register allocator does not support multiple
3331 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003334 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003335 // Ensure the value is in a byte register.
3336 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003338 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003340 // Temporary registers for the write barrier.
3341 if (needs_write_barrier) {
3342 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003343 // Ensure the card is in a byte register.
3344 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003345 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003346 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347}
3348
3349void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3350 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003352 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003353 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003354 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003355 bool needs_runtime_call = locations->WillCall();
3356 bool needs_write_barrier =
3357 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003358
3359 switch (value_type) {
3360 case Primitive::kPrimBoolean:
3361 case Primitive::kPrimByte: {
3362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003363 if (index.IsConstant()) {
3364 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003365 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003366 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003367 } else {
3368 __ movb(Address(obj, offset),
3369 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3370 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003371 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003372 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003374 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003375 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003377 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3378 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003381 break;
3382 }
3383
3384 case Primitive::kPrimShort:
3385 case Primitive::kPrimChar: {
3386 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003387 if (index.IsConstant()) {
3388 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003389 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003390 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003391 } else {
3392 __ movw(Address(obj, offset),
3393 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003395 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003396 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3398 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003399 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003400 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003401 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3402 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003403 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 break;
3406 }
3407
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003408 case Primitive::kPrimInt:
3409 case Primitive::kPrimNot: {
3410 if (!needs_runtime_call) {
3411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3412 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003413 size_t offset =
3414 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003415 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003416 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003417 } else {
3418 DCHECK(value.IsConstant()) << value;
3419 __ movl(Address(obj, offset),
3420 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3421 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003422 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003423 DCHECK(index.IsRegister()) << index;
3424 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3426 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003427 } else {
3428 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003430 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3431 }
3432 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003434
3435 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 Register temp = locations->GetTemp(0).AsRegister<Register>();
3437 Register card = locations->GetTemp(1).AsRegister<Register>();
3438 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003441 DCHECK_EQ(value_type, Primitive::kPrimNot);
3442 DCHECK(!codegen_->IsLeafMethod());
3443 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3444 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003445 }
3446 break;
3447 }
3448
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003449 case Primitive::kPrimLong: {
3450 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451 if (index.IsConstant()) {
3452 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003453 if (value.IsRegisterPair()) {
3454 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003455 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003456 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003457 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003458 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003459 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3460 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003461 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003462 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003465 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003467 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003470 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003471 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003472 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003473 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003475 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003477 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003478 Immediate(High32Bits(val)));
3479 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 }
3481 break;
3482 }
3483
Mark Mendell7c8d0092015-01-26 11:21:33 -05003484 case Primitive::kPrimFloat: {
3485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3486 DCHECK(value.IsFpuRegister());
3487 if (index.IsConstant()) {
3488 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3489 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3490 } else {
3491 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3492 value.AsFpuRegister<XmmRegister>());
3493 }
3494 break;
3495 }
3496
3497 case Primitive::kPrimDouble: {
3498 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3499 DCHECK(value.IsFpuRegister());
3500 if (index.IsConstant()) {
3501 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3502 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3503 } else {
3504 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3505 value.AsFpuRegister<XmmRegister>());
3506 }
3507 break;
3508 }
3509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003510 case Primitive::kPrimVoid:
3511 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003512 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003513 }
3514}
3515
3516void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003518 locations->SetInAt(0, Location::RequiresRegister());
3519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003520 instruction->SetLocations(locations);
3521}
3522
3523void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3524 LocationSummary* locations = instruction->GetLocations();
3525 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003526 Register obj = locations->InAt(0).AsRegister<Register>();
3527 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003530}
3531
3532void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003533 LocationSummary* locations =
3534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003535 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003537 if (instruction->HasUses()) {
3538 locations->SetOut(Location::SameAsFirstInput());
3539 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003540}
3541
3542void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3543 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003544 Location index_loc = locations->InAt(0);
3545 Location length_loc = locations->InAt(1);
3546 SlowPathCodeX86* slow_path =
3547 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003548 codegen_->AddSlowPath(slow_path);
3549
Mark Mendellf60c90b2015-03-04 15:12:59 -05003550 Register length = length_loc.AsRegister<Register>();
3551 if (index_loc.IsConstant()) {
3552 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3553 __ cmpl(length, Immediate(value));
3554 } else {
3555 __ cmpl(length, index_loc.AsRegister<Register>());
3556 }
3557 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558}
3559
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003560void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3561 temp->SetLocations(nullptr);
3562}
3563
3564void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3565 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003567}
3568
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003569void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003570 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003571 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003572}
3573
3574void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003575 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3576}
3577
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003578void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3579 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3580}
3581
3582void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003583 HBasicBlock* block = instruction->GetBlock();
3584 if (block->GetLoopInformation() != nullptr) {
3585 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3586 // The back edge will generate the suspend check.
3587 return;
3588 }
3589 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3590 // The goto will generate the suspend check.
3591 return;
3592 }
3593 GenerateSuspendCheck(instruction, nullptr);
3594}
3595
3596void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3597 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003598 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003599 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003600 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003601 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003602 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003603 if (successor == nullptr) {
3604 __ j(kNotEqual, slow_path->GetEntryLabel());
3605 __ Bind(slow_path->GetReturnLabel());
3606 } else {
3607 __ j(kEqual, codegen_->GetLabelOf(successor));
3608 __ jmp(slow_path->GetEntryLabel());
3609 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003610}
3611
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003612X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3613 return codegen_->GetAssembler();
3614}
3615
Mark Mendell7c8d0092015-01-26 11:21:33 -05003616void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003617 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003618 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003619 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003620 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003621 __ movl(temp_reg, Address(ESP, src + stack_offset));
3622 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3623}
3624
3625void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3626 ScratchRegisterScope ensure_scratch(
3627 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3628 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3629 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3630 __ movl(temp_reg, Address(ESP, src + stack_offset));
3631 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3632 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3633 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003634}
3635
3636void ParallelMoveResolverX86::EmitMove(size_t index) {
3637 MoveOperands* move = moves_.Get(index);
3638 Location source = move->GetSource();
3639 Location destination = move->GetDestination();
3640
3641 if (source.IsRegister()) {
3642 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003644 } else {
3645 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003647 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003648 } else if (source.IsFpuRegister()) {
3649 if (destination.IsFpuRegister()) {
3650 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3651 } else if (destination.IsStackSlot()) {
3652 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3653 } else {
3654 DCHECK(destination.IsDoubleStackSlot());
3655 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3656 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003657 } else if (source.IsStackSlot()) {
3658 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003660 } else if (destination.IsFpuRegister()) {
3661 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003662 } else {
3663 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003664 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3665 }
3666 } else if (source.IsDoubleStackSlot()) {
3667 if (destination.IsFpuRegister()) {
3668 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3669 } else {
3670 DCHECK(destination.IsDoubleStackSlot()) << destination;
3671 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003672 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003673 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003674 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003675 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003676 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003677 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003678 if (value == 0) {
3679 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3680 } else {
3681 __ movl(destination.AsRegister<Register>(), Immediate(value));
3682 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003683 } else {
3684 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003685 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003686 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003687 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003688 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003689 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003690 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003691 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003692 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3693 if (value == 0) {
3694 // Easy handling of 0.0.
3695 __ xorps(dest, dest);
3696 } else {
3697 ScratchRegisterScope ensure_scratch(
3698 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3699 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3700 __ movl(temp, Immediate(value));
3701 __ movd(dest, temp);
3702 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003703 } else {
3704 DCHECK(destination.IsStackSlot()) << destination;
3705 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3706 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003707 } else if (constant->IsLongConstant()) {
3708 int64_t value = constant->AsLongConstant()->GetValue();
3709 int32_t low_value = Low32Bits(value);
3710 int32_t high_value = High32Bits(value);
3711 Immediate low(low_value);
3712 Immediate high(high_value);
3713 if (destination.IsDoubleStackSlot()) {
3714 __ movl(Address(ESP, destination.GetStackIndex()), low);
3715 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3716 } else {
3717 __ movl(destination.AsRegisterPairLow<Register>(), low);
3718 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3719 }
3720 } else {
3721 DCHECK(constant->IsDoubleConstant());
3722 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003723 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003724 int32_t low_value = Low32Bits(value);
3725 int32_t high_value = High32Bits(value);
3726 Immediate low(low_value);
3727 Immediate high(high_value);
3728 if (destination.IsFpuRegister()) {
3729 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3730 if (value == 0) {
3731 // Easy handling of 0.0.
3732 __ xorpd(dest, dest);
3733 } else {
3734 __ pushl(high);
3735 __ pushl(low);
3736 __ movsd(dest, Address(ESP, 0));
3737 __ addl(ESP, Immediate(8));
3738 }
3739 } else {
3740 DCHECK(destination.IsDoubleStackSlot()) << destination;
3741 __ movl(Address(ESP, destination.GetStackIndex()), low);
3742 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3743 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003744 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003745 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003746 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003747 }
3748}
3749
3750void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003751 Register suggested_scratch = reg == EAX ? EBX : EAX;
3752 ScratchRegisterScope ensure_scratch(
3753 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3754
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003755 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3756 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3757 __ movl(Address(ESP, mem + stack_offset), reg);
3758 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3759}
3760
Mark Mendell7c8d0092015-01-26 11:21:33 -05003761void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3762 ScratchRegisterScope ensure_scratch(
3763 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3764
3765 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3766 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3767 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3768 __ movss(Address(ESP, mem + stack_offset), reg);
3769 __ movd(reg, temp_reg);
3770}
3771
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003772void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3773 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003774 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3775
3776 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003777 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003778 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3779
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003780 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3781 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3782 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3783 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3784 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3785 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3786}
3787
3788void ParallelMoveResolverX86::EmitSwap(size_t index) {
3789 MoveOperands* move = moves_.Get(index);
3790 Location source = move->GetSource();
3791 Location destination = move->GetDestination();
3792
3793 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003795 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003797 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003799 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3800 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003801 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3802 // Use XOR Swap algorithm to avoid a temporary.
3803 DCHECK_NE(source.reg(), destination.reg());
3804 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3805 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3806 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3807 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3808 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3809 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3810 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003811 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
3812 // Take advantage of the 16 bytes in the XMM register.
3813 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
3814 Address stack(ESP, destination.GetStackIndex());
3815 // Load the double into the high doubleword.
3816 __ movhpd(reg, stack);
3817
3818 // Store the low double into the destination.
3819 __ movsd(stack, reg);
3820
3821 // Move the high double to the low double.
3822 __ psrldq(reg, Immediate(8));
3823 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
3824 // Take advantage of the 16 bytes in the XMM register.
3825 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
3826 Address stack(ESP, source.GetStackIndex());
3827 // Load the double into the high doubleword.
3828 __ movhpd(reg, stack);
3829
3830 // Store the low double into the destination.
3831 __ movsd(stack, reg);
3832
3833 // Move the high double to the low double.
3834 __ psrldq(reg, Immediate(8));
3835 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
3836 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3837 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003838 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003839 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003840 }
3841}
3842
3843void ParallelMoveResolverX86::SpillScratch(int reg) {
3844 __ pushl(static_cast<Register>(reg));
3845}
3846
3847void ParallelMoveResolverX86::RestoreScratch(int reg) {
3848 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003849}
3850
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003851void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003852 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3853 ? LocationSummary::kCallOnSlowPath
3854 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003855 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003856 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003857 locations->SetOut(Location::RequiresRegister());
3858}
3859
3860void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003861 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003862 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003863 DCHECK(!cls->CanCallRuntime());
3864 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003865 codegen_->LoadCurrentMethod(out);
3866 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3867 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003868 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003869 codegen_->LoadCurrentMethod(out);
3870 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3871 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003872
3873 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3874 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3875 codegen_->AddSlowPath(slow_path);
3876 __ testl(out, out);
3877 __ j(kEqual, slow_path->GetEntryLabel());
3878 if (cls->MustGenerateClinitCheck()) {
3879 GenerateClassInitializationCheck(slow_path, out);
3880 } else {
3881 __ Bind(slow_path->GetExitLabel());
3882 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003883 }
3884}
3885
3886void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3887 LocationSummary* locations =
3888 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3889 locations->SetInAt(0, Location::RequiresRegister());
3890 if (check->HasUses()) {
3891 locations->SetOut(Location::SameAsFirstInput());
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003896 // We assume the class to not be null.
3897 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3898 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003899 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003900 GenerateClassInitializationCheck(slow_path,
3901 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003902}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003903
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003904void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3905 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003906 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3907 Immediate(mirror::Class::kStatusInitialized));
3908 __ j(kLess, slow_path->GetEntryLabel());
3909 __ Bind(slow_path->GetExitLabel());
3910 // No need for memory fence, thanks to the X86 memory model.
3911}
3912
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003913void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3914 LocationSummary* locations =
3915 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3916 locations->SetOut(Location::RequiresRegister());
3917}
3918
3919void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3920 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3921 codegen_->AddSlowPath(slow_path);
3922
Roland Levillain271ab9c2014-11-27 15:23:57 +00003923 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003924 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003925 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3926 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003927 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3928 __ testl(out, out);
3929 __ j(kEqual, slow_path->GetEntryLabel());
3930 __ Bind(slow_path->GetExitLabel());
3931}
3932
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003933void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3934 LocationSummary* locations =
3935 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3936 locations->SetOut(Location::RequiresRegister());
3937}
3938
3939void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3940 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003942 __ fs()->movl(address, Immediate(0));
3943}
3944
3945void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3946 LocationSummary* locations =
3947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3948 InvokeRuntimeCallingConvention calling_convention;
3949 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3950}
3951
3952void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3953 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3954 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3955}
3956
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003957void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003958 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3959 ? LocationSummary::kNoCall
3960 : LocationSummary::kCallOnSlowPath;
3961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3962 locations->SetInAt(0, Location::RequiresRegister());
3963 locations->SetInAt(1, Location::Any());
3964 locations->SetOut(Location::RequiresRegister());
3965}
3966
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003967void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003968 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003969 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003970 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003971 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003972 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3973 Label done, zero;
3974 SlowPathCodeX86* slow_path = nullptr;
3975
3976 // Return 0 if `obj` is null.
3977 // TODO: avoid this check if we know obj is not null.
3978 __ testl(obj, obj);
3979 __ j(kEqual, &zero);
3980 __ movl(out, Address(obj, class_offset));
3981 // Compare the class of `obj` with `cls`.
3982 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003983 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003984 } else {
3985 DCHECK(cls.IsStackSlot()) << cls;
3986 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3987 }
3988
3989 if (instruction->IsClassFinal()) {
3990 // Classes must be equal for the instanceof to succeed.
3991 __ j(kNotEqual, &zero);
3992 __ movl(out, Immediate(1));
3993 __ jmp(&done);
3994 } else {
3995 // If the classes are not equal, we go into a slow path.
3996 DCHECK(locations->OnlyCallsOnSlowPath());
3997 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003998 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003999 codegen_->AddSlowPath(slow_path);
4000 __ j(kNotEqual, slow_path->GetEntryLabel());
4001 __ movl(out, Immediate(1));
4002 __ jmp(&done);
4003 }
4004 __ Bind(&zero);
4005 __ movl(out, Immediate(0));
4006 if (slow_path != nullptr) {
4007 __ Bind(slow_path->GetExitLabel());
4008 }
4009 __ Bind(&done);
4010}
4011
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004012void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4013 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4014 instruction, LocationSummary::kCallOnSlowPath);
4015 locations->SetInAt(0, Location::RequiresRegister());
4016 locations->SetInAt(1, Location::Any());
4017 locations->AddTemp(Location::RequiresRegister());
4018}
4019
4020void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4021 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004023 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004025 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4026 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4027 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4028 codegen_->AddSlowPath(slow_path);
4029
4030 // TODO: avoid this check if we know obj is not null.
4031 __ testl(obj, obj);
4032 __ j(kEqual, slow_path->GetExitLabel());
4033 __ movl(temp, Address(obj, class_offset));
4034
4035 // Compare the class of `obj` with `cls`.
4036 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004037 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004038 } else {
4039 DCHECK(cls.IsStackSlot()) << cls;
4040 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4041 }
4042
4043 __ j(kNotEqual, slow_path->GetEntryLabel());
4044 __ Bind(slow_path->GetExitLabel());
4045}
4046
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004047void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4048 LocationSummary* locations =
4049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4050 InvokeRuntimeCallingConvention calling_convention;
4051 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4052}
4053
4054void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4055 __ fs()->call(Address::Absolute(instruction->IsEnter()
4056 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4057 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4058 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4059}
4060
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004061void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4062void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4063void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4064
4065void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4066 LocationSummary* locations =
4067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4068 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4069 || instruction->GetResultType() == Primitive::kPrimLong);
4070 locations->SetInAt(0, Location::RequiresRegister());
4071 locations->SetInAt(1, Location::Any());
4072 locations->SetOut(Location::SameAsFirstInput());
4073}
4074
4075void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4076 HandleBitwiseOperation(instruction);
4077}
4078
4079void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4080 HandleBitwiseOperation(instruction);
4081}
4082
4083void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4084 HandleBitwiseOperation(instruction);
4085}
4086
4087void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4088 LocationSummary* locations = instruction->GetLocations();
4089 Location first = locations->InAt(0);
4090 Location second = locations->InAt(1);
4091 DCHECK(first.Equals(locations->Out()));
4092
4093 if (instruction->GetResultType() == Primitive::kPrimInt) {
4094 if (second.IsRegister()) {
4095 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004096 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004097 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004099 } else {
4100 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004101 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004102 }
4103 } else if (second.IsConstant()) {
4104 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004105 __ andl(first.AsRegister<Register>(),
4106 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004107 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004108 __ orl(first.AsRegister<Register>(),
4109 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004110 } else {
4111 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004112 __ xorl(first.AsRegister<Register>(),
4113 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004114 }
4115 } else {
4116 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004117 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004118 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004119 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004120 } else {
4121 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004122 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004123 }
4124 }
4125 } else {
4126 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4127 if (second.IsRegisterPair()) {
4128 if (instruction->IsAnd()) {
4129 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4130 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4131 } else if (instruction->IsOr()) {
4132 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4133 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4134 } else {
4135 DCHECK(instruction->IsXor());
4136 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4137 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4138 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004139 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004140 if (instruction->IsAnd()) {
4141 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4142 __ andl(first.AsRegisterPairHigh<Register>(),
4143 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4144 } else if (instruction->IsOr()) {
4145 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4146 __ orl(first.AsRegisterPairHigh<Register>(),
4147 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4148 } else {
4149 DCHECK(instruction->IsXor());
4150 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4151 __ xorl(first.AsRegisterPairHigh<Register>(),
4152 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4153 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004154 } else {
4155 DCHECK(second.IsConstant()) << second;
4156 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004157 int32_t low_value = Low32Bits(value);
4158 int32_t high_value = High32Bits(value);
4159 Immediate low(low_value);
4160 Immediate high(high_value);
4161 Register first_low = first.AsRegisterPairLow<Register>();
4162 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004163 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004164 if (low_value == 0) {
4165 __ xorl(first_low, first_low);
4166 } else if (low_value != -1) {
4167 __ andl(first_low, low);
4168 }
4169 if (high_value == 0) {
4170 __ xorl(first_high, first_high);
4171 } else if (high_value != -1) {
4172 __ andl(first_high, high);
4173 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004174 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004175 if (low_value != 0) {
4176 __ orl(first_low, low);
4177 }
4178 if (high_value != 0) {
4179 __ orl(first_high, high);
4180 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004181 } else {
4182 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004183 if (low_value != 0) {
4184 __ xorl(first_low, low);
4185 }
4186 if (high_value != 0) {
4187 __ xorl(first_high, high);
4188 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004189 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004190 }
4191 }
4192}
4193
Calin Juravleb1498f62015-02-16 13:13:29 +00004194void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4195 // Nothing to do, this should be removed during prepare for register allocator.
4196 UNUSED(instruction);
4197 LOG(FATAL) << "Unreachable";
4198}
4199
4200void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4201 // Nothing to do, this should be removed during prepare for register allocator.
4202 UNUSED(instruction);
4203 LOG(FATAL) << "Unreachable";
4204}
4205
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004206} // namespace x86
4207} // namespace art