blob: a6fb07fa98ec5449919cb9160d256554c7870a3b [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
116 length_location_,
117 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100118 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700119 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 }
121
122 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100123 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100124 const Location index_location_;
125 const Location length_location_;
126
127 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
128};
129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000132 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000138 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700140 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 if (successor_ == nullptr) {
143 __ jmp(GetReturnLabel());
144 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100145 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 }
148
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 Label* GetReturnLabel() {
150 DCHECK(successor_ == nullptr);
151 return &return_label_;
152 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153
154 private:
155 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100156 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 Label return_label_;
158
159 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
160};
161
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000162class LoadStringSlowPathX86 : public SlowPathCodeX86 {
163 public:
164 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
165
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000167 LocationSummary* locations = instruction_->GetLocations();
168 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
169
170 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000173
174 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800175 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
176 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000177 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000181
182 __ jmp(GetExitLabel());
183 }
184
185 private:
186 HLoadString* const instruction_;
187
188 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
189};
190
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191class LoadClassSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 LoadClassSlowPathX86(HLoadClass* cls,
194 HInstruction* at,
195 uint32_t dex_pc,
196 bool do_clinit)
197 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
198 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
199 }
200
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 LocationSummary* locations = at_->GetLocations();
203 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
204 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000205 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206
207 InvokeRuntimeCallingConvention calling_convention;
208 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
209 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
210 __ fs()->call(Address::Absolute(do_clinit_
211 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
212 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214
215 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000216 Location out = locations->Out();
217 if (out.IsValid()) {
218 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
219 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000222 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 __ jmp(GetExitLabel());
224 }
225
226 private:
227 // The class this slow path will load.
228 HLoadClass* const cls_;
229
230 // The instruction where this slow path is happening.
231 // (Might be the load class or an initialization check).
232 HInstruction* const at_;
233
234 // The dex PC of `at_`.
235 const uint32_t dex_pc_;
236
237 // Whether to initialize the class.
238 const bool do_clinit_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
241};
242
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000243class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
244 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000245 TypeCheckSlowPathX86(HInstruction* instruction,
246 Location class_to_check,
247 Location object_class,
248 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 class_to_check_(class_to_check),
251 object_class_(object_class),
252 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000254 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000255 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000256 DCHECK(instruction_->IsCheckCast()
257 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
259 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
260 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000261 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
263 // We're moving two locations to locations that could overlap, so we need a parallel
264 // move resolver.
265 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000266 x86_codegen->EmitParallelMoves(
267 class_to_check_,
268 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
269 object_class_,
270 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000272 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000273 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
274 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 } else {
276 DCHECK(instruction_->IsCheckCast());
277 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
278 }
279
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 if (instruction_->IsInstanceOf()) {
282 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
283 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
286 __ jmp(GetExitLabel());
287 }
288
289 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000290 HInstruction* const instruction_;
291 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
296};
297
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700298class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
299 public:
300 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
301 : instruction_(instruction) {}
302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
304 __ Bind(GetEntryLabel());
305 SaveLiveRegisters(codegen, instruction_->GetLocations());
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
307 // No need to restore live registers.
308 DCHECK(instruction_->IsDeoptimize());
309 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
310 uint32_t dex_pc = deoptimize->GetDexPc();
311 codegen->RecordPcInfo(instruction_, dex_pc, this);
312 }
313
314 private:
315 HInstruction* const instruction_;
316 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
317};
318
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100319#undef __
320#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
321
Dave Allison20dfc792014-06-16 20:44:29 -0700322inline Condition X86Condition(IfCondition cond) {
323 switch (cond) {
324 case kCondEQ: return kEqual;
325 case kCondNE: return kNotEqual;
326 case kCondLT: return kLess;
327 case kCondLE: return kLessEqual;
328 case kCondGT: return kGreater;
329 case kCondGE: return kGreaterEqual;
330 default:
331 LOG(FATAL) << "Unknown if condition";
332 }
333 return kEqual;
334}
335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100336void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
337 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
338}
339
340void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
341 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
342}
343
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100344size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
345 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
346 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100347}
348
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100349size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
350 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
351 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100352}
353
Mark Mendell7c8d0092015-01-26 11:21:33 -0500354size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
355 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
356 return GetFloatingPointSpillSlotSize();
357}
358
359size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
360 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
361 return GetFloatingPointSpillSlotSize();
362}
363
Mark Mendellfb8d2792015-03-31 22:16:59 -0400364CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
365 const X86InstructionSetFeatures& isa_features,
366 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500367 : CodeGenerator(graph,
368 kNumberOfCpuRegisters,
369 kNumberOfXmmRegisters,
370 kNumberOfRegisterPairs,
371 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
372 arraysize(kCoreCalleeSaves))
373 | (1 << kFakeReturnRegister),
374 0,
375 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100378 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400379 move_resolver_(graph->GetArena(), this),
380 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000381 // Use a fake return address register to mimic Quick.
382 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100383}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100385Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386 switch (type) {
387 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100388 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389 X86ManagedRegister pair =
390 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
392 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
394 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100395 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100396 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100397 }
398
399 case Primitive::kPrimByte:
400 case Primitive::kPrimBoolean:
401 case Primitive::kPrimChar:
402 case Primitive::kPrimShort:
403 case Primitive::kPrimInt:
404 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100405 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100406 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
409 X86ManagedRegister current =
410 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
411 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 }
414 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100415 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100416 }
417
418 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100419 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100420 return Location::FpuRegisterLocation(
421 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423
424 case Primitive::kPrimVoid:
425 LOG(FATAL) << "Unreachable type " << type;
426 }
427
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100428 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100429}
430
Mark Mendell5f874182015-03-04 15:42:45 -0500431void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100432 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434
435 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100437
Mark Mendell5f874182015-03-04 15:42:45 -0500438 if (is_baseline) {
439 blocked_core_registers_[EBP] = true;
440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
442 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100443
444 UpdateBlockedPairRegisters();
445}
446
447void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
448 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
449 X86ManagedRegister current =
450 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
451 if (blocked_core_registers_[current.AsRegisterPairLow()]
452 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
453 blocked_register_pairs_[i] = true;
454 }
455 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100456}
457
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100458InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
459 : HGraphVisitor(graph),
460 assembler_(codegen->GetAssembler()),
461 codegen_(codegen) {}
462
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100463static dwarf::Reg DWARFReg(Register reg) {
464 return dwarf::Reg::X86Core(static_cast<int>(reg));
465}
466
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000467void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100468 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000469 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000470 bool skip_overflow_check =
471 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000472 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000473
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000474 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100476 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100477 }
478
Mark Mendell5f874182015-03-04 15:42:45 -0500479 if (HasEmptyFrame()) {
480 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000481 }
Mark Mendell5f874182015-03-04 15:42:45 -0500482
483 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
484 Register reg = kCoreCalleeSaves[i];
485 if (allocated_registers_.ContainsCoreRegister(reg)) {
486 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100487 __ cfi().AdjustCFAOffset(kX86WordSize);
488 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500489 }
490 }
491
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100492 int adjust = GetFrameSize() - FrameEntrySpillSize();
493 __ subl(ESP, Immediate(adjust));
494 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500495 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000496}
497
498void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500499 if (HasEmptyFrame()) {
500 return;
501 }
502
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503 int adjust = GetFrameSize() - FrameEntrySpillSize();
504 __ addl(ESP, Immediate(adjust));
505 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500506
507 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
508 Register reg = kCoreCalleeSaves[i];
509 if (allocated_registers_.ContainsCoreRegister(reg)) {
510 __ popl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100511 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
512 __ cfi().Restore(DWARFReg(reg));
Mark Mendell5f874182015-03-04 15:42:45 -0500513 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000514 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000515}
516
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100517void CodeGeneratorX86::Bind(HBasicBlock* block) {
518 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000519}
520
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100521void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000522 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100523 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000524}
525
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100526Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
527 switch (load->GetType()) {
528 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100529 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100531
532 case Primitive::kPrimInt:
533 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536
537 case Primitive::kPrimBoolean:
538 case Primitive::kPrimByte:
539 case Primitive::kPrimChar:
540 case Primitive::kPrimShort:
541 case Primitive::kPrimVoid:
542 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700543 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100544 }
545
546 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700547 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548}
549
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100550Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
551 switch (type) {
552 case Primitive::kPrimBoolean:
553 case Primitive::kPrimByte:
554 case Primitive::kPrimChar:
555 case Primitive::kPrimShort:
556 case Primitive::kPrimInt:
557 case Primitive::kPrimNot: {
558 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000559 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100562 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000563 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100564 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100565 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000567 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 uint32_t index = gp_index_;
569 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000570 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100571 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
573 calling_convention.GetRegisterPairAt(index));
574 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
577 }
578 }
579
580 case Primitive::kPrimFloat: {
581 uint32_t index = fp_index_++;
582 stack_index_++;
583 if (index < calling_convention.GetNumberOfFpuRegisters()) {
584 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
585 } else {
586 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
587 }
588 }
589
590 case Primitive::kPrimDouble: {
591 uint32_t index = fp_index_++;
592 stack_index_ += 2;
593 if (index < calling_convention.GetNumberOfFpuRegisters()) {
594 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
595 } else {
596 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 }
598 }
599
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100600 case Primitive::kPrimVoid:
601 LOG(FATAL) << "Unexpected parameter type " << type;
602 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100603 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100604 return Location();
605}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100606
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607void CodeGeneratorX86::Move32(Location destination, Location source) {
608 if (source.Equals(destination)) {
609 return;
610 }
611 if (destination.IsRegister()) {
612 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000613 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000615 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616 } else {
617 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000618 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100619 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else if (destination.IsFpuRegister()) {
621 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else {
626 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000630 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000634 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500635 } else if (source.IsConstant()) {
636 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000637 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500638 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 } else {
640 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100641 __ pushl(Address(ESP, source.GetStackIndex()));
642 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 }
644 }
645}
646
647void CodeGeneratorX86::Move64(Location destination, Location source) {
648 if (source.Equals(destination)) {
649 return;
650 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100651 if (destination.IsRegisterPair()) {
652 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 EmitParallelMoves(
654 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
655 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
656 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
657 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100658 } else if (source.IsFpuRegister()) {
659 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100663 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
664 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
666 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500668 if (source.IsFpuRegister()) {
669 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
670 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100672 } else {
673 LOG(FATAL) << "Unimplemented";
674 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000676 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100677 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000678 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100679 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100681 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000683 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000684 } else if (source.IsConstant()) {
685 HConstant* constant = source.GetConstant();
686 int64_t value;
687 if (constant->IsLongConstant()) {
688 value = constant->AsLongConstant()->GetValue();
689 } else {
690 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000691 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000692 }
693 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
694 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000696 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000697 EmitParallelMoves(
698 Location::StackSlot(source.GetStackIndex()),
699 Location::StackSlot(destination.GetStackIndex()),
700 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
701 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 }
703 }
704}
705
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100706void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000707 LocationSummary* locations = instruction->GetLocations();
708 if (locations != nullptr && locations->Out().Equals(location)) {
709 return;
710 }
711
712 if (locations != nullptr && locations->Out().IsConstant()) {
713 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000714 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
715 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000716 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 } else if (location.IsStackSlot()) {
719 __ movl(Address(ESP, location.GetStackIndex()), imm);
720 } else {
721 DCHECK(location.IsConstant());
722 DCHECK_EQ(location.GetConstant(), const_to_move);
723 }
724 } else if (const_to_move->IsLongConstant()) {
725 int64_t value = const_to_move->AsLongConstant()->GetValue();
726 if (location.IsRegisterPair()) {
727 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
728 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
729 } else if (location.IsDoubleStackSlot()) {
730 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000731 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
732 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000733 } else {
734 DCHECK(location.IsConstant());
735 DCHECK_EQ(location.GetConstant(), instruction);
736 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100737 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000738 } else if (instruction->IsTemporary()) {
739 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000740 if (temp_location.IsStackSlot()) {
741 Move32(location, temp_location);
742 } else {
743 DCHECK(temp_location.IsDoubleStackSlot());
744 Move64(location, temp_location);
745 }
Roland Levillain476df552014-10-09 17:51:36 +0100746 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 switch (instruction->GetType()) {
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimInt:
754 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 case Primitive::kPrimFloat:
756 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 break;
758
759 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 case Primitive::kPrimDouble:
761 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 break;
763
764 default:
765 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
766 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000767 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100768 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 switch (instruction->GetType()) {
770 case Primitive::kPrimBoolean:
771 case Primitive::kPrimByte:
772 case Primitive::kPrimChar:
773 case Primitive::kPrimShort:
774 case Primitive::kPrimInt:
775 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000777 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 break;
779
780 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000782 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 break;
784
785 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788 }
789}
790
791void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000792 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000796 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100797 DCHECK(!successor->IsExitBlock());
798
799 HBasicBlock* block = got->GetBlock();
800 HInstruction* previous = got->GetPrevious();
801
802 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000803 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100804 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
805 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
806 return;
807 }
808
809 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
810 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
811 }
812 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000814 }
815}
816
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000818 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819}
820
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700822 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000823}
824
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700825void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
826 Label* true_target,
827 Label* false_target,
828 Label* always_true_target) {
829 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 if (cond->IsIntConstant()) {
831 // Constant condition, statically compared against 1.
832 int32_t cond_value = cond->AsIntConstant()->GetValue();
833 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700834 if (always_true_target != nullptr) {
835 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100836 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100838 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 DCHECK_EQ(cond_value, 0);
840 }
841 } else {
842 bool materialized =
843 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
844 // Moves do not affect the eflags register, so if the condition is
845 // evaluated just before the if, we don't need to evaluate it
846 // again.
847 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700848 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 if (materialized) {
850 if (!eflags_set) {
851 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700852 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100853 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500854 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100855 } else {
856 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
857 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700858 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100859 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 }
862 } else {
863 Location lhs = cond->GetLocations()->InAt(0);
864 Location rhs = cond->GetLocations()->InAt(1);
865 // LHS is guaranteed to be in a register (see
866 // LocationsBuilderX86::VisitCondition).
867 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500870 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
871 if (constant == 0) {
872 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
873 } else {
874 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
875 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100876 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000877 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100878 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700879 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700880 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100881 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700882 if (false_target != nullptr) {
883 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000884 }
885}
886
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887void LocationsBuilderX86::VisitIf(HIf* if_instr) {
888 LocationSummary* locations =
889 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
890 HInstruction* cond = if_instr->InputAt(0);
891 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
892 locations->SetInAt(0, Location::Any());
893 }
894}
895
896void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
897 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
898 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
899 Label* always_true_target = true_target;
900 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
901 if_instr->IfTrueSuccessor())) {
902 always_true_target = nullptr;
903 }
904 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
905 if_instr->IfFalseSuccessor())) {
906 false_target = nullptr;
907 }
908 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
909}
910
911void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
912 LocationSummary* locations = new (GetGraph()->GetArena())
913 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
914 HInstruction* cond = deoptimize->InputAt(0);
915 DCHECK(cond->IsCondition());
916 if (cond->AsCondition()->NeedsMaterialization()) {
917 locations->SetInAt(0, Location::Any());
918 }
919}
920
921void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
922 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
923 DeoptimizationSlowPathX86(deoptimize);
924 codegen_->AddSlowPath(slow_path);
925 Label* slow_path_entry = slow_path->GetEntryLabel();
926 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
927}
928
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000930 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000931}
932
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
934 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000935}
936
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000937void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100938 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939}
940
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100942 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700943 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000944}
945
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100946void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100947 LocationSummary* locations =
948 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100949 switch (store->InputAt(1)->GetType()) {
950 case Primitive::kPrimBoolean:
951 case Primitive::kPrimByte:
952 case Primitive::kPrimChar:
953 case Primitive::kPrimShort:
954 case Primitive::kPrimInt:
955 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
958 break;
959
960 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
963 break;
964
965 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 }
968 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969}
970
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000971void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700972 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000973}
974
Dave Allison20dfc792014-06-16 20:44:29 -0700975void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100976 LocationSummary* locations =
977 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100978 locations->SetInAt(0, Location::RequiresRegister());
979 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100980 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500981 // We need a byte register.
982 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100983 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984}
985
Dave Allison20dfc792014-06-16 20:44:29 -0700986void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
987 if (comp->NeedsMaterialization()) {
988 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000989 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100990 // Clear register: setcc only sets the low byte.
991 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500992 Location lhs = locations->InAt(0);
993 Location rhs = locations->InAt(1);
994 if (rhs.IsRegister()) {
995 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
996 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800997 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500998 if (constant == 0) {
999 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1000 } else {
1001 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1002 }
Dave Allison20dfc792014-06-16 20:44:29 -07001003 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001004 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001005 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001006 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001007 }
Dave Allison20dfc792014-06-16 20:44:29 -07001008}
1009
1010void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1015 VisitCondition(comp);
1016}
1017
1018void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1019 VisitCondition(comp);
1020}
1021
1022void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1047 VisitCondition(comp);
1048}
1049
1050void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1051 VisitCondition(comp);
1052}
1053
1054void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1055 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056}
1057
1058void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001059 LocationSummary* locations =
1060 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001061 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001062}
1063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001065 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001066 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067}
1068
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001069void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1070 LocationSummary* locations =
1071 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1072 locations->SetOut(Location::ConstantLocation(constant));
1073}
1074
1075void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1076 // Will be generated at use site.
1077 UNUSED(constant);
1078}
1079
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001081 LocationSummary* locations =
1082 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001083 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084}
1085
1086void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1087 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001088 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089}
1090
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001091void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1092 LocationSummary* locations =
1093 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1094 locations->SetOut(Location::ConstantLocation(constant));
1095}
1096
1097void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1098 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001100}
1101
1102void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1105 locations->SetOut(Location::ConstantLocation(constant));
1106}
1107
1108void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1109 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001111}
1112
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001113void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001114 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115}
1116
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001117void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001118 UNUSED(ret);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001119 __ cfi().RememberState();
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001120 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001121 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001122 __ cfi().RestoreState();
1123 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001124}
1125
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001126void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001129 switch (ret->InputAt(0)->GetType()) {
1130 case Primitive::kPrimBoolean:
1131 case Primitive::kPrimByte:
1132 case Primitive::kPrimChar:
1133 case Primitive::kPrimShort:
1134 case Primitive::kPrimInt:
1135 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001137 break;
1138
1139 case Primitive::kPrimLong:
1140 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001141 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142 break;
1143
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001144 case Primitive::kPrimFloat:
1145 case Primitive::kPrimDouble:
1146 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 break;
1149
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001151 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153}
1154
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001155void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 if (kIsDebugBuild) {
1157 switch (ret->InputAt(0)->GetType()) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001164 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 break;
1166
1167 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1169 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 break;
1171
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001172 case Primitive::kPrimFloat:
1173 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001174 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001175 break;
1176
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001178 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 }
1180 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001181 __ cfi().RememberState();
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183 __ ret();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001184 __ cfi().RestoreState();
1185 __ cfi().DefCFAOffset(codegen_->GetFrameSize());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001186}
1187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001188void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001189 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001190 if (intrinsic.TryDispatch(invoke)) {
1191 return;
1192 }
1193
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001194 HandleInvoke(invoke);
1195}
1196
Mark Mendell09ed1a32015-03-25 08:30:06 -04001197static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1198 if (invoke->GetLocations()->Intrinsified()) {
1199 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1200 intrinsic.Dispatch(invoke);
1201 return true;
1202 }
1203 return false;
1204}
1205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001206void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001207 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1208 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001209 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210
Mark Mendell09ed1a32015-03-25 08:30:06 -04001211 codegen_->GenerateStaticOrDirectCall(
1212 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001213}
1214
1215void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1216 HandleInvoke(invoke);
1217}
1218
1219void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001220 LocationSummary* locations =
1221 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001222 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001223
1224 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001226 HInstruction* input = invoke->InputAt(i);
1227 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1228 }
1229
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 switch (invoke->GetType()) {
1231 case Primitive::kPrimBoolean:
1232 case Primitive::kPrimByte:
1233 case Primitive::kPrimChar:
1234 case Primitive::kPrimShort:
1235 case Primitive::kPrimInt:
1236 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001237 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 break;
1239
1240 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001241 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 break;
1243
1244 case Primitive::kPrimVoid:
1245 break;
1246
1247 case Primitive::kPrimDouble:
1248 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001249 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 break;
1251 }
1252
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001253 invoke->SetLocations(locations);
1254}
1255
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001256void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001257 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001258 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1259 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1260 LocationSummary* locations = invoke->GetLocations();
1261 Location receiver = locations->InAt(0);
1262 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1263 // temp = object->GetClass();
1264 if (receiver.IsStackSlot()) {
1265 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1266 __ movl(temp, Address(temp, class_offset));
1267 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001268 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001269 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001270 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001271 // temp = temp->GetMethodAt(method_offset);
1272 __ movl(temp, Address(temp, method_offset));
1273 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001274 __ call(Address(
1275 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001276
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001277 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001278 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001279}
1280
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001281void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1282 HandleInvoke(invoke);
1283 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001284 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001285}
1286
1287void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1288 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001290 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1291 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1292 LocationSummary* locations = invoke->GetLocations();
1293 Location receiver = locations->InAt(0);
1294 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1295
1296 // Set the hidden argument.
1297 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001298 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001299
1300 // temp = object->GetClass();
1301 if (receiver.IsStackSlot()) {
1302 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1303 __ movl(temp, Address(temp, class_offset));
1304 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001306 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001307 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308 // temp = temp->GetImtEntryAt(method_offset);
1309 __ movl(temp, Address(temp, method_offset));
1310 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001311 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001312 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001313
1314 DCHECK(!codegen_->IsLeafMethod());
1315 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1316}
1317
Roland Levillain88cb1752014-10-20 16:36:47 +01001318void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1319 LocationSummary* locations =
1320 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1321 switch (neg->GetResultType()) {
1322 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001323 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001324 locations->SetInAt(0, Location::RequiresRegister());
1325 locations->SetOut(Location::SameAsFirstInput());
1326 break;
1327
Roland Levillain88cb1752014-10-20 16:36:47 +01001328 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001329 locations->SetInAt(0, Location::RequiresFpuRegister());
1330 locations->SetOut(Location::SameAsFirstInput());
1331 locations->AddTemp(Location::RequiresRegister());
1332 locations->AddTemp(Location::RequiresFpuRegister());
1333 break;
1334
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001336 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001337 locations->SetOut(Location::SameAsFirstInput());
1338 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001339 break;
1340
1341 default:
1342 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1343 }
1344}
1345
1346void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1347 LocationSummary* locations = neg->GetLocations();
1348 Location out = locations->Out();
1349 Location in = locations->InAt(0);
1350 switch (neg->GetResultType()) {
1351 case Primitive::kPrimInt:
1352 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001354 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 break;
1356
1357 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001358 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001359 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001360 __ negl(out.AsRegisterPairLow<Register>());
1361 // Negation is similar to subtraction from zero. The least
1362 // significant byte triggers a borrow when it is different from
1363 // zero; to take it into account, add 1 to the most significant
1364 // byte if the carry flag (CF) is set to 1 after the first NEGL
1365 // operation.
1366 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1367 __ negl(out.AsRegisterPairHigh<Register>());
1368 break;
1369
Roland Levillain5368c212014-11-27 15:03:41 +00001370 case Primitive::kPrimFloat: {
1371 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001372 Register constant = locations->GetTemp(0).AsRegister<Register>();
1373 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001374 // Implement float negation with an exclusive or with value
1375 // 0x80000000 (mask for bit 31, representing the sign of a
1376 // single-precision floating-point number).
1377 __ movl(constant, Immediate(INT32_C(0x80000000)));
1378 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001379 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001381 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001382
Roland Levillain5368c212014-11-27 15:03:41 +00001383 case Primitive::kPrimDouble: {
1384 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001385 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001386 // Implement double negation with an exclusive or with value
1387 // 0x8000000000000000 (mask for bit 63, representing the sign of
1388 // a double-precision floating-point number).
1389 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001391 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001392 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001393
1394 default:
1395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1396 }
1397}
1398
Roland Levillaindff1f282014-11-05 14:15:05 +00001399void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 Primitive::Type result_type = conversion->GetResultType();
1401 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001402 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001403
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001404 // The float-to-long and double-to-long type conversions rely on a
1405 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001406 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001407 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1408 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001409 ? LocationSummary::kCall
1410 : LocationSummary::kNoCall;
1411 LocationSummary* locations =
1412 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1413
David Brazdilb2bd1c52015-03-25 11:17:37 +00001414 // The Java language does not allow treating boolean as an integral type but
1415 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001416
Roland Levillaindff1f282014-11-05 14:15:05 +00001417 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001418 case Primitive::kPrimByte:
1419 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001420 case Primitive::kPrimBoolean:
1421 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001422 case Primitive::kPrimShort:
1423 case Primitive::kPrimInt:
1424 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001425 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001426 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1427 // Make the output overlap to please the register allocator. This greatly simplifies
1428 // the validation of the linear scan implementation
1429 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001430 break;
1431
1432 default:
1433 LOG(FATAL) << "Unexpected type conversion from " << input_type
1434 << " to " << result_type;
1435 }
1436 break;
1437
Roland Levillain01a8d712014-11-14 16:27:39 +00001438 case Primitive::kPrimShort:
1439 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001440 case Primitive::kPrimBoolean:
1441 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001442 case Primitive::kPrimByte:
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimChar:
1445 // Processing a Dex `int-to-short' instruction.
1446 locations->SetInAt(0, Location::Any());
1447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1448 break;
1449
1450 default:
1451 LOG(FATAL) << "Unexpected type conversion from " << input_type
1452 << " to " << result_type;
1453 }
1454 break;
1455
Roland Levillain946e1432014-11-11 17:35:19 +00001456 case Primitive::kPrimInt:
1457 switch (input_type) {
1458 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001459 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001460 locations->SetInAt(0, Location::Any());
1461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1462 break;
1463
1464 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001465 // Processing a Dex `float-to-int' instruction.
1466 locations->SetInAt(0, Location::RequiresFpuRegister());
1467 locations->SetOut(Location::RequiresRegister());
1468 locations->AddTemp(Location::RequiresFpuRegister());
1469 break;
1470
Roland Levillain946e1432014-11-11 17:35:19 +00001471 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001472 // Processing a Dex `double-to-int' instruction.
1473 locations->SetInAt(0, Location::RequiresFpuRegister());
1474 locations->SetOut(Location::RequiresRegister());
1475 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001476 break;
1477
1478 default:
1479 LOG(FATAL) << "Unexpected type conversion from " << input_type
1480 << " to " << result_type;
1481 }
1482 break;
1483
Roland Levillaindff1f282014-11-05 14:15:05 +00001484 case Primitive::kPrimLong:
1485 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001486 case Primitive::kPrimBoolean:
1487 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001488 case Primitive::kPrimByte:
1489 case Primitive::kPrimShort:
1490 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001491 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001492 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001493 locations->SetInAt(0, Location::RegisterLocation(EAX));
1494 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1495 break;
1496
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001497 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001498 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001499 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001500 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001501 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1502 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1503
Vladimir Marko949c91f2015-01-27 10:48:44 +00001504 // The runtime helper puts the result in EAX, EDX.
1505 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001506 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001507 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001508
1509 default:
1510 LOG(FATAL) << "Unexpected type conversion from " << input_type
1511 << " to " << result_type;
1512 }
1513 break;
1514
Roland Levillain981e4542014-11-14 11:47:14 +00001515 case Primitive::kPrimChar:
1516 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001517 case Primitive::kPrimBoolean:
1518 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001519 case Primitive::kPrimByte:
1520 case Primitive::kPrimShort:
1521 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001522 // Processing a Dex `int-to-char' instruction.
1523 locations->SetInAt(0, Location::Any());
1524 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1525 break;
1526
1527 default:
1528 LOG(FATAL) << "Unexpected type conversion from " << input_type
1529 << " to " << result_type;
1530 }
1531 break;
1532
Roland Levillaindff1f282014-11-05 14:15:05 +00001533 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001534 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001535 case Primitive::kPrimBoolean:
1536 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001537 case Primitive::kPrimByte:
1538 case Primitive::kPrimShort:
1539 case Primitive::kPrimInt:
1540 case Primitive::kPrimChar:
1541 // Processing a Dex `int-to-float' instruction.
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetOut(Location::RequiresFpuRegister());
1544 break;
1545
1546 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001547 // Processing a Dex `long-to-float' instruction.
1548 locations->SetInAt(0, Location::RequiresRegister());
1549 locations->SetOut(Location::RequiresFpuRegister());
1550 locations->AddTemp(Location::RequiresFpuRegister());
1551 locations->AddTemp(Location::RequiresFpuRegister());
1552 break;
1553
Roland Levillaincff13742014-11-17 14:32:17 +00001554 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001555 // Processing a Dex `double-to-float' instruction.
1556 locations->SetInAt(0, Location::RequiresFpuRegister());
1557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001558 break;
1559
1560 default:
1561 LOG(FATAL) << "Unexpected type conversion from " << input_type
1562 << " to " << result_type;
1563 };
1564 break;
1565
Roland Levillaindff1f282014-11-05 14:15:05 +00001566 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001567 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001568 case Primitive::kPrimBoolean:
1569 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001570 case Primitive::kPrimByte:
1571 case Primitive::kPrimShort:
1572 case Primitive::kPrimInt:
1573 case Primitive::kPrimChar:
1574 // Processing a Dex `int-to-double' instruction.
1575 locations->SetInAt(0, Location::RequiresRegister());
1576 locations->SetOut(Location::RequiresFpuRegister());
1577 break;
1578
1579 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001580 // Processing a Dex `long-to-double' instruction.
1581 locations->SetInAt(0, Location::RequiresRegister());
1582 locations->SetOut(Location::RequiresFpuRegister());
1583 locations->AddTemp(Location::RequiresFpuRegister());
1584 locations->AddTemp(Location::RequiresFpuRegister());
1585 break;
1586
Roland Levillaincff13742014-11-17 14:32:17 +00001587 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001588 // Processing a Dex `float-to-double' instruction.
1589 locations->SetInAt(0, Location::RequiresFpuRegister());
1590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001591 break;
1592
1593 default:
1594 LOG(FATAL) << "Unexpected type conversion from " << input_type
1595 << " to " << result_type;
1596 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 break;
1598
1599 default:
1600 LOG(FATAL) << "Unexpected type conversion from " << input_type
1601 << " to " << result_type;
1602 }
1603}
1604
1605void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1606 LocationSummary* locations = conversion->GetLocations();
1607 Location out = locations->Out();
1608 Location in = locations->InAt(0);
1609 Primitive::Type result_type = conversion->GetResultType();
1610 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001611 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001612 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001613 case Primitive::kPrimByte:
1614 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001615 case Primitive::kPrimBoolean:
1616 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001617 case Primitive::kPrimShort:
1618 case Primitive::kPrimInt:
1619 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001620 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001621 if (in.IsRegister()) {
1622 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001623 } else {
1624 DCHECK(in.GetConstant()->IsIntConstant());
1625 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1626 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1627 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001628 break;
1629
1630 default:
1631 LOG(FATAL) << "Unexpected type conversion from " << input_type
1632 << " to " << result_type;
1633 }
1634 break;
1635
Roland Levillain01a8d712014-11-14 16:27:39 +00001636 case Primitive::kPrimShort:
1637 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001638 case Primitive::kPrimBoolean:
1639 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001640 case Primitive::kPrimByte:
1641 case Primitive::kPrimInt:
1642 case Primitive::kPrimChar:
1643 // Processing a Dex `int-to-short' instruction.
1644 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001645 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001646 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001648 } else {
1649 DCHECK(in.GetConstant()->IsIntConstant());
1650 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001651 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001652 }
1653 break;
1654
1655 default:
1656 LOG(FATAL) << "Unexpected type conversion from " << input_type
1657 << " to " << result_type;
1658 }
1659 break;
1660
Roland Levillain946e1432014-11-11 17:35:19 +00001661 case Primitive::kPrimInt:
1662 switch (input_type) {
1663 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001664 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001665 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001667 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001669 } else {
1670 DCHECK(in.IsConstant());
1671 DCHECK(in.GetConstant()->IsLongConstant());
1672 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001674 }
1675 break;
1676
Roland Levillain3f8f9362014-12-02 17:45:01 +00001677 case Primitive::kPrimFloat: {
1678 // Processing a Dex `float-to-int' instruction.
1679 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1680 Register output = out.AsRegister<Register>();
1681 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1682 Label done, nan;
1683
1684 __ movl(output, Immediate(kPrimIntMax));
1685 // temp = int-to-float(output)
1686 __ cvtsi2ss(temp, output);
1687 // if input >= temp goto done
1688 __ comiss(input, temp);
1689 __ j(kAboveEqual, &done);
1690 // if input == NaN goto nan
1691 __ j(kUnordered, &nan);
1692 // output = float-to-int-truncate(input)
1693 __ cvttss2si(output, input);
1694 __ jmp(&done);
1695 __ Bind(&nan);
1696 // output = 0
1697 __ xorl(output, output);
1698 __ Bind(&done);
1699 break;
1700 }
1701
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001702 case Primitive::kPrimDouble: {
1703 // Processing a Dex `double-to-int' instruction.
1704 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1705 Register output = out.AsRegister<Register>();
1706 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1707 Label done, nan;
1708
1709 __ movl(output, Immediate(kPrimIntMax));
1710 // temp = int-to-double(output)
1711 __ cvtsi2sd(temp, output);
1712 // if input >= temp goto done
1713 __ comisd(input, temp);
1714 __ j(kAboveEqual, &done);
1715 // if input == NaN goto nan
1716 __ j(kUnordered, &nan);
1717 // output = double-to-int-truncate(input)
1718 __ cvttsd2si(output, input);
1719 __ jmp(&done);
1720 __ Bind(&nan);
1721 // output = 0
1722 __ xorl(output, output);
1723 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001724 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001725 }
Roland Levillain946e1432014-11-11 17:35:19 +00001726
1727 default:
1728 LOG(FATAL) << "Unexpected type conversion from " << input_type
1729 << " to " << result_type;
1730 }
1731 break;
1732
Roland Levillaindff1f282014-11-05 14:15:05 +00001733 case Primitive::kPrimLong:
1734 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001735 case Primitive::kPrimBoolean:
1736 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001737 case Primitive::kPrimByte:
1738 case Primitive::kPrimShort:
1739 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001740 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001741 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001742 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1743 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001744 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001745 __ cdq();
1746 break;
1747
1748 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001749 // Processing a Dex `float-to-long' instruction.
1750 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001751 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1752 break;
1753
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001755 // Processing a Dex `double-to-long' instruction.
1756 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1757 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 break;
1759
1760 default:
1761 LOG(FATAL) << "Unexpected type conversion from " << input_type
1762 << " to " << result_type;
1763 }
1764 break;
1765
Roland Levillain981e4542014-11-14 11:47:14 +00001766 case Primitive::kPrimChar:
1767 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001768 case Primitive::kPrimBoolean:
1769 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001770 case Primitive::kPrimByte:
1771 case Primitive::kPrimShort:
1772 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001773 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1774 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001775 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001776 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001778 } else {
1779 DCHECK(in.GetConstant()->IsIntConstant());
1780 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001781 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001782 }
1783 break;
1784
1785 default:
1786 LOG(FATAL) << "Unexpected type conversion from " << input_type
1787 << " to " << result_type;
1788 }
1789 break;
1790
Roland Levillaindff1f282014-11-05 14:15:05 +00001791 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001792 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001793 case Primitive::kPrimBoolean:
1794 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001795 case Primitive::kPrimByte:
1796 case Primitive::kPrimShort:
1797 case Primitive::kPrimInt:
1798 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001799 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001801 break;
1802
Roland Levillain6d0e4832014-11-27 18:31:21 +00001803 case Primitive::kPrimLong: {
1804 // Processing a Dex `long-to-float' instruction.
1805 Register low = in.AsRegisterPairLow<Register>();
1806 Register high = in.AsRegisterPairHigh<Register>();
1807 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1808 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1809 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1810
1811 // Operations use doubles for precision reasons (each 32-bit
1812 // half of a long fits in the 53-bit mantissa of a double,
1813 // but not in the 24-bit mantissa of a float). This is
1814 // especially important for the low bits. The result is
1815 // eventually converted to float.
1816
1817 // low = low - 2^31 (to prevent bit 31 of `low` to be
1818 // interpreted as a sign bit)
1819 __ subl(low, Immediate(0x80000000));
1820 // temp = int-to-double(high)
1821 __ cvtsi2sd(temp, high);
1822 // temp = temp * 2^32
1823 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1824 __ mulsd(temp, constant);
1825 // result = int-to-double(low)
1826 __ cvtsi2sd(result, low);
1827 // result = result + 2^31 (restore the original value of `low`)
1828 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1829 __ addsd(result, constant);
1830 // result = result + temp
1831 __ addsd(result, temp);
1832 // result = double-to-float(result)
1833 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001834 // Restore low.
1835 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001836 break;
1837 }
1838
Roland Levillaincff13742014-11-17 14:32:17 +00001839 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001840 // Processing a Dex `double-to-float' instruction.
1841 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001842 break;
1843
1844 default:
1845 LOG(FATAL) << "Unexpected type conversion from " << input_type
1846 << " to " << result_type;
1847 };
1848 break;
1849
Roland Levillaindff1f282014-11-05 14:15:05 +00001850 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001851 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001852 case Primitive::kPrimBoolean:
1853 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001854 case Primitive::kPrimByte:
1855 case Primitive::kPrimShort:
1856 case Primitive::kPrimInt:
1857 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001858 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001860 break;
1861
Roland Levillain647b9ed2014-11-27 12:06:00 +00001862 case Primitive::kPrimLong: {
1863 // Processing a Dex `long-to-double' instruction.
1864 Register low = in.AsRegisterPairLow<Register>();
1865 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1867 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1868 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001869
Roland Levillain647b9ed2014-11-27 12:06:00 +00001870 // low = low - 2^31 (to prevent bit 31 of `low` to be
1871 // interpreted as a sign bit)
1872 __ subl(low, Immediate(0x80000000));
1873 // temp = int-to-double(high)
1874 __ cvtsi2sd(temp, high);
1875 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001876 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001877 __ mulsd(temp, constant);
1878 // result = int-to-double(low)
1879 __ cvtsi2sd(result, low);
1880 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001881 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001882 __ addsd(result, constant);
1883 // result = result + temp
1884 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001885 // Restore low.
1886 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001887 break;
1888 }
1889
Roland Levillaincff13742014-11-17 14:32:17 +00001890 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001891 // Processing a Dex `float-to-double' instruction.
1892 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001899 break;
1900
1901 default:
1902 LOG(FATAL) << "Unexpected type conversion from " << input_type
1903 << " to " << result_type;
1904 }
1905}
1906
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001907void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001910 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001911 case Primitive::kPrimInt: {
1912 locations->SetInAt(0, Location::RequiresRegister());
1913 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1915 break;
1916 }
1917
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001919 locations->SetInAt(0, Location::RequiresRegister());
1920 locations->SetInAt(1, Location::Any());
1921 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 break;
1923 }
1924
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001925 case Primitive::kPrimFloat:
1926 case Primitive::kPrimDouble: {
1927 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001928 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001929 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001933 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1935 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001936 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001937}
1938
1939void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1940 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 Location first = locations->InAt(0);
1942 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001943 Location out = locations->Out();
1944
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001948 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1949 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1950 } else {
1951 __ leal(out.AsRegister<Register>(), Address(
1952 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1953 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001955 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1956 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1957 __ addl(out.AsRegister<Register>(), Immediate(value));
1958 } else {
1959 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1960 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001961 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001962 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001964 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001965 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 }
1967
1968 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001969 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001970 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1971 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001972 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1974 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001976 } else {
1977 DCHECK(second.IsConstant()) << second;
1978 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1979 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1980 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001981 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001982 break;
1983 }
1984
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001985 case Primitive::kPrimFloat: {
1986 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001987 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001989 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 }
1991
1992 case Primitive::kPrimDouble: {
1993 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001994 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995 }
1996 break;
1997 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001998
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001999 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002001 }
2002}
2003
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002004void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002005 LocationSummary* locations =
2006 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002007 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002008 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002009 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002010 locations->SetInAt(0, Location::RequiresRegister());
2011 locations->SetInAt(1, Location::Any());
2012 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002013 break;
2014 }
Calin Juravle11351682014-10-23 15:38:15 +01002015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble: {
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetInAt(1, Location::RequiresFpuRegister());
2019 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002020 break;
Calin Juravle11351682014-10-23 15:38:15 +01002021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002023 default:
Calin Juravle11351682014-10-23 15:38:15 +01002024 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002025 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002026}
2027
2028void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2029 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002030 Location first = locations->InAt(0);
2031 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002032 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002033 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002037 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002038 __ subl(first.AsRegister<Register>(),
2039 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002040 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002042 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044 }
2045
2046 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002047 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002048 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2049 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002050 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002051 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 __ sbbl(first.AsRegisterPairHigh<Register>(),
2053 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002054 } else {
2055 DCHECK(second.IsConstant()) << second;
2056 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2057 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2058 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002059 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002060 break;
2061 }
2062
Calin Juravle11351682014-10-23 15:38:15 +01002063 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 break;
Calin Juravle11351682014-10-23 15:38:15 +01002066 }
2067
2068 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002070 break;
2071 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002073 default:
Calin Juravle11351682014-10-23 15:38:15 +01002074 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002075 }
2076}
2077
Calin Juravle34bacdf2014-10-07 20:23:36 +01002078void LocationsBuilderX86::VisitMul(HMul* mul) {
2079 LocationSummary* locations =
2080 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2081 switch (mul->GetResultType()) {
2082 case Primitive::kPrimInt:
2083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetInAt(1, Location::Any());
2085 locations->SetOut(Location::SameAsFirstInput());
2086 break;
2087 case Primitive::kPrimLong: {
2088 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002089 locations->SetInAt(1, Location::Any());
2090 locations->SetOut(Location::SameAsFirstInput());
2091 // Needed for imul on 32bits with 64bits output.
2092 locations->AddTemp(Location::RegisterLocation(EAX));
2093 locations->AddTemp(Location::RegisterLocation(EDX));
2094 break;
2095 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002096 case Primitive::kPrimFloat:
2097 case Primitive::kPrimDouble: {
2098 locations->SetInAt(0, Location::RequiresFpuRegister());
2099 locations->SetInAt(1, Location::RequiresFpuRegister());
2100 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002101 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002102 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002103
2104 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002105 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002106 }
2107}
2108
2109void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2110 LocationSummary* locations = mul->GetLocations();
2111 Location first = locations->InAt(0);
2112 Location second = locations->InAt(1);
2113 DCHECK(first.Equals(locations->Out()));
2114
2115 switch (mul->GetResultType()) {
2116 case Primitive::kPrimInt: {
2117 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 } else if (second.IsConstant()) {
2120 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002122 } else {
2123 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002125 }
2126 break;
2127 }
2128
2129 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002130 Register in1_hi = first.AsRegisterPairHigh<Register>();
2131 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 Register eax = locations->GetTemp(0).AsRegister<Register>();
2133 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134
2135 DCHECK_EQ(EAX, eax);
2136 DCHECK_EQ(EDX, edx);
2137
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002138 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139 // output: in1
2140 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2141 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2142 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002143 if (second.IsConstant()) {
2144 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002145
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002146 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2147 int32_t low_value = Low32Bits(value);
2148 int32_t high_value = High32Bits(value);
2149 Immediate low(low_value);
2150 Immediate high(high_value);
2151
2152 __ movl(eax, high);
2153 // eax <- in1.lo * in2.hi
2154 __ imull(eax, in1_lo);
2155 // in1.hi <- in1.hi * in2.lo
2156 __ imull(in1_hi, low);
2157 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2158 __ addl(in1_hi, eax);
2159 // move in2_lo to eax to prepare for double precision
2160 __ movl(eax, low);
2161 // edx:eax <- in1.lo * in2.lo
2162 __ mull(in1_lo);
2163 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2164 __ addl(in1_hi, edx);
2165 // in1.lo <- (in1.lo * in2.lo)[31:0];
2166 __ movl(in1_lo, eax);
2167 } else if (second.IsRegisterPair()) {
2168 Register in2_hi = second.AsRegisterPairHigh<Register>();
2169 Register in2_lo = second.AsRegisterPairLow<Register>();
2170
2171 __ movl(eax, in2_hi);
2172 // eax <- in1.lo * in2.hi
2173 __ imull(eax, in1_lo);
2174 // in1.hi <- in1.hi * in2.lo
2175 __ imull(in1_hi, in2_lo);
2176 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2177 __ addl(in1_hi, eax);
2178 // move in1_lo to eax to prepare for double precision
2179 __ movl(eax, in1_lo);
2180 // edx:eax <- in1.lo * in2.lo
2181 __ mull(in2_lo);
2182 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2183 __ addl(in1_hi, edx);
2184 // in1.lo <- (in1.lo * in2.lo)[31:0];
2185 __ movl(in1_lo, eax);
2186 } else {
2187 DCHECK(second.IsDoubleStackSlot()) << second;
2188 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2189 Address in2_lo(ESP, second.GetStackIndex());
2190
2191 __ movl(eax, in2_hi);
2192 // eax <- in1.lo * in2.hi
2193 __ imull(eax, in1_lo);
2194 // in1.hi <- in1.hi * in2.lo
2195 __ imull(in1_hi, in2_lo);
2196 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2197 __ addl(in1_hi, eax);
2198 // move in1_lo to eax to prepare for double precision
2199 __ movl(eax, in1_lo);
2200 // edx:eax <- in1.lo * in2.lo
2201 __ mull(in2_lo);
2202 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2203 __ addl(in1_hi, edx);
2204 // in1.lo <- (in1.lo * in2.lo)[31:0];
2205 __ movl(in1_lo, eax);
2206 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002207
2208 break;
2209 }
2210
Calin Juravleb5bfa962014-10-21 18:02:24 +01002211 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002212 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002213 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002214 }
2215
2216 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002218 break;
2219 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002220
2221 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002222 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002223 }
2224}
2225
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002226void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2227 uint32_t stack_adjustment, bool is_float) {
2228 if (source.IsStackSlot()) {
2229 DCHECK(is_float);
2230 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2231 } else if (source.IsDoubleStackSlot()) {
2232 DCHECK(!is_float);
2233 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2234 } else {
2235 // Write the value to the temporary location on the stack and load to FP stack.
2236 if (is_float) {
2237 Location stack_temp = Location::StackSlot(temp_offset);
2238 codegen_->Move32(stack_temp, source);
2239 __ flds(Address(ESP, temp_offset));
2240 } else {
2241 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2242 codegen_->Move64(stack_temp, source);
2243 __ fldl(Address(ESP, temp_offset));
2244 }
2245 }
2246}
2247
2248void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2249 Primitive::Type type = rem->GetResultType();
2250 bool is_float = type == Primitive::kPrimFloat;
2251 size_t elem_size = Primitive::ComponentSize(type);
2252 LocationSummary* locations = rem->GetLocations();
2253 Location first = locations->InAt(0);
2254 Location second = locations->InAt(1);
2255 Location out = locations->Out();
2256
2257 // Create stack space for 2 elements.
2258 // TODO: enhance register allocator to ask for stack temporaries.
2259 __ subl(ESP, Immediate(2 * elem_size));
2260
2261 // Load the values to the FP stack in reverse order, using temporaries if needed.
2262 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2263 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2264
2265 // Loop doing FPREM until we stabilize.
2266 Label retry;
2267 __ Bind(&retry);
2268 __ fprem();
2269
2270 // Move FP status to AX.
2271 __ fstsw();
2272
2273 // And see if the argument reduction is complete. This is signaled by the
2274 // C2 FPU flag bit set to 0.
2275 __ andl(EAX, Immediate(kC2ConditionMask));
2276 __ j(kNotEqual, &retry);
2277
2278 // We have settled on the final value. Retrieve it into an XMM register.
2279 // Store FP top of stack to real stack.
2280 if (is_float) {
2281 __ fsts(Address(ESP, 0));
2282 } else {
2283 __ fstl(Address(ESP, 0));
2284 }
2285
2286 // Pop the 2 items from the FP stack.
2287 __ fucompp();
2288
2289 // Load the value from the stack into an XMM register.
2290 DCHECK(out.IsFpuRegister()) << out;
2291 if (is_float) {
2292 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2293 } else {
2294 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2295 }
2296
2297 // And remove the temporary stack space we allocated.
2298 __ addl(ESP, Immediate(2 * elem_size));
2299}
2300
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002301
2302void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2303 DCHECK(instruction->IsDiv() || instruction->IsRem());
2304
2305 LocationSummary* locations = instruction->GetLocations();
2306 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002307 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002308
2309 Register out_register = locations->Out().AsRegister<Register>();
2310 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002311 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002312
2313 DCHECK(imm == 1 || imm == -1);
2314
2315 if (instruction->IsRem()) {
2316 __ xorl(out_register, out_register);
2317 } else {
2318 __ movl(out_register, input_register);
2319 if (imm == -1) {
2320 __ negl(out_register);
2321 }
2322 }
2323}
2324
2325
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002326void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002327 LocationSummary* locations = instruction->GetLocations();
2328
2329 Register out_register = locations->Out().AsRegister<Register>();
2330 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002331 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002332
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002333 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002334 Register num = locations->GetTemp(0).AsRegister<Register>();
2335
2336 __ leal(num, Address(input_register, std::abs(imm) - 1));
2337 __ testl(input_register, input_register);
2338 __ cmovl(kGreaterEqual, num, input_register);
2339 int shift = CTZ(imm);
2340 __ sarl(num, Immediate(shift));
2341
2342 if (imm < 0) {
2343 __ negl(num);
2344 }
2345
2346 __ movl(out_register, num);
2347}
2348
2349void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2350 DCHECK(instruction->IsDiv() || instruction->IsRem());
2351
2352 LocationSummary* locations = instruction->GetLocations();
2353 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2354
2355 Register eax = locations->InAt(0).AsRegister<Register>();
2356 Register out = locations->Out().AsRegister<Register>();
2357 Register num;
2358 Register edx;
2359
2360 if (instruction->IsDiv()) {
2361 edx = locations->GetTemp(0).AsRegister<Register>();
2362 num = locations->GetTemp(1).AsRegister<Register>();
2363 } else {
2364 edx = locations->Out().AsRegister<Register>();
2365 num = locations->GetTemp(0).AsRegister<Register>();
2366 }
2367
2368 DCHECK_EQ(EAX, eax);
2369 DCHECK_EQ(EDX, edx);
2370 if (instruction->IsDiv()) {
2371 DCHECK_EQ(EAX, out);
2372 } else {
2373 DCHECK_EQ(EDX, out);
2374 }
2375
2376 int64_t magic;
2377 int shift;
2378 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2379
2380 Label ndiv;
2381 Label end;
2382 // If numerator is 0, the result is 0, no computation needed.
2383 __ testl(eax, eax);
2384 __ j(kNotEqual, &ndiv);
2385
2386 __ xorl(out, out);
2387 __ jmp(&end);
2388
2389 __ Bind(&ndiv);
2390
2391 // Save the numerator.
2392 __ movl(num, eax);
2393
2394 // EAX = magic
2395 __ movl(eax, Immediate(magic));
2396
2397 // EDX:EAX = magic * numerator
2398 __ imull(num);
2399
2400 if (imm > 0 && magic < 0) {
2401 // EDX += num
2402 __ addl(edx, num);
2403 } else if (imm < 0 && magic > 0) {
2404 __ subl(edx, num);
2405 }
2406
2407 // Shift if needed.
2408 if (shift != 0) {
2409 __ sarl(edx, Immediate(shift));
2410 }
2411
2412 // EDX += 1 if EDX < 0
2413 __ movl(eax, edx);
2414 __ shrl(edx, Immediate(31));
2415 __ addl(edx, eax);
2416
2417 if (instruction->IsRem()) {
2418 __ movl(eax, num);
2419 __ imull(edx, Immediate(imm));
2420 __ subl(eax, edx);
2421 __ movl(edx, eax);
2422 } else {
2423 __ movl(eax, edx);
2424 }
2425 __ Bind(&end);
2426}
2427
Calin Juravlebacfec32014-11-14 15:54:36 +00002428void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2429 DCHECK(instruction->IsDiv() || instruction->IsRem());
2430
2431 LocationSummary* locations = instruction->GetLocations();
2432 Location out = locations->Out();
2433 Location first = locations->InAt(0);
2434 Location second = locations->InAt(1);
2435 bool is_div = instruction->IsDiv();
2436
2437 switch (instruction->GetResultType()) {
2438 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 DCHECK_EQ(EAX, first.AsRegister<Register>());
2440 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002441
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002442 if (instruction->InputAt(1)->IsIntConstant()) {
2443 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002444
2445 if (imm == 0) {
2446 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2447 } else if (imm == 1 || imm == -1) {
2448 DivRemOneOrMinusOne(instruction);
2449 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002450 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002451 } else {
2452 DCHECK(imm <= -2 || imm >= 2);
2453 GenerateDivRemWithAnyConstant(instruction);
2454 }
2455 } else {
2456 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002457 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002458 is_div);
2459 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002460
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002461 Register second_reg = second.AsRegister<Register>();
2462 // 0x80000000/-1 triggers an arithmetic exception!
2463 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2464 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002465
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002466 __ cmpl(second_reg, Immediate(-1));
2467 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002468
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002469 // edx:eax <- sign-extended of eax
2470 __ cdq();
2471 // eax = quotient, edx = remainder
2472 __ idivl(second_reg);
2473 __ Bind(slow_path->GetExitLabel());
2474 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002475 break;
2476 }
2477
2478 case Primitive::kPrimLong: {
2479 InvokeRuntimeCallingConvention calling_convention;
2480 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2481 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2482 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2483 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2484 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2485 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2486
2487 if (is_div) {
2488 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2489 } else {
2490 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2491 }
2492 uint32_t dex_pc = is_div
2493 ? instruction->AsDiv()->GetDexPc()
2494 : instruction->AsRem()->GetDexPc();
2495 codegen_->RecordPcInfo(instruction, dex_pc);
2496
2497 break;
2498 }
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2502 }
2503}
2504
Calin Juravle7c4954d2014-10-28 16:57:40 +00002505void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002506 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002507 ? LocationSummary::kCall
2508 : LocationSummary::kNoCall;
2509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2510
Calin Juravle7c4954d2014-10-28 16:57:40 +00002511 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002512 case Primitive::kPrimInt: {
2513 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002514 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002515 locations->SetOut(Location::SameAsFirstInput());
2516 // Intel uses edx:eax as the dividend.
2517 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002518 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2519 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2520 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002521 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002522 locations->AddTemp(Location::RequiresRegister());
2523 }
Calin Juravled0d48522014-11-04 16:40:20 +00002524 break;
2525 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002526 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002527 InvokeRuntimeCallingConvention calling_convention;
2528 locations->SetInAt(0, Location::RegisterPairLocation(
2529 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2530 locations->SetInAt(1, Location::RegisterPairLocation(
2531 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2532 // Runtime helper puts the result in EAX, EDX.
2533 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002534 break;
2535 }
2536 case Primitive::kPrimFloat:
2537 case Primitive::kPrimDouble: {
2538 locations->SetInAt(0, Location::RequiresFpuRegister());
2539 locations->SetInAt(1, Location::RequiresFpuRegister());
2540 locations->SetOut(Location::SameAsFirstInput());
2541 break;
2542 }
2543
2544 default:
2545 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2546 }
2547}
2548
2549void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2550 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002551 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002552 Location first = locations->InAt(0);
2553 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002554
2555 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002556 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002557 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002558 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 break;
2560 }
2561
2562 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002563 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002565 break;
2566 }
2567
2568 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002569 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002571 break;
2572 }
2573
2574 default:
2575 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2576 }
2577}
2578
Calin Juravlebacfec32014-11-14 15:54:36 +00002579void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002580 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002581
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002582 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2583 ? LocationSummary::kCall
2584 : LocationSummary::kNoCall;
2585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002586
Calin Juravled2ec87d2014-12-08 14:24:46 +00002587 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002588 case Primitive::kPrimInt: {
2589 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002590 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002592 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2593 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2594 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002595 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002596 locations->AddTemp(Location::RequiresRegister());
2597 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002598 break;
2599 }
2600 case Primitive::kPrimLong: {
2601 InvokeRuntimeCallingConvention calling_convention;
2602 locations->SetInAt(0, Location::RegisterPairLocation(
2603 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2604 locations->SetInAt(1, Location::RegisterPairLocation(
2605 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2606 // Runtime helper puts the result in EAX, EDX.
2607 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2608 break;
2609 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002610 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002611 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002612 locations->SetInAt(0, Location::Any());
2613 locations->SetInAt(1, Location::Any());
2614 locations->SetOut(Location::RequiresFpuRegister());
2615 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002616 break;
2617 }
2618
2619 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002620 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 }
2622}
2623
2624void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2625 Primitive::Type type = rem->GetResultType();
2626 switch (type) {
2627 case Primitive::kPrimInt:
2628 case Primitive::kPrimLong: {
2629 GenerateDivRemIntegral(rem);
2630 break;
2631 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002632 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002633 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002634 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002635 break;
2636 }
2637 default:
2638 LOG(FATAL) << "Unexpected rem type " << type;
2639 }
2640}
2641
Calin Juravled0d48522014-11-04 16:40:20 +00002642void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2643 LocationSummary* locations =
2644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002645 switch (instruction->GetType()) {
2646 case Primitive::kPrimInt: {
2647 locations->SetInAt(0, Location::Any());
2648 break;
2649 }
2650 case Primitive::kPrimLong: {
2651 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2652 if (!instruction->IsConstant()) {
2653 locations->AddTemp(Location::RequiresRegister());
2654 }
2655 break;
2656 }
2657 default:
2658 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2659 }
Calin Juravled0d48522014-11-04 16:40:20 +00002660 if (instruction->HasUses()) {
2661 locations->SetOut(Location::SameAsFirstInput());
2662 }
2663}
2664
2665void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2666 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2667 codegen_->AddSlowPath(slow_path);
2668
2669 LocationSummary* locations = instruction->GetLocations();
2670 Location value = locations->InAt(0);
2671
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002672 switch (instruction->GetType()) {
2673 case Primitive::kPrimInt: {
2674 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002676 __ j(kEqual, slow_path->GetEntryLabel());
2677 } else if (value.IsStackSlot()) {
2678 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2679 __ j(kEqual, slow_path->GetEntryLabel());
2680 } else {
2681 DCHECK(value.IsConstant()) << value;
2682 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2683 __ jmp(slow_path->GetEntryLabel());
2684 }
2685 }
2686 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002687 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002688 case Primitive::kPrimLong: {
2689 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002690 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002691 __ movl(temp, value.AsRegisterPairLow<Register>());
2692 __ orl(temp, value.AsRegisterPairHigh<Register>());
2693 __ j(kEqual, slow_path->GetEntryLabel());
2694 } else {
2695 DCHECK(value.IsConstant()) << value;
2696 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2697 __ jmp(slow_path->GetEntryLabel());
2698 }
2699 }
2700 break;
2701 }
2702 default:
2703 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002704 }
Calin Juravled0d48522014-11-04 16:40:20 +00002705}
2706
Calin Juravle9aec02f2014-11-18 23:06:35 +00002707void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2708 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2709
2710 LocationSummary* locations =
2711 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2712
2713 switch (op->GetResultType()) {
2714 case Primitive::kPrimInt: {
Mark Mendell222fcf92015-03-30 14:13:30 -04002715 locations->SetInAt(0, Location::Any());
2716 // The shift count needs to be in CL or a constant.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002717 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2718 locations->SetOut(Location::SameAsFirstInput());
2719 break;
2720 }
2721 case Primitive::kPrimLong: {
2722 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell222fcf92015-03-30 14:13:30 -04002723 // The shift count needs to be in CL or a constant.
2724 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002725 locations->SetOut(Location::SameAsFirstInput());
2726 break;
2727 }
2728 default:
2729 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2730 }
2731}
2732
2733void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2734 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2735
2736 LocationSummary* locations = op->GetLocations();
2737 Location first = locations->InAt(0);
2738 Location second = locations->InAt(1);
2739 DCHECK(first.Equals(locations->Out()));
2740
2741 switch (op->GetResultType()) {
2742 case Primitive::kPrimInt: {
Mark Mendell222fcf92015-03-30 14:13:30 -04002743 if (first.IsRegister()) {
2744 Register first_reg = first.AsRegister<Register>();
2745 if (second.IsRegister()) {
2746 Register second_reg = second.AsRegister<Register>();
2747 DCHECK_EQ(ECX, second_reg);
2748 if (op->IsShl()) {
2749 __ shll(first_reg, second_reg);
2750 } else if (op->IsShr()) {
2751 __ sarl(first_reg, second_reg);
2752 } else {
2753 __ shrl(first_reg, second_reg);
2754 }
2755 } else {
2756 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2757 if (shift == 0) {
2758 return;
2759 }
2760 Immediate imm(shift);
2761 if (op->IsShl()) {
2762 __ shll(first_reg, imm);
2763 } else if (op->IsShr()) {
2764 __ sarl(first_reg, imm);
2765 } else {
2766 __ shrl(first_reg, imm);
2767 }
2768 }
2769 } else {
2770 DCHECK(first.IsStackSlot()) << first;
2771 Address addr(ESP, first.GetStackIndex());
2772 if (second.IsRegister()) {
2773 Register second_reg = second.AsRegister<Register>();
2774 DCHECK_EQ(ECX, second_reg);
2775 if (op->IsShl()) {
2776 __ shll(addr, second_reg);
2777 } else if (op->IsShr()) {
2778 __ sarl(addr, second_reg);
2779 } else {
2780 __ shrl(addr, second_reg);
2781 }
2782 } else {
2783 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2784 if (shift == 0) {
2785 return;
2786 }
2787 Immediate imm(shift);
2788 if (op->IsShl()) {
2789 __ shll(addr, imm);
2790 } else if (op->IsShr()) {
2791 __ sarl(addr, imm);
2792 } else {
2793 __ shrl(addr, imm);
2794 }
2795 }
2796 }
2797
2798 break;
2799 }
2800 case Primitive::kPrimLong: {
Calin Juravle9aec02f2014-11-18 23:06:35 +00002801 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002803 DCHECK_EQ(ECX, second_reg);
2804 if (op->IsShl()) {
Mark Mendell222fcf92015-03-30 14:13:30 -04002805 GenerateShlLong(first, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002806 } else if (op->IsShr()) {
Mark Mendell222fcf92015-03-30 14:13:30 -04002807 GenerateShrLong(first, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002808 } else {
Mark Mendell222fcf92015-03-30 14:13:30 -04002809 GenerateUShrLong(first, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002810 }
2811 } else {
Mark Mendell222fcf92015-03-30 14:13:30 -04002812 // Shift by a constant.
2813 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2814 // Nothing to do if the shift is 0, as the input is already the output.
2815 if (shift != 0) {
2816 if (op->IsShl()) {
2817 GenerateShlLong(first, shift);
2818 } else if (op->IsShr()) {
2819 GenerateShrLong(first, shift);
2820 } else {
2821 GenerateUShrLong(first, shift);
2822 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002823 }
2824 }
2825 break;
2826 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002827 default:
2828 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2829 }
2830}
2831
Mark Mendell222fcf92015-03-30 14:13:30 -04002832void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2833 Register low = loc.AsRegisterPairLow<Register>();
2834 Register high = loc.AsRegisterPairHigh<Register>();
2835 if (shift == 32) {
2836 // Shift by 32 is easy. High gets low, and low gets 0.
2837 codegen_->EmitParallelMoves(
2838 loc.ToLow(), loc.ToHigh(),
2839 Location::ConstantLocation(GetGraph()->GetIntConstant(0)), loc.ToLow());
2840 } else if (shift > 32) {
2841 // Low part becomes 0. High part is low part << (shift-32).
2842 __ movl(high, low);
2843 __ shll(high, Immediate(shift - 32));
2844 __ xorl(low, low);
2845 } else {
2846 // Between 1 and 31.
2847 __ shld(high, low, Immediate(shift));
2848 __ shll(low, Immediate(shift));
2849 }
2850}
2851
Calin Juravle9aec02f2014-11-18 23:06:35 +00002852void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2853 Label done;
2854 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2855 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2856 __ testl(shifter, Immediate(32));
2857 __ j(kEqual, &done);
2858 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2859 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2860 __ Bind(&done);
2861}
2862
Mark Mendell222fcf92015-03-30 14:13:30 -04002863void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2864 Register low = loc.AsRegisterPairLow<Register>();
2865 Register high = loc.AsRegisterPairHigh<Register>();
2866 if (shift == 32) {
2867 // Need to copy the sign.
2868 DCHECK_NE(low, high);
2869 __ movl(low, high);
2870 __ sarl(high, Immediate(31));
2871 } else if (shift > 32) {
2872 DCHECK_NE(low, high);
2873 // High part becomes sign. Low part is shifted by shift - 32.
2874 __ movl(low, high);
2875 __ sarl(high, Immediate(31));
2876 __ shrl(low, Immediate(shift - 32));
2877 } else {
2878 // Between 1 and 31.
2879 __ shrd(low, high, Immediate(shift));
2880 __ sarl(high, Immediate(shift));
2881 }
2882}
2883
Calin Juravle9aec02f2014-11-18 23:06:35 +00002884void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2885 Label done;
2886 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2887 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2888 __ testl(shifter, Immediate(32));
2889 __ j(kEqual, &done);
2890 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2891 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2892 __ Bind(&done);
2893}
2894
Mark Mendell222fcf92015-03-30 14:13:30 -04002895void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2896 Register low = loc.AsRegisterPairLow<Register>();
2897 Register high = loc.AsRegisterPairHigh<Register>();
2898 if (shift == 32) {
2899 // Shift by 32 is easy. Low gets high, and high gets 0.
2900 codegen_->EmitParallelMoves(
2901 loc.ToHigh(), loc.ToLow(),
2902 Location::ConstantLocation(GetGraph()->GetIntConstant(0)), loc.ToHigh());
2903 } else if (shift > 32) {
2904 // Low part is high >> (shift - 32). High part becomes 0.
2905 __ movl(low, high);
2906 __ shrl(low, Immediate(shift - 32));
2907 __ xorl(high, high);
2908 } else {
2909 // Between 1 and 31.
2910 __ shrd(low, high, Immediate(shift));
2911 __ shrl(high, Immediate(shift));
2912 }
2913}
2914
Calin Juravle9aec02f2014-11-18 23:06:35 +00002915void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2916 Label done;
2917 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2918 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2919 __ testl(shifter, Immediate(32));
2920 __ j(kEqual, &done);
2921 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2922 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2923 __ Bind(&done);
2924}
2925
2926void LocationsBuilderX86::VisitShl(HShl* shl) {
2927 HandleShift(shl);
2928}
2929
2930void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2931 HandleShift(shl);
2932}
2933
2934void LocationsBuilderX86::VisitShr(HShr* shr) {
2935 HandleShift(shr);
2936}
2937
2938void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2939 HandleShift(shr);
2940}
2941
2942void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2943 HandleShift(ushr);
2944}
2945
2946void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2947 HandleShift(ushr);
2948}
2949
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002950void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002951 LocationSummary* locations =
2952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002953 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002954 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002955 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2956 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002957}
2958
2959void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2960 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002961 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002962 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002963
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002964 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002965
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002967 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002968}
2969
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002970void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2971 LocationSummary* locations =
2972 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2973 locations->SetOut(Location::RegisterLocation(EAX));
2974 InvokeRuntimeCallingConvention calling_convention;
2975 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002976 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2977 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002978}
2979
2980void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2981 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002982 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002983 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2984
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002985 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002986
2987 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2988 DCHECK(!codegen_->IsLeafMethod());
2989}
2990
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002991void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 LocationSummary* locations =
2993 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002994 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2995 if (location.IsStackSlot()) {
2996 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2997 } else if (location.IsDoubleStackSlot()) {
2998 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002999 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003000 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003001}
3002
3003void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003004 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003005}
3006
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003007void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003008 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003009 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003010 locations->SetInAt(0, Location::RequiresRegister());
3011 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003012}
3013
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003014void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3015 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003016 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003017 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003018 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003019 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003020 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003022 break;
3023
3024 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003025 __ notl(out.AsRegisterPairLow<Register>());
3026 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003027 break;
3028
3029 default:
3030 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3031 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003032}
3033
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003034void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003035 LocationSummary* locations =
3036 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003037 switch (compare->InputAt(0)->GetType()) {
3038 case Primitive::kPrimLong: {
3039 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003040 locations->SetInAt(1, Location::Any());
3041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3042 break;
3043 }
3044 case Primitive::kPrimFloat:
3045 case Primitive::kPrimDouble: {
3046 locations->SetInAt(0, Location::RequiresFpuRegister());
3047 locations->SetInAt(1, Location::RequiresFpuRegister());
3048 locations->SetOut(Location::RequiresRegister());
3049 break;
3050 }
3051 default:
3052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003054}
3055
3056void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003059 Location left = locations->InAt(0);
3060 Location right = locations->InAt(1);
3061
3062 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003063 switch (compare->InputAt(0)->GetType()) {
3064 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003065 Register left_low = left.AsRegisterPairLow<Register>();
3066 Register left_high = left.AsRegisterPairHigh<Register>();
3067 int32_t val_low = 0;
3068 int32_t val_high = 0;
3069 bool right_is_const = false;
3070
3071 if (right.IsConstant()) {
3072 DCHECK(right.GetConstant()->IsLongConstant());
3073 right_is_const = true;
3074 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3075 val_low = Low32Bits(val);
3076 val_high = High32Bits(val);
3077 }
3078
Calin Juravleddb7df22014-11-25 20:56:51 +00003079 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003080 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003082 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003083 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003084 DCHECK(right_is_const) << right;
3085 if (val_high == 0) {
3086 __ testl(left_high, left_high);
3087 } else {
3088 __ cmpl(left_high, Immediate(val_high));
3089 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003090 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003091 __ j(kLess, &less); // Signed compare.
3092 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003093 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003094 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003095 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003096 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003097 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003098 DCHECK(right_is_const) << right;
3099 if (val_low == 0) {
3100 __ testl(left_low, left_low);
3101 } else {
3102 __ cmpl(left_low, Immediate(val_low));
3103 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003104 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003105 break;
3106 }
3107 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003109 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3110 break;
3111 }
3112 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003114 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003115 break;
3116 }
3117 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003118 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003119 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003120 __ movl(out, Immediate(0));
3121 __ j(kEqual, &done);
3122 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3123
3124 __ Bind(&greater);
3125 __ movl(out, Immediate(1));
3126 __ jmp(&done);
3127
3128 __ Bind(&less);
3129 __ movl(out, Immediate(-1));
3130
3131 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003132}
3133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003134void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 LocationSummary* locations =
3136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003137 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3138 locations->SetInAt(i, Location::Any());
3139 }
3140 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003141}
3142
3143void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003144 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003145 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003146}
3147
Calin Juravle52c48962014-12-16 17:02:57 +00003148void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3149 /*
3150 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3151 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3152 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3153 */
3154 switch (kind) {
3155 case MemBarrierKind::kAnyAny: {
3156 __ mfence();
3157 break;
3158 }
3159 case MemBarrierKind::kAnyStore:
3160 case MemBarrierKind::kLoadAny:
3161 case MemBarrierKind::kStoreStore: {
3162 // nop
3163 break;
3164 }
3165 default:
3166 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003167 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003168}
3169
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003170
Mark Mendell09ed1a32015-03-25 08:30:06 -04003171void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3172 Register temp) {
3173 // TODO: Implement all kinds of calls:
3174 // 1) boot -> boot
3175 // 2) app -> boot
3176 // 3) app -> app
3177 //
3178 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3179 // temp = method;
3180 LoadCurrentMethod(temp);
3181 if (!invoke->IsRecursive()) {
3182 // temp = temp->dex_cache_resolved_methods_;
3183 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3184 // temp = temp[index_in_cache]
3185 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3186 // (temp + offset_of_quick_compiled_code)()
3187 __ call(Address(
3188 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3189 } else {
3190 __ call(GetFrameEntryLabel());
3191 }
3192
3193 DCHECK(!IsLeafMethod());
3194 RecordPcInfo(invoke, invoke->GetDexPc());
3195}
3196
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003197void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003198 Label is_null;
3199 __ testl(value, value);
3200 __ j(kEqual, &is_null);
3201 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3202 __ movl(temp, object);
3203 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003204 __ movb(Address(temp, card, TIMES_1, 0),
3205 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003206 __ Bind(&is_null);
3207}
3208
Calin Juravle52c48962014-12-16 17:02:57 +00003209void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3210 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003211 LocationSummary* locations =
3212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003213 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003214
3215 // The output overlaps in case of long: we don't want the low move to overwrite
3216 // the object's location.
3217 locations->SetOut(Location::RequiresRegister(),
3218 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3219 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00003220
3221 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3222 // Long values can be loaded atomically into an XMM using movsd.
3223 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3224 // and then copy the XMM into the output 32bits at a time).
3225 locations->AddTemp(Location::RequiresFpuRegister());
3226 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003227}
3228
Calin Juravle52c48962014-12-16 17:02:57 +00003229void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3230 const FieldInfo& field_info) {
3231 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003232
Calin Juravle52c48962014-12-16 17:02:57 +00003233 LocationSummary* locations = instruction->GetLocations();
3234 Register base = locations->InAt(0).AsRegister<Register>();
3235 Location out = locations->Out();
3236 bool is_volatile = field_info.IsVolatile();
3237 Primitive::Type field_type = field_info.GetFieldType();
3238 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3239
3240 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003241 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003242 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003243 break;
3244 }
3245
3246 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003247 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003248 break;
3249 }
3250
3251 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003252 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003253 break;
3254 }
3255
3256 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003257 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003258 break;
3259 }
3260
3261 case Primitive::kPrimInt:
3262 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003263 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264 break;
3265 }
3266
3267 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003268 if (is_volatile) {
3269 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3270 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003271 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003272 __ movd(out.AsRegisterPairLow<Register>(), temp);
3273 __ psrlq(temp, Immediate(32));
3274 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3275 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003276 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003277 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003278 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003279 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3280 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003281 break;
3282 }
3283
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003284 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003285 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003286 break;
3287 }
3288
3289 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003290 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003291 break;
3292 }
3293
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003295 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003296 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003297 }
Calin Juravle52c48962014-12-16 17:02:57 +00003298
Calin Juravle77520bc2015-01-12 18:45:46 +00003299 // Longs are handled in the switch.
3300 if (field_type != Primitive::kPrimLong) {
3301 codegen_->MaybeRecordImplicitNullCheck(instruction);
3302 }
3303
Calin Juravle52c48962014-12-16 17:02:57 +00003304 if (is_volatile) {
3305 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3306 }
3307}
3308
3309void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3310 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3311
3312 LocationSummary* locations =
3313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3314 locations->SetInAt(0, Location::RequiresRegister());
3315 bool is_volatile = field_info.IsVolatile();
3316 Primitive::Type field_type = field_info.GetFieldType();
3317 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3318 || (field_type == Primitive::kPrimByte);
3319
3320 // The register allocator does not support multiple
3321 // inputs that die at entry with one in a specific register.
3322 if (is_byte_type) {
3323 // Ensure the value is in a byte register.
3324 locations->SetInAt(1, Location::RegisterLocation(EAX));
3325 } else {
3326 locations->SetInAt(1, Location::RequiresRegister());
3327 }
3328 // Temporary registers for the write barrier.
3329 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3330 locations->AddTemp(Location::RequiresRegister());
3331 // Ensure the card is in a byte register.
3332 locations->AddTemp(Location::RegisterLocation(ECX));
3333 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3334 // 64bits value can be atomically written to an address with movsd and an XMM register.
3335 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3336 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3337 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3338 // isolated cases when we need this it isn't worth adding the extra complexity.
3339 locations->AddTemp(Location::RequiresFpuRegister());
3340 locations->AddTemp(Location::RequiresFpuRegister());
3341 }
3342}
3343
3344void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3345 const FieldInfo& field_info) {
3346 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3347
3348 LocationSummary* locations = instruction->GetLocations();
3349 Register base = locations->InAt(0).AsRegister<Register>();
3350 Location value = locations->InAt(1);
3351 bool is_volatile = field_info.IsVolatile();
3352 Primitive::Type field_type = field_info.GetFieldType();
3353 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3354
3355 if (is_volatile) {
3356 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3357 }
3358
3359 switch (field_type) {
3360 case Primitive::kPrimBoolean:
3361 case Primitive::kPrimByte: {
3362 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3363 break;
3364 }
3365
3366 case Primitive::kPrimShort:
3367 case Primitive::kPrimChar: {
3368 __ movw(Address(base, offset), value.AsRegister<Register>());
3369 break;
3370 }
3371
3372 case Primitive::kPrimInt:
3373 case Primitive::kPrimNot: {
3374 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003375 break;
3376 }
3377
3378 case Primitive::kPrimLong: {
3379 if (is_volatile) {
3380 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3381 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3382 __ movd(temp1, value.AsRegisterPairLow<Register>());
3383 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3384 __ punpckldq(temp1, temp2);
3385 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003386 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003387 } else {
3388 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003390 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3391 }
3392 break;
3393 }
3394
3395 case Primitive::kPrimFloat: {
3396 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3397 break;
3398 }
3399
3400 case Primitive::kPrimDouble: {
3401 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3402 break;
3403 }
3404
3405 case Primitive::kPrimVoid:
3406 LOG(FATAL) << "Unreachable type " << field_type;
3407 UNREACHABLE();
3408 }
3409
Calin Juravle77520bc2015-01-12 18:45:46 +00003410 // Longs are handled in the switch.
3411 if (field_type != Primitive::kPrimLong) {
3412 codegen_->MaybeRecordImplicitNullCheck(instruction);
3413 }
3414
3415 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3416 Register temp = locations->GetTemp(0).AsRegister<Register>();
3417 Register card = locations->GetTemp(1).AsRegister<Register>();
3418 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3419 }
3420
Calin Juravle52c48962014-12-16 17:02:57 +00003421 if (is_volatile) {
3422 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3423 }
3424}
3425
3426void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3427 HandleFieldGet(instruction, instruction->GetFieldInfo());
3428}
3429
3430void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3431 HandleFieldGet(instruction, instruction->GetFieldInfo());
3432}
3433
3434void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3435 HandleFieldSet(instruction, instruction->GetFieldInfo());
3436}
3437
3438void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3439 HandleFieldSet(instruction, instruction->GetFieldInfo());
3440}
3441
3442void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3443 HandleFieldSet(instruction, instruction->GetFieldInfo());
3444}
3445
3446void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3447 HandleFieldSet(instruction, instruction->GetFieldInfo());
3448}
3449
3450void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3451 HandleFieldGet(instruction, instruction->GetFieldInfo());
3452}
3453
3454void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3455 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003456}
3457
3458void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003461 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3462 ? Location::RequiresRegister()
3463 : Location::Any();
3464 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003465 if (instruction->HasUses()) {
3466 locations->SetOut(Location::SameAsFirstInput());
3467 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003468}
3469
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003470void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003471 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3472 return;
3473 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003474 LocationSummary* locations = instruction->GetLocations();
3475 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003476
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003477 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3478 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3479}
3480
3481void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003482 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003483 codegen_->AddSlowPath(slow_path);
3484
3485 LocationSummary* locations = instruction->GetLocations();
3486 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003487
3488 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003489 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003490 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003491 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003492 } else {
3493 DCHECK(obj.IsConstant()) << obj;
3494 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3495 __ jmp(slow_path->GetEntryLabel());
3496 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003497 }
3498 __ j(kEqual, slow_path->GetEntryLabel());
3499}
3500
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003501void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3502 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3503 GenerateImplicitNullCheck(instruction);
3504 } else {
3505 GenerateExplicitNullCheck(instruction);
3506 }
3507}
3508
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003509void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003510 LocationSummary* locations =
3511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003512 locations->SetInAt(0, Location::RequiresRegister());
3513 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003514 // The output overlaps in case of long: we don't want the low move to overwrite
3515 // the array's location.
3516 locations->SetOut(Location::RequiresRegister(),
3517 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3518 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519}
3520
3521void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3522 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003523 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003524 Location index = locations->InAt(1);
3525
Calin Juravle77520bc2015-01-12 18:45:46 +00003526 Primitive::Type type = instruction->GetType();
3527 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003528 case Primitive::kPrimBoolean: {
3529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003530 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003531 if (index.IsConstant()) {
3532 __ movzxb(out, Address(obj,
3533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3534 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003535 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003536 }
3537 break;
3538 }
3539
3540 case Primitive::kPrimByte: {
3541 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003542 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003543 if (index.IsConstant()) {
3544 __ movsxb(out, Address(obj,
3545 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3546 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003547 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003548 }
3549 break;
3550 }
3551
3552 case Primitive::kPrimShort: {
3553 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003554 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003555 if (index.IsConstant()) {
3556 __ movsxw(out, Address(obj,
3557 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3558 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 }
3561 break;
3562 }
3563
3564 case Primitive::kPrimChar: {
3565 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 if (index.IsConstant()) {
3568 __ movzxw(out, Address(obj,
3569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3570 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003571 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572 }
3573 break;
3574 }
3575
3576 case Primitive::kPrimInt:
3577 case Primitive::kPrimNot: {
3578 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003580 if (index.IsConstant()) {
3581 __ movl(out, Address(obj,
3582 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3583 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585 }
3586 break;
3587 }
3588
3589 case Primitive::kPrimLong: {
3590 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003591 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003592 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 if (index.IsConstant()) {
3594 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003595 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003596 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003597 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003598 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003599 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003601 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003602 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 }
3605 break;
3606 }
3607
Mark Mendell7c8d0092015-01-26 11:21:33 -05003608 case Primitive::kPrimFloat: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3610 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3611 if (index.IsConstant()) {
3612 __ movss(out, Address(obj,
3613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3614 } else {
3615 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3616 }
3617 break;
3618 }
3619
3620 case Primitive::kPrimDouble: {
3621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3622 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3623 if (index.IsConstant()) {
3624 __ movsd(out, Address(obj,
3625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3626 } else {
3627 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3628 }
3629 break;
3630 }
3631
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003633 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003634 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003636
3637 if (type != Primitive::kPrimLong) {
3638 codegen_->MaybeRecordImplicitNullCheck(instruction);
3639 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640}
3641
3642void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003643 // This location builder might end up asking to up to four registers, which is
3644 // not currently possible for baseline. The situation in which we need four
3645 // registers cannot be met by baseline though, because it has not run any
3646 // optimization.
3647
Nicolas Geoffray39468442014-09-02 15:17:15 +01003648 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003649 bool needs_write_barrier =
3650 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3651
Mark Mendell5f874182015-03-04 15:42:45 -05003652 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003653
Nicolas Geoffray39468442014-09-02 15:17:15 +01003654 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3655 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003656 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003657
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003658 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003660 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3661 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3662 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003664 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3665 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003666 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003667 // In case of a byte operation, the register allocator does not support multiple
3668 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003669 locations->SetInAt(0, Location::RequiresRegister());
3670 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003671 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003672 // Ensure the value is in a byte register.
3673 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003674 } else {
Serguei Katkov55501ce2015-04-08 13:26:09 +06003675 bool is_fp_type = (value_type == Primitive::kPrimFloat)
3676 || (value_type == Primitive::kPrimDouble);
3677 if (is_fp_type) {
3678 locations->SetInAt(2, Location::RequiresFpuRegister());
3679 } else {
3680 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
3681 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003682 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003683 // Temporary registers for the write barrier.
3684 if (needs_write_barrier) {
3685 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003686 // Ensure the card is in a byte register.
3687 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003688 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003689 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003690}
3691
3692void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3693 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003694 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003696 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003697 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003698 bool needs_runtime_call = locations->WillCall();
3699 bool needs_write_barrier =
3700 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003701
3702 switch (value_type) {
3703 case Primitive::kPrimBoolean:
3704 case Primitive::kPrimByte: {
3705 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003706 if (index.IsConstant()) {
3707 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003708 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003709 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003710 } else {
3711 __ movb(Address(obj, offset),
3712 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003714 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003715 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003716 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003717 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003718 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003719 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003720 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003723 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003724 break;
3725 }
3726
3727 case Primitive::kPrimShort:
3728 case Primitive::kPrimChar: {
3729 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 if (index.IsConstant()) {
3731 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003732 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003734 } else {
3735 __ movw(Address(obj, offset),
3736 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3737 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003739 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3741 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003742 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003743 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003744 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748 break;
3749 }
3750
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003751 case Primitive::kPrimInt:
3752 case Primitive::kPrimNot: {
3753 if (!needs_runtime_call) {
3754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3755 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003756 size_t offset =
3757 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003758 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003760 } else {
3761 DCHECK(value.IsConstant()) << value;
3762 __ movl(Address(obj, offset),
3763 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3764 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003765 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003766 DCHECK(index.IsRegister()) << index;
3767 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003768 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3769 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 } else {
3771 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003773 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3774 }
3775 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003776 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003777
3778 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 Register temp = locations->GetTemp(0).AsRegister<Register>();
3780 Register card = locations->GetTemp(1).AsRegister<Register>();
3781 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003783 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003784 DCHECK_EQ(value_type, Primitive::kPrimNot);
3785 DCHECK(!codegen_->IsLeafMethod());
3786 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3787 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 }
3789 break;
3790 }
3791
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 case Primitive::kPrimLong: {
3793 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 if (index.IsConstant()) {
3795 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003796 if (value.IsRegisterPair()) {
3797 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003799 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003801 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003802 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3803 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003805 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3806 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003808 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003810 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003813 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003814 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003815 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003816 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003818 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003821 Immediate(High32Bits(val)));
3822 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 }
3824 break;
3825 }
3826
Mark Mendell7c8d0092015-01-26 11:21:33 -05003827 case Primitive::kPrimFloat: {
3828 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3829 DCHECK(value.IsFpuRegister());
3830 if (index.IsConstant()) {
3831 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3832 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3833 } else {
3834 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3835 value.AsFpuRegister<XmmRegister>());
3836 }
3837 break;
3838 }
3839
3840 case Primitive::kPrimDouble: {
3841 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3842 DCHECK(value.IsFpuRegister());
3843 if (index.IsConstant()) {
3844 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3845 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3846 } else {
3847 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3848 value.AsFpuRegister<XmmRegister>());
3849 }
3850 break;
3851 }
3852
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853 case Primitive::kPrimVoid:
3854 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003855 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856 }
3857}
3858
3859void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003861 locations->SetInAt(0, Location::RequiresRegister());
3862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 instruction->SetLocations(locations);
3864}
3865
3866void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3867 LocationSummary* locations = instruction->GetLocations();
3868 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 Register obj = locations->InAt(0).AsRegister<Register>();
3870 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003872 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003873}
3874
3875void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003876 LocationSummary* locations =
3877 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003878 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003880 if (instruction->HasUses()) {
3881 locations->SetOut(Location::SameAsFirstInput());
3882 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003883}
3884
3885void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3886 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003887 Location index_loc = locations->InAt(0);
3888 Location length_loc = locations->InAt(1);
3889 SlowPathCodeX86* slow_path =
3890 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891 codegen_->AddSlowPath(slow_path);
3892
Mark Mendellf60c90b2015-03-04 15:12:59 -05003893 Register length = length_loc.AsRegister<Register>();
3894 if (index_loc.IsConstant()) {
3895 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3896 __ cmpl(length, Immediate(value));
3897 } else {
3898 __ cmpl(length, index_loc.AsRegister<Register>());
3899 }
3900 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003901}
3902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3904 temp->SetLocations(nullptr);
3905}
3906
3907void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3908 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003909 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003910}
3911
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003912void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003913 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003914 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003915}
3916
3917void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003918 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3919}
3920
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003921void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3923}
3924
3925void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003926 HBasicBlock* block = instruction->GetBlock();
3927 if (block->GetLoopInformation() != nullptr) {
3928 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3929 // The back edge will generate the suspend check.
3930 return;
3931 }
3932 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3933 // The goto will generate the suspend check.
3934 return;
3935 }
3936 GenerateSuspendCheck(instruction, nullptr);
3937}
3938
3939void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3940 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003941 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003942 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003943 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003944 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003945 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003946 if (successor == nullptr) {
3947 __ j(kNotEqual, slow_path->GetEntryLabel());
3948 __ Bind(slow_path->GetReturnLabel());
3949 } else {
3950 __ j(kEqual, codegen_->GetLabelOf(successor));
3951 __ jmp(slow_path->GetEntryLabel());
3952 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003953}
3954
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003955X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3956 return codegen_->GetAssembler();
3957}
3958
Mark Mendell7c8d0092015-01-26 11:21:33 -05003959void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003960 ScratchRegisterScope possible_scratch(
3961 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
3962 int temp = possible_scratch.GetRegister();
3963 if (temp == kNoRegister) {
3964 // Use the stack.
3965 __ pushl(Address(ESP, src));
3966 __ popl(Address(ESP, dst));
3967 } else {
3968 Register temp_reg = static_cast<Register>(temp);
3969 __ movl(temp_reg, Address(ESP, src));
3970 __ movl(Address(ESP, dst), temp_reg);
3971 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003972}
3973
3974void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003975 ScratchRegisterScope possible_scratch(
3976 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
3977 int temp = possible_scratch.GetRegister();
3978 if (temp == kNoRegister) {
3979 // Use the stack instead.
3980 // Push src low word.
3981 __ pushl(Address(ESP, src));
3982 // Push src high word. Stack offset = 4.
3983 __ pushl(Address(ESP, src + 4 /* offset */ + kX86WordSize /* high */));
3984
3985 // Pop into dst high word. Stack offset = 8.
3986 // Pop with ESP address uses the 'after increment' value of ESP.
3987 __ popl(Address(ESP, dst + 4 /* offset */ + kX86WordSize /* high */));
3988 // Finally dst low word. Stack offset = 4.
3989 __ popl(Address(ESP, dst));
3990 } else {
3991 Register temp_reg = static_cast<Register>(temp);
3992 __ movl(temp_reg, Address(ESP, src));
3993 __ movl(Address(ESP, dst), temp_reg);
3994 __ movl(temp_reg, Address(ESP, src + kX86WordSize));
3995 __ movl(Address(ESP, dst + kX86WordSize), temp_reg);
3996 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003997}
3998
3999void ParallelMoveResolverX86::EmitMove(size_t index) {
4000 MoveOperands* move = moves_.Get(index);
4001 Location source = move->GetSource();
4002 Location destination = move->GetDestination();
4003
4004 if (source.IsRegister()) {
4005 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004007 } else {
4008 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004009 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004010 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004011 } else if (source.IsFpuRegister()) {
4012 if (destination.IsFpuRegister()) {
4013 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4014 } else if (destination.IsStackSlot()) {
4015 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4016 } else {
4017 DCHECK(destination.IsDoubleStackSlot());
4018 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4019 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004020 } else if (source.IsStackSlot()) {
4021 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004023 } else if (destination.IsFpuRegister()) {
4024 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004025 } else {
4026 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004027 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4028 }
4029 } else if (source.IsDoubleStackSlot()) {
4030 if (destination.IsFpuRegister()) {
4031 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4032 } else {
4033 DCHECK(destination.IsDoubleStackSlot()) << destination;
4034 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004035 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004036 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004037 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004038 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004039 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004040 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004041 if (value == 0) {
4042 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4043 } else {
4044 __ movl(destination.AsRegister<Register>(), Immediate(value));
4045 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004046 } else {
4047 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004048 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004049 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004050 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004051 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004052 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004053 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004054 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004055 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4056 if (value == 0) {
4057 // Easy handling of 0.0.
4058 __ xorps(dest, dest);
4059 } else {
4060 ScratchRegisterScope ensure_scratch(
Mark Mendella5c19ce2015-04-01 12:51:05 -04004061 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
4062 int temp_reg = ensure_scratch.GetRegister();
4063 if (temp_reg == kNoRegister) {
4064 // Avoid spilling/restoring a scratch register by using the stack.
4065 __ pushl(Immediate(value));
4066 __ movss(dest, Address(ESP, 0));
4067 __ addl(ESP, Immediate(4));
4068 } else {
4069 Register temp = static_cast<Register>(temp_reg);
4070 __ movl(temp, Immediate(value));
4071 __ movd(dest, temp);
4072 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004073 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004074 } else {
4075 DCHECK(destination.IsStackSlot()) << destination;
4076 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4077 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004078 } else if (constant->IsLongConstant()) {
4079 int64_t value = constant->AsLongConstant()->GetValue();
4080 int32_t low_value = Low32Bits(value);
4081 int32_t high_value = High32Bits(value);
4082 Immediate low(low_value);
4083 Immediate high(high_value);
4084 if (destination.IsDoubleStackSlot()) {
4085 __ movl(Address(ESP, destination.GetStackIndex()), low);
4086 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4087 } else {
4088 __ movl(destination.AsRegisterPairLow<Register>(), low);
4089 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4090 }
4091 } else {
4092 DCHECK(constant->IsDoubleConstant());
4093 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004094 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004095 int32_t low_value = Low32Bits(value);
4096 int32_t high_value = High32Bits(value);
4097 Immediate low(low_value);
4098 Immediate high(high_value);
4099 if (destination.IsFpuRegister()) {
4100 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4101 if (value == 0) {
4102 // Easy handling of 0.0.
4103 __ xorpd(dest, dest);
4104 } else {
4105 __ pushl(high);
4106 __ pushl(low);
4107 __ movsd(dest, Address(ESP, 0));
4108 __ addl(ESP, Immediate(8));
4109 }
4110 } else {
4111 DCHECK(destination.IsDoubleStackSlot()) << destination;
4112 __ movl(Address(ESP, destination.GetStackIndex()), low);
4113 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4114 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004115 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004116 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004117 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004118 }
4119}
4120
Mark Mendella5c19ce2015-04-01 12:51:05 -04004121void ParallelMoveResolverX86::Exchange(Register reg1, Register reg2) {
4122 // Prefer to avoid xchg as it isn't speedy on smaller processors.
4123 ScratchRegisterScope possible_scratch(
4124 this, reg1, codegen_->GetNumberOfCoreRegisters());
4125 int temp_reg = possible_scratch.GetRegister();
4126 if (temp_reg == kNoRegister || temp_reg == reg2) {
4127 __ pushl(reg1);
4128 __ movl(reg1, reg2);
4129 __ popl(reg2);
4130 } else {
4131 Register temp = static_cast<Register>(temp_reg);
4132 __ movl(temp, reg1);
4133 __ movl(reg1, reg2);
4134 __ movl(reg2, temp);
4135 }
4136}
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004137
Mark Mendella5c19ce2015-04-01 12:51:05 -04004138void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
4139 ScratchRegisterScope possible_scratch(
4140 this, reg, codegen_->GetNumberOfCoreRegisters());
4141 int temp_reg = possible_scratch.GetRegister();
4142 if (temp_reg == kNoRegister) {
4143 __ pushl(Address(ESP, mem));
4144 __ movl(Address(ESP, mem + kX86WordSize), reg);
4145 __ popl(reg);
4146 } else {
4147 Register temp = static_cast<Register>(temp_reg);
4148 __ movl(temp, Address(ESP, mem));
4149 __ movl(Address(ESP, mem), reg);
4150 __ movl(reg, temp);
4151 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004152}
4153
Mark Mendell7c8d0092015-01-26 11:21:33 -05004154void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004155 ScratchRegisterScope possible_scratch(
4156 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
4157 int temp_reg = possible_scratch.GetRegister();
4158 if (temp_reg == kNoRegister) {
4159 __ pushl(Address(ESP, mem));
4160 __ movss(Address(ESP, mem + kX86WordSize), reg);
4161 __ movss(reg, Address(ESP, 0));
4162 __ addl(ESP, Immediate(kX86WordSize));
4163 } else {
4164 Register temp = static_cast<Register>(temp_reg);
4165 __ movl(temp, Address(ESP, mem));
4166 __ movss(Address(ESP, mem), reg);
4167 __ movd(reg, temp);
4168 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004169}
4170
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004171void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004172 ScratchRegisterScope possible_scratch1(
4173 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
4174 int temp_reg1 = possible_scratch1.GetRegister();
4175 if (temp_reg1 == kNoRegister) {
4176 // No free registers. Use the stack.
4177 __ pushl(Address(ESP, mem1));
4178 __ pushl(Address(ESP, mem2 + kX86WordSize));
4179 // Pop with ESP address uses the 'after increment' value of ESP.
4180 __ popl(Address(ESP, mem1 + kX86WordSize));
4181 __ popl(Address(ESP, mem2));
4182 } else {
4183 // Got the first one. Try for a second.
4184 ScratchRegisterScope possible_scratch2(
4185 this, temp_reg1, codegen_->GetNumberOfCoreRegisters());
4186 int temp_reg2 = possible_scratch2.GetRegister();
4187 if (temp_reg2 == kNoRegister) {
4188 Register temp = static_cast<Register>(temp_reg1);
4189 // Bummer. Only have one free register to use.
4190 // Save mem1 on the stack.
4191 __ pushl(Address(ESP, mem1));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004192
Mark Mendella5c19ce2015-04-01 12:51:05 -04004193 // Copy mem2 into mem1.
4194 __ movl(temp, Address(ESP, mem2 + kX86WordSize));
4195 __ movl(Address(ESP, mem1 + kX86WordSize), temp);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004196
Mark Mendella5c19ce2015-04-01 12:51:05 -04004197 // Now pop mem1 into mem2.
4198 // Pop with ESP address uses the 'after increment' value of ESP.
4199 __ popl(Address(ESP, mem2));
4200 } else {
4201 // Great. We have 2 registers to play with.
4202 Register temp1 = static_cast<Register>(temp_reg1);
4203 Register temp2 = static_cast<Register>(temp_reg2);
4204 DCHECK_NE(temp1, temp2);
4205 __ movl(temp1, Address(ESP, mem1));
4206 __ movl(temp2, Address(ESP, mem2));
4207 __ movl(Address(ESP, mem2), temp1);
4208 __ movl(Address(ESP, mem1), temp2);
4209 }
4210 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004211}
4212
4213void ParallelMoveResolverX86::EmitSwap(size_t index) {
4214 MoveOperands* move = moves_.Get(index);
4215 Location source = move->GetSource();
4216 Location destination = move->GetDestination();
4217
4218 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004219 Exchange(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004220 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004222 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4225 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004226 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4227 // Use XOR Swap algorithm to avoid a temporary.
4228 DCHECK_NE(source.reg(), destination.reg());
4229 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4230 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4231 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4232 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4233 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4234 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4235 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004236 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4237 // Take advantage of the 16 bytes in the XMM register.
4238 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4239 Address stack(ESP, destination.GetStackIndex());
4240 // Load the double into the high doubleword.
4241 __ movhpd(reg, stack);
4242
4243 // Store the low double into the destination.
4244 __ movsd(stack, reg);
4245
4246 // Move the high double to the low double.
4247 __ psrldq(reg, Immediate(8));
4248 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4249 // Take advantage of the 16 bytes in the XMM register.
4250 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4251 Address stack(ESP, source.GetStackIndex());
4252 // Load the double into the high doubleword.
4253 __ movhpd(reg, stack);
4254
4255 // Store the low double into the destination.
4256 __ movsd(stack, reg);
4257
4258 // Move the high double to the low double.
4259 __ psrldq(reg, Immediate(8));
4260 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4261 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4262 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004263 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004264 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004265 }
4266}
4267
4268void ParallelMoveResolverX86::SpillScratch(int reg) {
4269 __ pushl(static_cast<Register>(reg));
4270}
4271
4272void ParallelMoveResolverX86::RestoreScratch(int reg) {
4273 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004274}
4275
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004276void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004277 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4278 ? LocationSummary::kCallOnSlowPath
4279 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004281 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004282 locations->SetOut(Location::RequiresRegister());
4283}
4284
4285void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004287 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004288 DCHECK(!cls->CanCallRuntime());
4289 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004290 codegen_->LoadCurrentMethod(out);
4291 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4292 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004293 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004294 codegen_->LoadCurrentMethod(out);
4295 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4296 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004297
4298 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4299 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4300 codegen_->AddSlowPath(slow_path);
4301 __ testl(out, out);
4302 __ j(kEqual, slow_path->GetEntryLabel());
4303 if (cls->MustGenerateClinitCheck()) {
4304 GenerateClassInitializationCheck(slow_path, out);
4305 } else {
4306 __ Bind(slow_path->GetExitLabel());
4307 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004308 }
4309}
4310
4311void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4312 LocationSummary* locations =
4313 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4314 locations->SetInAt(0, Location::RequiresRegister());
4315 if (check->HasUses()) {
4316 locations->SetOut(Location::SameAsFirstInput());
4317 }
4318}
4319
4320void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004321 // We assume the class to not be null.
4322 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4323 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004324 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004325 GenerateClassInitializationCheck(slow_path,
4326 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004327}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004328
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004329void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4330 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004331 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4332 Immediate(mirror::Class::kStatusInitialized));
4333 __ j(kLess, slow_path->GetEntryLabel());
4334 __ Bind(slow_path->GetExitLabel());
4335 // No need for memory fence, thanks to the X86 memory model.
4336}
4337
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004338void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4339 LocationSummary* locations =
4340 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4341 locations->SetOut(Location::RequiresRegister());
4342}
4343
4344void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4345 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4346 codegen_->AddSlowPath(slow_path);
4347
Roland Levillain271ab9c2014-11-27 15:23:57 +00004348 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004349 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004350 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4351 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004352 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4353 __ testl(out, out);
4354 __ j(kEqual, slow_path->GetEntryLabel());
4355 __ Bind(slow_path->GetExitLabel());
4356}
4357
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004358void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4359 LocationSummary* locations =
4360 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4361 locations->SetOut(Location::RequiresRegister());
4362}
4363
4364void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4365 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004366 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004367 __ fs()->movl(address, Immediate(0));
4368}
4369
4370void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4371 LocationSummary* locations =
4372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4373 InvokeRuntimeCallingConvention calling_convention;
4374 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4375}
4376
4377void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4378 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4379 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4380}
4381
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004382void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004383 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4384 ? LocationSummary::kNoCall
4385 : LocationSummary::kCallOnSlowPath;
4386 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4387 locations->SetInAt(0, Location::RequiresRegister());
4388 locations->SetInAt(1, Location::Any());
4389 locations->SetOut(Location::RequiresRegister());
4390}
4391
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004392void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004393 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004395 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004397 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4398 Label done, zero;
4399 SlowPathCodeX86* slow_path = nullptr;
4400
4401 // Return 0 if `obj` is null.
4402 // TODO: avoid this check if we know obj is not null.
4403 __ testl(obj, obj);
4404 __ j(kEqual, &zero);
4405 __ movl(out, Address(obj, class_offset));
4406 // Compare the class of `obj` with `cls`.
4407 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 } else {
4410 DCHECK(cls.IsStackSlot()) << cls;
4411 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4412 }
4413
4414 if (instruction->IsClassFinal()) {
4415 // Classes must be equal for the instanceof to succeed.
4416 __ j(kNotEqual, &zero);
4417 __ movl(out, Immediate(1));
4418 __ jmp(&done);
4419 } else {
4420 // If the classes are not equal, we go into a slow path.
4421 DCHECK(locations->OnlyCallsOnSlowPath());
4422 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004423 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004424 codegen_->AddSlowPath(slow_path);
4425 __ j(kNotEqual, slow_path->GetEntryLabel());
4426 __ movl(out, Immediate(1));
4427 __ jmp(&done);
4428 }
4429 __ Bind(&zero);
4430 __ movl(out, Immediate(0));
4431 if (slow_path != nullptr) {
4432 __ Bind(slow_path->GetExitLabel());
4433 }
4434 __ Bind(&done);
4435}
4436
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004437void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4438 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4439 instruction, LocationSummary::kCallOnSlowPath);
4440 locations->SetInAt(0, Location::RequiresRegister());
4441 locations->SetInAt(1, Location::Any());
4442 locations->AddTemp(Location::RequiresRegister());
4443}
4444
4445void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4446 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004448 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004449 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004450 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4451 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4452 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4453 codegen_->AddSlowPath(slow_path);
4454
4455 // TODO: avoid this check if we know obj is not null.
4456 __ testl(obj, obj);
4457 __ j(kEqual, slow_path->GetExitLabel());
4458 __ movl(temp, Address(obj, class_offset));
4459
4460 // Compare the class of `obj` with `cls`.
4461 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004462 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004463 } else {
4464 DCHECK(cls.IsStackSlot()) << cls;
4465 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4466 }
4467
4468 __ j(kNotEqual, slow_path->GetEntryLabel());
4469 __ Bind(slow_path->GetExitLabel());
4470}
4471
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004472void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4473 LocationSummary* locations =
4474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4475 InvokeRuntimeCallingConvention calling_convention;
4476 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4477}
4478
4479void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4480 __ fs()->call(Address::Absolute(instruction->IsEnter()
4481 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4482 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4483 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4484}
4485
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004486void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4487void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4488void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4489
4490void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4491 LocationSummary* locations =
4492 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4493 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4494 || instruction->GetResultType() == Primitive::kPrimLong);
4495 locations->SetInAt(0, Location::RequiresRegister());
4496 locations->SetInAt(1, Location::Any());
4497 locations->SetOut(Location::SameAsFirstInput());
4498}
4499
4500void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4501 HandleBitwiseOperation(instruction);
4502}
4503
4504void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4505 HandleBitwiseOperation(instruction);
4506}
4507
4508void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4509 HandleBitwiseOperation(instruction);
4510}
4511
4512void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4513 LocationSummary* locations = instruction->GetLocations();
4514 Location first = locations->InAt(0);
4515 Location second = locations->InAt(1);
4516 DCHECK(first.Equals(locations->Out()));
4517
4518 if (instruction->GetResultType() == Primitive::kPrimInt) {
4519 if (second.IsRegister()) {
4520 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004521 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004522 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004523 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004524 } else {
4525 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004526 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004527 }
4528 } else if (second.IsConstant()) {
4529 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004530 __ andl(first.AsRegister<Register>(),
4531 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004532 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004533 __ orl(first.AsRegister<Register>(),
4534 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004535 } else {
4536 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004537 __ xorl(first.AsRegister<Register>(),
4538 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004539 }
4540 } else {
4541 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004542 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004543 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004544 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004545 } else {
4546 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004547 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004548 }
4549 }
4550 } else {
4551 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4552 if (second.IsRegisterPair()) {
4553 if (instruction->IsAnd()) {
4554 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4555 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4556 } else if (instruction->IsOr()) {
4557 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4558 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4559 } else {
4560 DCHECK(instruction->IsXor());
4561 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4562 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4563 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004564 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004565 if (instruction->IsAnd()) {
4566 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4567 __ andl(first.AsRegisterPairHigh<Register>(),
4568 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4569 } else if (instruction->IsOr()) {
4570 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4571 __ orl(first.AsRegisterPairHigh<Register>(),
4572 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4573 } else {
4574 DCHECK(instruction->IsXor());
4575 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4576 __ xorl(first.AsRegisterPairHigh<Register>(),
4577 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4578 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004579 } else {
4580 DCHECK(second.IsConstant()) << second;
4581 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004582 int32_t low_value = Low32Bits(value);
4583 int32_t high_value = High32Bits(value);
4584 Immediate low(low_value);
4585 Immediate high(high_value);
4586 Register first_low = first.AsRegisterPairLow<Register>();
4587 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004588 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004589 if (low_value == 0) {
4590 __ xorl(first_low, first_low);
4591 } else if (low_value != -1) {
4592 __ andl(first_low, low);
4593 }
4594 if (high_value == 0) {
4595 __ xorl(first_high, first_high);
4596 } else if (high_value != -1) {
4597 __ andl(first_high, high);
4598 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004599 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004600 if (low_value != 0) {
4601 __ orl(first_low, low);
4602 }
4603 if (high_value != 0) {
4604 __ orl(first_high, high);
4605 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004606 } else {
4607 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004608 if (low_value != 0) {
4609 __ xorl(first_low, low);
4610 }
4611 if (high_value != 0) {
4612 __ xorl(first_high, high);
4613 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004614 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004615 }
4616 }
4617}
4618
Calin Juravleb1498f62015-02-16 13:13:29 +00004619void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4620 // Nothing to do, this should be removed during prepare for register allocator.
4621 UNUSED(instruction);
4622 LOG(FATAL) << "Unreachable";
4623}
4624
4625void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4626 // Nothing to do, this should be removed during prepare for register allocator.
4627 UNUSED(instruction);
4628 LOG(FATAL) << "Unreachable";
4629}
4630
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004631} // namespace x86
4632} // namespace art