blob: 92b62e2c840012eac7d8569e237a54136485a684 [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)));
Mingyao Yang2be48692015-03-31 17:03:08 -070054 RecordPcInfo(codegen, 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)));
Mingyao Yang2be48692015-03-31 17:03:08 -070069 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000070 }
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)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700118 RecordPcInfo(codegen, 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)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700139 RecordPcInfo(codegen, 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
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700297class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
298 public:
299 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
300 : instruction_(instruction) {}
301
302 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
303 __ Bind(GetEntryLabel());
304 SaveLiveRegisters(codegen, instruction_->GetLocations());
305 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
306 // No need to restore live registers.
307 DCHECK(instruction_->IsDeoptimize());
308 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
309 uint32_t dex_pc = deoptimize->GetDexPc();
310 codegen->RecordPcInfo(instruction_, dex_pc, this);
311 }
312
313 private:
314 HInstruction* const instruction_;
315 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
316};
317
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100318#undef __
319#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
320
Dave Allison20dfc792014-06-16 20:44:29 -0700321inline Condition X86Condition(IfCondition cond) {
322 switch (cond) {
323 case kCondEQ: return kEqual;
324 case kCondNE: return kNotEqual;
325 case kCondLT: return kLess;
326 case kCondLE: return kLessEqual;
327 case kCondGT: return kGreater;
328 case kCondGE: return kGreaterEqual;
329 default:
330 LOG(FATAL) << "Unknown if condition";
331 }
332 return kEqual;
333}
334
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
336 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
337}
338
339void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
340 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
341}
342
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100343size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
344 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
345 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100346}
347
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100348size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
349 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
350 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100351}
352
Mark Mendell7c8d0092015-01-26 11:21:33 -0500353size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
354 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
355 return GetFloatingPointSpillSlotSize();
356}
357
358size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
359 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
360 return GetFloatingPointSpillSlotSize();
361}
362
Mark Mendellfb8d2792015-03-31 22:16:59 -0400363CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
364 const X86InstructionSetFeatures& isa_features,
365 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500366 : CodeGenerator(graph,
367 kNumberOfCpuRegisters,
368 kNumberOfXmmRegisters,
369 kNumberOfRegisterPairs,
370 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
371 arraysize(kCoreCalleeSaves))
372 | (1 << kFakeReturnRegister),
373 0,
374 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100375 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100377 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400378 move_resolver_(graph->GetArena(), this),
379 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000380 // Use a fake return address register to mimic Quick.
381 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Mark Mendell5f874182015-03-04 15:42:45 -0500430void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
Mark Mendell5f874182015-03-04 15:42:45 -0500437 if (is_baseline) {
438 blocked_core_registers_[EBP] = true;
439 blocked_core_registers_[ESI] = true;
440 blocked_core_registers_[EDI] = true;
441 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000463 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000464 bool skip_overflow_check =
465 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000467
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Mark Mendell5f874182015-03-04 15:42:45 -0500473 if (HasEmptyFrame()) {
474 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000475 }
Mark Mendell5f874182015-03-04 15:42:45 -0500476
477 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
478 Register reg = kCoreCalleeSaves[i];
479 if (allocated_registers_.ContainsCoreRegister(reg)) {
480 __ pushl(reg);
481 }
482 }
483
484 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
485 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000486}
487
488void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500489 if (HasEmptyFrame()) {
490 return;
491 }
492
493 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
494
495 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
496 Register reg = kCoreCalleeSaves[i];
497 if (allocated_registers_.ContainsCoreRegister(reg)) {
498 __ popl(reg);
499 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000500 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000501}
502
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100503void CodeGeneratorX86::Bind(HBasicBlock* block) {
504 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000505}
506
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100507void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000508 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100509 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000510}
511
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100512Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
513 switch (load->GetType()) {
514 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100516 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517
518 case Primitive::kPrimInt:
519 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100520 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522
523 case Primitive::kPrimBoolean:
524 case Primitive::kPrimByte:
525 case Primitive::kPrimChar:
526 case Primitive::kPrimShort:
527 case Primitive::kPrimVoid:
528 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700529 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 }
531
532 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700533 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534}
535
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
537 switch (type) {
538 case Primitive::kPrimBoolean:
539 case Primitive::kPrimByte:
540 case Primitive::kPrimChar:
541 case Primitive::kPrimShort:
542 case Primitive::kPrimInt:
543 case Primitive::kPrimNot: {
544 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000545 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100546 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100547 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000549 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100550 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100551 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000553 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 uint32_t index = gp_index_;
555 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000556 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100558 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
559 calling_convention.GetRegisterPairAt(index));
560 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000562 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
563 }
564 }
565
566 case Primitive::kPrimFloat: {
567 uint32_t index = fp_index_++;
568 stack_index_++;
569 if (index < calling_convention.GetNumberOfFpuRegisters()) {
570 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
571 } else {
572 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
573 }
574 }
575
576 case Primitive::kPrimDouble: {
577 uint32_t index = fp_index_++;
578 stack_index_ += 2;
579 if (index < calling_convention.GetNumberOfFpuRegisters()) {
580 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
581 } else {
582 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 }
584 }
585
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100586 case Primitive::kPrimVoid:
587 LOG(FATAL) << "Unexpected parameter type " << type;
588 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100589 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100590 return Location();
591}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100592
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593void CodeGeneratorX86::Move32(Location destination, Location source) {
594 if (source.Equals(destination)) {
595 return;
596 }
597 if (destination.IsRegister()) {
598 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000599 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100600 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000601 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100602 } else {
603 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000604 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100605 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 } else if (destination.IsFpuRegister()) {
607 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000608 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 } else {
612 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000613 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000616 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000618 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000620 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500621 } else if (source.IsConstant()) {
622 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000623 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500624 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
626 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100627 __ pushl(Address(ESP, source.GetStackIndex()));
628 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 }
630 }
631}
632
633void CodeGeneratorX86::Move64(Location destination, Location source) {
634 if (source.Equals(destination)) {
635 return;
636 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100637 if (destination.IsRegisterPair()) {
638 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000639 EmitParallelMoves(
640 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
641 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
642 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
643 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 } else if (source.IsFpuRegister()) {
645 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100646 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000647 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
650 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
652 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500654 if (source.IsFpuRegister()) {
655 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
656 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100658 } else {
659 LOG(FATAL) << "Unimplemented";
660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100663 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000664 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100665 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100667 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000670 } else if (source.IsConstant()) {
671 HConstant* constant = source.GetConstant();
672 int64_t value;
673 if (constant->IsLongConstant()) {
674 value = constant->AsLongConstant()->GetValue();
675 } else {
676 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000677 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000678 }
679 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
680 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000682 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000683 EmitParallelMoves(
684 Location::StackSlot(source.GetStackIndex()),
685 Location::StackSlot(destination.GetStackIndex()),
686 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
687 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 }
689 }
690}
691
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100692void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000693 LocationSummary* locations = instruction->GetLocations();
694 if (locations != nullptr && locations->Out().Equals(location)) {
695 return;
696 }
697
698 if (locations != nullptr && locations->Out().IsConstant()) {
699 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000700 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
701 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000702 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000704 } else if (location.IsStackSlot()) {
705 __ movl(Address(ESP, location.GetStackIndex()), imm);
706 } else {
707 DCHECK(location.IsConstant());
708 DCHECK_EQ(location.GetConstant(), const_to_move);
709 }
710 } else if (const_to_move->IsLongConstant()) {
711 int64_t value = const_to_move->AsLongConstant()->GetValue();
712 if (location.IsRegisterPair()) {
713 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
714 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
715 } else if (location.IsDoubleStackSlot()) {
716 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000717 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
718 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000719 } else {
720 DCHECK(location.IsConstant());
721 DCHECK_EQ(location.GetConstant(), instruction);
722 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000724 } else if (instruction->IsTemporary()) {
725 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000726 if (temp_location.IsStackSlot()) {
727 Move32(location, temp_location);
728 } else {
729 DCHECK(temp_location.IsDoubleStackSlot());
730 Move64(location, temp_location);
731 }
Roland Levillain476df552014-10-09 17:51:36 +0100732 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
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:
742 Move32(location, Location::StackSlot(slot));
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:
747 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 break;
749
750 default:
751 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
752 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000753 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100754 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 switch (instruction->GetType()) {
756 case Primitive::kPrimBoolean:
757 case Primitive::kPrimByte:
758 case Primitive::kPrimChar:
759 case Primitive::kPrimShort:
760 case Primitive::kPrimInt:
761 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000763 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 break;
765
766 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000768 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 break;
770
771 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774 }
775}
776
777void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779}
780
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000781void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000782 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100783 DCHECK(!successor->IsExitBlock());
784
785 HBasicBlock* block = got->GetBlock();
786 HInstruction* previous = got->GetPrevious();
787
788 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000789 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100790 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
791 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
792 return;
793 }
794
795 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
796 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
797 }
798 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000799 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000800 }
801}
802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805}
806
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000807void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700808 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000809}
810
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700811void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
812 Label* true_target,
813 Label* false_target,
814 Label* always_true_target) {
815 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 if (cond->IsIntConstant()) {
817 // Constant condition, statically compared against 1.
818 int32_t cond_value = cond->AsIntConstant()->GetValue();
819 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700820 if (always_true_target != nullptr) {
821 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100822 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100824 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 DCHECK_EQ(cond_value, 0);
826 }
827 } else {
828 bool materialized =
829 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
830 // Moves do not affect the eflags register, so if the condition is
831 // evaluated just before the if, we don't need to evaluate it
832 // again.
833 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700834 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100835 if (materialized) {
836 if (!eflags_set) {
837 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700838 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500840 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 } else {
842 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
843 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700844 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 }
848 } else {
849 Location lhs = cond->GetLocations()->InAt(0);
850 Location rhs = cond->GetLocations()->InAt(1);
851 // LHS is guaranteed to be in a register (see
852 // LocationsBuilderX86::VisitCondition).
853 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000854 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100855 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500856 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
857 if (constant == 0) {
858 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
859 } else {
860 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
861 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000863 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700865 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700866 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700868 if (false_target != nullptr) {
869 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870 }
871}
872
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700873void LocationsBuilderX86::VisitIf(HIf* if_instr) {
874 LocationSummary* locations =
875 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
876 HInstruction* cond = if_instr->InputAt(0);
877 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
878 locations->SetInAt(0, Location::Any());
879 }
880}
881
882void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
883 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
884 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
885 Label* always_true_target = true_target;
886 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
887 if_instr->IfTrueSuccessor())) {
888 always_true_target = nullptr;
889 }
890 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
891 if_instr->IfFalseSuccessor())) {
892 false_target = nullptr;
893 }
894 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
895}
896
897void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
898 LocationSummary* locations = new (GetGraph()->GetArena())
899 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
900 HInstruction* cond = deoptimize->InputAt(0);
901 DCHECK(cond->IsCondition());
902 if (cond->AsCondition()->NeedsMaterialization()) {
903 locations->SetInAt(0, Location::Any());
904 }
905}
906
907void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
908 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
909 DeoptimizationSlowPathX86(deoptimize);
910 codegen_->AddSlowPath(slow_path);
911 Label* slow_path_entry = slow_path->GetEntryLabel();
912 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
913}
914
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000915void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000916 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000917}
918
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
920 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000921}
922
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000923void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100924 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000925}
926
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000927void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100928 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700929 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000930}
931
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100932void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100933 LocationSummary* locations =
934 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100935 switch (store->InputAt(1)->GetType()) {
936 case Primitive::kPrimBoolean:
937 case Primitive::kPrimByte:
938 case Primitive::kPrimChar:
939 case Primitive::kPrimShort:
940 case Primitive::kPrimInt:
941 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100942 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
944 break;
945
946 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
949 break;
950
951 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 }
954 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000955}
956
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000957void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700958 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000959}
960
Dave Allison20dfc792014-06-16 20:44:29 -0700961void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100962 LocationSummary* locations =
963 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100964 locations->SetInAt(0, Location::RequiresRegister());
965 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100966 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500967 // We need a byte register.
968 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100969 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000970}
971
Dave Allison20dfc792014-06-16 20:44:29 -0700972void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
973 if (comp->NeedsMaterialization()) {
974 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000975 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100976 // Clear register: setcc only sets the low byte.
977 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500978 Location lhs = locations->InAt(0);
979 Location rhs = locations->InAt(1);
980 if (rhs.IsRegister()) {
981 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
982 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800983 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500984 if (constant == 0) {
985 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
986 } else {
987 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
988 }
Dave Allison20dfc792014-06-16 20:44:29 -0700989 } else {
Mark Mendell09b84632015-02-13 17:48:38 -0500990 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700991 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000992 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100993 }
Dave Allison20dfc792014-06-16 20:44:29 -0700994}
995
996void LocationsBuilderX86::VisitEqual(HEqual* comp) {
997 VisitCondition(comp);
998}
999
1000void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1001 VisitCondition(comp);
1002}
1003
1004void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1005 VisitCondition(comp);
1006}
1007
1008void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1009 VisitCondition(comp);
1010}
1011
1012void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1013 VisitCondition(comp);
1014}
1015
1016void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1017 VisitCondition(comp);
1018}
1019
1020void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1041 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001042}
1043
1044void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001045 LocationSummary* locations =
1046 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001047 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001048}
1049
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001051 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001052 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053}
1054
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001055void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1056 LocationSummary* locations =
1057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1058 locations->SetOut(Location::ConstantLocation(constant));
1059}
1060
1061void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1062 // Will be generated at use site.
1063 UNUSED(constant);
1064}
1065
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 LocationSummary* locations =
1068 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001069 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070}
1071
1072void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1073 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001074 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075}
1076
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001077void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1078 LocationSummary* locations =
1079 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1080 locations->SetOut(Location::ConstantLocation(constant));
1081}
1082
1083void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1084 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001085 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001086}
1087
1088void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1089 LocationSummary* locations =
1090 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1091 locations->SetOut(Location::ConstantLocation(constant));
1092}
1093
1094void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1095 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001097}
1098
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001100 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101}
1102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001103void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001104 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106 __ ret();
1107}
1108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001110 LocationSummary* locations =
1111 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 switch (ret->InputAt(0)->GetType()) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimChar:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 break;
1121
1122 case Primitive::kPrimLong:
1123 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 break;
1126
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001127 case Primitive::kPrimFloat:
1128 case Primitive::kPrimDouble:
1129 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001131 break;
1132
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001134 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001136}
1137
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001138void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 if (kIsDebugBuild) {
1140 switch (ret->InputAt(0)->GetType()) {
1141 case Primitive::kPrimBoolean:
1142 case Primitive::kPrimByte:
1143 case Primitive::kPrimChar:
1144 case Primitive::kPrimShort:
1145 case Primitive::kPrimInt:
1146 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001147 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 break;
1149
1150 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1152 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 case Primitive::kPrimFloat:
1156 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001157 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 break;
1159
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001160 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 }
1163 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001164 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001165 __ ret();
1166}
1167
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001168void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001169 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001170 if (intrinsic.TryDispatch(invoke)) {
1171 return;
1172 }
1173
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001174 HandleInvoke(invoke);
1175}
1176
Mark Mendell09ed1a32015-03-25 08:30:06 -04001177static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1178 if (invoke->GetLocations()->Intrinsified()) {
1179 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1180 intrinsic.Dispatch(invoke);
1181 return true;
1182 }
1183 return false;
1184}
1185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001186void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001187 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1188 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001189 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001190
Mark Mendell09ed1a32015-03-25 08:30:06 -04001191 codegen_->GenerateStaticOrDirectCall(
1192 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193}
1194
1195void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1196 HandleInvoke(invoke);
1197}
1198
1199void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001200 LocationSummary* locations =
1201 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001202 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001203
1204 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001205 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001206 HInstruction* input = invoke->InputAt(i);
1207 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1208 }
1209
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 switch (invoke->GetType()) {
1211 case Primitive::kPrimBoolean:
1212 case Primitive::kPrimByte:
1213 case Primitive::kPrimChar:
1214 case Primitive::kPrimShort:
1215 case Primitive::kPrimInt:
1216 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001217 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 break;
1219
1220 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001221 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 break;
1223
1224 case Primitive::kPrimVoid:
1225 break;
1226
1227 case Primitive::kPrimDouble:
1228 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001229 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 break;
1231 }
1232
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001233 invoke->SetLocations(locations);
1234}
1235
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001236void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001238 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1239 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1240 LocationSummary* locations = invoke->GetLocations();
1241 Location receiver = locations->InAt(0);
1242 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1243 // temp = object->GetClass();
1244 if (receiver.IsStackSlot()) {
1245 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1246 __ movl(temp, Address(temp, class_offset));
1247 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001249 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001250 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001251 // temp = temp->GetMethodAt(method_offset);
1252 __ movl(temp, Address(temp, method_offset));
1253 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001254 __ call(Address(
1255 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001256
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001257 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001258 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001259}
1260
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001261void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1262 HandleInvoke(invoke);
1263 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001264 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001265}
1266
1267void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1268 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001269 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001270 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1271 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1272 LocationSummary* locations = invoke->GetLocations();
1273 Location receiver = locations->InAt(0);
1274 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1275
1276 // Set the hidden argument.
1277 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001279
1280 // temp = object->GetClass();
1281 if (receiver.IsStackSlot()) {
1282 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1283 __ movl(temp, Address(temp, class_offset));
1284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001288 // temp = temp->GetImtEntryAt(method_offset);
1289 __ movl(temp, Address(temp, method_offset));
1290 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001291 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001292 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001293
1294 DCHECK(!codegen_->IsLeafMethod());
1295 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1296}
1297
Roland Levillain88cb1752014-10-20 16:36:47 +01001298void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1299 LocationSummary* locations =
1300 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1301 switch (neg->GetResultType()) {
1302 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001303 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001304 locations->SetInAt(0, Location::RequiresRegister());
1305 locations->SetOut(Location::SameAsFirstInput());
1306 break;
1307
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001309 locations->SetInAt(0, Location::RequiresFpuRegister());
1310 locations->SetOut(Location::SameAsFirstInput());
1311 locations->AddTemp(Location::RequiresRegister());
1312 locations->AddTemp(Location::RequiresFpuRegister());
1313 break;
1314
Roland Levillain88cb1752014-10-20 16:36:47 +01001315 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001316 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001317 locations->SetOut(Location::SameAsFirstInput());
1318 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001319 break;
1320
1321 default:
1322 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1323 }
1324}
1325
1326void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1327 LocationSummary* locations = neg->GetLocations();
1328 Location out = locations->Out();
1329 Location in = locations->InAt(0);
1330 switch (neg->GetResultType()) {
1331 case Primitive::kPrimInt:
1332 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001333 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001334 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 break;
1336
1337 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001338 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001339 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001340 __ negl(out.AsRegisterPairLow<Register>());
1341 // Negation is similar to subtraction from zero. The least
1342 // significant byte triggers a borrow when it is different from
1343 // zero; to take it into account, add 1 to the most significant
1344 // byte if the carry flag (CF) is set to 1 after the first NEGL
1345 // operation.
1346 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1347 __ negl(out.AsRegisterPairHigh<Register>());
1348 break;
1349
Roland Levillain5368c212014-11-27 15:03:41 +00001350 case Primitive::kPrimFloat: {
1351 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001352 Register constant = locations->GetTemp(0).AsRegister<Register>();
1353 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001354 // Implement float negation with an exclusive or with value
1355 // 0x80000000 (mask for bit 31, representing the sign of a
1356 // single-precision floating-point number).
1357 __ movl(constant, Immediate(INT32_C(0x80000000)));
1358 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001360 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001361 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001362
Roland Levillain5368c212014-11-27 15:03:41 +00001363 case Primitive::kPrimDouble: {
1364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001366 // Implement double negation with an exclusive or with value
1367 // 0x8000000000000000 (mask for bit 63, representing the sign of
1368 // a double-precision floating-point number).
1369 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001371 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001372 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001373
1374 default:
1375 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1376 }
1377}
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001380 Primitive::Type result_type = conversion->GetResultType();
1381 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001382 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001383
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001384 // The float-to-long and double-to-long type conversions rely on a
1385 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001386 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001387 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1388 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001389 ? LocationSummary::kCall
1390 : LocationSummary::kNoCall;
1391 LocationSummary* locations =
1392 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1393
David Brazdilb2bd1c52015-03-25 11:17:37 +00001394 // The Java language does not allow treating boolean as an integral type but
1395 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001396
Roland Levillaindff1f282014-11-05 14:15:05 +00001397 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001398 case Primitive::kPrimByte:
1399 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001400 case Primitive::kPrimBoolean:
1401 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001402 case Primitive::kPrimShort:
1403 case Primitive::kPrimInt:
1404 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001405 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001406 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1407 // Make the output overlap to please the register allocator. This greatly simplifies
1408 // the validation of the linear scan implementation
1409 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001410 break;
1411
1412 default:
1413 LOG(FATAL) << "Unexpected type conversion from " << input_type
1414 << " to " << result_type;
1415 }
1416 break;
1417
Roland Levillain01a8d712014-11-14 16:27:39 +00001418 case Primitive::kPrimShort:
1419 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001420 case Primitive::kPrimBoolean:
1421 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001422 case Primitive::kPrimByte:
1423 case Primitive::kPrimInt:
1424 case Primitive::kPrimChar:
1425 // Processing a Dex `int-to-short' instruction.
1426 locations->SetInAt(0, Location::Any());
1427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1428 break;
1429
1430 default:
1431 LOG(FATAL) << "Unexpected type conversion from " << input_type
1432 << " to " << result_type;
1433 }
1434 break;
1435
Roland Levillain946e1432014-11-11 17:35:19 +00001436 case Primitive::kPrimInt:
1437 switch (input_type) {
1438 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001439 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001440 locations->SetInAt(0, Location::Any());
1441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1442 break;
1443
1444 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001445 // Processing a Dex `float-to-int' instruction.
1446 locations->SetInAt(0, Location::RequiresFpuRegister());
1447 locations->SetOut(Location::RequiresRegister());
1448 locations->AddTemp(Location::RequiresFpuRegister());
1449 break;
1450
Roland Levillain946e1432014-11-11 17:35:19 +00001451 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001452 // Processing a Dex `double-to-int' instruction.
1453 locations->SetInAt(0, Location::RequiresFpuRegister());
1454 locations->SetOut(Location::RequiresRegister());
1455 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001456 break;
1457
1458 default:
1459 LOG(FATAL) << "Unexpected type conversion from " << input_type
1460 << " to " << result_type;
1461 }
1462 break;
1463
Roland Levillaindff1f282014-11-05 14:15:05 +00001464 case Primitive::kPrimLong:
1465 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001466 case Primitive::kPrimBoolean:
1467 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001468 case Primitive::kPrimByte:
1469 case Primitive::kPrimShort:
1470 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001471 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001472 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 locations->SetInAt(0, Location::RegisterLocation(EAX));
1474 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1475 break;
1476
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001477 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001478 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001479 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001480 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001481 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1482 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1483
Vladimir Marko949c91f2015-01-27 10:48:44 +00001484 // The runtime helper puts the result in EAX, EDX.
1485 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001486 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001487 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001488
1489 default:
1490 LOG(FATAL) << "Unexpected type conversion from " << input_type
1491 << " to " << result_type;
1492 }
1493 break;
1494
Roland Levillain981e4542014-11-14 11:47:14 +00001495 case Primitive::kPrimChar:
1496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001497 case Primitive::kPrimBoolean:
1498 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001499 case Primitive::kPrimByte:
1500 case Primitive::kPrimShort:
1501 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001502 // Processing a Dex `int-to-char' instruction.
1503 locations->SetInAt(0, Location::Any());
1504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected type conversion from " << input_type
1509 << " to " << result_type;
1510 }
1511 break;
1512
Roland Levillaindff1f282014-11-05 14:15:05 +00001513 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001514 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001515 case Primitive::kPrimBoolean:
1516 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001517 case Primitive::kPrimByte:
1518 case Primitive::kPrimShort:
1519 case Primitive::kPrimInt:
1520 case Primitive::kPrimChar:
1521 // Processing a Dex `int-to-float' instruction.
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetOut(Location::RequiresFpuRegister());
1524 break;
1525
1526 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001527 // Processing a Dex `long-to-float' instruction.
1528 locations->SetInAt(0, Location::RequiresRegister());
1529 locations->SetOut(Location::RequiresFpuRegister());
1530 locations->AddTemp(Location::RequiresFpuRegister());
1531 locations->AddTemp(Location::RequiresFpuRegister());
1532 break;
1533
Roland Levillaincff13742014-11-17 14:32:17 +00001534 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001535 // Processing a Dex `double-to-float' instruction.
1536 locations->SetInAt(0, Location::RequiresFpuRegister());
1537 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001538 break;
1539
1540 default:
1541 LOG(FATAL) << "Unexpected type conversion from " << input_type
1542 << " to " << result_type;
1543 };
1544 break;
1545
Roland Levillaindff1f282014-11-05 14:15:05 +00001546 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001547 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001548 case Primitive::kPrimBoolean:
1549 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001550 case Primitive::kPrimByte:
1551 case Primitive::kPrimShort:
1552 case Primitive::kPrimInt:
1553 case Primitive::kPrimChar:
1554 // Processing a Dex `int-to-double' instruction.
1555 locations->SetInAt(0, Location::RequiresRegister());
1556 locations->SetOut(Location::RequiresFpuRegister());
1557 break;
1558
1559 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001560 // Processing a Dex `long-to-double' instruction.
1561 locations->SetInAt(0, Location::RequiresRegister());
1562 locations->SetOut(Location::RequiresFpuRegister());
1563 locations->AddTemp(Location::RequiresFpuRegister());
1564 locations->AddTemp(Location::RequiresFpuRegister());
1565 break;
1566
Roland Levillaincff13742014-11-17 14:32:17 +00001567 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001568 // Processing a Dex `float-to-double' instruction.
1569 locations->SetInAt(0, Location::RequiresFpuRegister());
1570 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001571 break;
1572
1573 default:
1574 LOG(FATAL) << "Unexpected type conversion from " << input_type
1575 << " to " << result_type;
1576 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 }
1583}
1584
1585void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1586 LocationSummary* locations = conversion->GetLocations();
1587 Location out = locations->Out();
1588 Location in = locations->InAt(0);
1589 Primitive::Type result_type = conversion->GetResultType();
1590 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001591 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001592 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001593 case Primitive::kPrimByte:
1594 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001595 case Primitive::kPrimBoolean:
1596 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001597 case Primitive::kPrimShort:
1598 case Primitive::kPrimInt:
1599 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001600 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001601 if (in.IsRegister()) {
1602 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001603 } else {
1604 DCHECK(in.GetConstant()->IsIntConstant());
1605 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1606 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1607 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
1614 break;
1615
Roland Levillain01a8d712014-11-14 16:27:39 +00001616 case Primitive::kPrimShort:
1617 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001618 case Primitive::kPrimBoolean:
1619 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001620 case Primitive::kPrimByte:
1621 case Primitive::kPrimInt:
1622 case Primitive::kPrimChar:
1623 // Processing a Dex `int-to-short' instruction.
1624 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001625 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001626 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001627 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001628 } else {
1629 DCHECK(in.GetConstant()->IsIntConstant());
1630 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001631 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001632 }
1633 break;
1634
1635 default:
1636 LOG(FATAL) << "Unexpected type conversion from " << input_type
1637 << " to " << result_type;
1638 }
1639 break;
1640
Roland Levillain946e1432014-11-11 17:35:19 +00001641 case Primitive::kPrimInt:
1642 switch (input_type) {
1643 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001644 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001645 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001646 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001647 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001648 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001649 } else {
1650 DCHECK(in.IsConstant());
1651 DCHECK(in.GetConstant()->IsLongConstant());
1652 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001654 }
1655 break;
1656
Roland Levillain3f8f9362014-12-02 17:45:01 +00001657 case Primitive::kPrimFloat: {
1658 // Processing a Dex `float-to-int' instruction.
1659 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1660 Register output = out.AsRegister<Register>();
1661 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1662 Label done, nan;
1663
1664 __ movl(output, Immediate(kPrimIntMax));
1665 // temp = int-to-float(output)
1666 __ cvtsi2ss(temp, output);
1667 // if input >= temp goto done
1668 __ comiss(input, temp);
1669 __ j(kAboveEqual, &done);
1670 // if input == NaN goto nan
1671 __ j(kUnordered, &nan);
1672 // output = float-to-int-truncate(input)
1673 __ cvttss2si(output, input);
1674 __ jmp(&done);
1675 __ Bind(&nan);
1676 // output = 0
1677 __ xorl(output, output);
1678 __ Bind(&done);
1679 break;
1680 }
1681
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001682 case Primitive::kPrimDouble: {
1683 // Processing a Dex `double-to-int' instruction.
1684 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1685 Register output = out.AsRegister<Register>();
1686 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1687 Label done, nan;
1688
1689 __ movl(output, Immediate(kPrimIntMax));
1690 // temp = int-to-double(output)
1691 __ cvtsi2sd(temp, output);
1692 // if input >= temp goto done
1693 __ comisd(input, temp);
1694 __ j(kAboveEqual, &done);
1695 // if input == NaN goto nan
1696 __ j(kUnordered, &nan);
1697 // output = double-to-int-truncate(input)
1698 __ cvttsd2si(output, input);
1699 __ jmp(&done);
1700 __ Bind(&nan);
1701 // output = 0
1702 __ xorl(output, output);
1703 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001704 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001705 }
Roland Levillain946e1432014-11-11 17:35:19 +00001706
1707 default:
1708 LOG(FATAL) << "Unexpected type conversion from " << input_type
1709 << " to " << result_type;
1710 }
1711 break;
1712
Roland Levillaindff1f282014-11-05 14:15:05 +00001713 case Primitive::kPrimLong:
1714 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001715 case Primitive::kPrimBoolean:
1716 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001717 case Primitive::kPrimByte:
1718 case Primitive::kPrimShort:
1719 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001720 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001721 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001722 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1723 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001725 __ cdq();
1726 break;
1727
1728 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001729 // Processing a Dex `float-to-long' instruction.
1730 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001731 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1732 break;
1733
Roland Levillaindff1f282014-11-05 14:15:05 +00001734 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001735 // Processing a Dex `double-to-long' instruction.
1736 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1737 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001738 break;
1739
1740 default:
1741 LOG(FATAL) << "Unexpected type conversion from " << input_type
1742 << " to " << result_type;
1743 }
1744 break;
1745
Roland Levillain981e4542014-11-14 11:47:14 +00001746 case Primitive::kPrimChar:
1747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001748 case Primitive::kPrimBoolean:
1749 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001750 case Primitive::kPrimByte:
1751 case Primitive::kPrimShort:
1752 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001753 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1754 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001756 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001758 } else {
1759 DCHECK(in.GetConstant()->IsIntConstant());
1760 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001762 }
1763 break;
1764
1765 default:
1766 LOG(FATAL) << "Unexpected type conversion from " << input_type
1767 << " to " << result_type;
1768 }
1769 break;
1770
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001772 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001773 case Primitive::kPrimBoolean:
1774 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001775 case Primitive::kPrimByte:
1776 case Primitive::kPrimShort:
1777 case Primitive::kPrimInt:
1778 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001779 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001781 break;
1782
Roland Levillain6d0e4832014-11-27 18:31:21 +00001783 case Primitive::kPrimLong: {
1784 // Processing a Dex `long-to-float' instruction.
1785 Register low = in.AsRegisterPairLow<Register>();
1786 Register high = in.AsRegisterPairHigh<Register>();
1787 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1788 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1789 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1790
1791 // Operations use doubles for precision reasons (each 32-bit
1792 // half of a long fits in the 53-bit mantissa of a double,
1793 // but not in the 24-bit mantissa of a float). This is
1794 // especially important for the low bits. The result is
1795 // eventually converted to float.
1796
1797 // 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
1803 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1804 __ mulsd(temp, constant);
1805 // result = int-to-double(low)
1806 __ cvtsi2sd(result, low);
1807 // result = result + 2^31 (restore the original value of `low`)
1808 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1809 __ addsd(result, constant);
1810 // result = result + temp
1811 __ addsd(result, temp);
1812 // result = double-to-float(result)
1813 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001814 // Restore low.
1815 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 break;
1817 }
1818
Roland Levillaincff13742014-11-17 14:32:17 +00001819 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001820 // Processing a Dex `double-to-float' instruction.
1821 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001822 break;
1823
1824 default:
1825 LOG(FATAL) << "Unexpected type conversion from " << input_type
1826 << " to " << result_type;
1827 };
1828 break;
1829
Roland Levillaindff1f282014-11-05 14:15:05 +00001830 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001831 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001832 case Primitive::kPrimBoolean:
1833 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001834 case Primitive::kPrimByte:
1835 case Primitive::kPrimShort:
1836 case Primitive::kPrimInt:
1837 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001838 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001839 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001840 break;
1841
Roland Levillain647b9ed2014-11-27 12:06:00 +00001842 case Primitive::kPrimLong: {
1843 // Processing a Dex `long-to-double' instruction.
1844 Register low = in.AsRegisterPairLow<Register>();
1845 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001846 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1847 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1848 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001849
Roland Levillain647b9ed2014-11-27 12:06:00 +00001850 // low = low - 2^31 (to prevent bit 31 of `low` to be
1851 // interpreted as a sign bit)
1852 __ subl(low, Immediate(0x80000000));
1853 // temp = int-to-double(high)
1854 __ cvtsi2sd(temp, high);
1855 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001856 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001857 __ mulsd(temp, constant);
1858 // result = int-to-double(low)
1859 __ cvtsi2sd(result, low);
1860 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001861 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001862 __ addsd(result, constant);
1863 // result = result + temp
1864 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001865 // Restore low.
1866 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001867 break;
1868 }
1869
Roland Levillaincff13742014-11-17 14:32:17 +00001870 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001871 // Processing a Dex `float-to-double' instruction.
1872 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001873 break;
1874
1875 default:
1876 LOG(FATAL) << "Unexpected type conversion from " << input_type
1877 << " to " << result_type;
1878 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001879 break;
1880
1881 default:
1882 LOG(FATAL) << "Unexpected type conversion from " << input_type
1883 << " to " << result_type;
1884 }
1885}
1886
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001887void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001891 case Primitive::kPrimInt: {
1892 locations->SetInAt(0, Location::RequiresRegister());
1893 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1895 break;
1896 }
1897
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001899 locations->SetInAt(0, Location::RequiresRegister());
1900 locations->SetInAt(1, Location::Any());
1901 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903 }
1904
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble: {
1907 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001908 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001913 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001914 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1915 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001916 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917}
1918
1919void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1920 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001921 Location first = locations->InAt(0);
1922 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001923 Location out = locations->Out();
1924
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001925 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001928 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1929 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1930 } else {
1931 __ leal(out.AsRegister<Register>(), Address(
1932 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1933 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001935 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1936 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1937 __ addl(out.AsRegister<Register>(), Immediate(value));
1938 } else {
1939 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1940 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001941 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001942 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001944 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 }
1947
1948 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001949 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1951 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001952 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001953 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1954 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001955 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001956 } else {
1957 DCHECK(second.IsConstant()) << second;
1958 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1959 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1960 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001961 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 break;
1963 }
1964
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 case Primitive::kPrimFloat: {
1966 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001968 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001969 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001970 }
1971
1972 case Primitive::kPrimDouble: {
1973 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001974 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 }
1976 break;
1977 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001978
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001979 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001980 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001981 }
1982}
1983
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001984void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001985 LocationSummary* locations =
1986 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001987 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001988 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001989 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001990 locations->SetInAt(0, Location::RequiresRegister());
1991 locations->SetInAt(1, Location::Any());
1992 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001993 break;
1994 }
Calin Juravle11351682014-10-23 15:38:15 +01001995 case Primitive::kPrimFloat:
1996 case Primitive::kPrimDouble: {
1997 locations->SetInAt(0, Location::RequiresFpuRegister());
1998 locations->SetInAt(1, Location::RequiresFpuRegister());
1999 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002000 break;
Calin Juravle11351682014-10-23 15:38:15 +01002001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002002
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002003 default:
Calin Juravle11351682014-10-23 15:38:15 +01002004 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002005 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002006}
2007
2008void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2009 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002010 Location first = locations->InAt(0);
2011 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002012 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002013 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002014 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002015 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002017 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002018 __ subl(first.AsRegister<Register>(),
2019 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002021 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002022 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002023 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024 }
2025
2026 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002027 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002028 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2029 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002030 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002031 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002032 __ sbbl(first.AsRegisterPairHigh<Register>(),
2033 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002034 } else {
2035 DCHECK(second.IsConstant()) << second;
2036 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2037 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2038 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002039 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040 break;
2041 }
2042
Calin Juravle11351682014-10-23 15:38:15 +01002043 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045 break;
Calin Juravle11351682014-10-23 15:38:15 +01002046 }
2047
2048 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002050 break;
2051 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002052
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002053 default:
Calin Juravle11351682014-10-23 15:38:15 +01002054 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002055 }
2056}
2057
Calin Juravle34bacdf2014-10-07 20:23:36 +01002058void LocationsBuilderX86::VisitMul(HMul* mul) {
2059 LocationSummary* locations =
2060 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2061 switch (mul->GetResultType()) {
2062 case Primitive::kPrimInt:
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::Any());
2065 locations->SetOut(Location::SameAsFirstInput());
2066 break;
2067 case Primitive::kPrimLong: {
2068 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002069 locations->SetInAt(1, Location::Any());
2070 locations->SetOut(Location::SameAsFirstInput());
2071 // Needed for imul on 32bits with 64bits output.
2072 locations->AddTemp(Location::RegisterLocation(EAX));
2073 locations->AddTemp(Location::RegisterLocation(EDX));
2074 break;
2075 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002076 case Primitive::kPrimFloat:
2077 case Primitive::kPrimDouble: {
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetInAt(1, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002081 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002082 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083
2084 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002085 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002086 }
2087}
2088
2089void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2090 LocationSummary* locations = mul->GetLocations();
2091 Location first = locations->InAt(0);
2092 Location second = locations->InAt(1);
2093 DCHECK(first.Equals(locations->Out()));
2094
2095 switch (mul->GetResultType()) {
2096 case Primitive::kPrimInt: {
2097 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002098 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002099 } else if (second.IsConstant()) {
2100 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102 } else {
2103 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002105 }
2106 break;
2107 }
2108
2109 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 Register in1_hi = first.AsRegisterPairHigh<Register>();
2111 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 Register eax = locations->GetTemp(0).AsRegister<Register>();
2113 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114
2115 DCHECK_EQ(EAX, eax);
2116 DCHECK_EQ(EDX, edx);
2117
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002118 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 // output: in1
2120 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2121 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2122 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002123 if (second.IsConstant()) {
2124 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002125
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002126 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2127 int32_t low_value = Low32Bits(value);
2128 int32_t high_value = High32Bits(value);
2129 Immediate low(low_value);
2130 Immediate high(high_value);
2131
2132 __ movl(eax, high);
2133 // eax <- in1.lo * in2.hi
2134 __ imull(eax, in1_lo);
2135 // in1.hi <- in1.hi * in2.lo
2136 __ imull(in1_hi, low);
2137 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2138 __ addl(in1_hi, eax);
2139 // move in2_lo to eax to prepare for double precision
2140 __ movl(eax, low);
2141 // edx:eax <- in1.lo * in2.lo
2142 __ mull(in1_lo);
2143 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2144 __ addl(in1_hi, edx);
2145 // in1.lo <- (in1.lo * in2.lo)[31:0];
2146 __ movl(in1_lo, eax);
2147 } else if (second.IsRegisterPair()) {
2148 Register in2_hi = second.AsRegisterPairHigh<Register>();
2149 Register in2_lo = second.AsRegisterPairLow<Register>();
2150
2151 __ movl(eax, in2_hi);
2152 // eax <- in1.lo * in2.hi
2153 __ imull(eax, in1_lo);
2154 // in1.hi <- in1.hi * in2.lo
2155 __ imull(in1_hi, in2_lo);
2156 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2157 __ addl(in1_hi, eax);
2158 // move in1_lo to eax to prepare for double precision
2159 __ movl(eax, in1_lo);
2160 // edx:eax <- in1.lo * in2.lo
2161 __ mull(in2_lo);
2162 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2163 __ addl(in1_hi, edx);
2164 // in1.lo <- (in1.lo * in2.lo)[31:0];
2165 __ movl(in1_lo, eax);
2166 } else {
2167 DCHECK(second.IsDoubleStackSlot()) << second;
2168 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2169 Address in2_lo(ESP, second.GetStackIndex());
2170
2171 __ movl(eax, in2_hi);
2172 // eax <- in1.lo * in2.hi
2173 __ imull(eax, in1_lo);
2174 // in1.hi <- in1.hi * in2.lo
2175 __ imull(in1_hi, in2_lo);
2176 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2177 __ addl(in1_hi, eax);
2178 // move in1_lo to eax to prepare for double precision
2179 __ movl(eax, in1_lo);
2180 // edx:eax <- in1.lo * in2.lo
2181 __ mull(in2_lo);
2182 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2183 __ addl(in1_hi, edx);
2184 // in1.lo <- (in1.lo * in2.lo)[31:0];
2185 __ movl(in1_lo, eax);
2186 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002187
2188 break;
2189 }
2190
Calin Juravleb5bfa962014-10-21 18:02:24 +01002191 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002192 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002193 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002194 }
2195
2196 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002197 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002198 break;
2199 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002200
2201 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002202 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002203 }
2204}
2205
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002206void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2207 uint32_t stack_adjustment, bool is_float) {
2208 if (source.IsStackSlot()) {
2209 DCHECK(is_float);
2210 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2211 } else if (source.IsDoubleStackSlot()) {
2212 DCHECK(!is_float);
2213 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2214 } else {
2215 // Write the value to the temporary location on the stack and load to FP stack.
2216 if (is_float) {
2217 Location stack_temp = Location::StackSlot(temp_offset);
2218 codegen_->Move32(stack_temp, source);
2219 __ flds(Address(ESP, temp_offset));
2220 } else {
2221 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2222 codegen_->Move64(stack_temp, source);
2223 __ fldl(Address(ESP, temp_offset));
2224 }
2225 }
2226}
2227
2228void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2229 Primitive::Type type = rem->GetResultType();
2230 bool is_float = type == Primitive::kPrimFloat;
2231 size_t elem_size = Primitive::ComponentSize(type);
2232 LocationSummary* locations = rem->GetLocations();
2233 Location first = locations->InAt(0);
2234 Location second = locations->InAt(1);
2235 Location out = locations->Out();
2236
2237 // Create stack space for 2 elements.
2238 // TODO: enhance register allocator to ask for stack temporaries.
2239 __ subl(ESP, Immediate(2 * elem_size));
2240
2241 // Load the values to the FP stack in reverse order, using temporaries if needed.
2242 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2243 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2244
2245 // Loop doing FPREM until we stabilize.
2246 Label retry;
2247 __ Bind(&retry);
2248 __ fprem();
2249
2250 // Move FP status to AX.
2251 __ fstsw();
2252
2253 // And see if the argument reduction is complete. This is signaled by the
2254 // C2 FPU flag bit set to 0.
2255 __ andl(EAX, Immediate(kC2ConditionMask));
2256 __ j(kNotEqual, &retry);
2257
2258 // We have settled on the final value. Retrieve it into an XMM register.
2259 // Store FP top of stack to real stack.
2260 if (is_float) {
2261 __ fsts(Address(ESP, 0));
2262 } else {
2263 __ fstl(Address(ESP, 0));
2264 }
2265
2266 // Pop the 2 items from the FP stack.
2267 __ fucompp();
2268
2269 // Load the value from the stack into an XMM register.
2270 DCHECK(out.IsFpuRegister()) << out;
2271 if (is_float) {
2272 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2273 } else {
2274 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2275 }
2276
2277 // And remove the temporary stack space we allocated.
2278 __ addl(ESP, Immediate(2 * elem_size));
2279}
2280
Calin Juravlebacfec32014-11-14 15:54:36 +00002281void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2282 DCHECK(instruction->IsDiv() || instruction->IsRem());
2283
2284 LocationSummary* locations = instruction->GetLocations();
2285 Location out = locations->Out();
2286 Location first = locations->InAt(0);
2287 Location second = locations->InAt(1);
2288 bool is_div = instruction->IsDiv();
2289
2290 switch (instruction->GetResultType()) {
2291 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 Register second_reg = second.AsRegister<Register>();
2293 DCHECK_EQ(EAX, first.AsRegister<Register>());
2294 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002295
2296 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002297 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2298 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002299 codegen_->AddSlowPath(slow_path);
2300
2301 // 0x80000000/-1 triggers an arithmetic exception!
2302 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2303 // it's safe to just use negl instead of more complex comparisons.
2304
2305 __ cmpl(second_reg, Immediate(-1));
2306 __ j(kEqual, slow_path->GetEntryLabel());
2307
2308 // edx:eax <- sign-extended of eax
2309 __ cdq();
2310 // eax = quotient, edx = remainder
2311 __ idivl(second_reg);
2312
2313 __ Bind(slow_path->GetExitLabel());
2314 break;
2315 }
2316
2317 case Primitive::kPrimLong: {
2318 InvokeRuntimeCallingConvention calling_convention;
2319 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2320 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2321 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2322 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2323 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2324 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2325
2326 if (is_div) {
2327 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2328 } else {
2329 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2330 }
2331 uint32_t dex_pc = is_div
2332 ? instruction->AsDiv()->GetDexPc()
2333 : instruction->AsRem()->GetDexPc();
2334 codegen_->RecordPcInfo(instruction, dex_pc);
2335
2336 break;
2337 }
2338
2339 default:
2340 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2341 }
2342}
2343
Calin Juravle7c4954d2014-10-28 16:57:40 +00002344void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002345 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002346 ? LocationSummary::kCall
2347 : LocationSummary::kNoCall;
2348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2349
Calin Juravle7c4954d2014-10-28 16:57:40 +00002350 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002351 case Primitive::kPrimInt: {
2352 locations->SetInAt(0, Location::RegisterLocation(EAX));
2353 locations->SetInAt(1, Location::RequiresRegister());
2354 locations->SetOut(Location::SameAsFirstInput());
2355 // Intel uses edx:eax as the dividend.
2356 locations->AddTemp(Location::RegisterLocation(EDX));
2357 break;
2358 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002359 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 InvokeRuntimeCallingConvention calling_convention;
2361 locations->SetInAt(0, Location::RegisterPairLocation(
2362 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2363 locations->SetInAt(1, Location::RegisterPairLocation(
2364 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2365 // Runtime helper puts the result in EAX, EDX.
2366 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002367 break;
2368 }
2369 case Primitive::kPrimFloat:
2370 case Primitive::kPrimDouble: {
2371 locations->SetInAt(0, Location::RequiresFpuRegister());
2372 locations->SetInAt(1, Location::RequiresFpuRegister());
2373 locations->SetOut(Location::SameAsFirstInput());
2374 break;
2375 }
2376
2377 default:
2378 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2379 }
2380}
2381
2382void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2383 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002384 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002385 Location first = locations->InAt(0);
2386 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002387
2388 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002389 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002390 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002391 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002392 break;
2393 }
2394
2395 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002396 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002398 break;
2399 }
2400
2401 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002402 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002403 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002404 break;
2405 }
2406
2407 default:
2408 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2409 }
2410}
2411
Calin Juravlebacfec32014-11-14 15:54:36 +00002412void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002413 Primitive::Type type = rem->GetResultType();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002414 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2415 ? LocationSummary::kCall
2416 : LocationSummary::kNoCall;
2417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002418
Calin Juravled2ec87d2014-12-08 14:24:46 +00002419 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002420 case Primitive::kPrimInt: {
2421 locations->SetInAt(0, Location::RegisterLocation(EAX));
2422 locations->SetInAt(1, Location::RequiresRegister());
2423 locations->SetOut(Location::RegisterLocation(EDX));
2424 break;
2425 }
2426 case Primitive::kPrimLong: {
2427 InvokeRuntimeCallingConvention calling_convention;
2428 locations->SetInAt(0, Location::RegisterPairLocation(
2429 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2430 locations->SetInAt(1, Location::RegisterPairLocation(
2431 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2432 // Runtime helper puts the result in EAX, EDX.
2433 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2434 break;
2435 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002436 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002437 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002438 locations->SetInAt(0, Location::Any());
2439 locations->SetInAt(1, Location::Any());
2440 locations->SetOut(Location::RequiresFpuRegister());
2441 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002442 break;
2443 }
2444
2445 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002446 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002447 }
2448}
2449
2450void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2451 Primitive::Type type = rem->GetResultType();
2452 switch (type) {
2453 case Primitive::kPrimInt:
2454 case Primitive::kPrimLong: {
2455 GenerateDivRemIntegral(rem);
2456 break;
2457 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002458 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002459 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002460 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002461 break;
2462 }
2463 default:
2464 LOG(FATAL) << "Unexpected rem type " << type;
2465 }
2466}
2467
Calin Juravled0d48522014-11-04 16:40:20 +00002468void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2469 LocationSummary* locations =
2470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002471 switch (instruction->GetType()) {
2472 case Primitive::kPrimInt: {
2473 locations->SetInAt(0, Location::Any());
2474 break;
2475 }
2476 case Primitive::kPrimLong: {
2477 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2478 if (!instruction->IsConstant()) {
2479 locations->AddTemp(Location::RequiresRegister());
2480 }
2481 break;
2482 }
2483 default:
2484 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2485 }
Calin Juravled0d48522014-11-04 16:40:20 +00002486 if (instruction->HasUses()) {
2487 locations->SetOut(Location::SameAsFirstInput());
2488 }
2489}
2490
2491void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2492 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2493 codegen_->AddSlowPath(slow_path);
2494
2495 LocationSummary* locations = instruction->GetLocations();
2496 Location value = locations->InAt(0);
2497
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002498 switch (instruction->GetType()) {
2499 case Primitive::kPrimInt: {
2500 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002502 __ j(kEqual, slow_path->GetEntryLabel());
2503 } else if (value.IsStackSlot()) {
2504 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2505 __ j(kEqual, slow_path->GetEntryLabel());
2506 } else {
2507 DCHECK(value.IsConstant()) << value;
2508 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2509 __ jmp(slow_path->GetEntryLabel());
2510 }
2511 }
2512 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002513 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002514 case Primitive::kPrimLong: {
2515 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002517 __ movl(temp, value.AsRegisterPairLow<Register>());
2518 __ orl(temp, value.AsRegisterPairHigh<Register>());
2519 __ j(kEqual, slow_path->GetEntryLabel());
2520 } else {
2521 DCHECK(value.IsConstant()) << value;
2522 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2523 __ jmp(slow_path->GetEntryLabel());
2524 }
2525 }
2526 break;
2527 }
2528 default:
2529 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002530 }
Calin Juravled0d48522014-11-04 16:40:20 +00002531}
2532
Calin Juravle9aec02f2014-11-18 23:06:35 +00002533void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2534 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2535
2536 LocationSummary* locations =
2537 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2538
2539 switch (op->GetResultType()) {
2540 case Primitive::kPrimInt: {
2541 locations->SetInAt(0, Location::RequiresRegister());
2542 // The shift count needs to be in CL.
2543 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2544 locations->SetOut(Location::SameAsFirstInput());
2545 break;
2546 }
2547 case Primitive::kPrimLong: {
2548 locations->SetInAt(0, Location::RequiresRegister());
2549 // The shift count needs to be in CL.
2550 locations->SetInAt(1, Location::RegisterLocation(ECX));
2551 locations->SetOut(Location::SameAsFirstInput());
2552 break;
2553 }
2554 default:
2555 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2556 }
2557}
2558
2559void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2560 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2561
2562 LocationSummary* locations = op->GetLocations();
2563 Location first = locations->InAt(0);
2564 Location second = locations->InAt(1);
2565 DCHECK(first.Equals(locations->Out()));
2566
2567 switch (op->GetResultType()) {
2568 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002569 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002570 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002571 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002572 DCHECK_EQ(ECX, second_reg);
2573 if (op->IsShl()) {
2574 __ shll(first_reg, second_reg);
2575 } else if (op->IsShr()) {
2576 __ sarl(first_reg, second_reg);
2577 } else {
2578 __ shrl(first_reg, second_reg);
2579 }
2580 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002581 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002582 if (op->IsShl()) {
2583 __ shll(first_reg, imm);
2584 } else if (op->IsShr()) {
2585 __ sarl(first_reg, imm);
2586 } else {
2587 __ shrl(first_reg, imm);
2588 }
2589 }
2590 break;
2591 }
2592 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002594 DCHECK_EQ(ECX, second_reg);
2595 if (op->IsShl()) {
2596 GenerateShlLong(first, second_reg);
2597 } else if (op->IsShr()) {
2598 GenerateShrLong(first, second_reg);
2599 } else {
2600 GenerateUShrLong(first, second_reg);
2601 }
2602 break;
2603 }
2604 default:
2605 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2606 }
2607}
2608
2609void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2610 Label done;
2611 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2612 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2613 __ testl(shifter, Immediate(32));
2614 __ j(kEqual, &done);
2615 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2616 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2617 __ Bind(&done);
2618}
2619
2620void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2621 Label done;
2622 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2623 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2624 __ testl(shifter, Immediate(32));
2625 __ j(kEqual, &done);
2626 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2627 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2628 __ Bind(&done);
2629}
2630
2631void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2632 Label done;
2633 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2634 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2635 __ testl(shifter, Immediate(32));
2636 __ j(kEqual, &done);
2637 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2638 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2639 __ Bind(&done);
2640}
2641
2642void LocationsBuilderX86::VisitShl(HShl* shl) {
2643 HandleShift(shl);
2644}
2645
2646void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2647 HandleShift(shl);
2648}
2649
2650void LocationsBuilderX86::VisitShr(HShr* shr) {
2651 HandleShift(shr);
2652}
2653
2654void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2655 HandleShift(shr);
2656}
2657
2658void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2659 HandleShift(ushr);
2660}
2661
2662void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2663 HandleShift(ushr);
2664}
2665
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002666void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002667 LocationSummary* locations =
2668 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002669 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002670 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002671 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2672 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002673}
2674
2675void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2676 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002677 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002678 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002679
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002680 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002681
Nicolas Geoffray39468442014-09-02 15:17:15 +01002682 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002683 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002684}
2685
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002686void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2687 LocationSummary* locations =
2688 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2689 locations->SetOut(Location::RegisterLocation(EAX));
2690 InvokeRuntimeCallingConvention calling_convention;
2691 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002692 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2693 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002694}
2695
2696void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2697 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002698 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002699 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2700
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002701 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002702
2703 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2704 DCHECK(!codegen_->IsLeafMethod());
2705}
2706
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002707void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002708 LocationSummary* locations =
2709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002710 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2711 if (location.IsStackSlot()) {
2712 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2713 } else if (location.IsDoubleStackSlot()) {
2714 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002715 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002716 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002717}
2718
2719void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002720 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721}
2722
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002723void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002724 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002725 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002728}
2729
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002730void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2731 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002732 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002733 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002734 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002735 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002736 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002737 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002738 break;
2739
2740 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002741 __ notl(out.AsRegisterPairLow<Register>());
2742 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002743 break;
2744
2745 default:
2746 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2747 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002748}
2749
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002750void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002751 LocationSummary* locations =
2752 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002753 switch (compare->InputAt(0)->GetType()) {
2754 case Primitive::kPrimLong: {
2755 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002756 locations->SetInAt(1, Location::Any());
2757 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2758 break;
2759 }
2760 case Primitive::kPrimFloat:
2761 case Primitive::kPrimDouble: {
2762 locations->SetInAt(0, Location::RequiresFpuRegister());
2763 locations->SetInAt(1, Location::RequiresFpuRegister());
2764 locations->SetOut(Location::RequiresRegister());
2765 break;
2766 }
2767 default:
2768 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2769 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002770}
2771
2772void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002773 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002775 Location left = locations->InAt(0);
2776 Location right = locations->InAt(1);
2777
2778 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002779 switch (compare->InputAt(0)->GetType()) {
2780 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002781 Register left_low = left.AsRegisterPairLow<Register>();
2782 Register left_high = left.AsRegisterPairHigh<Register>();
2783 int32_t val_low = 0;
2784 int32_t val_high = 0;
2785 bool right_is_const = false;
2786
2787 if (right.IsConstant()) {
2788 DCHECK(right.GetConstant()->IsLongConstant());
2789 right_is_const = true;
2790 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2791 val_low = Low32Bits(val);
2792 val_high = High32Bits(val);
2793 }
2794
Calin Juravleddb7df22014-11-25 20:56:51 +00002795 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002796 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002797 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002798 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002799 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002800 DCHECK(right_is_const) << right;
2801 if (val_high == 0) {
2802 __ testl(left_high, left_high);
2803 } else {
2804 __ cmpl(left_high, Immediate(val_high));
2805 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002806 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002807 __ j(kLess, &less); // Signed compare.
2808 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002809 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002810 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002811 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002812 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002813 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002814 DCHECK(right_is_const) << right;
2815 if (val_low == 0) {
2816 __ testl(left_low, left_low);
2817 } else {
2818 __ cmpl(left_low, Immediate(val_low));
2819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002820 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002821 break;
2822 }
2823 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002825 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2826 break;
2827 }
2828 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002829 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002830 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002831 break;
2832 }
2833 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002834 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002835 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002836 __ movl(out, Immediate(0));
2837 __ j(kEqual, &done);
2838 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2839
2840 __ Bind(&greater);
2841 __ movl(out, Immediate(1));
2842 __ jmp(&done);
2843
2844 __ Bind(&less);
2845 __ movl(out, Immediate(-1));
2846
2847 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002848}
2849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002850void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002853 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2854 locations->SetInAt(i, Location::Any());
2855 }
2856 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002857}
2858
2859void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002860 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002861 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002862}
2863
Calin Juravle52c48962014-12-16 17:02:57 +00002864void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2865 /*
2866 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2867 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2868 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2869 */
2870 switch (kind) {
2871 case MemBarrierKind::kAnyAny: {
2872 __ mfence();
2873 break;
2874 }
2875 case MemBarrierKind::kAnyStore:
2876 case MemBarrierKind::kLoadAny:
2877 case MemBarrierKind::kStoreStore: {
2878 // nop
2879 break;
2880 }
2881 default:
2882 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002883 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002884}
2885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002886
Mark Mendell09ed1a32015-03-25 08:30:06 -04002887void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
2888 Register temp) {
2889 // TODO: Implement all kinds of calls:
2890 // 1) boot -> boot
2891 // 2) app -> boot
2892 // 3) app -> app
2893 //
2894 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2895 // temp = method;
2896 LoadCurrentMethod(temp);
2897 if (!invoke->IsRecursive()) {
2898 // temp = temp->dex_cache_resolved_methods_;
2899 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2900 // temp = temp[index_in_cache]
2901 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
2902 // (temp + offset_of_quick_compiled_code)()
2903 __ call(Address(
2904 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
2905 } else {
2906 __ call(GetFrameEntryLabel());
2907 }
2908
2909 DCHECK(!IsLeafMethod());
2910 RecordPcInfo(invoke, invoke->GetDexPc());
2911}
2912
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002913void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002914 Label is_null;
2915 __ testl(value, value);
2916 __ j(kEqual, &is_null);
2917 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2918 __ movl(temp, object);
2919 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002920 __ movb(Address(temp, card, TIMES_1, 0),
2921 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002922 __ Bind(&is_null);
2923}
2924
Calin Juravle52c48962014-12-16 17:02:57 +00002925void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2926 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002927 LocationSummary* locations =
2928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002929 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002930
2931 // The output overlaps in case of long: we don't want the low move to overwrite
2932 // the object's location.
2933 locations->SetOut(Location::RequiresRegister(),
2934 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
2935 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002936
2937 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2938 // Long values can be loaded atomically into an XMM using movsd.
2939 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2940 // and then copy the XMM into the output 32bits at a time).
2941 locations->AddTemp(Location::RequiresFpuRegister());
2942 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002943}
2944
Calin Juravle52c48962014-12-16 17:02:57 +00002945void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2946 const FieldInfo& field_info) {
2947 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002948
Calin Juravle52c48962014-12-16 17:02:57 +00002949 LocationSummary* locations = instruction->GetLocations();
2950 Register base = locations->InAt(0).AsRegister<Register>();
2951 Location out = locations->Out();
2952 bool is_volatile = field_info.IsVolatile();
2953 Primitive::Type field_type = field_info.GetFieldType();
2954 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2955
2956 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002957 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002958 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002959 break;
2960 }
2961
2962 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002963 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002964 break;
2965 }
2966
2967 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002968 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002969 break;
2970 }
2971
2972 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002973 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974 break;
2975 }
2976
2977 case Primitive::kPrimInt:
2978 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002979 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002980 break;
2981 }
2982
2983 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002984 if (is_volatile) {
2985 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2986 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002988 __ movd(out.AsRegisterPairLow<Register>(), temp);
2989 __ psrlq(temp, Immediate(32));
2990 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2991 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002992 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002993 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002994 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002995 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002997 break;
2998 }
2999
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003000 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003001 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003002 break;
3003 }
3004
3005 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003006 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003007 break;
3008 }
3009
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003010 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003011 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003012 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003013 }
Calin Juravle52c48962014-12-16 17:02:57 +00003014
Calin Juravle77520bc2015-01-12 18:45:46 +00003015 // Longs are handled in the switch.
3016 if (field_type != Primitive::kPrimLong) {
3017 codegen_->MaybeRecordImplicitNullCheck(instruction);
3018 }
3019
Calin Juravle52c48962014-12-16 17:02:57 +00003020 if (is_volatile) {
3021 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3022 }
3023}
3024
3025void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3026 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3027
3028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3030 locations->SetInAt(0, Location::RequiresRegister());
3031 bool is_volatile = field_info.IsVolatile();
3032 Primitive::Type field_type = field_info.GetFieldType();
3033 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3034 || (field_type == Primitive::kPrimByte);
3035
3036 // The register allocator does not support multiple
3037 // inputs that die at entry with one in a specific register.
3038 if (is_byte_type) {
3039 // Ensure the value is in a byte register.
3040 locations->SetInAt(1, Location::RegisterLocation(EAX));
3041 } else {
3042 locations->SetInAt(1, Location::RequiresRegister());
3043 }
3044 // Temporary registers for the write barrier.
3045 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3046 locations->AddTemp(Location::RequiresRegister());
3047 // Ensure the card is in a byte register.
3048 locations->AddTemp(Location::RegisterLocation(ECX));
3049 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3050 // 64bits value can be atomically written to an address with movsd and an XMM register.
3051 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3052 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3053 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3054 // isolated cases when we need this it isn't worth adding the extra complexity.
3055 locations->AddTemp(Location::RequiresFpuRegister());
3056 locations->AddTemp(Location::RequiresFpuRegister());
3057 }
3058}
3059
3060void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3061 const FieldInfo& field_info) {
3062 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3063
3064 LocationSummary* locations = instruction->GetLocations();
3065 Register base = locations->InAt(0).AsRegister<Register>();
3066 Location value = locations->InAt(1);
3067 bool is_volatile = field_info.IsVolatile();
3068 Primitive::Type field_type = field_info.GetFieldType();
3069 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3070
3071 if (is_volatile) {
3072 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3073 }
3074
3075 switch (field_type) {
3076 case Primitive::kPrimBoolean:
3077 case Primitive::kPrimByte: {
3078 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3079 break;
3080 }
3081
3082 case Primitive::kPrimShort:
3083 case Primitive::kPrimChar: {
3084 __ movw(Address(base, offset), value.AsRegister<Register>());
3085 break;
3086 }
3087
3088 case Primitive::kPrimInt:
3089 case Primitive::kPrimNot: {
3090 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003091 break;
3092 }
3093
3094 case Primitive::kPrimLong: {
3095 if (is_volatile) {
3096 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3097 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3098 __ movd(temp1, value.AsRegisterPairLow<Register>());
3099 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3100 __ punpckldq(temp1, temp2);
3101 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003102 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003103 } else {
3104 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003105 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003106 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3107 }
3108 break;
3109 }
3110
3111 case Primitive::kPrimFloat: {
3112 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3113 break;
3114 }
3115
3116 case Primitive::kPrimDouble: {
3117 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3118 break;
3119 }
3120
3121 case Primitive::kPrimVoid:
3122 LOG(FATAL) << "Unreachable type " << field_type;
3123 UNREACHABLE();
3124 }
3125
Calin Juravle77520bc2015-01-12 18:45:46 +00003126 // Longs are handled in the switch.
3127 if (field_type != Primitive::kPrimLong) {
3128 codegen_->MaybeRecordImplicitNullCheck(instruction);
3129 }
3130
3131 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3132 Register temp = locations->GetTemp(0).AsRegister<Register>();
3133 Register card = locations->GetTemp(1).AsRegister<Register>();
3134 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3135 }
3136
Calin Juravle52c48962014-12-16 17:02:57 +00003137 if (is_volatile) {
3138 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3139 }
3140}
3141
3142void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3143 HandleFieldGet(instruction, instruction->GetFieldInfo());
3144}
3145
3146void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3147 HandleFieldGet(instruction, instruction->GetFieldInfo());
3148}
3149
3150void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3151 HandleFieldSet(instruction, instruction->GetFieldInfo());
3152}
3153
3154void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3155 HandleFieldSet(instruction, instruction->GetFieldInfo());
3156}
3157
3158void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3159 HandleFieldSet(instruction, instruction->GetFieldInfo());
3160}
3161
3162void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3163 HandleFieldSet(instruction, instruction->GetFieldInfo());
3164}
3165
3166void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3167 HandleFieldGet(instruction, instruction->GetFieldInfo());
3168}
3169
3170void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3171 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003172}
3173
3174void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003175 LocationSummary* locations =
3176 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003177 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3178 ? Location::RequiresRegister()
3179 : Location::Any();
3180 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003181 if (instruction->HasUses()) {
3182 locations->SetOut(Location::SameAsFirstInput());
3183 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184}
3185
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003186void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003187 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3188 return;
3189 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003190 LocationSummary* locations = instruction->GetLocations();
3191 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003192
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003193 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3194 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3195}
3196
3197void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003198 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199 codegen_->AddSlowPath(slow_path);
3200
3201 LocationSummary* locations = instruction->GetLocations();
3202 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203
3204 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003205 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003206 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003208 } else {
3209 DCHECK(obj.IsConstant()) << obj;
3210 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3211 __ jmp(slow_path->GetEntryLabel());
3212 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003213 }
3214 __ j(kEqual, slow_path->GetEntryLabel());
3215}
3216
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003217void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3218 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3219 GenerateImplicitNullCheck(instruction);
3220 } else {
3221 GenerateExplicitNullCheck(instruction);
3222 }
3223}
3224
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003225void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003226 LocationSummary* locations =
3227 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003230 // The output overlaps in case of long: we don't want the low move to overwrite
3231 // the array's location.
3232 locations->SetOut(Location::RequiresRegister(),
3233 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3234 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003235}
3236
3237void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3238 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003240 Location index = locations->InAt(1);
3241
Calin Juravle77520bc2015-01-12 18:45:46 +00003242 Primitive::Type type = instruction->GetType();
3243 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003244 case Primitive::kPrimBoolean: {
3245 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003246 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003247 if (index.IsConstant()) {
3248 __ movzxb(out, Address(obj,
3249 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3250 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003252 }
3253 break;
3254 }
3255
3256 case Primitive::kPrimByte: {
3257 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259 if (index.IsConstant()) {
3260 __ movsxb(out, Address(obj,
3261 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3262 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 }
3265 break;
3266 }
3267
3268 case Primitive::kPrimShort: {
3269 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003271 if (index.IsConstant()) {
3272 __ movsxw(out, Address(obj,
3273 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3274 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003275 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003276 }
3277 break;
3278 }
3279
3280 case Primitive::kPrimChar: {
3281 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 if (index.IsConstant()) {
3284 __ movzxw(out, Address(obj,
3285 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3286 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003288 }
3289 break;
3290 }
3291
3292 case Primitive::kPrimInt:
3293 case Primitive::kPrimNot: {
3294 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003296 if (index.IsConstant()) {
3297 __ movl(out, Address(obj,
3298 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3299 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 }
3302 break;
3303 }
3304
3305 case Primitive::kPrimLong: {
3306 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003307 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003308 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309 if (index.IsConstant()) {
3310 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003311 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003313 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003314 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003315 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003316 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003317 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003318 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 }
3321 break;
3322 }
3323
Mark Mendell7c8d0092015-01-26 11:21:33 -05003324 case Primitive::kPrimFloat: {
3325 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3326 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3327 if (index.IsConstant()) {
3328 __ movss(out, Address(obj,
3329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3330 } else {
3331 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3332 }
3333 break;
3334 }
3335
3336 case Primitive::kPrimDouble: {
3337 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3338 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3339 if (index.IsConstant()) {
3340 __ movsd(out, Address(obj,
3341 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3342 } else {
3343 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3344 }
3345 break;
3346 }
3347
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003349 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003350 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003351 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003352
3353 if (type != Primitive::kPrimLong) {
3354 codegen_->MaybeRecordImplicitNullCheck(instruction);
3355 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356}
3357
3358void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003359 // This location builder might end up asking to up to four registers, which is
3360 // not currently possible for baseline. The situation in which we need four
3361 // registers cannot be met by baseline though, because it has not run any
3362 // optimization.
3363
Nicolas Geoffray39468442014-09-02 15:17:15 +01003364 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003365 bool needs_write_barrier =
3366 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3367
Mark Mendell5f874182015-03-04 15:42:45 -05003368 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003369
Nicolas Geoffray39468442014-09-02 15:17:15 +01003370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3371 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003372 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003373
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003374 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003375 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003376 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3377 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3378 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003380 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3381 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003382 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003383 // In case of a byte operation, the register allocator does not support multiple
3384 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003385 locations->SetInAt(0, Location::RequiresRegister());
3386 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003387 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003388 // Ensure the value is in a byte register.
3389 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003390 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003391 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003392 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003393 // Temporary registers for the write barrier.
3394 if (needs_write_barrier) {
3395 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003396 // Ensure the card is in a byte register.
3397 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003398 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003399 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003400}
3401
3402void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3403 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003404 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003406 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003407 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003408 bool needs_runtime_call = locations->WillCall();
3409 bool needs_write_barrier =
3410 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411
3412 switch (value_type) {
3413 case Primitive::kPrimBoolean:
3414 case Primitive::kPrimByte: {
3415 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003416 if (index.IsConstant()) {
3417 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003418 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003419 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003420 } else {
3421 __ movb(Address(obj, offset),
3422 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003427 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003428 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003430 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3431 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003432 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003434 break;
3435 }
3436
3437 case Primitive::kPrimShort:
3438 case Primitive::kPrimChar: {
3439 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440 if (index.IsConstant()) {
3441 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003442 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003443 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003444 } else {
3445 __ movw(Address(obj, offset),
3446 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003448 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003449 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3451 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003452 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003454 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3455 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003457 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003458 break;
3459 }
3460
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003461 case Primitive::kPrimInt:
3462 case Primitive::kPrimNot: {
3463 if (!needs_runtime_call) {
3464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3465 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003466 size_t offset =
3467 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003468 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003470 } else {
3471 DCHECK(value.IsConstant()) << value;
3472 __ movl(Address(obj, offset),
3473 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3474 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003475 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003476 DCHECK(index.IsRegister()) << index;
3477 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003478 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3479 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003480 } else {
3481 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003483 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3484 }
3485 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003487
3488 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 Register temp = locations->GetTemp(0).AsRegister<Register>();
3490 Register card = locations->GetTemp(1).AsRegister<Register>();
3491 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003492 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003493 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003494 DCHECK_EQ(value_type, Primitive::kPrimNot);
3495 DCHECK(!codegen_->IsLeafMethod());
3496 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3497 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003498 }
3499 break;
3500 }
3501
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003502 case Primitive::kPrimLong: {
3503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003504 if (index.IsConstant()) {
3505 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003506 if (value.IsRegisterPair()) {
3507 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003508 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003509 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003510 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003511 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003512 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3513 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003514 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003515 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3516 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003518 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003520 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003523 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003524 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003525 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003526 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003527 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003528 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003530 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003531 Immediate(High32Bits(val)));
3532 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003533 }
3534 break;
3535 }
3536
Mark Mendell7c8d0092015-01-26 11:21:33 -05003537 case Primitive::kPrimFloat: {
3538 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3539 DCHECK(value.IsFpuRegister());
3540 if (index.IsConstant()) {
3541 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3542 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3543 } else {
3544 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3545 value.AsFpuRegister<XmmRegister>());
3546 }
3547 break;
3548 }
3549
3550 case Primitive::kPrimDouble: {
3551 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3552 DCHECK(value.IsFpuRegister());
3553 if (index.IsConstant()) {
3554 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3555 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3556 } else {
3557 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3558 value.AsFpuRegister<XmmRegister>());
3559 }
3560 break;
3561 }
3562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003563 case Primitive::kPrimVoid:
3564 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003565 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003566 }
3567}
3568
3569void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003571 locations->SetInAt(0, Location::RequiresRegister());
3572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 instruction->SetLocations(locations);
3574}
3575
3576void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3577 LocationSummary* locations = instruction->GetLocations();
3578 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 Register obj = locations->InAt(0).AsRegister<Register>();
3580 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003581 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003582 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003583}
3584
3585void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003586 LocationSummary* locations =
3587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003588 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003590 if (instruction->HasUses()) {
3591 locations->SetOut(Location::SameAsFirstInput());
3592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593}
3594
3595void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3596 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003597 Location index_loc = locations->InAt(0);
3598 Location length_loc = locations->InAt(1);
3599 SlowPathCodeX86* slow_path =
3600 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 codegen_->AddSlowPath(slow_path);
3602
Mark Mendellf60c90b2015-03-04 15:12:59 -05003603 Register length = length_loc.AsRegister<Register>();
3604 if (index_loc.IsConstant()) {
3605 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3606 __ cmpl(length, Immediate(value));
3607 } else {
3608 __ cmpl(length, index_loc.AsRegister<Register>());
3609 }
3610 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611}
3612
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003613void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3614 temp->SetLocations(nullptr);
3615}
3616
3617void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3618 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003619 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620}
3621
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003622void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003623 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003624 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003625}
3626
3627void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003628 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3629}
3630
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003631void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3632 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3633}
3634
3635void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003636 HBasicBlock* block = instruction->GetBlock();
3637 if (block->GetLoopInformation() != nullptr) {
3638 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3639 // The back edge will generate the suspend check.
3640 return;
3641 }
3642 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3643 // The goto will generate the suspend check.
3644 return;
3645 }
3646 GenerateSuspendCheck(instruction, nullptr);
3647}
3648
3649void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3650 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003651 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003652 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003653 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003654 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003655 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003656 if (successor == nullptr) {
3657 __ j(kNotEqual, slow_path->GetEntryLabel());
3658 __ Bind(slow_path->GetReturnLabel());
3659 } else {
3660 __ j(kEqual, codegen_->GetLabelOf(successor));
3661 __ jmp(slow_path->GetEntryLabel());
3662 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003663}
3664
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003665X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3666 return codegen_->GetAssembler();
3667}
3668
Mark Mendell7c8d0092015-01-26 11:21:33 -05003669void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003670 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003671 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003672 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003673 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003674 __ movl(temp_reg, Address(ESP, src + stack_offset));
3675 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3676}
3677
3678void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3679 ScratchRegisterScope ensure_scratch(
3680 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3681 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3682 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3683 __ movl(temp_reg, Address(ESP, src + stack_offset));
3684 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3685 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3686 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003687}
3688
3689void ParallelMoveResolverX86::EmitMove(size_t index) {
3690 MoveOperands* move = moves_.Get(index);
3691 Location source = move->GetSource();
3692 Location destination = move->GetDestination();
3693
3694 if (source.IsRegister()) {
3695 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003697 } else {
3698 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003700 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003701 } else if (source.IsFpuRegister()) {
3702 if (destination.IsFpuRegister()) {
3703 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3704 } else if (destination.IsStackSlot()) {
3705 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3706 } else {
3707 DCHECK(destination.IsDoubleStackSlot());
3708 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3709 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003710 } else if (source.IsStackSlot()) {
3711 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003712 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003713 } else if (destination.IsFpuRegister()) {
3714 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003715 } else {
3716 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003717 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3718 }
3719 } else if (source.IsDoubleStackSlot()) {
3720 if (destination.IsFpuRegister()) {
3721 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3722 } else {
3723 DCHECK(destination.IsDoubleStackSlot()) << destination;
3724 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003725 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003726 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003727 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003728 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003729 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003730 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003731 if (value == 0) {
3732 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3733 } else {
3734 __ movl(destination.AsRegister<Register>(), Immediate(value));
3735 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003736 } else {
3737 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003738 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003739 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003740 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003741 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003742 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003743 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003744 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003745 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3746 if (value == 0) {
3747 // Easy handling of 0.0.
3748 __ xorps(dest, dest);
3749 } else {
3750 ScratchRegisterScope ensure_scratch(
3751 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3752 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3753 __ movl(temp, Immediate(value));
3754 __ movd(dest, temp);
3755 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003756 } else {
3757 DCHECK(destination.IsStackSlot()) << destination;
3758 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3759 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003760 } else if (constant->IsLongConstant()) {
3761 int64_t value = constant->AsLongConstant()->GetValue();
3762 int32_t low_value = Low32Bits(value);
3763 int32_t high_value = High32Bits(value);
3764 Immediate low(low_value);
3765 Immediate high(high_value);
3766 if (destination.IsDoubleStackSlot()) {
3767 __ movl(Address(ESP, destination.GetStackIndex()), low);
3768 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3769 } else {
3770 __ movl(destination.AsRegisterPairLow<Register>(), low);
3771 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3772 }
3773 } else {
3774 DCHECK(constant->IsDoubleConstant());
3775 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003776 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003777 int32_t low_value = Low32Bits(value);
3778 int32_t high_value = High32Bits(value);
3779 Immediate low(low_value);
3780 Immediate high(high_value);
3781 if (destination.IsFpuRegister()) {
3782 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3783 if (value == 0) {
3784 // Easy handling of 0.0.
3785 __ xorpd(dest, dest);
3786 } else {
3787 __ pushl(high);
3788 __ pushl(low);
3789 __ movsd(dest, Address(ESP, 0));
3790 __ addl(ESP, Immediate(8));
3791 }
3792 } else {
3793 DCHECK(destination.IsDoubleStackSlot()) << destination;
3794 __ movl(Address(ESP, destination.GetStackIndex()), low);
3795 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3796 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003797 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003798 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003799 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003800 }
3801}
3802
3803void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003804 Register suggested_scratch = reg == EAX ? EBX : EAX;
3805 ScratchRegisterScope ensure_scratch(
3806 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3807
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003808 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3809 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3810 __ movl(Address(ESP, mem + stack_offset), reg);
3811 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3812}
3813
Mark Mendell7c8d0092015-01-26 11:21:33 -05003814void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3815 ScratchRegisterScope ensure_scratch(
3816 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3817
3818 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3819 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3820 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3821 __ movss(Address(ESP, mem + stack_offset), reg);
3822 __ movd(reg, temp_reg);
3823}
3824
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003825void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3826 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003827 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3828
3829 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003830 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003831 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3832
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003833 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3834 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3835 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3836 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3837 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3838 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3839}
3840
3841void ParallelMoveResolverX86::EmitSwap(size_t index) {
3842 MoveOperands* move = moves_.Get(index);
3843 Location source = move->GetSource();
3844 Location destination = move->GetDestination();
3845
3846 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003848 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003850 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003852 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3853 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003854 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3855 // Use XOR Swap algorithm to avoid a temporary.
3856 DCHECK_NE(source.reg(), destination.reg());
3857 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3858 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3859 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3860 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3861 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3862 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3863 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003864 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
3865 // Take advantage of the 16 bytes in the XMM register.
3866 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
3867 Address stack(ESP, destination.GetStackIndex());
3868 // Load the double into the high doubleword.
3869 __ movhpd(reg, stack);
3870
3871 // Store the low double into the destination.
3872 __ movsd(stack, reg);
3873
3874 // Move the high double to the low double.
3875 __ psrldq(reg, Immediate(8));
3876 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
3877 // Take advantage of the 16 bytes in the XMM register.
3878 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
3879 Address stack(ESP, source.GetStackIndex());
3880 // Load the double into the high doubleword.
3881 __ movhpd(reg, stack);
3882
3883 // Store the low double into the destination.
3884 __ movsd(stack, reg);
3885
3886 // Move the high double to the low double.
3887 __ psrldq(reg, Immediate(8));
3888 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
3889 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3890 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003891 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003892 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003893 }
3894}
3895
3896void ParallelMoveResolverX86::SpillScratch(int reg) {
3897 __ pushl(static_cast<Register>(reg));
3898}
3899
3900void ParallelMoveResolverX86::RestoreScratch(int reg) {
3901 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003902}
3903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003904void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003905 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3906 ? LocationSummary::kCallOnSlowPath
3907 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003908 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003909 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003910 locations->SetOut(Location::RequiresRegister());
3911}
3912
3913void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003914 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003915 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003916 DCHECK(!cls->CanCallRuntime());
3917 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003918 codegen_->LoadCurrentMethod(out);
3919 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3920 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003921 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003922 codegen_->LoadCurrentMethod(out);
3923 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3924 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003925
3926 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3927 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3928 codegen_->AddSlowPath(slow_path);
3929 __ testl(out, out);
3930 __ j(kEqual, slow_path->GetEntryLabel());
3931 if (cls->MustGenerateClinitCheck()) {
3932 GenerateClassInitializationCheck(slow_path, out);
3933 } else {
3934 __ Bind(slow_path->GetExitLabel());
3935 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003936 }
3937}
3938
3939void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3940 LocationSummary* locations =
3941 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3942 locations->SetInAt(0, Location::RequiresRegister());
3943 if (check->HasUses()) {
3944 locations->SetOut(Location::SameAsFirstInput());
3945 }
3946}
3947
3948void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003949 // We assume the class to not be null.
3950 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3951 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003952 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003953 GenerateClassInitializationCheck(slow_path,
3954 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003955}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003956
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003957void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3958 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003959 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3960 Immediate(mirror::Class::kStatusInitialized));
3961 __ j(kLess, slow_path->GetEntryLabel());
3962 __ Bind(slow_path->GetExitLabel());
3963 // No need for memory fence, thanks to the X86 memory model.
3964}
3965
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003966void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3967 LocationSummary* locations =
3968 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3969 locations->SetOut(Location::RequiresRegister());
3970}
3971
3972void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3973 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3974 codegen_->AddSlowPath(slow_path);
3975
Roland Levillain271ab9c2014-11-27 15:23:57 +00003976 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003977 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003978 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3979 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003980 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3981 __ testl(out, out);
3982 __ j(kEqual, slow_path->GetEntryLabel());
3983 __ Bind(slow_path->GetExitLabel());
3984}
3985
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003986void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3987 LocationSummary* locations =
3988 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3989 locations->SetOut(Location::RequiresRegister());
3990}
3991
3992void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3993 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003995 __ fs()->movl(address, Immediate(0));
3996}
3997
3998void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3999 LocationSummary* locations =
4000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4001 InvokeRuntimeCallingConvention calling_convention;
4002 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4003}
4004
4005void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4006 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4007 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4008}
4009
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004010void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004011 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4012 ? LocationSummary::kNoCall
4013 : LocationSummary::kCallOnSlowPath;
4014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4015 locations->SetInAt(0, Location::RequiresRegister());
4016 locations->SetInAt(1, Location::Any());
4017 locations->SetOut(Location::RequiresRegister());
4018}
4019
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004020void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004021 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004023 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004025 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4026 Label done, zero;
4027 SlowPathCodeX86* slow_path = nullptr;
4028
4029 // Return 0 if `obj` is null.
4030 // TODO: avoid this check if we know obj is not null.
4031 __ testl(obj, obj);
4032 __ j(kEqual, &zero);
4033 __ movl(out, Address(obj, class_offset));
4034 // Compare the class of `obj` with `cls`.
4035 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004036 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004037 } else {
4038 DCHECK(cls.IsStackSlot()) << cls;
4039 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4040 }
4041
4042 if (instruction->IsClassFinal()) {
4043 // Classes must be equal for the instanceof to succeed.
4044 __ j(kNotEqual, &zero);
4045 __ movl(out, Immediate(1));
4046 __ jmp(&done);
4047 } else {
4048 // If the classes are not equal, we go into a slow path.
4049 DCHECK(locations->OnlyCallsOnSlowPath());
4050 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004051 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004052 codegen_->AddSlowPath(slow_path);
4053 __ j(kNotEqual, slow_path->GetEntryLabel());
4054 __ movl(out, Immediate(1));
4055 __ jmp(&done);
4056 }
4057 __ Bind(&zero);
4058 __ movl(out, Immediate(0));
4059 if (slow_path != nullptr) {
4060 __ Bind(slow_path->GetExitLabel());
4061 }
4062 __ Bind(&done);
4063}
4064
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004065void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4067 instruction, LocationSummary::kCallOnSlowPath);
4068 locations->SetInAt(0, Location::RequiresRegister());
4069 locations->SetInAt(1, Location::Any());
4070 locations->AddTemp(Location::RequiresRegister());
4071}
4072
4073void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4074 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004076 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004077 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004078 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4079 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4080 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4081 codegen_->AddSlowPath(slow_path);
4082
4083 // TODO: avoid this check if we know obj is not null.
4084 __ testl(obj, obj);
4085 __ j(kEqual, slow_path->GetExitLabel());
4086 __ movl(temp, Address(obj, class_offset));
4087
4088 // Compare the class of `obj` with `cls`.
4089 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004090 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004091 } else {
4092 DCHECK(cls.IsStackSlot()) << cls;
4093 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4094 }
4095
4096 __ j(kNotEqual, slow_path->GetEntryLabel());
4097 __ Bind(slow_path->GetExitLabel());
4098}
4099
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004100void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4101 LocationSummary* locations =
4102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4103 InvokeRuntimeCallingConvention calling_convention;
4104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4105}
4106
4107void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4108 __ fs()->call(Address::Absolute(instruction->IsEnter()
4109 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4110 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4111 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4112}
4113
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004114void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4115void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4116void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4117
4118void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4119 LocationSummary* locations =
4120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4121 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4122 || instruction->GetResultType() == Primitive::kPrimLong);
4123 locations->SetInAt(0, Location::RequiresRegister());
4124 locations->SetInAt(1, Location::Any());
4125 locations->SetOut(Location::SameAsFirstInput());
4126}
4127
4128void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4129 HandleBitwiseOperation(instruction);
4130}
4131
4132void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4133 HandleBitwiseOperation(instruction);
4134}
4135
4136void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4137 HandleBitwiseOperation(instruction);
4138}
4139
4140void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4141 LocationSummary* locations = instruction->GetLocations();
4142 Location first = locations->InAt(0);
4143 Location second = locations->InAt(1);
4144 DCHECK(first.Equals(locations->Out()));
4145
4146 if (instruction->GetResultType() == Primitive::kPrimInt) {
4147 if (second.IsRegister()) {
4148 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004149 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004150 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004151 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004152 } else {
4153 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004155 }
4156 } else if (second.IsConstant()) {
4157 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004158 __ andl(first.AsRegister<Register>(),
4159 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004160 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004161 __ orl(first.AsRegister<Register>(),
4162 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004163 } else {
4164 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004165 __ xorl(first.AsRegister<Register>(),
4166 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004167 }
4168 } else {
4169 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004170 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004171 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004172 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004173 } else {
4174 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004175 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004176 }
4177 }
4178 } else {
4179 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4180 if (second.IsRegisterPair()) {
4181 if (instruction->IsAnd()) {
4182 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4183 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4184 } else if (instruction->IsOr()) {
4185 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4186 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4187 } else {
4188 DCHECK(instruction->IsXor());
4189 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4190 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4191 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004192 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004193 if (instruction->IsAnd()) {
4194 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4195 __ andl(first.AsRegisterPairHigh<Register>(),
4196 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4197 } else if (instruction->IsOr()) {
4198 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4199 __ orl(first.AsRegisterPairHigh<Register>(),
4200 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4201 } else {
4202 DCHECK(instruction->IsXor());
4203 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4204 __ xorl(first.AsRegisterPairHigh<Register>(),
4205 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4206 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004207 } else {
4208 DCHECK(second.IsConstant()) << second;
4209 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004210 int32_t low_value = Low32Bits(value);
4211 int32_t high_value = High32Bits(value);
4212 Immediate low(low_value);
4213 Immediate high(high_value);
4214 Register first_low = first.AsRegisterPairLow<Register>();
4215 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004216 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004217 if (low_value == 0) {
4218 __ xorl(first_low, first_low);
4219 } else if (low_value != -1) {
4220 __ andl(first_low, low);
4221 }
4222 if (high_value == 0) {
4223 __ xorl(first_high, first_high);
4224 } else if (high_value != -1) {
4225 __ andl(first_high, high);
4226 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004227 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004228 if (low_value != 0) {
4229 __ orl(first_low, low);
4230 }
4231 if (high_value != 0) {
4232 __ orl(first_high, high);
4233 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004234 } else {
4235 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004236 if (low_value != 0) {
4237 __ xorl(first_low, low);
4238 }
4239 if (high_value != 0) {
4240 __ xorl(first_high, high);
4241 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004242 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004243 }
4244 }
4245}
4246
Calin Juravleb1498f62015-02-16 13:13:29 +00004247void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4248 // Nothing to do, this should be removed during prepare for register allocator.
4249 UNUSED(instruction);
4250 LOG(FATAL) << "Unreachable";
4251}
4252
4253void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4254 // Nothing to do, this should be removed during prepare for register allocator.
4255 UNUSED(instruction);
4256 LOG(FATAL) << "Unreachable";
4257}
4258
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004259} // namespace x86
4260} // namespace art