blob: 8fbc64ea5eee3a9ab0ebe55b6153a52b6d7d4666 [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
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100462static dwarf::Reg DWARFReg(Register reg) {
463 return dwarf::Reg::X86Core(static_cast<int>(reg));
464}
465
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000466void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000468 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000469 bool skip_overflow_check =
470 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000471 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000472
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000473 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100474 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100475 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 }
477
Mark Mendell5f874182015-03-04 15:42:45 -0500478 if (HasEmptyFrame()) {
479 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000480 }
Mark Mendell5f874182015-03-04 15:42:45 -0500481
482 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
483 Register reg = kCoreCalleeSaves[i];
484 if (allocated_registers_.ContainsCoreRegister(reg)) {
485 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100486 __ cfi().AdjustCFAOffset(kX86WordSize);
487 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500488 }
489 }
490
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 int adjust = GetFrameSize() - FrameEntrySpillSize();
492 __ subl(ESP, Immediate(adjust));
493 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500494 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000495}
496
497void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500498 if (HasEmptyFrame()) {
499 return;
500 }
501
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100502 int adjust = GetFrameSize() - FrameEntrySpillSize();
503 __ addl(ESP, Immediate(adjust));
504 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500505
506 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
507 Register reg = kCoreCalleeSaves[i];
508 if (allocated_registers_.ContainsCoreRegister(reg)) {
509 __ popl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100510 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
511 __ cfi().Restore(DWARFReg(reg));
Mark Mendell5f874182015-03-04 15:42:45 -0500512 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000513 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000514}
515
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100516void CodeGeneratorX86::Bind(HBasicBlock* block) {
517 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000518}
519
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100520void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000521 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100522 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000523}
524
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100525Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
526 switch (load->GetType()) {
527 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100528 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100529 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530
531 case Primitive::kPrimInt:
532 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
536 case Primitive::kPrimBoolean:
537 case Primitive::kPrimByte:
538 case Primitive::kPrimChar:
539 case Primitive::kPrimShort:
540 case Primitive::kPrimVoid:
541 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700542 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 }
544
545 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700546 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100547}
548
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100549Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
550 switch (type) {
551 case Primitive::kPrimBoolean:
552 case Primitive::kPrimByte:
553 case Primitive::kPrimChar:
554 case Primitive::kPrimShort:
555 case Primitive::kPrimInt:
556 case Primitive::kPrimNot: {
557 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000558 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100560 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000562 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100563 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100564 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000566 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 uint32_t index = gp_index_;
568 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000569 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
572 calling_convention.GetRegisterPairAt(index));
573 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000575 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
576 }
577 }
578
579 case Primitive::kPrimFloat: {
580 uint32_t index = fp_index_++;
581 stack_index_++;
582 if (index < calling_convention.GetNumberOfFpuRegisters()) {
583 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
584 } else {
585 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
586 }
587 }
588
589 case Primitive::kPrimDouble: {
590 uint32_t index = fp_index_++;
591 stack_index_ += 2;
592 if (index < calling_convention.GetNumberOfFpuRegisters()) {
593 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
594 } else {
595 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100596 }
597 }
598
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 case Primitive::kPrimVoid:
600 LOG(FATAL) << "Unexpected parameter type " << type;
601 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100602 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 return Location();
604}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100605
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100606void CodeGeneratorX86::Move32(Location destination, Location source) {
607 if (source.Equals(destination)) {
608 return;
609 }
610 if (destination.IsRegister()) {
611 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000612 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100613 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 } else {
616 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 } else if (destination.IsFpuRegister()) {
620 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else {
625 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000629 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500634 } else if (source.IsConstant()) {
635 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000636 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500637 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
639 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100640 __ pushl(Address(ESP, source.GetStackIndex()));
641 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 }
643 }
644}
645
646void CodeGeneratorX86::Move64(Location destination, Location source) {
647 if (source.Equals(destination)) {
648 return;
649 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100650 if (destination.IsRegisterPair()) {
651 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000652 EmitParallelMoves(
653 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
654 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
655 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
656 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else if (source.IsFpuRegister()) {
658 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000660 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
663 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
665 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500667 if (source.IsFpuRegister()) {
668 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
669 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100671 } else {
672 LOG(FATAL) << "Unimplemented";
673 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000675 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100676 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000677 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100678 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100680 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100681 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000683 } else if (source.IsConstant()) {
684 HConstant* constant = source.GetConstant();
685 int64_t value;
686 if (constant->IsLongConstant()) {
687 value = constant->AsLongConstant()->GetValue();
688 } else {
689 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000690 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000691 }
692 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
693 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000695 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000696 EmitParallelMoves(
697 Location::StackSlot(source.GetStackIndex()),
698 Location::StackSlot(destination.GetStackIndex()),
699 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
700 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 }
702 }
703}
704
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100705void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000706 LocationSummary* locations = instruction->GetLocations();
707 if (locations != nullptr && locations->Out().Equals(location)) {
708 return;
709 }
710
711 if (locations != nullptr && locations->Out().IsConstant()) {
712 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000713 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
714 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000716 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000717 } else if (location.IsStackSlot()) {
718 __ movl(Address(ESP, location.GetStackIndex()), imm);
719 } else {
720 DCHECK(location.IsConstant());
721 DCHECK_EQ(location.GetConstant(), const_to_move);
722 }
723 } else if (const_to_move->IsLongConstant()) {
724 int64_t value = const_to_move->AsLongConstant()->GetValue();
725 if (location.IsRegisterPair()) {
726 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
727 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
728 } else if (location.IsDoubleStackSlot()) {
729 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000730 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
731 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000732 } else {
733 DCHECK(location.IsConstant());
734 DCHECK_EQ(location.GetConstant(), instruction);
735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000737 } else if (instruction->IsTemporary()) {
738 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000739 if (temp_location.IsStackSlot()) {
740 Move32(location, temp_location);
741 } else {
742 DCHECK(temp_location.IsDoubleStackSlot());
743 Move64(location, temp_location);
744 }
Roland Levillain476df552014-10-09 17:51:36 +0100745 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 switch (instruction->GetType()) {
748 case Primitive::kPrimBoolean:
749 case Primitive::kPrimByte:
750 case Primitive::kPrimChar:
751 case Primitive::kPrimShort:
752 case Primitive::kPrimInt:
753 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100754 case Primitive::kPrimFloat:
755 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 break;
757
758 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 case Primitive::kPrimDouble:
760 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 break;
762
763 default:
764 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
765 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000766 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100767 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 switch (instruction->GetType()) {
769 case Primitive::kPrimBoolean:
770 case Primitive::kPrimByte:
771 case Primitive::kPrimChar:
772 case Primitive::kPrimShort:
773 case Primitive::kPrimInt:
774 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100775 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000776 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 break;
778
779 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100780 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000781 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100782 break;
783
784 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787 }
788}
789
790void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000791 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000792}
793
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000794void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000795 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100796 DCHECK(!successor->IsExitBlock());
797
798 HBasicBlock* block = got->GetBlock();
799 HInstruction* previous = got->GetPrevious();
800
801 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000802 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100803 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
804 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
805 return;
806 }
807
808 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
809 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
810 }
811 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000812 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000813 }
814}
815
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000816void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000817 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818}
819
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700821 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000822}
823
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700824void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
825 Label* true_target,
826 Label* false_target,
827 Label* always_true_target) {
828 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100829 if (cond->IsIntConstant()) {
830 // Constant condition, statically compared against 1.
831 int32_t cond_value = cond->AsIntConstant()->GetValue();
832 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700833 if (always_true_target != nullptr) {
834 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100835 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100836 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100837 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 DCHECK_EQ(cond_value, 0);
839 }
840 } else {
841 bool materialized =
842 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
843 // Moves do not affect the eflags register, so if the condition is
844 // evaluated just before the if, we don't need to evaluate it
845 // again.
846 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700847 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 if (materialized) {
849 if (!eflags_set) {
850 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700851 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500853 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100854 } else {
855 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
856 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700857 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100858 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700859 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100860 }
861 } else {
862 Location lhs = cond->GetLocations()->InAt(0);
863 Location rhs = cond->GetLocations()->InAt(1);
864 // LHS is guaranteed to be in a register (see
865 // LocationsBuilderX86::VisitCondition).
866 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000867 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100868 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500869 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
870 if (constant == 0) {
871 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
872 } else {
873 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
874 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100875 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700878 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700879 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100880 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700881 if (false_target != nullptr) {
882 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000883 }
884}
885
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700886void LocationsBuilderX86::VisitIf(HIf* if_instr) {
887 LocationSummary* locations =
888 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
889 HInstruction* cond = if_instr->InputAt(0);
890 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
891 locations->SetInAt(0, Location::Any());
892 }
893}
894
895void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
896 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
897 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
898 Label* always_true_target = true_target;
899 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
900 if_instr->IfTrueSuccessor())) {
901 always_true_target = nullptr;
902 }
903 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
904 if_instr->IfFalseSuccessor())) {
905 false_target = nullptr;
906 }
907 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
908}
909
910void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
911 LocationSummary* locations = new (GetGraph()->GetArena())
912 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
913 HInstruction* cond = deoptimize->InputAt(0);
914 DCHECK(cond->IsCondition());
915 if (cond->AsCondition()->NeedsMaterialization()) {
916 locations->SetInAt(0, Location::Any());
917 }
918}
919
920void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
921 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
922 DeoptimizationSlowPathX86(deoptimize);
923 codegen_->AddSlowPath(slow_path);
924 Label* slow_path_entry = slow_path->GetEntryLabel();
925 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
926}
927
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000928void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000929 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000930}
931
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
933 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000934}
935
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000936void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100937 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000938}
939
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100941 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700942 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000943}
944
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100946 LocationSummary* locations =
947 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 switch (store->InputAt(1)->GetType()) {
949 case Primitive::kPrimBoolean:
950 case Primitive::kPrimByte:
951 case Primitive::kPrimChar:
952 case Primitive::kPrimShort:
953 case Primitive::kPrimInt:
954 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
957 break;
958
959 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100960 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100961 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
962 break;
963
964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100965 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966 }
967 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000968}
969
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700971 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972}
973
Dave Allison20dfc792014-06-16 20:44:29 -0700974void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100975 LocationSummary* locations =
976 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100977 locations->SetInAt(0, Location::RequiresRegister());
978 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100979 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500980 // We need a byte register.
981 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100982 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000983}
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
986 if (comp->NeedsMaterialization()) {
987 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000988 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100989 // Clear register: setcc only sets the low byte.
990 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500991 Location lhs = locations->InAt(0);
992 Location rhs = locations->InAt(1);
993 if (rhs.IsRegister()) {
994 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
995 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800996 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500997 if (constant == 0) {
998 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
999 } else {
1000 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1001 }
Dave Allison20dfc792014-06-16 20:44:29 -07001002 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001003 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001004 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001005 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001006 }
Dave Allison20dfc792014-06-16 20:44:29 -07001007}
1008
1009void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1010 VisitCondition(comp);
1011}
1012
1013void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1014 VisitCondition(comp);
1015}
1016
1017void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1018 VisitCondition(comp);
1019}
1020
1021void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1022 VisitCondition(comp);
1023}
1024
1025void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1026 VisitCondition(comp);
1027}
1028
1029void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1030 VisitCondition(comp);
1031}
1032
1033void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1034 VisitCondition(comp);
1035}
1036
1037void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1038 VisitCondition(comp);
1039}
1040
1041void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1042 VisitCondition(comp);
1043}
1044
1045void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1046 VisitCondition(comp);
1047}
1048
1049void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1054 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001055}
1056
1057void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001058 LocationSummary* locations =
1059 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001060 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001061}
1062
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001063void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001064 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001065 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001068void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1069 LocationSummary* locations =
1070 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1071 locations->SetOut(Location::ConstantLocation(constant));
1072}
1073
1074void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1075 // Will be generated at use site.
1076 UNUSED(constant);
1077}
1078
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001080 LocationSummary* locations =
1081 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001082 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083}
1084
1085void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1086 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001087 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088}
1089
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001090void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1091 LocationSummary* locations =
1092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1093 locations->SetOut(Location::ConstantLocation(constant));
1094}
1095
1096void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1097 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001098 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001099}
1100
1101void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1102 LocationSummary* locations =
1103 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1104 locations->SetOut(Location::ConstantLocation(constant));
1105}
1106
1107void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1108 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001109 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001110}
1111
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001113 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114}
1115
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001117 UNUSED(ret);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001118 __ cfi().RememberState();
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001120 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001123}
1124
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001125void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001126 LocationSummary* locations =
1127 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 switch (ret->InputAt(0)->GetType()) {
1129 case Primitive::kPrimBoolean:
1130 case Primitive::kPrimByte:
1131 case Primitive::kPrimChar:
1132 case Primitive::kPrimShort:
1133 case Primitive::kPrimInt:
1134 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001135 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136 break;
1137
1138 case Primitive::kPrimLong:
1139 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001140 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 break;
1142
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001143 case Primitive::kPrimFloat:
1144 case Primitive::kPrimDouble:
1145 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 break;
1148
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001152}
1153
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001154void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 if (kIsDebugBuild) {
1156 switch (ret->InputAt(0)->GetType()) {
1157 case Primitive::kPrimBoolean:
1158 case Primitive::kPrimByte:
1159 case Primitive::kPrimChar:
1160 case Primitive::kPrimShort:
1161 case Primitive::kPrimInt:
1162 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001163 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 break;
1165
1166 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1168 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 break;
1170
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 case Primitive::kPrimFloat:
1172 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001174 break;
1175
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 }
1179 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001180 __ cfi().RememberState();
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001181 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001183 __ cfi().RestoreState();
1184 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185}
1186
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001187void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001188 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001189 if (intrinsic.TryDispatch(invoke)) {
1190 return;
1191 }
1192
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193 HandleInvoke(invoke);
1194}
1195
Mark Mendell09ed1a32015-03-25 08:30:06 -04001196static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1197 if (invoke->GetLocations()->Intrinsified()) {
1198 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1199 intrinsic.Dispatch(invoke);
1200 return true;
1201 }
1202 return false;
1203}
1204
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001205void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001206 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1207 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001208 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001209
Mark Mendell09ed1a32015-03-25 08:30:06 -04001210 codegen_->GenerateStaticOrDirectCall(
1211 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001212}
1213
1214void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1215 HandleInvoke(invoke);
1216}
1217
1218void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001219 LocationSummary* locations =
1220 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001221 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001222
1223 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001225 HInstruction* input = invoke->InputAt(i);
1226 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1227 }
1228
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 switch (invoke->GetType()) {
1230 case Primitive::kPrimBoolean:
1231 case Primitive::kPrimByte:
1232 case Primitive::kPrimChar:
1233 case Primitive::kPrimShort:
1234 case Primitive::kPrimInt:
1235 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001236 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 break;
1238
1239 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001240 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 break;
1242
1243 case Primitive::kPrimVoid:
1244 break;
1245
1246 case Primitive::kPrimDouble:
1247 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001248 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 break;
1250 }
1251
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001252 invoke->SetLocations(locations);
1253}
1254
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001255void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001257 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1258 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1259 LocationSummary* locations = invoke->GetLocations();
1260 Location receiver = locations->InAt(0);
1261 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1262 // temp = object->GetClass();
1263 if (receiver.IsStackSlot()) {
1264 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1265 __ movl(temp, Address(temp, class_offset));
1266 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001267 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001269 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001270 // temp = temp->GetMethodAt(method_offset);
1271 __ movl(temp, Address(temp, method_offset));
1272 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001273 __ call(Address(
1274 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001275
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001276 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001277 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001278}
1279
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001280void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1281 HandleInvoke(invoke);
1282 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001283 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001284}
1285
1286void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1287 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001288 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001289 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1290 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1291 LocationSummary* locations = invoke->GetLocations();
1292 Location receiver = locations->InAt(0);
1293 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1294
1295 // Set the hidden argument.
1296 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001297 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001298
1299 // temp = object->GetClass();
1300 if (receiver.IsStackSlot()) {
1301 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1302 __ movl(temp, Address(temp, class_offset));
1303 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001304 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001305 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001306 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307 // temp = temp->GetImtEntryAt(method_offset);
1308 __ movl(temp, Address(temp, method_offset));
1309 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001310 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001311 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001312
1313 DCHECK(!codegen_->IsLeafMethod());
1314 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1315}
1316
Roland Levillain88cb1752014-10-20 16:36:47 +01001317void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1318 LocationSummary* locations =
1319 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1320 switch (neg->GetResultType()) {
1321 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001322 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001323 locations->SetInAt(0, Location::RequiresRegister());
1324 locations->SetOut(Location::SameAsFirstInput());
1325 break;
1326
Roland Levillain88cb1752014-10-20 16:36:47 +01001327 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001328 locations->SetInAt(0, Location::RequiresFpuRegister());
1329 locations->SetOut(Location::SameAsFirstInput());
1330 locations->AddTemp(Location::RequiresRegister());
1331 locations->AddTemp(Location::RequiresFpuRegister());
1332 break;
1333
Roland Levillain88cb1752014-10-20 16:36:47 +01001334 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001335 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001336 locations->SetOut(Location::SameAsFirstInput());
1337 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001338 break;
1339
1340 default:
1341 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1342 }
1343}
1344
1345void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1346 LocationSummary* locations = neg->GetLocations();
1347 Location out = locations->Out();
1348 Location in = locations->InAt(0);
1349 switch (neg->GetResultType()) {
1350 case Primitive::kPrimInt:
1351 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001352 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001353 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001354 break;
1355
1356 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001357 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001358 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001359 __ negl(out.AsRegisterPairLow<Register>());
1360 // Negation is similar to subtraction from zero. The least
1361 // significant byte triggers a borrow when it is different from
1362 // zero; to take it into account, add 1 to the most significant
1363 // byte if the carry flag (CF) is set to 1 after the first NEGL
1364 // operation.
1365 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1366 __ negl(out.AsRegisterPairHigh<Register>());
1367 break;
1368
Roland Levillain5368c212014-11-27 15:03:41 +00001369 case Primitive::kPrimFloat: {
1370 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 Register constant = locations->GetTemp(0).AsRegister<Register>();
1372 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001373 // Implement float negation with an exclusive or with value
1374 // 0x80000000 (mask for bit 31, representing the sign of a
1375 // single-precision floating-point number).
1376 __ movl(constant, Immediate(INT32_C(0x80000000)));
1377 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001378 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001379 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001380 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001381
Roland Levillain5368c212014-11-27 15:03:41 +00001382 case Primitive::kPrimDouble: {
1383 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001384 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001385 // Implement double negation with an exclusive or with value
1386 // 0x8000000000000000 (mask for bit 63, representing the sign of
1387 // a double-precision floating-point number).
1388 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001389 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001390 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001391 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001392
1393 default:
1394 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1395 }
1396}
1397
Roland Levillaindff1f282014-11-05 14:15:05 +00001398void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001399 Primitive::Type result_type = conversion->GetResultType();
1400 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001401 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001402
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001403 // The float-to-long and double-to-long type conversions rely on a
1404 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001405 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001406 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1407 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001408 ? LocationSummary::kCall
1409 : LocationSummary::kNoCall;
1410 LocationSummary* locations =
1411 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1412
David Brazdilb2bd1c52015-03-25 11:17:37 +00001413 // The Java language does not allow treating boolean as an integral type but
1414 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001415
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001417 case Primitive::kPrimByte:
1418 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001419 case Primitive::kPrimBoolean:
1420 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001421 case Primitive::kPrimShort:
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001424 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001425 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1426 // Make the output overlap to please the register allocator. This greatly simplifies
1427 // the validation of the linear scan implementation
1428 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001429 break;
1430
1431 default:
1432 LOG(FATAL) << "Unexpected type conversion from " << input_type
1433 << " to " << result_type;
1434 }
1435 break;
1436
Roland Levillain01a8d712014-11-14 16:27:39 +00001437 case Primitive::kPrimShort:
1438 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001439 case Primitive::kPrimBoolean:
1440 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001441 case Primitive::kPrimByte:
1442 case Primitive::kPrimInt:
1443 case Primitive::kPrimChar:
1444 // Processing a Dex `int-to-short' instruction.
1445 locations->SetInAt(0, Location::Any());
1446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1447 break;
1448
1449 default:
1450 LOG(FATAL) << "Unexpected type conversion from " << input_type
1451 << " to " << result_type;
1452 }
1453 break;
1454
Roland Levillain946e1432014-11-11 17:35:19 +00001455 case Primitive::kPrimInt:
1456 switch (input_type) {
1457 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001458 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001459 locations->SetInAt(0, Location::Any());
1460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1461 break;
1462
1463 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001464 // Processing a Dex `float-to-int' instruction.
1465 locations->SetInAt(0, Location::RequiresFpuRegister());
1466 locations->SetOut(Location::RequiresRegister());
1467 locations->AddTemp(Location::RequiresFpuRegister());
1468 break;
1469
Roland Levillain946e1432014-11-11 17:35:19 +00001470 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001471 // Processing a Dex `double-to-int' instruction.
1472 locations->SetInAt(0, Location::RequiresFpuRegister());
1473 locations->SetOut(Location::RequiresRegister());
1474 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001475 break;
1476
1477 default:
1478 LOG(FATAL) << "Unexpected type conversion from " << input_type
1479 << " to " << result_type;
1480 }
1481 break;
1482
Roland Levillaindff1f282014-11-05 14:15:05 +00001483 case Primitive::kPrimLong:
1484 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001485 case Primitive::kPrimBoolean:
1486 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001487 case Primitive::kPrimByte:
1488 case Primitive::kPrimShort:
1489 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001490 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001491 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001492 locations->SetInAt(0, Location::RegisterLocation(EAX));
1493 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1494 break;
1495
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001496 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001497 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001498 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001499 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001500 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1501 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1502
Vladimir Marko949c91f2015-01-27 10:48:44 +00001503 // The runtime helper puts the result in EAX, EDX.
1504 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001505 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001506 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001507
1508 default:
1509 LOG(FATAL) << "Unexpected type conversion from " << input_type
1510 << " to " << result_type;
1511 }
1512 break;
1513
Roland Levillain981e4542014-11-14 11:47:14 +00001514 case Primitive::kPrimChar:
1515 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001516 case Primitive::kPrimBoolean:
1517 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001518 case Primitive::kPrimByte:
1519 case Primitive::kPrimShort:
1520 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001521 // Processing a Dex `int-to-char' instruction.
1522 locations->SetInAt(0, Location::Any());
1523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1524 break;
1525
1526 default:
1527 LOG(FATAL) << "Unexpected type conversion from " << input_type
1528 << " to " << result_type;
1529 }
1530 break;
1531
Roland Levillaindff1f282014-11-05 14:15:05 +00001532 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001533 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001534 case Primitive::kPrimBoolean:
1535 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001536 case Primitive::kPrimByte:
1537 case Primitive::kPrimShort:
1538 case Primitive::kPrimInt:
1539 case Primitive::kPrimChar:
1540 // Processing a Dex `int-to-float' instruction.
1541 locations->SetInAt(0, Location::RequiresRegister());
1542 locations->SetOut(Location::RequiresFpuRegister());
1543 break;
1544
1545 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001546 // Processing a Dex `long-to-float' instruction.
1547 locations->SetInAt(0, Location::RequiresRegister());
1548 locations->SetOut(Location::RequiresFpuRegister());
1549 locations->AddTemp(Location::RequiresFpuRegister());
1550 locations->AddTemp(Location::RequiresFpuRegister());
1551 break;
1552
Roland Levillaincff13742014-11-17 14:32:17 +00001553 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001554 // Processing a Dex `double-to-float' instruction.
1555 locations->SetInAt(0, Location::RequiresFpuRegister());
1556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001557 break;
1558
1559 default:
1560 LOG(FATAL) << "Unexpected type conversion from " << input_type
1561 << " to " << result_type;
1562 };
1563 break;
1564
Roland Levillaindff1f282014-11-05 14:15:05 +00001565 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001566 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001567 case Primitive::kPrimBoolean:
1568 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001569 case Primitive::kPrimByte:
1570 case Primitive::kPrimShort:
1571 case Primitive::kPrimInt:
1572 case Primitive::kPrimChar:
1573 // Processing a Dex `int-to-double' instruction.
1574 locations->SetInAt(0, Location::RequiresRegister());
1575 locations->SetOut(Location::RequiresFpuRegister());
1576 break;
1577
1578 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001579 // Processing a Dex `long-to-double' instruction.
1580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetOut(Location::RequiresFpuRegister());
1582 locations->AddTemp(Location::RequiresFpuRegister());
1583 locations->AddTemp(Location::RequiresFpuRegister());
1584 break;
1585
Roland Levillaincff13742014-11-17 14:32:17 +00001586 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001587 // Processing a Dex `float-to-double' instruction.
1588 locations->SetInAt(0, Location::RequiresFpuRegister());
1589 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001590 break;
1591
1592 default:
1593 LOG(FATAL) << "Unexpected type conversion from " << input_type
1594 << " to " << result_type;
1595 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001596 break;
1597
1598 default:
1599 LOG(FATAL) << "Unexpected type conversion from " << input_type
1600 << " to " << result_type;
1601 }
1602}
1603
1604void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1605 LocationSummary* locations = conversion->GetLocations();
1606 Location out = locations->Out();
1607 Location in = locations->InAt(0);
1608 Primitive::Type result_type = conversion->GetResultType();
1609 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001610 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001611 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001612 case Primitive::kPrimByte:
1613 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001614 case Primitive::kPrimBoolean:
1615 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001616 case Primitive::kPrimShort:
1617 case Primitive::kPrimInt:
1618 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001619 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001620 if (in.IsRegister()) {
1621 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001622 } else {
1623 DCHECK(in.GetConstant()->IsIntConstant());
1624 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1625 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1626 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001627 break;
1628
1629 default:
1630 LOG(FATAL) << "Unexpected type conversion from " << input_type
1631 << " to " << result_type;
1632 }
1633 break;
1634
Roland Levillain01a8d712014-11-14 16:27:39 +00001635 case Primitive::kPrimShort:
1636 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001637 case Primitive::kPrimBoolean:
1638 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001639 case Primitive::kPrimByte:
1640 case Primitive::kPrimInt:
1641 case Primitive::kPrimChar:
1642 // Processing a Dex `int-to-short' instruction.
1643 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001644 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001645 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001646 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001647 } else {
1648 DCHECK(in.GetConstant()->IsIntConstant());
1649 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001650 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001651 }
1652 break;
1653
1654 default:
1655 LOG(FATAL) << "Unexpected type conversion from " << input_type
1656 << " to " << result_type;
1657 }
1658 break;
1659
Roland Levillain946e1432014-11-11 17:35:19 +00001660 case Primitive::kPrimInt:
1661 switch (input_type) {
1662 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001663 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001664 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001666 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001668 } else {
1669 DCHECK(in.IsConstant());
1670 DCHECK(in.GetConstant()->IsLongConstant());
1671 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001673 }
1674 break;
1675
Roland Levillain3f8f9362014-12-02 17:45:01 +00001676 case Primitive::kPrimFloat: {
1677 // Processing a Dex `float-to-int' instruction.
1678 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1679 Register output = out.AsRegister<Register>();
1680 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1681 Label done, nan;
1682
1683 __ movl(output, Immediate(kPrimIntMax));
1684 // temp = int-to-float(output)
1685 __ cvtsi2ss(temp, output);
1686 // if input >= temp goto done
1687 __ comiss(input, temp);
1688 __ j(kAboveEqual, &done);
1689 // if input == NaN goto nan
1690 __ j(kUnordered, &nan);
1691 // output = float-to-int-truncate(input)
1692 __ cvttss2si(output, input);
1693 __ jmp(&done);
1694 __ Bind(&nan);
1695 // output = 0
1696 __ xorl(output, output);
1697 __ Bind(&done);
1698 break;
1699 }
1700
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001701 case Primitive::kPrimDouble: {
1702 // Processing a Dex `double-to-int' instruction.
1703 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1704 Register output = out.AsRegister<Register>();
1705 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1706 Label done, nan;
1707
1708 __ movl(output, Immediate(kPrimIntMax));
1709 // temp = int-to-double(output)
1710 __ cvtsi2sd(temp, output);
1711 // if input >= temp goto done
1712 __ comisd(input, temp);
1713 __ j(kAboveEqual, &done);
1714 // if input == NaN goto nan
1715 __ j(kUnordered, &nan);
1716 // output = double-to-int-truncate(input)
1717 __ cvttsd2si(output, input);
1718 __ jmp(&done);
1719 __ Bind(&nan);
1720 // output = 0
1721 __ xorl(output, output);
1722 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001723 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001724 }
Roland Levillain946e1432014-11-11 17:35:19 +00001725
1726 default:
1727 LOG(FATAL) << "Unexpected type conversion from " << input_type
1728 << " to " << result_type;
1729 }
1730 break;
1731
Roland Levillaindff1f282014-11-05 14:15:05 +00001732 case Primitive::kPrimLong:
1733 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001734 case Primitive::kPrimBoolean:
1735 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001736 case Primitive::kPrimByte:
1737 case Primitive::kPrimShort:
1738 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001739 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001740 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001741 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1742 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001743 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 __ cdq();
1745 break;
1746
1747 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001748 // Processing a Dex `float-to-long' instruction.
1749 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001750 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1751 break;
1752
Roland Levillaindff1f282014-11-05 14:15:05 +00001753 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001754 // Processing a Dex `double-to-long' instruction.
1755 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1756 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001757 break;
1758
1759 default:
1760 LOG(FATAL) << "Unexpected type conversion from " << input_type
1761 << " to " << result_type;
1762 }
1763 break;
1764
Roland Levillain981e4542014-11-14 11:47:14 +00001765 case Primitive::kPrimChar:
1766 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001767 case Primitive::kPrimBoolean:
1768 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001769 case Primitive::kPrimByte:
1770 case Primitive::kPrimShort:
1771 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001772 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1773 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001774 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001775 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001776 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001777 } else {
1778 DCHECK(in.GetConstant()->IsIntConstant());
1779 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001781 }
1782 break;
1783
1784 default:
1785 LOG(FATAL) << "Unexpected type conversion from " << input_type
1786 << " to " << result_type;
1787 }
1788 break;
1789
Roland Levillaindff1f282014-11-05 14:15:05 +00001790 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001791 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001792 case Primitive::kPrimBoolean:
1793 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001794 case Primitive::kPrimByte:
1795 case Primitive::kPrimShort:
1796 case Primitive::kPrimInt:
1797 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001798 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001799 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001800 break;
1801
Roland Levillain6d0e4832014-11-27 18:31:21 +00001802 case Primitive::kPrimLong: {
1803 // Processing a Dex `long-to-float' instruction.
1804 Register low = in.AsRegisterPairLow<Register>();
1805 Register high = in.AsRegisterPairHigh<Register>();
1806 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1807 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1808 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1809
1810 // Operations use doubles for precision reasons (each 32-bit
1811 // half of a long fits in the 53-bit mantissa of a double,
1812 // but not in the 24-bit mantissa of a float). This is
1813 // especially important for the low bits. The result is
1814 // eventually converted to float.
1815
1816 // low = low - 2^31 (to prevent bit 31 of `low` to be
1817 // interpreted as a sign bit)
1818 __ subl(low, Immediate(0x80000000));
1819 // temp = int-to-double(high)
1820 __ cvtsi2sd(temp, high);
1821 // temp = temp * 2^32
1822 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1823 __ mulsd(temp, constant);
1824 // result = int-to-double(low)
1825 __ cvtsi2sd(result, low);
1826 // result = result + 2^31 (restore the original value of `low`)
1827 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1828 __ addsd(result, constant);
1829 // result = result + temp
1830 __ addsd(result, temp);
1831 // result = double-to-float(result)
1832 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001833 // Restore low.
1834 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001835 break;
1836 }
1837
Roland Levillaincff13742014-11-17 14:32:17 +00001838 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001839 // Processing a Dex `double-to-float' instruction.
1840 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001841 break;
1842
1843 default:
1844 LOG(FATAL) << "Unexpected type conversion from " << input_type
1845 << " to " << result_type;
1846 };
1847 break;
1848
Roland Levillaindff1f282014-11-05 14:15:05 +00001849 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001850 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001851 case Primitive::kPrimBoolean:
1852 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001853 case Primitive::kPrimByte:
1854 case Primitive::kPrimShort:
1855 case Primitive::kPrimInt:
1856 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001857 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001858 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001859 break;
1860
Roland Levillain647b9ed2014-11-27 12:06:00 +00001861 case Primitive::kPrimLong: {
1862 // Processing a Dex `long-to-double' instruction.
1863 Register low = in.AsRegisterPairLow<Register>();
1864 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001865 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1866 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1867 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001868
Roland Levillain647b9ed2014-11-27 12:06:00 +00001869 // low = low - 2^31 (to prevent bit 31 of `low` to be
1870 // interpreted as a sign bit)
1871 __ subl(low, Immediate(0x80000000));
1872 // temp = int-to-double(high)
1873 __ cvtsi2sd(temp, high);
1874 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001875 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876 __ mulsd(temp, constant);
1877 // result = int-to-double(low)
1878 __ cvtsi2sd(result, low);
1879 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001880 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 __ addsd(result, constant);
1882 // result = result + temp
1883 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001884 // Restore low.
1885 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001886 break;
1887 }
1888
Roland Levillaincff13742014-11-17 14:32:17 +00001889 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001890 // Processing a Dex `float-to-double' instruction.
1891 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected type conversion from " << input_type
1896 << " to " << result_type;
1897 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001898 break;
1899
1900 default:
1901 LOG(FATAL) << "Unexpected type conversion from " << input_type
1902 << " to " << result_type;
1903 }
1904}
1905
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001906void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001907 LocationSummary* locations =
1908 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001909 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001910 case Primitive::kPrimInt: {
1911 locations->SetInAt(0, Location::RequiresRegister());
1912 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1914 break;
1915 }
1916
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001918 locations->SetInAt(0, Location::RequiresRegister());
1919 locations->SetInAt(1, Location::Any());
1920 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 break;
1922 }
1923
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 case Primitive::kPrimFloat:
1925 case Primitive::kPrimDouble: {
1926 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001927 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001928 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001932 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1934 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001935 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001936}
1937
1938void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1939 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 Location first = locations->InAt(0);
1941 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001942 Location out = locations->Out();
1943
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001944 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001946 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001947 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1948 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1949 } else {
1950 __ leal(out.AsRegister<Register>(), Address(
1951 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1952 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001954 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1955 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1956 __ addl(out.AsRegister<Register>(), Immediate(value));
1957 } else {
1958 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1959 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001960 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001961 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001963 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001964 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 }
1966
1967 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001968 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001969 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1970 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001971 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001972 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1973 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001974 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001975 } else {
1976 DCHECK(second.IsConstant()) << second;
1977 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1978 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1979 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001980 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001981 break;
1982 }
1983
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 case Primitive::kPrimFloat: {
1985 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001986 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001988 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001989 }
1990
1991 case Primitive::kPrimDouble: {
1992 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001993 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 }
1995 break;
1996 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001998 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002000 }
2001}
2002
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002003void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002004 LocationSummary* locations =
2005 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002006 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002007 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetInAt(1, Location::Any());
2011 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012 break;
2013 }
Calin Juravle11351682014-10-23 15:38:15 +01002014 case Primitive::kPrimFloat:
2015 case Primitive::kPrimDouble: {
2016 locations->SetInAt(0, Location::RequiresFpuRegister());
2017 locations->SetInAt(1, Location::RequiresFpuRegister());
2018 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019 break;
Calin Juravle11351682014-10-23 15:38:15 +01002020 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002022 default:
Calin Juravle11351682014-10-23 15:38:15 +01002023 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002024 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002025}
2026
2027void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2028 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 Location first = locations->InAt(0);
2030 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002031 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002032 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002034 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002036 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002037 __ subl(first.AsRegister<Register>(),
2038 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002039 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002041 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043 }
2044
2045 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002046 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002047 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2048 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002049 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002050 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 __ sbbl(first.AsRegisterPairHigh<Register>(),
2052 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002053 } else {
2054 DCHECK(second.IsConstant()) << second;
2055 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2056 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2057 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059 break;
2060 }
2061
Calin Juravle11351682014-10-23 15:38:15 +01002062 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064 break;
Calin Juravle11351682014-10-23 15:38:15 +01002065 }
2066
2067 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002069 break;
2070 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002071
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002072 default:
Calin Juravle11351682014-10-23 15:38:15 +01002073 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002074 }
2075}
2076
Calin Juravle34bacdf2014-10-07 20:23:36 +01002077void LocationsBuilderX86::VisitMul(HMul* mul) {
2078 LocationSummary* locations =
2079 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2080 switch (mul->GetResultType()) {
2081 case Primitive::kPrimInt:
2082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetInAt(1, Location::Any());
2084 locations->SetOut(Location::SameAsFirstInput());
2085 break;
2086 case Primitive::kPrimLong: {
2087 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002088 locations->SetInAt(1, Location::Any());
2089 locations->SetOut(Location::SameAsFirstInput());
2090 // Needed for imul on 32bits with 64bits output.
2091 locations->AddTemp(Location::RegisterLocation(EAX));
2092 locations->AddTemp(Location::RegisterLocation(EDX));
2093 break;
2094 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002095 case Primitive::kPrimFloat:
2096 case Primitive::kPrimDouble: {
2097 locations->SetInAt(0, Location::RequiresFpuRegister());
2098 locations->SetInAt(1, Location::RequiresFpuRegister());
2099 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002100 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002101 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102
2103 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002104 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002105 }
2106}
2107
2108void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2109 LocationSummary* locations = mul->GetLocations();
2110 Location first = locations->InAt(0);
2111 Location second = locations->InAt(1);
2112 DCHECK(first.Equals(locations->Out()));
2113
2114 switch (mul->GetResultType()) {
2115 case Primitive::kPrimInt: {
2116 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118 } else if (second.IsConstant()) {
2119 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002121 } else {
2122 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002124 }
2125 break;
2126 }
2127
2128 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002129 Register in1_hi = first.AsRegisterPairHigh<Register>();
2130 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 Register eax = locations->GetTemp(0).AsRegister<Register>();
2132 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133
2134 DCHECK_EQ(EAX, eax);
2135 DCHECK_EQ(EDX, edx);
2136
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002137 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 // output: in1
2139 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2140 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2141 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002142 if (second.IsConstant()) {
2143 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002145 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2146 int32_t low_value = Low32Bits(value);
2147 int32_t high_value = High32Bits(value);
2148 Immediate low(low_value);
2149 Immediate high(high_value);
2150
2151 __ movl(eax, high);
2152 // eax <- in1.lo * in2.hi
2153 __ imull(eax, in1_lo);
2154 // in1.hi <- in1.hi * in2.lo
2155 __ imull(in1_hi, low);
2156 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2157 __ addl(in1_hi, eax);
2158 // move in2_lo to eax to prepare for double precision
2159 __ movl(eax, low);
2160 // edx:eax <- in1.lo * in2.lo
2161 __ mull(in1_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 if (second.IsRegisterPair()) {
2167 Register in2_hi = second.AsRegisterPairHigh<Register>();
2168 Register in2_lo = second.AsRegisterPairLow<Register>();
2169
2170 __ movl(eax, in2_hi);
2171 // eax <- in1.lo * in2.hi
2172 __ imull(eax, in1_lo);
2173 // in1.hi <- in1.hi * in2.lo
2174 __ imull(in1_hi, in2_lo);
2175 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2176 __ addl(in1_hi, eax);
2177 // move in1_lo to eax to prepare for double precision
2178 __ movl(eax, in1_lo);
2179 // edx:eax <- in1.lo * in2.lo
2180 __ mull(in2_lo);
2181 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2182 __ addl(in1_hi, edx);
2183 // in1.lo <- (in1.lo * in2.lo)[31:0];
2184 __ movl(in1_lo, eax);
2185 } else {
2186 DCHECK(second.IsDoubleStackSlot()) << second;
2187 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2188 Address in2_lo(ESP, second.GetStackIndex());
2189
2190 __ movl(eax, in2_hi);
2191 // eax <- in1.lo * in2.hi
2192 __ imull(eax, in1_lo);
2193 // in1.hi <- in1.hi * in2.lo
2194 __ imull(in1_hi, in2_lo);
2195 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2196 __ addl(in1_hi, eax);
2197 // move in1_lo to eax to prepare for double precision
2198 __ movl(eax, in1_lo);
2199 // edx:eax <- in1.lo * in2.lo
2200 __ mull(in2_lo);
2201 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2202 __ addl(in1_hi, edx);
2203 // in1.lo <- (in1.lo * in2.lo)[31:0];
2204 __ movl(in1_lo, eax);
2205 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002206
2207 break;
2208 }
2209
Calin Juravleb5bfa962014-10-21 18:02:24 +01002210 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002213 }
2214
2215 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002216 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002217 break;
2218 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002219
2220 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222 }
2223}
2224
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002225void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2226 uint32_t stack_adjustment, bool is_float) {
2227 if (source.IsStackSlot()) {
2228 DCHECK(is_float);
2229 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2230 } else if (source.IsDoubleStackSlot()) {
2231 DCHECK(!is_float);
2232 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2233 } else {
2234 // Write the value to the temporary location on the stack and load to FP stack.
2235 if (is_float) {
2236 Location stack_temp = Location::StackSlot(temp_offset);
2237 codegen_->Move32(stack_temp, source);
2238 __ flds(Address(ESP, temp_offset));
2239 } else {
2240 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2241 codegen_->Move64(stack_temp, source);
2242 __ fldl(Address(ESP, temp_offset));
2243 }
2244 }
2245}
2246
2247void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2248 Primitive::Type type = rem->GetResultType();
2249 bool is_float = type == Primitive::kPrimFloat;
2250 size_t elem_size = Primitive::ComponentSize(type);
2251 LocationSummary* locations = rem->GetLocations();
2252 Location first = locations->InAt(0);
2253 Location second = locations->InAt(1);
2254 Location out = locations->Out();
2255
2256 // Create stack space for 2 elements.
2257 // TODO: enhance register allocator to ask for stack temporaries.
2258 __ subl(ESP, Immediate(2 * elem_size));
2259
2260 // Load the values to the FP stack in reverse order, using temporaries if needed.
2261 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2262 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2263
2264 // Loop doing FPREM until we stabilize.
2265 Label retry;
2266 __ Bind(&retry);
2267 __ fprem();
2268
2269 // Move FP status to AX.
2270 __ fstsw();
2271
2272 // And see if the argument reduction is complete. This is signaled by the
2273 // C2 FPU flag bit set to 0.
2274 __ andl(EAX, Immediate(kC2ConditionMask));
2275 __ j(kNotEqual, &retry);
2276
2277 // We have settled on the final value. Retrieve it into an XMM register.
2278 // Store FP top of stack to real stack.
2279 if (is_float) {
2280 __ fsts(Address(ESP, 0));
2281 } else {
2282 __ fstl(Address(ESP, 0));
2283 }
2284
2285 // Pop the 2 items from the FP stack.
2286 __ fucompp();
2287
2288 // Load the value from the stack into an XMM register.
2289 DCHECK(out.IsFpuRegister()) << out;
2290 if (is_float) {
2291 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2292 } else {
2293 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2294 }
2295
2296 // And remove the temporary stack space we allocated.
2297 __ addl(ESP, Immediate(2 * elem_size));
2298}
2299
Calin Juravlebacfec32014-11-14 15:54:36 +00002300void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2301 DCHECK(instruction->IsDiv() || instruction->IsRem());
2302
2303 LocationSummary* locations = instruction->GetLocations();
2304 Location out = locations->Out();
2305 Location first = locations->InAt(0);
2306 Location second = locations->InAt(1);
2307 bool is_div = instruction->IsDiv();
2308
2309 switch (instruction->GetResultType()) {
2310 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002311 Register second_reg = second.AsRegister<Register>();
2312 DCHECK_EQ(EAX, first.AsRegister<Register>());
2313 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002314
2315 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002316 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2317 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002318 codegen_->AddSlowPath(slow_path);
2319
2320 // 0x80000000/-1 triggers an arithmetic exception!
2321 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2322 // it's safe to just use negl instead of more complex comparisons.
2323
2324 __ cmpl(second_reg, Immediate(-1));
2325 __ j(kEqual, slow_path->GetEntryLabel());
2326
2327 // edx:eax <- sign-extended of eax
2328 __ cdq();
2329 // eax = quotient, edx = remainder
2330 __ idivl(second_reg);
2331
2332 __ Bind(slow_path->GetExitLabel());
2333 break;
2334 }
2335
2336 case Primitive::kPrimLong: {
2337 InvokeRuntimeCallingConvention calling_convention;
2338 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2339 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2340 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2341 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2342 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2343 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2344
2345 if (is_div) {
2346 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2347 } else {
2348 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2349 }
2350 uint32_t dex_pc = is_div
2351 ? instruction->AsDiv()->GetDexPc()
2352 : instruction->AsRem()->GetDexPc();
2353 codegen_->RecordPcInfo(instruction, dex_pc);
2354
2355 break;
2356 }
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2360 }
2361}
2362
Calin Juravle7c4954d2014-10-28 16:57:40 +00002363void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002364 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002365 ? LocationSummary::kCall
2366 : LocationSummary::kNoCall;
2367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2368
Calin Juravle7c4954d2014-10-28 16:57:40 +00002369 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002370 case Primitive::kPrimInt: {
2371 locations->SetInAt(0, Location::RegisterLocation(EAX));
2372 locations->SetInAt(1, Location::RequiresRegister());
2373 locations->SetOut(Location::SameAsFirstInput());
2374 // Intel uses edx:eax as the dividend.
2375 locations->AddTemp(Location::RegisterLocation(EDX));
2376 break;
2377 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002378 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002379 InvokeRuntimeCallingConvention calling_convention;
2380 locations->SetInAt(0, Location::RegisterPairLocation(
2381 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2382 locations->SetInAt(1, Location::RegisterPairLocation(
2383 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2384 // Runtime helper puts the result in EAX, EDX.
2385 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002386 break;
2387 }
2388 case Primitive::kPrimFloat:
2389 case Primitive::kPrimDouble: {
2390 locations->SetInAt(0, Location::RequiresFpuRegister());
2391 locations->SetInAt(1, Location::RequiresFpuRegister());
2392 locations->SetOut(Location::SameAsFirstInput());
2393 break;
2394 }
2395
2396 default:
2397 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2398 }
2399}
2400
2401void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2402 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002403 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002404 Location first = locations->InAt(0);
2405 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002406
2407 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002408 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002409 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002410 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002411 break;
2412 }
2413
2414 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002415 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002417 break;
2418 }
2419
2420 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002421 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002422 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002423 break;
2424 }
2425
2426 default:
2427 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2428 }
2429}
2430
Calin Juravlebacfec32014-11-14 15:54:36 +00002431void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002432 Primitive::Type type = rem->GetResultType();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002433 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2434 ? LocationSummary::kCall
2435 : LocationSummary::kNoCall;
2436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002437
Calin Juravled2ec87d2014-12-08 14:24:46 +00002438 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002439 case Primitive::kPrimInt: {
2440 locations->SetInAt(0, Location::RegisterLocation(EAX));
2441 locations->SetInAt(1, Location::RequiresRegister());
2442 locations->SetOut(Location::RegisterLocation(EDX));
2443 break;
2444 }
2445 case Primitive::kPrimLong: {
2446 InvokeRuntimeCallingConvention calling_convention;
2447 locations->SetInAt(0, Location::RegisterPairLocation(
2448 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2449 locations->SetInAt(1, Location::RegisterPairLocation(
2450 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2451 // Runtime helper puts the result in EAX, EDX.
2452 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2453 break;
2454 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002455 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002456 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002457 locations->SetInAt(0, Location::Any());
2458 locations->SetInAt(1, Location::Any());
2459 locations->SetOut(Location::RequiresFpuRegister());
2460 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002461 break;
2462 }
2463
2464 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002465 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002466 }
2467}
2468
2469void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2470 Primitive::Type type = rem->GetResultType();
2471 switch (type) {
2472 case Primitive::kPrimInt:
2473 case Primitive::kPrimLong: {
2474 GenerateDivRemIntegral(rem);
2475 break;
2476 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002477 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002478 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002479 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002480 break;
2481 }
2482 default:
2483 LOG(FATAL) << "Unexpected rem type " << type;
2484 }
2485}
2486
Calin Juravled0d48522014-11-04 16:40:20 +00002487void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2488 LocationSummary* locations =
2489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002490 switch (instruction->GetType()) {
2491 case Primitive::kPrimInt: {
2492 locations->SetInAt(0, Location::Any());
2493 break;
2494 }
2495 case Primitive::kPrimLong: {
2496 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2497 if (!instruction->IsConstant()) {
2498 locations->AddTemp(Location::RequiresRegister());
2499 }
2500 break;
2501 }
2502 default:
2503 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2504 }
Calin Juravled0d48522014-11-04 16:40:20 +00002505 if (instruction->HasUses()) {
2506 locations->SetOut(Location::SameAsFirstInput());
2507 }
2508}
2509
2510void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2511 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2512 codegen_->AddSlowPath(slow_path);
2513
2514 LocationSummary* locations = instruction->GetLocations();
2515 Location value = locations->InAt(0);
2516
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002517 switch (instruction->GetType()) {
2518 case Primitive::kPrimInt: {
2519 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002520 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002521 __ j(kEqual, slow_path->GetEntryLabel());
2522 } else if (value.IsStackSlot()) {
2523 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2524 __ j(kEqual, slow_path->GetEntryLabel());
2525 } else {
2526 DCHECK(value.IsConstant()) << value;
2527 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2528 __ jmp(slow_path->GetEntryLabel());
2529 }
2530 }
2531 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002532 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002533 case Primitive::kPrimLong: {
2534 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002535 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002536 __ movl(temp, value.AsRegisterPairLow<Register>());
2537 __ orl(temp, value.AsRegisterPairHigh<Register>());
2538 __ j(kEqual, slow_path->GetEntryLabel());
2539 } else {
2540 DCHECK(value.IsConstant()) << value;
2541 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2542 __ jmp(slow_path->GetEntryLabel());
2543 }
2544 }
2545 break;
2546 }
2547 default:
2548 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002549 }
Calin Juravled0d48522014-11-04 16:40:20 +00002550}
2551
Calin Juravle9aec02f2014-11-18 23:06:35 +00002552void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2553 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2554
2555 LocationSummary* locations =
2556 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2557
2558 switch (op->GetResultType()) {
2559 case Primitive::kPrimInt: {
2560 locations->SetInAt(0, Location::RequiresRegister());
2561 // The shift count needs to be in CL.
2562 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2563 locations->SetOut(Location::SameAsFirstInput());
2564 break;
2565 }
2566 case Primitive::kPrimLong: {
2567 locations->SetInAt(0, Location::RequiresRegister());
2568 // The shift count needs to be in CL.
2569 locations->SetInAt(1, Location::RegisterLocation(ECX));
2570 locations->SetOut(Location::SameAsFirstInput());
2571 break;
2572 }
2573 default:
2574 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2575 }
2576}
2577
2578void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2579 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2580
2581 LocationSummary* locations = op->GetLocations();
2582 Location first = locations->InAt(0);
2583 Location second = locations->InAt(1);
2584 DCHECK(first.Equals(locations->Out()));
2585
2586 switch (op->GetResultType()) {
2587 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002588 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002589 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002591 DCHECK_EQ(ECX, second_reg);
2592 if (op->IsShl()) {
2593 __ shll(first_reg, second_reg);
2594 } else if (op->IsShr()) {
2595 __ sarl(first_reg, second_reg);
2596 } else {
2597 __ shrl(first_reg, second_reg);
2598 }
2599 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002600 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002601 if (op->IsShl()) {
2602 __ shll(first_reg, imm);
2603 } else if (op->IsShr()) {
2604 __ sarl(first_reg, imm);
2605 } else {
2606 __ shrl(first_reg, imm);
2607 }
2608 }
2609 break;
2610 }
2611 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002612 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002613 DCHECK_EQ(ECX, second_reg);
2614 if (op->IsShl()) {
2615 GenerateShlLong(first, second_reg);
2616 } else if (op->IsShr()) {
2617 GenerateShrLong(first, second_reg);
2618 } else {
2619 GenerateUShrLong(first, second_reg);
2620 }
2621 break;
2622 }
2623 default:
2624 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2625 }
2626}
2627
2628void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2629 Label done;
2630 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2631 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2632 __ testl(shifter, Immediate(32));
2633 __ j(kEqual, &done);
2634 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2635 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2636 __ Bind(&done);
2637}
2638
2639void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2640 Label done;
2641 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2642 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2643 __ testl(shifter, Immediate(32));
2644 __ j(kEqual, &done);
2645 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2646 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2647 __ Bind(&done);
2648}
2649
2650void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2651 Label done;
2652 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2653 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2654 __ testl(shifter, Immediate(32));
2655 __ j(kEqual, &done);
2656 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2657 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2658 __ Bind(&done);
2659}
2660
2661void LocationsBuilderX86::VisitShl(HShl* shl) {
2662 HandleShift(shl);
2663}
2664
2665void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2666 HandleShift(shl);
2667}
2668
2669void LocationsBuilderX86::VisitShr(HShr* shr) {
2670 HandleShift(shr);
2671}
2672
2673void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2674 HandleShift(shr);
2675}
2676
2677void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2678 HandleShift(ushr);
2679}
2680
2681void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2682 HandleShift(ushr);
2683}
2684
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002685void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002686 LocationSummary* locations =
2687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002688 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002689 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002690 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2691 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002692}
2693
2694void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2695 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002696 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002697 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002698
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002699 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002700
Nicolas Geoffray39468442014-09-02 15:17:15 +01002701 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002702 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002703}
2704
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002705void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2708 locations->SetOut(Location::RegisterLocation(EAX));
2709 InvokeRuntimeCallingConvention calling_convention;
2710 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002711 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2712 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002713}
2714
2715void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2716 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002717 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002718 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2719
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002720 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002721
2722 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2723 DCHECK(!codegen_->IsLeafMethod());
2724}
2725
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002726void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002727 LocationSummary* locations =
2728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002729 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2730 if (location.IsStackSlot()) {
2731 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2732 } else if (location.IsDoubleStackSlot()) {
2733 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002734 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002735 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002736}
2737
2738void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002739 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002740}
2741
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002742void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002743 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002744 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002745 locations->SetInAt(0, Location::RequiresRegister());
2746 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002747}
2748
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002749void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2750 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002751 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002752 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002753 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002754 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002755 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002757 break;
2758
2759 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002760 __ notl(out.AsRegisterPairLow<Register>());
2761 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2766 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002767}
2768
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002769void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002770 LocationSummary* locations =
2771 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002772 switch (compare->InputAt(0)->GetType()) {
2773 case Primitive::kPrimLong: {
2774 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002775 locations->SetInAt(1, Location::Any());
2776 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2777 break;
2778 }
2779 case Primitive::kPrimFloat:
2780 case Primitive::kPrimDouble: {
2781 locations->SetInAt(0, Location::RequiresFpuRegister());
2782 locations->SetInAt(1, Location::RequiresFpuRegister());
2783 locations->SetOut(Location::RequiresRegister());
2784 break;
2785 }
2786 default:
2787 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2788 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002789}
2790
2791void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002792 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002794 Location left = locations->InAt(0);
2795 Location right = locations->InAt(1);
2796
2797 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002798 switch (compare->InputAt(0)->GetType()) {
2799 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002800 Register left_low = left.AsRegisterPairLow<Register>();
2801 Register left_high = left.AsRegisterPairHigh<Register>();
2802 int32_t val_low = 0;
2803 int32_t val_high = 0;
2804 bool right_is_const = false;
2805
2806 if (right.IsConstant()) {
2807 DCHECK(right.GetConstant()->IsLongConstant());
2808 right_is_const = true;
2809 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2810 val_low = Low32Bits(val);
2811 val_high = High32Bits(val);
2812 }
2813
Calin Juravleddb7df22014-11-25 20:56:51 +00002814 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002815 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002816 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002817 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002818 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002819 DCHECK(right_is_const) << right;
2820 if (val_high == 0) {
2821 __ testl(left_high, left_high);
2822 } else {
2823 __ cmpl(left_high, Immediate(val_high));
2824 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002825 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002826 __ j(kLess, &less); // Signed compare.
2827 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002828 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002829 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002830 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002831 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002832 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002833 DCHECK(right_is_const) << right;
2834 if (val_low == 0) {
2835 __ testl(left_low, left_low);
2836 } else {
2837 __ cmpl(left_low, Immediate(val_low));
2838 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002839 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002840 break;
2841 }
2842 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002843 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002844 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2845 break;
2846 }
2847 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002849 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002850 break;
2851 }
2852 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002853 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002854 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002855 __ movl(out, Immediate(0));
2856 __ j(kEqual, &done);
2857 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2858
2859 __ Bind(&greater);
2860 __ movl(out, Immediate(1));
2861 __ jmp(&done);
2862
2863 __ Bind(&less);
2864 __ movl(out, Immediate(-1));
2865
2866 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002867}
2868
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002869void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002870 LocationSummary* locations =
2871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002872 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2873 locations->SetInAt(i, Location::Any());
2874 }
2875 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002876}
2877
2878void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002879 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002880 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002881}
2882
Calin Juravle52c48962014-12-16 17:02:57 +00002883void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2884 /*
2885 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2886 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2887 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2888 */
2889 switch (kind) {
2890 case MemBarrierKind::kAnyAny: {
2891 __ mfence();
2892 break;
2893 }
2894 case MemBarrierKind::kAnyStore:
2895 case MemBarrierKind::kLoadAny:
2896 case MemBarrierKind::kStoreStore: {
2897 // nop
2898 break;
2899 }
2900 default:
2901 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002902 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002903}
2904
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002905
Mark Mendell09ed1a32015-03-25 08:30:06 -04002906void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
2907 Register temp) {
2908 // TODO: Implement all kinds of calls:
2909 // 1) boot -> boot
2910 // 2) app -> boot
2911 // 3) app -> app
2912 //
2913 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2914 // temp = method;
2915 LoadCurrentMethod(temp);
2916 if (!invoke->IsRecursive()) {
2917 // temp = temp->dex_cache_resolved_methods_;
2918 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2919 // temp = temp[index_in_cache]
2920 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
2921 // (temp + offset_of_quick_compiled_code)()
2922 __ call(Address(
2923 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
2924 } else {
2925 __ call(GetFrameEntryLabel());
2926 }
2927
2928 DCHECK(!IsLeafMethod());
2929 RecordPcInfo(invoke, invoke->GetDexPc());
2930}
2931
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002932void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002933 Label is_null;
2934 __ testl(value, value);
2935 __ j(kEqual, &is_null);
2936 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2937 __ movl(temp, object);
2938 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002939 __ movb(Address(temp, card, TIMES_1, 0),
2940 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 __ Bind(&is_null);
2942}
2943
Calin Juravle52c48962014-12-16 17:02:57 +00002944void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2945 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002946 LocationSummary* locations =
2947 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002948 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002949
2950 // The output overlaps in case of long: we don't want the low move to overwrite
2951 // the object's location.
2952 locations->SetOut(Location::RequiresRegister(),
2953 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
2954 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002955
2956 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2957 // Long values can be loaded atomically into an XMM using movsd.
2958 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2959 // and then copy the XMM into the output 32bits at a time).
2960 locations->AddTemp(Location::RequiresFpuRegister());
2961 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002962}
2963
Calin Juravle52c48962014-12-16 17:02:57 +00002964void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2965 const FieldInfo& field_info) {
2966 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002967
Calin Juravle52c48962014-12-16 17:02:57 +00002968 LocationSummary* locations = instruction->GetLocations();
2969 Register base = locations->InAt(0).AsRegister<Register>();
2970 Location out = locations->Out();
2971 bool is_volatile = field_info.IsVolatile();
2972 Primitive::Type field_type = field_info.GetFieldType();
2973 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2974
2975 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002976 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002977 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002978 break;
2979 }
2980
2981 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002982 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002983 break;
2984 }
2985
2986 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002987 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002988 break;
2989 }
2990
2991 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002992 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002993 break;
2994 }
2995
2996 case Primitive::kPrimInt:
2997 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002998 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002999 break;
3000 }
3001
3002 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003003 if (is_volatile) {
3004 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3005 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003006 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003007 __ movd(out.AsRegisterPairLow<Register>(), temp);
3008 __ psrlq(temp, Immediate(32));
3009 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3010 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003011 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003012 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003013 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003014 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3015 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003016 break;
3017 }
3018
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003019 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003020 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003021 break;
3022 }
3023
3024 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003025 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003026 break;
3027 }
3028
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003029 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003030 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003031 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003032 }
Calin Juravle52c48962014-12-16 17:02:57 +00003033
Calin Juravle77520bc2015-01-12 18:45:46 +00003034 // Longs are handled in the switch.
3035 if (field_type != Primitive::kPrimLong) {
3036 codegen_->MaybeRecordImplicitNullCheck(instruction);
3037 }
3038
Calin Juravle52c48962014-12-16 17:02:57 +00003039 if (is_volatile) {
3040 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3041 }
3042}
3043
3044void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3045 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3046
3047 LocationSummary* locations =
3048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3049 locations->SetInAt(0, Location::RequiresRegister());
3050 bool is_volatile = field_info.IsVolatile();
3051 Primitive::Type field_type = field_info.GetFieldType();
3052 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3053 || (field_type == Primitive::kPrimByte);
3054
3055 // The register allocator does not support multiple
3056 // inputs that die at entry with one in a specific register.
3057 if (is_byte_type) {
3058 // Ensure the value is in a byte register.
3059 locations->SetInAt(1, Location::RegisterLocation(EAX));
3060 } else {
3061 locations->SetInAt(1, Location::RequiresRegister());
3062 }
3063 // Temporary registers for the write barrier.
3064 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3065 locations->AddTemp(Location::RequiresRegister());
3066 // Ensure the card is in a byte register.
3067 locations->AddTemp(Location::RegisterLocation(ECX));
3068 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3069 // 64bits value can be atomically written to an address with movsd and an XMM register.
3070 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3071 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3072 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3073 // isolated cases when we need this it isn't worth adding the extra complexity.
3074 locations->AddTemp(Location::RequiresFpuRegister());
3075 locations->AddTemp(Location::RequiresFpuRegister());
3076 }
3077}
3078
3079void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3080 const FieldInfo& field_info) {
3081 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3082
3083 LocationSummary* locations = instruction->GetLocations();
3084 Register base = locations->InAt(0).AsRegister<Register>();
3085 Location value = locations->InAt(1);
3086 bool is_volatile = field_info.IsVolatile();
3087 Primitive::Type field_type = field_info.GetFieldType();
3088 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3089
3090 if (is_volatile) {
3091 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3092 }
3093
3094 switch (field_type) {
3095 case Primitive::kPrimBoolean:
3096 case Primitive::kPrimByte: {
3097 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3098 break;
3099 }
3100
3101 case Primitive::kPrimShort:
3102 case Primitive::kPrimChar: {
3103 __ movw(Address(base, offset), value.AsRegister<Register>());
3104 break;
3105 }
3106
3107 case Primitive::kPrimInt:
3108 case Primitive::kPrimNot: {
3109 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003110 break;
3111 }
3112
3113 case Primitive::kPrimLong: {
3114 if (is_volatile) {
3115 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3116 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3117 __ movd(temp1, value.AsRegisterPairLow<Register>());
3118 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3119 __ punpckldq(temp1, temp2);
3120 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003121 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003122 } else {
3123 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003124 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003125 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3126 }
3127 break;
3128 }
3129
3130 case Primitive::kPrimFloat: {
3131 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3132 break;
3133 }
3134
3135 case Primitive::kPrimDouble: {
3136 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3137 break;
3138 }
3139
3140 case Primitive::kPrimVoid:
3141 LOG(FATAL) << "Unreachable type " << field_type;
3142 UNREACHABLE();
3143 }
3144
Calin Juravle77520bc2015-01-12 18:45:46 +00003145 // Longs are handled in the switch.
3146 if (field_type != Primitive::kPrimLong) {
3147 codegen_->MaybeRecordImplicitNullCheck(instruction);
3148 }
3149
3150 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3151 Register temp = locations->GetTemp(0).AsRegister<Register>();
3152 Register card = locations->GetTemp(1).AsRegister<Register>();
3153 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3154 }
3155
Calin Juravle52c48962014-12-16 17:02:57 +00003156 if (is_volatile) {
3157 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3158 }
3159}
3160
3161void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3162 HandleFieldGet(instruction, instruction->GetFieldInfo());
3163}
3164
3165void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3166 HandleFieldGet(instruction, instruction->GetFieldInfo());
3167}
3168
3169void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3170 HandleFieldSet(instruction, instruction->GetFieldInfo());
3171}
3172
3173void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3174 HandleFieldSet(instruction, instruction->GetFieldInfo());
3175}
3176
3177void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3178 HandleFieldSet(instruction, instruction->GetFieldInfo());
3179}
3180
3181void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3182 HandleFieldSet(instruction, instruction->GetFieldInfo());
3183}
3184
3185void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3186 HandleFieldGet(instruction, instruction->GetFieldInfo());
3187}
3188
3189void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3190 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003191}
3192
3193void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003194 LocationSummary* locations =
3195 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003196 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3197 ? Location::RequiresRegister()
3198 : Location::Any();
3199 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003200 if (instruction->HasUses()) {
3201 locations->SetOut(Location::SameAsFirstInput());
3202 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203}
3204
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003205void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003206 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3207 return;
3208 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003209 LocationSummary* locations = instruction->GetLocations();
3210 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003211
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003212 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3213 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3214}
3215
3216void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003217 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003218 codegen_->AddSlowPath(slow_path);
3219
3220 LocationSummary* locations = instruction->GetLocations();
3221 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003222
3223 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003224 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003225 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003226 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003227 } else {
3228 DCHECK(obj.IsConstant()) << obj;
3229 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3230 __ jmp(slow_path->GetEntryLabel());
3231 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003232 }
3233 __ j(kEqual, slow_path->GetEntryLabel());
3234}
3235
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003236void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3237 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3238 GenerateImplicitNullCheck(instruction);
3239 } else {
3240 GenerateExplicitNullCheck(instruction);
3241 }
3242}
3243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003244void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003245 LocationSummary* locations =
3246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003247 locations->SetInAt(0, Location::RequiresRegister());
3248 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003249 // The output overlaps in case of long: we don't want the low move to overwrite
3250 // the array's location.
3251 locations->SetOut(Location::RequiresRegister(),
3252 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3253 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003254}
3255
3256void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3257 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259 Location index = locations->InAt(1);
3260
Calin Juravle77520bc2015-01-12 18:45:46 +00003261 Primitive::Type type = instruction->GetType();
3262 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003263 case Primitive::kPrimBoolean: {
3264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003266 if (index.IsConstant()) {
3267 __ movzxb(out, Address(obj,
3268 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3269 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003271 }
3272 break;
3273 }
3274
3275 case Primitive::kPrimByte: {
3276 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 if (index.IsConstant()) {
3279 __ movsxb(out, Address(obj,
3280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3281 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 }
3284 break;
3285 }
3286
3287 case Primitive::kPrimShort: {
3288 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003289 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003290 if (index.IsConstant()) {
3291 __ movsxw(out, Address(obj,
3292 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3293 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003294 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 }
3296 break;
3297 }
3298
3299 case Primitive::kPrimChar: {
3300 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003301 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003302 if (index.IsConstant()) {
3303 __ movzxw(out, Address(obj,
3304 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3305 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003307 }
3308 break;
3309 }
3310
3311 case Primitive::kPrimInt:
3312 case Primitive::kPrimNot: {
3313 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003314 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003315 if (index.IsConstant()) {
3316 __ movl(out, Address(obj,
3317 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3318 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 }
3321 break;
3322 }
3323
3324 case Primitive::kPrimLong: {
3325 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003326 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003327 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003328 if (index.IsConstant()) {
3329 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003330 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003332 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003333 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003334 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003335 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003337 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339 }
3340 break;
3341 }
3342
Mark Mendell7c8d0092015-01-26 11:21:33 -05003343 case Primitive::kPrimFloat: {
3344 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3345 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3346 if (index.IsConstant()) {
3347 __ movss(out, Address(obj,
3348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3349 } else {
3350 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3351 }
3352 break;
3353 }
3354
3355 case Primitive::kPrimDouble: {
3356 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3357 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3358 if (index.IsConstant()) {
3359 __ movsd(out, Address(obj,
3360 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3361 } else {
3362 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3363 }
3364 break;
3365 }
3366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003368 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003369 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003371
3372 if (type != Primitive::kPrimLong) {
3373 codegen_->MaybeRecordImplicitNullCheck(instruction);
3374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003375}
3376
3377void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003378 // This location builder might end up asking to up to four registers, which is
3379 // not currently possible for baseline. The situation in which we need four
3380 // registers cannot be met by baseline though, because it has not run any
3381 // optimization.
3382
Nicolas Geoffray39468442014-09-02 15:17:15 +01003383 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003384 bool needs_write_barrier =
3385 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3386
Mark Mendell5f874182015-03-04 15:42:45 -05003387 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003388
Nicolas Geoffray39468442014-09-02 15:17:15 +01003389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3390 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003391 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003393 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003394 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003395 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3396 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3397 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003399 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3400 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003401 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003402 // In case of a byte operation, the register allocator does not support multiple
3403 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003404 locations->SetInAt(0, Location::RequiresRegister());
3405 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003406 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003407 // Ensure the value is in a byte register.
3408 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003409 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003410 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003412 // Temporary registers for the write barrier.
3413 if (needs_write_barrier) {
3414 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003415 // Ensure the card is in a byte register.
3416 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003417 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003419}
3420
3421void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003425 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003426 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003427 bool needs_runtime_call = locations->WillCall();
3428 bool needs_write_barrier =
3429 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003430
3431 switch (value_type) {
3432 case Primitive::kPrimBoolean:
3433 case Primitive::kPrimByte: {
3434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003435 if (index.IsConstant()) {
3436 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003437 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003438 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003439 } else {
3440 __ movb(Address(obj, offset),
3441 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3442 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003443 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003444 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003446 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003447 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003449 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3450 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003453 break;
3454 }
3455
3456 case Primitive::kPrimShort:
3457 case Primitive::kPrimChar: {
3458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003459 if (index.IsConstant()) {
3460 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003461 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003463 } else {
3464 __ movw(Address(obj, offset),
3465 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3466 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003467 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003468 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3470 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003471 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003473 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3474 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003475 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 break;
3478 }
3479
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003480 case Primitive::kPrimInt:
3481 case Primitive::kPrimNot: {
3482 if (!needs_runtime_call) {
3483 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3484 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003485 size_t offset =
3486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003487 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003488 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003489 } else {
3490 DCHECK(value.IsConstant()) << value;
3491 __ movl(Address(obj, offset),
3492 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3493 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003494 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003495 DCHECK(index.IsRegister()) << index;
3496 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3498 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003499 } else {
3500 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003502 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3503 }
3504 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003505 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003506
3507 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 Register temp = locations->GetTemp(0).AsRegister<Register>();
3509 Register card = locations->GetTemp(1).AsRegister<Register>();
3510 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003511 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003512 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003513 DCHECK_EQ(value_type, Primitive::kPrimNot);
3514 DCHECK(!codegen_->IsLeafMethod());
3515 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3516 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 }
3518 break;
3519 }
3520
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003521 case Primitive::kPrimLong: {
3522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003523 if (index.IsConstant()) {
3524 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003525 if (value.IsRegisterPair()) {
3526 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003527 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003528 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003529 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003530 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003531 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3532 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003534 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3535 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003537 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003539 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003540 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003541 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003542 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003543 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003544 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003545 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003546 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003547 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003550 Immediate(High32Bits(val)));
3551 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003552 }
3553 break;
3554 }
3555
Mark Mendell7c8d0092015-01-26 11:21:33 -05003556 case Primitive::kPrimFloat: {
3557 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3558 DCHECK(value.IsFpuRegister());
3559 if (index.IsConstant()) {
3560 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3561 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3562 } else {
3563 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3564 value.AsFpuRegister<XmmRegister>());
3565 }
3566 break;
3567 }
3568
3569 case Primitive::kPrimDouble: {
3570 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3571 DCHECK(value.IsFpuRegister());
3572 if (index.IsConstant()) {
3573 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3574 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3575 } else {
3576 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3577 value.AsFpuRegister<XmmRegister>());
3578 }
3579 break;
3580 }
3581
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 case Primitive::kPrimVoid:
3583 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003584 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585 }
3586}
3587
3588void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003590 locations->SetInAt(0, Location::RequiresRegister());
3591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 instruction->SetLocations(locations);
3593}
3594
3595void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3596 LocationSummary* locations = instruction->GetLocations();
3597 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 Register obj = locations->InAt(0).AsRegister<Register>();
3599 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003601 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602}
3603
3604void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003605 LocationSummary* locations =
3606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003607 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003609 if (instruction->HasUses()) {
3610 locations->SetOut(Location::SameAsFirstInput());
3611 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003612}
3613
3614void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3615 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003616 Location index_loc = locations->InAt(0);
3617 Location length_loc = locations->InAt(1);
3618 SlowPathCodeX86* slow_path =
3619 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 codegen_->AddSlowPath(slow_path);
3621
Mark Mendellf60c90b2015-03-04 15:12:59 -05003622 Register length = length_loc.AsRegister<Register>();
3623 if (index_loc.IsConstant()) {
3624 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3625 __ cmpl(length, Immediate(value));
3626 } else {
3627 __ cmpl(length, index_loc.AsRegister<Register>());
3628 }
3629 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003630}
3631
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003632void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3633 temp->SetLocations(nullptr);
3634}
3635
3636void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3637 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003638 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003639}
3640
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003641void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003642 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003643 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003644}
3645
3646void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003647 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3648}
3649
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003650void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3652}
3653
3654void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003655 HBasicBlock* block = instruction->GetBlock();
3656 if (block->GetLoopInformation() != nullptr) {
3657 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3658 // The back edge will generate the suspend check.
3659 return;
3660 }
3661 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3662 // The goto will generate the suspend check.
3663 return;
3664 }
3665 GenerateSuspendCheck(instruction, nullptr);
3666}
3667
3668void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3669 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003670 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003671 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003672 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003673 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003674 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003675 if (successor == nullptr) {
3676 __ j(kNotEqual, slow_path->GetEntryLabel());
3677 __ Bind(slow_path->GetReturnLabel());
3678 } else {
3679 __ j(kEqual, codegen_->GetLabelOf(successor));
3680 __ jmp(slow_path->GetEntryLabel());
3681 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003682}
3683
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003684X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3685 return codegen_->GetAssembler();
3686}
3687
Mark Mendell7c8d0092015-01-26 11:21:33 -05003688void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003689 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003690 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003691 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003692 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003693 __ movl(temp_reg, Address(ESP, src + stack_offset));
3694 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3695}
3696
3697void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3698 ScratchRegisterScope ensure_scratch(
3699 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3700 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3701 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3702 __ movl(temp_reg, Address(ESP, src + stack_offset));
3703 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3704 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3705 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003706}
3707
3708void ParallelMoveResolverX86::EmitMove(size_t index) {
3709 MoveOperands* move = moves_.Get(index);
3710 Location source = move->GetSource();
3711 Location destination = move->GetDestination();
3712
3713 if (source.IsRegister()) {
3714 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003716 } else {
3717 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003719 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003720 } else if (source.IsFpuRegister()) {
3721 if (destination.IsFpuRegister()) {
3722 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3723 } else if (destination.IsStackSlot()) {
3724 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3725 } else {
3726 DCHECK(destination.IsDoubleStackSlot());
3727 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3728 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003729 } else if (source.IsStackSlot()) {
3730 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003731 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003732 } else if (destination.IsFpuRegister()) {
3733 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003734 } else {
3735 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003736 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3737 }
3738 } else if (source.IsDoubleStackSlot()) {
3739 if (destination.IsFpuRegister()) {
3740 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3741 } else {
3742 DCHECK(destination.IsDoubleStackSlot()) << destination;
3743 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003744 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003745 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003746 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003747 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003748 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003749 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003750 if (value == 0) {
3751 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3752 } else {
3753 __ movl(destination.AsRegister<Register>(), Immediate(value));
3754 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003755 } else {
3756 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003757 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003758 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003759 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003760 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003761 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003762 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003763 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003764 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3765 if (value == 0) {
3766 // Easy handling of 0.0.
3767 __ xorps(dest, dest);
3768 } else {
3769 ScratchRegisterScope ensure_scratch(
3770 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3771 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3772 __ movl(temp, Immediate(value));
3773 __ movd(dest, temp);
3774 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003775 } else {
3776 DCHECK(destination.IsStackSlot()) << destination;
3777 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3778 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003779 } else if (constant->IsLongConstant()) {
3780 int64_t value = constant->AsLongConstant()->GetValue();
3781 int32_t low_value = Low32Bits(value);
3782 int32_t high_value = High32Bits(value);
3783 Immediate low(low_value);
3784 Immediate high(high_value);
3785 if (destination.IsDoubleStackSlot()) {
3786 __ movl(Address(ESP, destination.GetStackIndex()), low);
3787 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3788 } else {
3789 __ movl(destination.AsRegisterPairLow<Register>(), low);
3790 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3791 }
3792 } else {
3793 DCHECK(constant->IsDoubleConstant());
3794 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003795 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003796 int32_t low_value = Low32Bits(value);
3797 int32_t high_value = High32Bits(value);
3798 Immediate low(low_value);
3799 Immediate high(high_value);
3800 if (destination.IsFpuRegister()) {
3801 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3802 if (value == 0) {
3803 // Easy handling of 0.0.
3804 __ xorpd(dest, dest);
3805 } else {
3806 __ pushl(high);
3807 __ pushl(low);
3808 __ movsd(dest, Address(ESP, 0));
3809 __ addl(ESP, Immediate(8));
3810 }
3811 } else {
3812 DCHECK(destination.IsDoubleStackSlot()) << destination;
3813 __ movl(Address(ESP, destination.GetStackIndex()), low);
3814 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3815 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003816 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003817 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003818 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003819 }
3820}
3821
3822void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003823 Register suggested_scratch = reg == EAX ? EBX : EAX;
3824 ScratchRegisterScope ensure_scratch(
3825 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3826
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003827 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3828 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3829 __ movl(Address(ESP, mem + stack_offset), reg);
3830 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3831}
3832
Mark Mendell7c8d0092015-01-26 11:21:33 -05003833void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3834 ScratchRegisterScope ensure_scratch(
3835 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3836
3837 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3838 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3839 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3840 __ movss(Address(ESP, mem + stack_offset), reg);
3841 __ movd(reg, temp_reg);
3842}
3843
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003844void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3845 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003846 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3847
3848 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003849 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003850 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3851
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003852 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3853 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3854 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3855 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3856 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3857 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3858}
3859
3860void ParallelMoveResolverX86::EmitSwap(size_t index) {
3861 MoveOperands* move = moves_.Get(index);
3862 Location source = move->GetSource();
3863 Location destination = move->GetDestination();
3864
3865 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003867 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003869 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003871 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3872 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003873 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3874 // Use XOR Swap algorithm to avoid a temporary.
3875 DCHECK_NE(source.reg(), destination.reg());
3876 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3877 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3878 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3879 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3880 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3881 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3882 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003883 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
3884 // Take advantage of the 16 bytes in the XMM register.
3885 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
3886 Address stack(ESP, destination.GetStackIndex());
3887 // Load the double into the high doubleword.
3888 __ movhpd(reg, stack);
3889
3890 // Store the low double into the destination.
3891 __ movsd(stack, reg);
3892
3893 // Move the high double to the low double.
3894 __ psrldq(reg, Immediate(8));
3895 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
3896 // Take advantage of the 16 bytes in the XMM register.
3897 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
3898 Address stack(ESP, source.GetStackIndex());
3899 // Load the double into the high doubleword.
3900 __ movhpd(reg, stack);
3901
3902 // Store the low double into the destination.
3903 __ movsd(stack, reg);
3904
3905 // Move the high double to the low double.
3906 __ psrldq(reg, Immediate(8));
3907 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
3908 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3909 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003910 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003911 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003912 }
3913}
3914
3915void ParallelMoveResolverX86::SpillScratch(int reg) {
3916 __ pushl(static_cast<Register>(reg));
3917}
3918
3919void ParallelMoveResolverX86::RestoreScratch(int reg) {
3920 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003921}
3922
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003923void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003924 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3925 ? LocationSummary::kCallOnSlowPath
3926 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003927 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003928 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003929 locations->SetOut(Location::RequiresRegister());
3930}
3931
3932void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003934 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003935 DCHECK(!cls->CanCallRuntime());
3936 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003937 codegen_->LoadCurrentMethod(out);
3938 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3939 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003940 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003941 codegen_->LoadCurrentMethod(out);
3942 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3943 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003944
3945 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3946 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3947 codegen_->AddSlowPath(slow_path);
3948 __ testl(out, out);
3949 __ j(kEqual, slow_path->GetEntryLabel());
3950 if (cls->MustGenerateClinitCheck()) {
3951 GenerateClassInitializationCheck(slow_path, out);
3952 } else {
3953 __ Bind(slow_path->GetExitLabel());
3954 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003955 }
3956}
3957
3958void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3959 LocationSummary* locations =
3960 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3961 locations->SetInAt(0, Location::RequiresRegister());
3962 if (check->HasUses()) {
3963 locations->SetOut(Location::SameAsFirstInput());
3964 }
3965}
3966
3967void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003968 // We assume the class to not be null.
3969 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3970 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003971 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003972 GenerateClassInitializationCheck(slow_path,
3973 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003974}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003975
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003976void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3977 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003978 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3979 Immediate(mirror::Class::kStatusInitialized));
3980 __ j(kLess, slow_path->GetEntryLabel());
3981 __ Bind(slow_path->GetExitLabel());
3982 // No need for memory fence, thanks to the X86 memory model.
3983}
3984
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003985void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3986 LocationSummary* locations =
3987 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3988 locations->SetOut(Location::RequiresRegister());
3989}
3990
3991void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3992 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3993 codegen_->AddSlowPath(slow_path);
3994
Roland Levillain271ab9c2014-11-27 15:23:57 +00003995 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003996 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003997 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3998 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003999 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4000 __ testl(out, out);
4001 __ j(kEqual, slow_path->GetEntryLabel());
4002 __ Bind(slow_path->GetExitLabel());
4003}
4004
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004005void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4006 LocationSummary* locations =
4007 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4008 locations->SetOut(Location::RequiresRegister());
4009}
4010
4011void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4012 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004014 __ fs()->movl(address, Immediate(0));
4015}
4016
4017void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4018 LocationSummary* locations =
4019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4020 InvokeRuntimeCallingConvention calling_convention;
4021 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4022}
4023
4024void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4025 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4026 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4027}
4028
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004029void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004030 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4031 ? LocationSummary::kNoCall
4032 : LocationSummary::kCallOnSlowPath;
4033 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4034 locations->SetInAt(0, Location::RequiresRegister());
4035 locations->SetInAt(1, Location::Any());
4036 locations->SetOut(Location::RequiresRegister());
4037}
4038
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004039void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004040 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004041 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004042 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004044 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4045 Label done, zero;
4046 SlowPathCodeX86* slow_path = nullptr;
4047
4048 // Return 0 if `obj` is null.
4049 // TODO: avoid this check if we know obj is not null.
4050 __ testl(obj, obj);
4051 __ j(kEqual, &zero);
4052 __ movl(out, Address(obj, class_offset));
4053 // Compare the class of `obj` with `cls`.
4054 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004056 } else {
4057 DCHECK(cls.IsStackSlot()) << cls;
4058 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4059 }
4060
4061 if (instruction->IsClassFinal()) {
4062 // Classes must be equal for the instanceof to succeed.
4063 __ j(kNotEqual, &zero);
4064 __ movl(out, Immediate(1));
4065 __ jmp(&done);
4066 } else {
4067 // If the classes are not equal, we go into a slow path.
4068 DCHECK(locations->OnlyCallsOnSlowPath());
4069 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004070 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004071 codegen_->AddSlowPath(slow_path);
4072 __ j(kNotEqual, slow_path->GetEntryLabel());
4073 __ movl(out, Immediate(1));
4074 __ jmp(&done);
4075 }
4076 __ Bind(&zero);
4077 __ movl(out, Immediate(0));
4078 if (slow_path != nullptr) {
4079 __ Bind(slow_path->GetExitLabel());
4080 }
4081 __ Bind(&done);
4082}
4083
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004084void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4086 instruction, LocationSummary::kCallOnSlowPath);
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::Any());
4089 locations->AddTemp(Location::RequiresRegister());
4090}
4091
4092void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4093 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004095 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004096 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004097 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4098 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4099 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4100 codegen_->AddSlowPath(slow_path);
4101
4102 // TODO: avoid this check if we know obj is not null.
4103 __ testl(obj, obj);
4104 __ j(kEqual, slow_path->GetExitLabel());
4105 __ movl(temp, Address(obj, class_offset));
4106
4107 // Compare the class of `obj` with `cls`.
4108 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004110 } else {
4111 DCHECK(cls.IsStackSlot()) << cls;
4112 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4113 }
4114
4115 __ j(kNotEqual, slow_path->GetEntryLabel());
4116 __ Bind(slow_path->GetExitLabel());
4117}
4118
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004119void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4120 LocationSummary* locations =
4121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4122 InvokeRuntimeCallingConvention calling_convention;
4123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4124}
4125
4126void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4127 __ fs()->call(Address::Absolute(instruction->IsEnter()
4128 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4129 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4130 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4131}
4132
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004133void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4134void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4135void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4136
4137void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4138 LocationSummary* locations =
4139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4140 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4141 || instruction->GetResultType() == Primitive::kPrimLong);
4142 locations->SetInAt(0, Location::RequiresRegister());
4143 locations->SetInAt(1, Location::Any());
4144 locations->SetOut(Location::SameAsFirstInput());
4145}
4146
4147void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4148 HandleBitwiseOperation(instruction);
4149}
4150
4151void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4152 HandleBitwiseOperation(instruction);
4153}
4154
4155void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4156 HandleBitwiseOperation(instruction);
4157}
4158
4159void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4160 LocationSummary* locations = instruction->GetLocations();
4161 Location first = locations->InAt(0);
4162 Location second = locations->InAt(1);
4163 DCHECK(first.Equals(locations->Out()));
4164
4165 if (instruction->GetResultType() == Primitive::kPrimInt) {
4166 if (second.IsRegister()) {
4167 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004168 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004169 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004170 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004171 } else {
4172 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004173 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004174 }
4175 } else if (second.IsConstant()) {
4176 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004177 __ andl(first.AsRegister<Register>(),
4178 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004179 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004180 __ orl(first.AsRegister<Register>(),
4181 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004182 } else {
4183 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004184 __ xorl(first.AsRegister<Register>(),
4185 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004186 }
4187 } else {
4188 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004190 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004191 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004192 } else {
4193 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004194 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004195 }
4196 }
4197 } else {
4198 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4199 if (second.IsRegisterPair()) {
4200 if (instruction->IsAnd()) {
4201 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4202 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4203 } else if (instruction->IsOr()) {
4204 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4205 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4206 } else {
4207 DCHECK(instruction->IsXor());
4208 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4209 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4210 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004211 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004212 if (instruction->IsAnd()) {
4213 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4214 __ andl(first.AsRegisterPairHigh<Register>(),
4215 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4216 } else if (instruction->IsOr()) {
4217 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4218 __ orl(first.AsRegisterPairHigh<Register>(),
4219 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4220 } else {
4221 DCHECK(instruction->IsXor());
4222 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4223 __ xorl(first.AsRegisterPairHigh<Register>(),
4224 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4225 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004226 } else {
4227 DCHECK(second.IsConstant()) << second;
4228 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004229 int32_t low_value = Low32Bits(value);
4230 int32_t high_value = High32Bits(value);
4231 Immediate low(low_value);
4232 Immediate high(high_value);
4233 Register first_low = first.AsRegisterPairLow<Register>();
4234 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004235 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004236 if (low_value == 0) {
4237 __ xorl(first_low, first_low);
4238 } else if (low_value != -1) {
4239 __ andl(first_low, low);
4240 }
4241 if (high_value == 0) {
4242 __ xorl(first_high, first_high);
4243 } else if (high_value != -1) {
4244 __ andl(first_high, high);
4245 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004246 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004247 if (low_value != 0) {
4248 __ orl(first_low, low);
4249 }
4250 if (high_value != 0) {
4251 __ orl(first_high, high);
4252 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004253 } else {
4254 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004255 if (low_value != 0) {
4256 __ xorl(first_low, low);
4257 }
4258 if (high_value != 0) {
4259 __ xorl(first_high, high);
4260 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004261 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004262 }
4263 }
4264}
4265
Calin Juravleb1498f62015-02-16 13:13:29 +00004266void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4267 // Nothing to do, this should be removed during prepare for register allocator.
4268 UNUSED(instruction);
4269 LOG(FATAL) << "Unreachable";
4270}
4271
4272void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4273 // Nothing to do, this should be removed during prepare for register allocator.
4274 UNUSED(instruction);
4275 LOG(FATAL) << "Unreachable";
4276}
4277
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004278} // namespace x86
4279} // namespace art