blob: 637064d101a89880a9d83faae1f448fefdc88842 [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() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100499 __ cfi().RememberState();
500 if (!HasEmptyFrame()) {
501 int adjust = GetFrameSize() - FrameEntrySpillSize();
502 __ addl(ESP, Immediate(adjust));
503 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500504
David Srbeckyc34dc932015-04-12 09:27:43 +0100505 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
506 Register reg = kCoreCalleeSaves[i];
507 if (allocated_registers_.ContainsCoreRegister(reg)) {
508 __ popl(reg);
509 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
510 __ cfi().Restore(DWARFReg(reg));
511 }
Mark Mendell5f874182015-03-04 15:42:45 -0500512 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000513 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100514 __ ret();
515 __ cfi().RestoreState();
516 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000517}
518
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100519void CodeGeneratorX86::Bind(HBasicBlock* block) {
520 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100523void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000524 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100525 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000526}
527
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100528Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
529 switch (load->GetType()) {
530 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100531 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533
534 case Primitive::kPrimInt:
535 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538
539 case Primitive::kPrimBoolean:
540 case Primitive::kPrimByte:
541 case Primitive::kPrimChar:
542 case Primitive::kPrimShort:
543 case Primitive::kPrimVoid:
544 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700545 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100546 }
547
548 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700549 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550}
551
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
553 switch (type) {
554 case Primitive::kPrimBoolean:
555 case Primitive::kPrimByte:
556 case Primitive::kPrimChar:
557 case Primitive::kPrimShort:
558 case Primitive::kPrimInt:
559 case Primitive::kPrimNot: {
560 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000561 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100562 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100563 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100564 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000565 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100566 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100567 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000569 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 uint32_t index = gp_index_;
571 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000572 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100573 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100574 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
575 calling_convention.GetRegisterPairAt(index));
576 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000578 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
579 }
580 }
581
582 case Primitive::kPrimFloat: {
583 uint32_t index = fp_index_++;
584 stack_index_++;
585 if (index < calling_convention.GetNumberOfFpuRegisters()) {
586 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
587 } else {
588 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
589 }
590 }
591
592 case Primitive::kPrimDouble: {
593 uint32_t index = fp_index_++;
594 stack_index_ += 2;
595 if (index < calling_convention.GetNumberOfFpuRegisters()) {
596 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
597 } else {
598 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 }
600 }
601
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100602 case Primitive::kPrimVoid:
603 LOG(FATAL) << "Unexpected parameter type " << type;
604 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100605 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 return Location();
607}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100608
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609void CodeGeneratorX86::Move32(Location destination, Location source) {
610 if (source.Equals(destination)) {
611 return;
612 }
613 if (destination.IsRegister()) {
614 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000615 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 } else {
619 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000620 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (destination.IsFpuRegister()) {
623 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else {
628 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000629 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100630 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000632 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000634 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500637 } else if (source.IsConstant()) {
638 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000639 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500640 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100641 } else {
642 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100643 __ pushl(Address(ESP, source.GetStackIndex()));
644 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 }
646 }
647}
648
649void CodeGeneratorX86::Move64(Location destination, Location source) {
650 if (source.Equals(destination)) {
651 return;
652 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 if (destination.IsRegisterPair()) {
654 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000655 EmitParallelMoves(
656 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
657 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
658 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
659 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 } else if (source.IsFpuRegister()) {
661 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100665 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
666 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
668 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500670 if (source.IsFpuRegister()) {
671 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
672 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000673 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 } else {
675 LOG(FATAL) << "Unimplemented";
676 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000678 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100679 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000680 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100681 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100683 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000685 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000686 } else if (source.IsConstant()) {
687 HConstant* constant = source.GetConstant();
688 int64_t value;
689 if (constant->IsLongConstant()) {
690 value = constant->AsLongConstant()->GetValue();
691 } else {
692 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000693 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000694 }
695 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
696 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000698 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000699 EmitParallelMoves(
700 Location::StackSlot(source.GetStackIndex()),
701 Location::StackSlot(destination.GetStackIndex()),
702 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
703 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 }
705 }
706}
707
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100708void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000709 LocationSummary* locations = instruction->GetLocations();
710 if (locations != nullptr && locations->Out().Equals(location)) {
711 return;
712 }
713
714 if (locations != nullptr && locations->Out().IsConstant()) {
715 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000716 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
717 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000719 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000720 } else if (location.IsStackSlot()) {
721 __ movl(Address(ESP, location.GetStackIndex()), imm);
722 } else {
723 DCHECK(location.IsConstant());
724 DCHECK_EQ(location.GetConstant(), const_to_move);
725 }
726 } else if (const_to_move->IsLongConstant()) {
727 int64_t value = const_to_move->AsLongConstant()->GetValue();
728 if (location.IsRegisterPair()) {
729 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
730 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
731 } else if (location.IsDoubleStackSlot()) {
732 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000733 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
734 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000735 } else {
736 DCHECK(location.IsConstant());
737 DCHECK_EQ(location.GetConstant(), instruction);
738 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000740 } else if (instruction->IsTemporary()) {
741 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000742 if (temp_location.IsStackSlot()) {
743 Move32(location, temp_location);
744 } else {
745 DCHECK(temp_location.IsDoubleStackSlot());
746 Move64(location, temp_location);
747 }
Roland Levillain476df552014-10-09 17:51:36 +0100748 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100749 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 switch (instruction->GetType()) {
751 case Primitive::kPrimBoolean:
752 case Primitive::kPrimByte:
753 case Primitive::kPrimChar:
754 case Primitive::kPrimShort:
755 case Primitive::kPrimInt:
756 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 case Primitive::kPrimFloat:
758 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 break;
760
761 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 case Primitive::kPrimDouble:
763 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 break;
765
766 default:
767 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
768 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000769 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100770 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 switch (instruction->GetType()) {
772 case Primitive::kPrimBoolean:
773 case Primitive::kPrimByte:
774 case Primitive::kPrimChar:
775 case Primitive::kPrimShort:
776 case Primitive::kPrimInt:
777 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000779 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100780 break;
781
782 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000784 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100785 break;
786
787 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000790 }
791}
792
793void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000794 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000795}
796
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000797void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000798 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100799 DCHECK(!successor->IsExitBlock());
800
801 HBasicBlock* block = got->GetBlock();
802 HInstruction* previous = got->GetPrevious();
803
804 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000805 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100806 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
807 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
808 return;
809 }
810
811 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
812 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
813 }
814 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000815 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000816 }
817}
818
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000821}
822
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000823void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700824 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000825}
826
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700827void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
828 Label* true_target,
829 Label* false_target,
830 Label* always_true_target) {
831 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 if (cond->IsIntConstant()) {
833 // Constant condition, statically compared against 1.
834 int32_t cond_value = cond->AsIntConstant()->GetValue();
835 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836 if (always_true_target != nullptr) {
837 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100838 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100840 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 DCHECK_EQ(cond_value, 0);
842 }
843 } else {
844 bool materialized =
845 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
846 // Moves do not affect the eflags register, so if the condition is
847 // evaluated just before the if, we don't need to evaluate it
848 // again.
849 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700850 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100851 if (materialized) {
852 if (!eflags_set) {
853 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700854 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100855 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500856 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100857 } else {
858 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
859 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700862 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 }
864 } else {
865 Location lhs = cond->GetLocations()->InAt(0);
866 Location rhs = cond->GetLocations()->InAt(1);
867 // LHS is guaranteed to be in a register (see
868 // LocationsBuilderX86::VisitCondition).
869 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500872 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
873 if (constant == 0) {
874 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
875 } else {
876 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
877 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100878 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000879 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100880 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700881 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700882 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100883 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700884 if (false_target != nullptr) {
885 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886 }
887}
888
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700889void LocationsBuilderX86::VisitIf(HIf* if_instr) {
890 LocationSummary* locations =
891 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
892 HInstruction* cond = if_instr->InputAt(0);
893 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
894 locations->SetInAt(0, Location::Any());
895 }
896}
897
898void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
899 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
900 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
901 Label* always_true_target = true_target;
902 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
903 if_instr->IfTrueSuccessor())) {
904 always_true_target = nullptr;
905 }
906 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
907 if_instr->IfFalseSuccessor())) {
908 false_target = nullptr;
909 }
910 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
911}
912
913void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
914 LocationSummary* locations = new (GetGraph()->GetArena())
915 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
916 HInstruction* cond = deoptimize->InputAt(0);
917 DCHECK(cond->IsCondition());
918 if (cond->AsCondition()->NeedsMaterialization()) {
919 locations->SetInAt(0, Location::Any());
920 }
921}
922
923void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
924 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
925 DeoptimizationSlowPathX86(deoptimize);
926 codegen_->AddSlowPath(slow_path);
927 Label* slow_path_entry = slow_path->GetEntryLabel();
928 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
929}
930
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000931void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000933}
934
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000935void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
936 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937}
938
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000939void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100940 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000941}
942
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000943void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100944 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700945 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000946}
947
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100949 LocationSummary* locations =
950 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 switch (store->InputAt(1)->GetType()) {
952 case Primitive::kPrimBoolean:
953 case Primitive::kPrimByte:
954 case Primitive::kPrimChar:
955 case Primitive::kPrimShort:
956 case Primitive::kPrimInt:
957 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
960 break;
961
962 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100963 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100964 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
965 break;
966
967 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100969 }
970 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000971}
972
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700974 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000975}
976
Dave Allison20dfc792014-06-16 20:44:29 -0700977void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100978 LocationSummary* locations =
979 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100980 locations->SetInAt(0, Location::RequiresRegister());
981 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100982 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500983 // We need a byte register.
984 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100985 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000986}
987
Dave Allison20dfc792014-06-16 20:44:29 -0700988void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
989 if (comp->NeedsMaterialization()) {
990 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000991 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100992 // Clear register: setcc only sets the low byte.
993 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500994 Location lhs = locations->InAt(0);
995 Location rhs = locations->InAt(1);
996 if (rhs.IsRegister()) {
997 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
998 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800999 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001000 if (constant == 0) {
1001 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1002 } else {
1003 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1004 }
Dave Allison20dfc792014-06-16 20:44:29 -07001005 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001006 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001007 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001008 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001009 }
Dave Allison20dfc792014-06-16 20:44:29 -07001010}
1011
1012void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1013 VisitCondition(comp);
1014}
1015
1016void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1017 VisitCondition(comp);
1018}
1019
1020void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1021 VisitCondition(comp);
1022}
1023
1024void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1025 VisitCondition(comp);
1026}
1027
1028void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1029 VisitCondition(comp);
1030}
1031
1032void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1033 VisitCondition(comp);
1034}
1035
1036void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1037 VisitCondition(comp);
1038}
1039
1040void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1041 VisitCondition(comp);
1042}
1043
1044void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1057 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001058}
1059
1060void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001061 LocationSummary* locations =
1062 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001063 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001064}
1065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001066void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001067 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001068 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001069}
1070
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1072 LocationSummary* locations =
1073 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1074 locations->SetOut(Location::ConstantLocation(constant));
1075}
1076
1077void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1078 // Will be generated at use site.
1079 UNUSED(constant);
1080}
1081
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001083 LocationSummary* locations =
1084 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001085 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086}
1087
1088void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1089 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091}
1092
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001093void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1094 LocationSummary* locations =
1095 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1096 locations->SetOut(Location::ConstantLocation(constant));
1097}
1098
1099void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1100 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001102}
1103
1104void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1105 LocationSummary* locations =
1106 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1107 locations->SetOut(Location::ConstantLocation(constant));
1108}
1109
1110void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1111 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001112 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001113}
1114
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001115void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001117}
1118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001119void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001120 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001121 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001122}
1123
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001124void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 switch (ret->InputAt(0)->GetType()) {
1128 case Primitive::kPrimBoolean:
1129 case Primitive::kPrimByte:
1130 case Primitive::kPrimChar:
1131 case Primitive::kPrimShort:
1132 case Primitive::kPrimInt:
1133 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 break;
1136
1137 case Primitive::kPrimLong:
1138 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001139 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001140 break;
1141
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001142 case Primitive::kPrimFloat:
1143 case Primitive::kPrimDouble:
1144 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 break;
1147
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001149 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001151}
1152
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001153void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 if (kIsDebugBuild) {
1155 switch (ret->InputAt(0)->GetType()) {
1156 case Primitive::kPrimBoolean:
1157 case Primitive::kPrimByte:
1158 case Primitive::kPrimChar:
1159 case Primitive::kPrimShort:
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001162 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 break;
1164
1165 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001166 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1167 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 break;
1169
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 case Primitive::kPrimFloat:
1171 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001172 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 break;
1174
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 }
1178 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001179 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001180}
1181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001182void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04001183 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001184 if (intrinsic.TryDispatch(invoke)) {
1185 return;
1186 }
1187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188 HandleInvoke(invoke);
1189}
1190
Mark Mendell09ed1a32015-03-25 08:30:06 -04001191static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1192 if (invoke->GetLocations()->Intrinsified()) {
1193 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1194 intrinsic.Dispatch(invoke);
1195 return true;
1196 }
1197 return false;
1198}
1199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001200void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001201 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1202 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001203 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001204
Mark Mendell09ed1a32015-03-25 08:30:06 -04001205 codegen_->GenerateStaticOrDirectCall(
1206 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001207}
1208
1209void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1210 HandleInvoke(invoke);
1211}
1212
1213void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001214 LocationSummary* locations =
1215 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001216 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001217
1218 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001219 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001220 HInstruction* input = invoke->InputAt(i);
1221 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1222 }
1223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 switch (invoke->GetType()) {
1225 case Primitive::kPrimBoolean:
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimChar:
1228 case Primitive::kPrimShort:
1229 case Primitive::kPrimInt:
1230 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001231 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 break;
1233
1234 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001235 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 break;
1237
1238 case Primitive::kPrimVoid:
1239 break;
1240
1241 case Primitive::kPrimDouble:
1242 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001243 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 break;
1245 }
1246
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001247 invoke->SetLocations(locations);
1248}
1249
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001250void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001252 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1253 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1254 LocationSummary* locations = invoke->GetLocations();
1255 Location receiver = locations->InAt(0);
1256 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1257 // temp = object->GetClass();
1258 if (receiver.IsStackSlot()) {
1259 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1260 __ movl(temp, Address(temp, class_offset));
1261 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001263 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001264 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001265 // temp = temp->GetMethodAt(method_offset);
1266 __ movl(temp, Address(temp, method_offset));
1267 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001268 __ call(Address(
1269 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001271 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001272 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001273}
1274
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001275void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1276 HandleInvoke(invoke);
1277 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001278 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001279}
1280
1281void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1282 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001284 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1285 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1286 LocationSummary* locations = invoke->GetLocations();
1287 Location receiver = locations->InAt(0);
1288 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1289
1290 // Set the hidden argument.
1291 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001292 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001293
1294 // temp = object->GetClass();
1295 if (receiver.IsStackSlot()) {
1296 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1297 __ movl(temp, Address(temp, class_offset));
1298 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001300 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001301 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302 // temp = temp->GetImtEntryAt(method_offset);
1303 __ movl(temp, Address(temp, method_offset));
1304 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001305 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001306 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001307
1308 DCHECK(!codegen_->IsLeafMethod());
1309 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1310}
1311
Roland Levillain88cb1752014-10-20 16:36:47 +01001312void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1313 LocationSummary* locations =
1314 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1315 switch (neg->GetResultType()) {
1316 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001317 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001318 locations->SetInAt(0, Location::RequiresRegister());
1319 locations->SetOut(Location::SameAsFirstInput());
1320 break;
1321
Roland Levillain88cb1752014-10-20 16:36:47 +01001322 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001323 locations->SetInAt(0, Location::RequiresFpuRegister());
1324 locations->SetOut(Location::SameAsFirstInput());
1325 locations->AddTemp(Location::RequiresRegister());
1326 locations->AddTemp(Location::RequiresFpuRegister());
1327 break;
1328
Roland Levillain88cb1752014-10-20 16:36:47 +01001329 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001330 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001331 locations->SetOut(Location::SameAsFirstInput());
1332 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001333 break;
1334
1335 default:
1336 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1337 }
1338}
1339
1340void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1341 LocationSummary* locations = neg->GetLocations();
1342 Location out = locations->Out();
1343 Location in = locations->InAt(0);
1344 switch (neg->GetResultType()) {
1345 case Primitive::kPrimInt:
1346 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001348 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 break;
1350
1351 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001352 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001353 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001354 __ negl(out.AsRegisterPairLow<Register>());
1355 // Negation is similar to subtraction from zero. The least
1356 // significant byte triggers a borrow when it is different from
1357 // zero; to take it into account, add 1 to the most significant
1358 // byte if the carry flag (CF) is set to 1 after the first NEGL
1359 // operation.
1360 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1361 __ negl(out.AsRegisterPairHigh<Register>());
1362 break;
1363
Roland Levillain5368c212014-11-27 15:03:41 +00001364 case Primitive::kPrimFloat: {
1365 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001366 Register constant = locations->GetTemp(0).AsRegister<Register>();
1367 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001368 // Implement float negation with an exclusive or with value
1369 // 0x80000000 (mask for bit 31, representing the sign of a
1370 // single-precision floating-point number).
1371 __ movl(constant, Immediate(INT32_C(0x80000000)));
1372 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001373 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001374 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001375 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001376
Roland Levillain5368c212014-11-27 15:03:41 +00001377 case Primitive::kPrimDouble: {
1378 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001379 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001380 // Implement double negation with an exclusive or with value
1381 // 0x8000000000000000 (mask for bit 63, representing the sign of
1382 // a double-precision floating-point number).
1383 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001384 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001385 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001386 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001387
1388 default:
1389 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1390 }
1391}
1392
Roland Levillaindff1f282014-11-05 14:15:05 +00001393void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001394 Primitive::Type result_type = conversion->GetResultType();
1395 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001396 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001397
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001398 // The float-to-long and double-to-long type conversions rely on a
1399 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001400 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001401 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1402 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001403 ? LocationSummary::kCall
1404 : LocationSummary::kNoCall;
1405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1407
David Brazdilb2bd1c52015-03-25 11:17:37 +00001408 // The Java language does not allow treating boolean as an integral type but
1409 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001410
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001412 case Primitive::kPrimByte:
1413 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001414 case Primitive::kPrimBoolean:
1415 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001416 case Primitive::kPrimShort:
1417 case Primitive::kPrimInt:
1418 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001419 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001420 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1421 // Make the output overlap to please the register allocator. This greatly simplifies
1422 // the validation of the linear scan implementation
1423 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001424 break;
1425
1426 default:
1427 LOG(FATAL) << "Unexpected type conversion from " << input_type
1428 << " to " << result_type;
1429 }
1430 break;
1431
Roland Levillain01a8d712014-11-14 16:27:39 +00001432 case Primitive::kPrimShort:
1433 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001434 case Primitive::kPrimBoolean:
1435 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001436 case Primitive::kPrimByte:
1437 case Primitive::kPrimInt:
1438 case Primitive::kPrimChar:
1439 // Processing a Dex `int-to-short' instruction.
1440 locations->SetInAt(0, Location::Any());
1441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1442 break;
1443
1444 default:
1445 LOG(FATAL) << "Unexpected type conversion from " << input_type
1446 << " to " << result_type;
1447 }
1448 break;
1449
Roland Levillain946e1432014-11-11 17:35:19 +00001450 case Primitive::kPrimInt:
1451 switch (input_type) {
1452 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001453 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001454 locations->SetInAt(0, Location::Any());
1455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1456 break;
1457
1458 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001459 // Processing a Dex `float-to-int' instruction.
1460 locations->SetInAt(0, Location::RequiresFpuRegister());
1461 locations->SetOut(Location::RequiresRegister());
1462 locations->AddTemp(Location::RequiresFpuRegister());
1463 break;
1464
Roland Levillain946e1432014-11-11 17:35:19 +00001465 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001466 // Processing a Dex `double-to-int' instruction.
1467 locations->SetInAt(0, Location::RequiresFpuRegister());
1468 locations->SetOut(Location::RequiresRegister());
1469 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001470 break;
1471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 }
1476 break;
1477
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 case Primitive::kPrimLong:
1479 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001480 case Primitive::kPrimBoolean:
1481 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001482 case Primitive::kPrimByte:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001485 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001486 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001487 locations->SetInAt(0, Location::RegisterLocation(EAX));
1488 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1489 break;
1490
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001491 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001492 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001493 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001494 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001495 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1496 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1497
Vladimir Marko949c91f2015-01-27 10:48:44 +00001498 // The runtime helper puts the result in EAX, EDX.
1499 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001500 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001501 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
1507 break;
1508
Roland Levillain981e4542014-11-14 11:47:14 +00001509 case Primitive::kPrimChar:
1510 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001511 case Primitive::kPrimBoolean:
1512 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001513 case Primitive::kPrimByte:
1514 case Primitive::kPrimShort:
1515 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001516 // Processing a Dex `int-to-char' instruction.
1517 locations->SetInAt(0, Location::Any());
1518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1519 break;
1520
1521 default:
1522 LOG(FATAL) << "Unexpected type conversion from " << input_type
1523 << " to " << result_type;
1524 }
1525 break;
1526
Roland Levillaindff1f282014-11-05 14:15:05 +00001527 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001529 case Primitive::kPrimBoolean:
1530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001531 case Primitive::kPrimByte:
1532 case Primitive::kPrimShort:
1533 case Primitive::kPrimInt:
1534 case Primitive::kPrimChar:
1535 // Processing a Dex `int-to-float' instruction.
1536 locations->SetInAt(0, Location::RequiresRegister());
1537 locations->SetOut(Location::RequiresFpuRegister());
1538 break;
1539
1540 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001541 // Processing a Dex `long-to-float' instruction.
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetOut(Location::RequiresFpuRegister());
1544 locations->AddTemp(Location::RequiresFpuRegister());
1545 locations->AddTemp(Location::RequiresFpuRegister());
1546 break;
1547
Roland Levillaincff13742014-11-17 14:32:17 +00001548 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001549 // Processing a Dex `double-to-float' instruction.
1550 locations->SetInAt(0, Location::RequiresFpuRegister());
1551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001552 break;
1553
1554 default:
1555 LOG(FATAL) << "Unexpected type conversion from " << input_type
1556 << " to " << result_type;
1557 };
1558 break;
1559
Roland Levillaindff1f282014-11-05 14:15:05 +00001560 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001561 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001562 case Primitive::kPrimBoolean:
1563 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001564 case Primitive::kPrimByte:
1565 case Primitive::kPrimShort:
1566 case Primitive::kPrimInt:
1567 case Primitive::kPrimChar:
1568 // Processing a Dex `int-to-double' instruction.
1569 locations->SetInAt(0, Location::RequiresRegister());
1570 locations->SetOut(Location::RequiresFpuRegister());
1571 break;
1572
1573 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001574 // Processing a Dex `long-to-double' instruction.
1575 locations->SetInAt(0, Location::RequiresRegister());
1576 locations->SetOut(Location::RequiresFpuRegister());
1577 locations->AddTemp(Location::RequiresFpuRegister());
1578 locations->AddTemp(Location::RequiresFpuRegister());
1579 break;
1580
Roland Levillaincff13742014-11-17 14:32:17 +00001581 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001582 // Processing a Dex `float-to-double' instruction.
1583 locations->SetInAt(0, Location::RequiresFpuRegister());
1584 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001585 break;
1586
1587 default:
1588 LOG(FATAL) << "Unexpected type conversion from " << input_type
1589 << " to " << result_type;
1590 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001591 break;
1592
1593 default:
1594 LOG(FATAL) << "Unexpected type conversion from " << input_type
1595 << " to " << result_type;
1596 }
1597}
1598
1599void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1600 LocationSummary* locations = conversion->GetLocations();
1601 Location out = locations->Out();
1602 Location in = locations->InAt(0);
1603 Primitive::Type result_type = conversion->GetResultType();
1604 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001605 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001606 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001607 case Primitive::kPrimByte:
1608 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001609 case Primitive::kPrimBoolean:
1610 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001611 case Primitive::kPrimShort:
1612 case Primitive::kPrimInt:
1613 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001614 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001615 if (in.IsRegister()) {
1616 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001617 } else {
1618 DCHECK(in.GetConstant()->IsIntConstant());
1619 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1620 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1621 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001622 break;
1623
1624 default:
1625 LOG(FATAL) << "Unexpected type conversion from " << input_type
1626 << " to " << result_type;
1627 }
1628 break;
1629
Roland Levillain01a8d712014-11-14 16:27:39 +00001630 case Primitive::kPrimShort:
1631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001632 case Primitive::kPrimBoolean:
1633 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001634 case Primitive::kPrimByte:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
1637 // Processing a Dex `int-to-short' instruction.
1638 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001639 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001640 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001641 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001642 } else {
1643 DCHECK(in.GetConstant()->IsIntConstant());
1644 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001645 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001646 }
1647 break;
1648
1649 default:
1650 LOG(FATAL) << "Unexpected type conversion from " << input_type
1651 << " to " << result_type;
1652 }
1653 break;
1654
Roland Levillain946e1432014-11-11 17:35:19 +00001655 case Primitive::kPrimInt:
1656 switch (input_type) {
1657 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001658 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001659 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001661 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001663 } else {
1664 DCHECK(in.IsConstant());
1665 DCHECK(in.GetConstant()->IsLongConstant());
1666 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001668 }
1669 break;
1670
Roland Levillain3f8f9362014-12-02 17:45:01 +00001671 case Primitive::kPrimFloat: {
1672 // Processing a Dex `float-to-int' instruction.
1673 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1674 Register output = out.AsRegister<Register>();
1675 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1676 Label done, nan;
1677
1678 __ movl(output, Immediate(kPrimIntMax));
1679 // temp = int-to-float(output)
1680 __ cvtsi2ss(temp, output);
1681 // if input >= temp goto done
1682 __ comiss(input, temp);
1683 __ j(kAboveEqual, &done);
1684 // if input == NaN goto nan
1685 __ j(kUnordered, &nan);
1686 // output = float-to-int-truncate(input)
1687 __ cvttss2si(output, input);
1688 __ jmp(&done);
1689 __ Bind(&nan);
1690 // output = 0
1691 __ xorl(output, output);
1692 __ Bind(&done);
1693 break;
1694 }
1695
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001696 case Primitive::kPrimDouble: {
1697 // Processing a Dex `double-to-int' instruction.
1698 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1699 Register output = out.AsRegister<Register>();
1700 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1701 Label done, nan;
1702
1703 __ movl(output, Immediate(kPrimIntMax));
1704 // temp = int-to-double(output)
1705 __ cvtsi2sd(temp, output);
1706 // if input >= temp goto done
1707 __ comisd(input, temp);
1708 __ j(kAboveEqual, &done);
1709 // if input == NaN goto nan
1710 __ j(kUnordered, &nan);
1711 // output = double-to-int-truncate(input)
1712 __ cvttsd2si(output, input);
1713 __ jmp(&done);
1714 __ Bind(&nan);
1715 // output = 0
1716 __ xorl(output, output);
1717 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001718 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 }
Roland Levillain946e1432014-11-11 17:35:19 +00001720
1721 default:
1722 LOG(FATAL) << "Unexpected type conversion from " << input_type
1723 << " to " << result_type;
1724 }
1725 break;
1726
Roland Levillaindff1f282014-11-05 14:15:05 +00001727 case Primitive::kPrimLong:
1728 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001729 case Primitive::kPrimBoolean:
1730 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001731 case Primitive::kPrimByte:
1732 case Primitive::kPrimShort:
1733 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001734 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001735 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001736 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1737 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001738 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001739 __ cdq();
1740 break;
1741
1742 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001743 // Processing a Dex `float-to-long' instruction.
1744 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001745 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1746 break;
1747
Roland Levillaindff1f282014-11-05 14:15:05 +00001748 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001749 // Processing a Dex `double-to-long' instruction.
1750 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1751 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 break;
1753
1754 default:
1755 LOG(FATAL) << "Unexpected type conversion from " << input_type
1756 << " to " << result_type;
1757 }
1758 break;
1759
Roland Levillain981e4542014-11-14 11:47:14 +00001760 case Primitive::kPrimChar:
1761 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001762 case Primitive::kPrimBoolean:
1763 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001764 case Primitive::kPrimByte:
1765 case Primitive::kPrimShort:
1766 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001767 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1768 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001769 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001770 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001771 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001772 } else {
1773 DCHECK(in.GetConstant()->IsIntConstant());
1774 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001775 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001776 }
1777 break;
1778
1779 default:
1780 LOG(FATAL) << "Unexpected type conversion from " << input_type
1781 << " to " << result_type;
1782 }
1783 break;
1784
Roland Levillaindff1f282014-11-05 14:15:05 +00001785 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001787 case Primitive::kPrimBoolean:
1788 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001793 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001795 break;
1796
Roland Levillain6d0e4832014-11-27 18:31:21 +00001797 case Primitive::kPrimLong: {
1798 // Processing a Dex `long-to-float' instruction.
1799 Register low = in.AsRegisterPairLow<Register>();
1800 Register high = in.AsRegisterPairHigh<Register>();
1801 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1802 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1803 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1804
1805 // Operations use doubles for precision reasons (each 32-bit
1806 // half of a long fits in the 53-bit mantissa of a double,
1807 // but not in the 24-bit mantissa of a float). This is
1808 // especially important for the low bits. The result is
1809 // eventually converted to float.
1810
1811 // low = low - 2^31 (to prevent bit 31 of `low` to be
1812 // interpreted as a sign bit)
1813 __ subl(low, Immediate(0x80000000));
1814 // temp = int-to-double(high)
1815 __ cvtsi2sd(temp, high);
1816 // temp = temp * 2^32
1817 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1818 __ mulsd(temp, constant);
1819 // result = int-to-double(low)
1820 __ cvtsi2sd(result, low);
1821 // result = result + 2^31 (restore the original value of `low`)
1822 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1823 __ addsd(result, constant);
1824 // result = result + temp
1825 __ addsd(result, temp);
1826 // result = double-to-float(result)
1827 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001828 // Restore low.
1829 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001830 break;
1831 }
1832
Roland Levillaincff13742014-11-17 14:32:17 +00001833 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001834 // Processing a Dex `double-to-float' instruction.
1835 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001836 break;
1837
1838 default:
1839 LOG(FATAL) << "Unexpected type conversion from " << input_type
1840 << " to " << result_type;
1841 };
1842 break;
1843
Roland Levillaindff1f282014-11-05 14:15:05 +00001844 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001845 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001846 case Primitive::kPrimBoolean:
1847 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001848 case Primitive::kPrimByte:
1849 case Primitive::kPrimShort:
1850 case Primitive::kPrimInt:
1851 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001852 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001853 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001854 break;
1855
Roland Levillain647b9ed2014-11-27 12:06:00 +00001856 case Primitive::kPrimLong: {
1857 // Processing a Dex `long-to-double' instruction.
1858 Register low = in.AsRegisterPairLow<Register>();
1859 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1861 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1862 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001863
Roland Levillain647b9ed2014-11-27 12:06:00 +00001864 // low = low - 2^31 (to prevent bit 31 of `low` to be
1865 // interpreted as a sign bit)
1866 __ subl(low, Immediate(0x80000000));
1867 // temp = int-to-double(high)
1868 __ cvtsi2sd(temp, high);
1869 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001870 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001871 __ mulsd(temp, constant);
1872 // result = int-to-double(low)
1873 __ cvtsi2sd(result, low);
1874 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001875 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876 __ addsd(result, constant);
1877 // result = result + temp
1878 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001879 // Restore low.
1880 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 break;
1882 }
1883
Roland Levillaincff13742014-11-17 14:32:17 +00001884 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001885 // Processing a Dex `float-to-double' instruction.
1886 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001887 break;
1888
1889 default:
1890 LOG(FATAL) << "Unexpected type conversion from " << input_type
1891 << " to " << result_type;
1892 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 }
1899}
1900
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001901void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001902 LocationSummary* locations =
1903 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001904 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001905 case Primitive::kPrimInt: {
1906 locations->SetInAt(0, Location::RequiresRegister());
1907 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909 break;
1910 }
1911
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001913 locations->SetInAt(0, Location::RequiresRegister());
1914 locations->SetInAt(1, Location::Any());
1915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 break;
1917 }
1918
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 case Primitive::kPrimFloat:
1920 case Primitive::kPrimDouble: {
1921 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001922 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001923 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001925 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001927 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001928 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1929 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001930 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001931}
1932
1933void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1934 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 Location first = locations->InAt(0);
1936 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001937 Location out = locations->Out();
1938
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001942 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1943 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1944 } else {
1945 __ leal(out.AsRegister<Register>(), Address(
1946 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1947 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001949 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1950 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1951 __ addl(out.AsRegister<Register>(), Immediate(value));
1952 } else {
1953 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1954 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001955 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001956 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001958 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001959 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960 }
1961
1962 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001963 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1965 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001966 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1968 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001970 } else {
1971 DCHECK(second.IsConstant()) << second;
1972 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1973 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1974 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001975 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 break;
1977 }
1978
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001979 case Primitive::kPrimFloat: {
1980 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001981 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001982 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001983 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001984 }
1985
1986 case Primitive::kPrimDouble: {
1987 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001988 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001989 }
1990 break;
1991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001993 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001994 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001995 }
1996}
1997
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001998void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002001 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002002 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002004 locations->SetInAt(0, Location::RequiresRegister());
2005 locations->SetInAt(1, Location::Any());
2006 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002007 break;
2008 }
Calin Juravle11351682014-10-23 15:38:15 +01002009 case Primitive::kPrimFloat:
2010 case Primitive::kPrimDouble: {
2011 locations->SetInAt(0, Location::RequiresFpuRegister());
2012 locations->SetInAt(1, Location::RequiresFpuRegister());
2013 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002014 break;
Calin Juravle11351682014-10-23 15:38:15 +01002015 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017 default:
Calin Juravle11351682014-10-23 15:38:15 +01002018 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002020}
2021
2022void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2023 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002024 Location first = locations->InAt(0);
2025 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002026 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002027 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002028 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002030 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002031 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002032 __ subl(first.AsRegister<Register>(),
2033 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002034 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002036 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002038 }
2039
2040 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002041 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002042 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2043 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002044 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002045 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 __ sbbl(first.AsRegisterPairHigh<Register>(),
2047 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002048 } else {
2049 DCHECK(second.IsConstant()) << second;
2050 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2051 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2052 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002054 break;
2055 }
2056
Calin Juravle11351682014-10-23 15:38:15 +01002057 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059 break;
Calin Juravle11351682014-10-23 15:38:15 +01002060 }
2061
2062 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002064 break;
2065 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002067 default:
Calin Juravle11351682014-10-23 15:38:15 +01002068 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002069 }
2070}
2071
Calin Juravle34bacdf2014-10-07 20:23:36 +01002072void LocationsBuilderX86::VisitMul(HMul* mul) {
2073 LocationSummary* locations =
2074 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2075 switch (mul->GetResultType()) {
2076 case Primitive::kPrimInt:
2077 locations->SetInAt(0, Location::RequiresRegister());
2078 locations->SetInAt(1, Location::Any());
2079 locations->SetOut(Location::SameAsFirstInput());
2080 break;
2081 case Primitive::kPrimLong: {
2082 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083 locations->SetInAt(1, Location::Any());
2084 locations->SetOut(Location::SameAsFirstInput());
2085 // Needed for imul on 32bits with 64bits output.
2086 locations->AddTemp(Location::RegisterLocation(EAX));
2087 locations->AddTemp(Location::RegisterLocation(EDX));
2088 break;
2089 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002090 case Primitive::kPrimFloat:
2091 case Primitive::kPrimDouble: {
2092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetInAt(1, Location::RequiresFpuRegister());
2094 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002095 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002096 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002097
2098 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002099 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002100 }
2101}
2102
2103void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2104 LocationSummary* locations = mul->GetLocations();
2105 Location first = locations->InAt(0);
2106 Location second = locations->InAt(1);
2107 DCHECK(first.Equals(locations->Out()));
2108
2109 switch (mul->GetResultType()) {
2110 case Primitive::kPrimInt: {
2111 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002113 } else if (second.IsConstant()) {
2114 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002115 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116 } else {
2117 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 }
2120 break;
2121 }
2122
2123 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002124 Register in1_hi = first.AsRegisterPairHigh<Register>();
2125 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002126 Register eax = locations->GetTemp(0).AsRegister<Register>();
2127 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002128
2129 DCHECK_EQ(EAX, eax);
2130 DCHECK_EQ(EDX, edx);
2131
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002132 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 // output: in1
2134 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2135 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2136 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002137 if (second.IsConstant()) {
2138 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002140 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2141 int32_t low_value = Low32Bits(value);
2142 int32_t high_value = High32Bits(value);
2143 Immediate low(low_value);
2144 Immediate high(high_value);
2145
2146 __ movl(eax, high);
2147 // eax <- in1.lo * in2.hi
2148 __ imull(eax, in1_lo);
2149 // in1.hi <- in1.hi * in2.lo
2150 __ imull(in1_hi, low);
2151 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2152 __ addl(in1_hi, eax);
2153 // move in2_lo to eax to prepare for double precision
2154 __ movl(eax, low);
2155 // edx:eax <- in1.lo * in2.lo
2156 __ mull(in1_lo);
2157 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2158 __ addl(in1_hi, edx);
2159 // in1.lo <- (in1.lo * in2.lo)[31:0];
2160 __ movl(in1_lo, eax);
2161 } else if (second.IsRegisterPair()) {
2162 Register in2_hi = second.AsRegisterPairHigh<Register>();
2163 Register in2_lo = second.AsRegisterPairLow<Register>();
2164
2165 __ movl(eax, in2_hi);
2166 // eax <- in1.lo * in2.hi
2167 __ imull(eax, in1_lo);
2168 // in1.hi <- in1.hi * in2.lo
2169 __ imull(in1_hi, in2_lo);
2170 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2171 __ addl(in1_hi, eax);
2172 // move in1_lo to eax to prepare for double precision
2173 __ movl(eax, in1_lo);
2174 // edx:eax <- in1.lo * in2.lo
2175 __ mull(in2_lo);
2176 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2177 __ addl(in1_hi, edx);
2178 // in1.lo <- (in1.lo * in2.lo)[31:0];
2179 __ movl(in1_lo, eax);
2180 } else {
2181 DCHECK(second.IsDoubleStackSlot()) << second;
2182 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2183 Address in2_lo(ESP, second.GetStackIndex());
2184
2185 __ movl(eax, in2_hi);
2186 // eax <- in1.lo * in2.hi
2187 __ imull(eax, in1_lo);
2188 // in1.hi <- in1.hi * in2.lo
2189 __ imull(in1_hi, in2_lo);
2190 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2191 __ addl(in1_hi, eax);
2192 // move in1_lo to eax to prepare for double precision
2193 __ movl(eax, in1_lo);
2194 // edx:eax <- in1.lo * in2.lo
2195 __ mull(in2_lo);
2196 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2197 __ addl(in1_hi, edx);
2198 // in1.lo <- (in1.lo * in2.lo)[31:0];
2199 __ movl(in1_lo, eax);
2200 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002201
2202 break;
2203 }
2204
Calin Juravleb5bfa962014-10-21 18:02:24 +01002205 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002207 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002208 }
2209
2210 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002211 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002212 break;
2213 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002214
2215 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002217 }
2218}
2219
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002220void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2221 uint32_t stack_adjustment, bool is_float) {
2222 if (source.IsStackSlot()) {
2223 DCHECK(is_float);
2224 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2225 } else if (source.IsDoubleStackSlot()) {
2226 DCHECK(!is_float);
2227 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2228 } else {
2229 // Write the value to the temporary location on the stack and load to FP stack.
2230 if (is_float) {
2231 Location stack_temp = Location::StackSlot(temp_offset);
2232 codegen_->Move32(stack_temp, source);
2233 __ flds(Address(ESP, temp_offset));
2234 } else {
2235 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2236 codegen_->Move64(stack_temp, source);
2237 __ fldl(Address(ESP, temp_offset));
2238 }
2239 }
2240}
2241
2242void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2243 Primitive::Type type = rem->GetResultType();
2244 bool is_float = type == Primitive::kPrimFloat;
2245 size_t elem_size = Primitive::ComponentSize(type);
2246 LocationSummary* locations = rem->GetLocations();
2247 Location first = locations->InAt(0);
2248 Location second = locations->InAt(1);
2249 Location out = locations->Out();
2250
2251 // Create stack space for 2 elements.
2252 // TODO: enhance register allocator to ask for stack temporaries.
2253 __ subl(ESP, Immediate(2 * elem_size));
2254
2255 // Load the values to the FP stack in reverse order, using temporaries if needed.
2256 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2257 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2258
2259 // Loop doing FPREM until we stabilize.
2260 Label retry;
2261 __ Bind(&retry);
2262 __ fprem();
2263
2264 // Move FP status to AX.
2265 __ fstsw();
2266
2267 // And see if the argument reduction is complete. This is signaled by the
2268 // C2 FPU flag bit set to 0.
2269 __ andl(EAX, Immediate(kC2ConditionMask));
2270 __ j(kNotEqual, &retry);
2271
2272 // We have settled on the final value. Retrieve it into an XMM register.
2273 // Store FP top of stack to real stack.
2274 if (is_float) {
2275 __ fsts(Address(ESP, 0));
2276 } else {
2277 __ fstl(Address(ESP, 0));
2278 }
2279
2280 // Pop the 2 items from the FP stack.
2281 __ fucompp();
2282
2283 // Load the value from the stack into an XMM register.
2284 DCHECK(out.IsFpuRegister()) << out;
2285 if (is_float) {
2286 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2287 } else {
2288 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2289 }
2290
2291 // And remove the temporary stack space we allocated.
2292 __ addl(ESP, Immediate(2 * elem_size));
2293}
2294
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002295
2296void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2297 DCHECK(instruction->IsDiv() || instruction->IsRem());
2298
2299 LocationSummary* locations = instruction->GetLocations();
2300 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002301 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002302
2303 Register out_register = locations->Out().AsRegister<Register>();
2304 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002305 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002306
2307 DCHECK(imm == 1 || imm == -1);
2308
2309 if (instruction->IsRem()) {
2310 __ xorl(out_register, out_register);
2311 } else {
2312 __ movl(out_register, input_register);
2313 if (imm == -1) {
2314 __ negl(out_register);
2315 }
2316 }
2317}
2318
2319
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002320void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002321 LocationSummary* locations = instruction->GetLocations();
2322
2323 Register out_register = locations->Out().AsRegister<Register>();
2324 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002325 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002326
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002327 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002328 Register num = locations->GetTemp(0).AsRegister<Register>();
2329
2330 __ leal(num, Address(input_register, std::abs(imm) - 1));
2331 __ testl(input_register, input_register);
2332 __ cmovl(kGreaterEqual, num, input_register);
2333 int shift = CTZ(imm);
2334 __ sarl(num, Immediate(shift));
2335
2336 if (imm < 0) {
2337 __ negl(num);
2338 }
2339
2340 __ movl(out_register, num);
2341}
2342
2343void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2344 DCHECK(instruction->IsDiv() || instruction->IsRem());
2345
2346 LocationSummary* locations = instruction->GetLocations();
2347 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2348
2349 Register eax = locations->InAt(0).AsRegister<Register>();
2350 Register out = locations->Out().AsRegister<Register>();
2351 Register num;
2352 Register edx;
2353
2354 if (instruction->IsDiv()) {
2355 edx = locations->GetTemp(0).AsRegister<Register>();
2356 num = locations->GetTemp(1).AsRegister<Register>();
2357 } else {
2358 edx = locations->Out().AsRegister<Register>();
2359 num = locations->GetTemp(0).AsRegister<Register>();
2360 }
2361
2362 DCHECK_EQ(EAX, eax);
2363 DCHECK_EQ(EDX, edx);
2364 if (instruction->IsDiv()) {
2365 DCHECK_EQ(EAX, out);
2366 } else {
2367 DCHECK_EQ(EDX, out);
2368 }
2369
2370 int64_t magic;
2371 int shift;
2372 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2373
2374 Label ndiv;
2375 Label end;
2376 // If numerator is 0, the result is 0, no computation needed.
2377 __ testl(eax, eax);
2378 __ j(kNotEqual, &ndiv);
2379
2380 __ xorl(out, out);
2381 __ jmp(&end);
2382
2383 __ Bind(&ndiv);
2384
2385 // Save the numerator.
2386 __ movl(num, eax);
2387
2388 // EAX = magic
2389 __ movl(eax, Immediate(magic));
2390
2391 // EDX:EAX = magic * numerator
2392 __ imull(num);
2393
2394 if (imm > 0 && magic < 0) {
2395 // EDX += num
2396 __ addl(edx, num);
2397 } else if (imm < 0 && magic > 0) {
2398 __ subl(edx, num);
2399 }
2400
2401 // Shift if needed.
2402 if (shift != 0) {
2403 __ sarl(edx, Immediate(shift));
2404 }
2405
2406 // EDX += 1 if EDX < 0
2407 __ movl(eax, edx);
2408 __ shrl(edx, Immediate(31));
2409 __ addl(edx, eax);
2410
2411 if (instruction->IsRem()) {
2412 __ movl(eax, num);
2413 __ imull(edx, Immediate(imm));
2414 __ subl(eax, edx);
2415 __ movl(edx, eax);
2416 } else {
2417 __ movl(eax, edx);
2418 }
2419 __ Bind(&end);
2420}
2421
Calin Juravlebacfec32014-11-14 15:54:36 +00002422void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2423 DCHECK(instruction->IsDiv() || instruction->IsRem());
2424
2425 LocationSummary* locations = instruction->GetLocations();
2426 Location out = locations->Out();
2427 Location first = locations->InAt(0);
2428 Location second = locations->InAt(1);
2429 bool is_div = instruction->IsDiv();
2430
2431 switch (instruction->GetResultType()) {
2432 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 DCHECK_EQ(EAX, first.AsRegister<Register>());
2434 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002435
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002436 if (instruction->InputAt(1)->IsIntConstant()) {
2437 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002438
2439 if (imm == 0) {
2440 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2441 } else if (imm == 1 || imm == -1) {
2442 DivRemOneOrMinusOne(instruction);
2443 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002444 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002445 } else {
2446 DCHECK(imm <= -2 || imm >= 2);
2447 GenerateDivRemWithAnyConstant(instruction);
2448 }
2449 } else {
2450 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002451 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002452 is_div);
2453 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002454
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002455 Register second_reg = second.AsRegister<Register>();
2456 // 0x80000000/-1 triggers an arithmetic exception!
2457 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2458 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002459
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002460 __ cmpl(second_reg, Immediate(-1));
2461 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002462
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002463 // edx:eax <- sign-extended of eax
2464 __ cdq();
2465 // eax = quotient, edx = remainder
2466 __ idivl(second_reg);
2467 __ Bind(slow_path->GetExitLabel());
2468 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002469 break;
2470 }
2471
2472 case Primitive::kPrimLong: {
2473 InvokeRuntimeCallingConvention calling_convention;
2474 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2475 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2476 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2477 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2478 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2479 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2480
2481 if (is_div) {
2482 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2483 } else {
2484 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2485 }
2486 uint32_t dex_pc = is_div
2487 ? instruction->AsDiv()->GetDexPc()
2488 : instruction->AsRem()->GetDexPc();
2489 codegen_->RecordPcInfo(instruction, dex_pc);
2490
2491 break;
2492 }
2493
2494 default:
2495 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2496 }
2497}
2498
Calin Juravle7c4954d2014-10-28 16:57:40 +00002499void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002500 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002501 ? LocationSummary::kCall
2502 : LocationSummary::kNoCall;
2503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2504
Calin Juravle7c4954d2014-10-28 16:57:40 +00002505 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002506 case Primitive::kPrimInt: {
2507 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002508 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002509 locations->SetOut(Location::SameAsFirstInput());
2510 // Intel uses edx:eax as the dividend.
2511 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002512 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2513 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2514 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002515 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002516 locations->AddTemp(Location::RequiresRegister());
2517 }
Calin Juravled0d48522014-11-04 16:40:20 +00002518 break;
2519 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002520 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002521 InvokeRuntimeCallingConvention calling_convention;
2522 locations->SetInAt(0, Location::RegisterPairLocation(
2523 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2524 locations->SetInAt(1, Location::RegisterPairLocation(
2525 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2526 // Runtime helper puts the result in EAX, EDX.
2527 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002528 break;
2529 }
2530 case Primitive::kPrimFloat:
2531 case Primitive::kPrimDouble: {
2532 locations->SetInAt(0, Location::RequiresFpuRegister());
2533 locations->SetInAt(1, Location::RequiresFpuRegister());
2534 locations->SetOut(Location::SameAsFirstInput());
2535 break;
2536 }
2537
2538 default:
2539 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2540 }
2541}
2542
2543void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2544 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002545 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002546 Location first = locations->InAt(0);
2547 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002548
2549 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002550 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002551 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002552 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002553 break;
2554 }
2555
2556 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002557 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 break;
2560 }
2561
2562 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002563 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002565 break;
2566 }
2567
2568 default:
2569 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2570 }
2571}
2572
Calin Juravlebacfec32014-11-14 15:54:36 +00002573void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002574 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002575
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002576 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2577 ? LocationSummary::kCall
2578 : LocationSummary::kNoCall;
2579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002580
Calin Juravled2ec87d2014-12-08 14:24:46 +00002581 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002582 case Primitive::kPrimInt: {
2583 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002584 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002585 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002586 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2587 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2588 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002589 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002590 locations->AddTemp(Location::RequiresRegister());
2591 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002592 break;
2593 }
2594 case Primitive::kPrimLong: {
2595 InvokeRuntimeCallingConvention calling_convention;
2596 locations->SetInAt(0, Location::RegisterPairLocation(
2597 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2598 locations->SetInAt(1, Location::RegisterPairLocation(
2599 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2600 // Runtime helper puts the result in EAX, EDX.
2601 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2602 break;
2603 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002604 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002605 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002606 locations->SetInAt(0, Location::Any());
2607 locations->SetInAt(1, Location::Any());
2608 locations->SetOut(Location::RequiresFpuRegister());
2609 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002610 break;
2611 }
2612
2613 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002614 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002615 }
2616}
2617
2618void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2619 Primitive::Type type = rem->GetResultType();
2620 switch (type) {
2621 case Primitive::kPrimInt:
2622 case Primitive::kPrimLong: {
2623 GenerateDivRemIntegral(rem);
2624 break;
2625 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002626 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002627 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002628 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002629 break;
2630 }
2631 default:
2632 LOG(FATAL) << "Unexpected rem type " << type;
2633 }
2634}
2635
Calin Juravled0d48522014-11-04 16:40:20 +00002636void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2637 LocationSummary* locations =
2638 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002639 switch (instruction->GetType()) {
2640 case Primitive::kPrimInt: {
2641 locations->SetInAt(0, Location::Any());
2642 break;
2643 }
2644 case Primitive::kPrimLong: {
2645 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2646 if (!instruction->IsConstant()) {
2647 locations->AddTemp(Location::RequiresRegister());
2648 }
2649 break;
2650 }
2651 default:
2652 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2653 }
Calin Juravled0d48522014-11-04 16:40:20 +00002654 if (instruction->HasUses()) {
2655 locations->SetOut(Location::SameAsFirstInput());
2656 }
2657}
2658
2659void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2660 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2661 codegen_->AddSlowPath(slow_path);
2662
2663 LocationSummary* locations = instruction->GetLocations();
2664 Location value = locations->InAt(0);
2665
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002666 switch (instruction->GetType()) {
2667 case Primitive::kPrimInt: {
2668 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002670 __ j(kEqual, slow_path->GetEntryLabel());
2671 } else if (value.IsStackSlot()) {
2672 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2673 __ j(kEqual, slow_path->GetEntryLabel());
2674 } else {
2675 DCHECK(value.IsConstant()) << value;
2676 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2677 __ jmp(slow_path->GetEntryLabel());
2678 }
2679 }
2680 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002681 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002682 case Primitive::kPrimLong: {
2683 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002685 __ movl(temp, value.AsRegisterPairLow<Register>());
2686 __ orl(temp, value.AsRegisterPairHigh<Register>());
2687 __ j(kEqual, slow_path->GetEntryLabel());
2688 } else {
2689 DCHECK(value.IsConstant()) << value;
2690 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2691 __ jmp(slow_path->GetEntryLabel());
2692 }
2693 }
2694 break;
2695 }
2696 default:
2697 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002698 }
Calin Juravled0d48522014-11-04 16:40:20 +00002699}
2700
Calin Juravle9aec02f2014-11-18 23:06:35 +00002701void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2702 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2703
2704 LocationSummary* locations =
2705 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2706
2707 switch (op->GetResultType()) {
2708 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002709 locations->SetInAt(0, Location::RequiresRegister());
2710 // The shift count needs to be in CL.
Calin Juravle9aec02f2014-11-18 23:06:35 +00002711 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2712 locations->SetOut(Location::SameAsFirstInput());
2713 break;
2714 }
2715 case Primitive::kPrimLong: {
2716 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002717 // The shift count needs to be in CL.
2718 locations->SetInAt(1, Location::RegisterLocation(ECX));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002719 locations->SetOut(Location::SameAsFirstInput());
2720 break;
2721 }
2722 default:
2723 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2724 }
2725}
2726
2727void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2728 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2729
2730 LocationSummary* locations = op->GetLocations();
2731 Location first = locations->InAt(0);
2732 Location second = locations->InAt(1);
2733 DCHECK(first.Equals(locations->Out()));
2734
2735 switch (op->GetResultType()) {
2736 case Primitive::kPrimInt: {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002737 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002738 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002739 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740 DCHECK_EQ(ECX, second_reg);
2741 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002742 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002743 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002744 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002745 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002746 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002747 }
2748 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002749 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2750 if (op->IsShl()) {
2751 __ shll(first_reg, imm);
2752 } else if (op->IsShr()) {
2753 __ sarl(first_reg, imm);
2754 } else {
2755 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002756 }
2757 }
2758 break;
2759 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002760 case Primitive::kPrimLong: {
2761 Register second_reg = second.AsRegister<Register>();
2762 DCHECK_EQ(ECX, second_reg);
2763 if (op->IsShl()) {
2764 GenerateShlLong(first, second_reg);
2765 } else if (op->IsShr()) {
2766 GenerateShrLong(first, second_reg);
2767 } else {
2768 GenerateUShrLong(first, second_reg);
2769 }
2770 break;
2771 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 default:
2773 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2774 }
2775}
2776
2777void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2778 Label done;
2779 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2780 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2781 __ testl(shifter, Immediate(32));
2782 __ j(kEqual, &done);
2783 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2784 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2785 __ Bind(&done);
2786}
2787
2788void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2789 Label done;
2790 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2791 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2792 __ testl(shifter, Immediate(32));
2793 __ j(kEqual, &done);
2794 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2795 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2796 __ Bind(&done);
2797}
2798
2799void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2800 Label done;
2801 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2802 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2803 __ testl(shifter, Immediate(32));
2804 __ j(kEqual, &done);
2805 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2806 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2807 __ Bind(&done);
2808}
2809
2810void LocationsBuilderX86::VisitShl(HShl* shl) {
2811 HandleShift(shl);
2812}
2813
2814void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2815 HandleShift(shl);
2816}
2817
2818void LocationsBuilderX86::VisitShr(HShr* shr) {
2819 HandleShift(shr);
2820}
2821
2822void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2823 HandleShift(shr);
2824}
2825
2826void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2827 HandleShift(ushr);
2828}
2829
2830void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2831 HandleShift(ushr);
2832}
2833
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002834void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002835 LocationSummary* locations =
2836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002837 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002838 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002839 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2840 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002841}
2842
2843void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2844 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002845 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002846 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002847
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002848 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002849
Nicolas Geoffray39468442014-09-02 15:17:15 +01002850 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002851 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002852}
2853
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002854void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2855 LocationSummary* locations =
2856 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2857 locations->SetOut(Location::RegisterLocation(EAX));
2858 InvokeRuntimeCallingConvention calling_convention;
2859 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002860 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2861 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002862}
2863
2864void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2865 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002866 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002867 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2868
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002869 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002870
2871 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2872 DCHECK(!codegen_->IsLeafMethod());
2873}
2874
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002875void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002876 LocationSummary* locations =
2877 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002878 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2879 if (location.IsStackSlot()) {
2880 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2881 } else if (location.IsDoubleStackSlot()) {
2882 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002883 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002884 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002885}
2886
2887void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002888 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002889}
2890
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002891void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002892 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002893 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002894 locations->SetInAt(0, Location::RequiresRegister());
2895 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002896}
2897
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002898void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2899 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002900 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002901 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002902 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002903 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002904 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002906 break;
2907
2908 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002909 __ notl(out.AsRegisterPairLow<Register>());
2910 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002911 break;
2912
2913 default:
2914 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2915 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002916}
2917
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002918void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002919 LocationSummary* locations =
2920 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002921 switch (compare->InputAt(0)->GetType()) {
2922 case Primitive::kPrimLong: {
2923 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002924 locations->SetInAt(1, Location::Any());
2925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2926 break;
2927 }
2928 case Primitive::kPrimFloat:
2929 case Primitive::kPrimDouble: {
2930 locations->SetInAt(0, Location::RequiresFpuRegister());
2931 locations->SetInAt(1, Location::RequiresFpuRegister());
2932 locations->SetOut(Location::RequiresRegister());
2933 break;
2934 }
2935 default:
2936 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2937 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002938}
2939
2940void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002941 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002942 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002943 Location left = locations->InAt(0);
2944 Location right = locations->InAt(1);
2945
2946 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002947 switch (compare->InputAt(0)->GetType()) {
2948 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002949 Register left_low = left.AsRegisterPairLow<Register>();
2950 Register left_high = left.AsRegisterPairHigh<Register>();
2951 int32_t val_low = 0;
2952 int32_t val_high = 0;
2953 bool right_is_const = false;
2954
2955 if (right.IsConstant()) {
2956 DCHECK(right.GetConstant()->IsLongConstant());
2957 right_is_const = true;
2958 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
2959 val_low = Low32Bits(val);
2960 val_high = High32Bits(val);
2961 }
2962
Calin Juravleddb7df22014-11-25 20:56:51 +00002963 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002964 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002965 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002966 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002967 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002968 DCHECK(right_is_const) << right;
2969 if (val_high == 0) {
2970 __ testl(left_high, left_high);
2971 } else {
2972 __ cmpl(left_high, Immediate(val_high));
2973 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002975 __ j(kLess, &less); // Signed compare.
2976 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002977 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002978 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002979 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002980 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002981 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002982 DCHECK(right_is_const) << right;
2983 if (val_low == 0) {
2984 __ testl(left_low, left_low);
2985 } else {
2986 __ cmpl(left_low, Immediate(val_low));
2987 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002988 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002989 break;
2990 }
2991 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002993 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2994 break;
2995 }
2996 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002997 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002998 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002999 break;
3000 }
3001 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003002 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003003 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003004 __ movl(out, Immediate(0));
3005 __ j(kEqual, &done);
3006 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3007
3008 __ Bind(&greater);
3009 __ movl(out, Immediate(1));
3010 __ jmp(&done);
3011
3012 __ Bind(&less);
3013 __ movl(out, Immediate(-1));
3014
3015 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003016}
3017
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003018void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003021 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3022 locations->SetInAt(i, Location::Any());
3023 }
3024 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003025}
3026
3027void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003028 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003029 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003030}
3031
Calin Juravle52c48962014-12-16 17:02:57 +00003032void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3033 /*
3034 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3035 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3036 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3037 */
3038 switch (kind) {
3039 case MemBarrierKind::kAnyAny: {
3040 __ mfence();
3041 break;
3042 }
3043 case MemBarrierKind::kAnyStore:
3044 case MemBarrierKind::kLoadAny:
3045 case MemBarrierKind::kStoreStore: {
3046 // nop
3047 break;
3048 }
3049 default:
3050 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003051 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003052}
3053
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003054
Mark Mendell09ed1a32015-03-25 08:30:06 -04003055void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3056 Register temp) {
3057 // TODO: Implement all kinds of calls:
3058 // 1) boot -> boot
3059 // 2) app -> boot
3060 // 3) app -> app
3061 //
3062 // Currently we implement the app -> app logic, which looks up in the resolve cache.
3063 // temp = method;
3064 LoadCurrentMethod(temp);
3065 if (!invoke->IsRecursive()) {
3066 // temp = temp->dex_cache_resolved_methods_;
3067 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3068 // temp = temp[index_in_cache]
3069 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3070 // (temp + offset_of_quick_compiled_code)()
3071 __ call(Address(
3072 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3073 } else {
3074 __ call(GetFrameEntryLabel());
3075 }
3076
3077 DCHECK(!IsLeafMethod());
3078 RecordPcInfo(invoke, invoke->GetDexPc());
3079}
3080
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003081void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 Label is_null;
3083 __ testl(value, value);
3084 __ j(kEqual, &is_null);
3085 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3086 __ movl(temp, object);
3087 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003088 __ movb(Address(temp, card, TIMES_1, 0),
3089 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 __ Bind(&is_null);
3091}
3092
Calin Juravle52c48962014-12-16 17:02:57 +00003093void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3094 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003097 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003098
3099 // The output overlaps in case of long: we don't want the low move to overwrite
3100 // the object's location.
3101 locations->SetOut(Location::RequiresRegister(),
3102 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3103 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00003104
3105 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3106 // Long values can be loaded atomically into an XMM using movsd.
3107 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3108 // and then copy the XMM into the output 32bits at a time).
3109 locations->AddTemp(Location::RequiresFpuRegister());
3110 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003111}
3112
Calin Juravle52c48962014-12-16 17:02:57 +00003113void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3114 const FieldInfo& field_info) {
3115 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003116
Calin Juravle52c48962014-12-16 17:02:57 +00003117 LocationSummary* locations = instruction->GetLocations();
3118 Register base = locations->InAt(0).AsRegister<Register>();
3119 Location out = locations->Out();
3120 bool is_volatile = field_info.IsVolatile();
3121 Primitive::Type field_type = field_info.GetFieldType();
3122 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3123
3124 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003125 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003126 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127 break;
3128 }
3129
3130 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003131 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003132 break;
3133 }
3134
3135 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003136 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003137 break;
3138 }
3139
3140 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003141 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003142 break;
3143 }
3144
3145 case Primitive::kPrimInt:
3146 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003147 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003148 break;
3149 }
3150
3151 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003152 if (is_volatile) {
3153 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3154 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003156 __ movd(out.AsRegisterPairLow<Register>(), temp);
3157 __ psrlq(temp, Immediate(32));
3158 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3159 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003160 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003161 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003163 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3164 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003165 break;
3166 }
3167
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003168 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003169 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003170 break;
3171 }
3172
3173 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003174 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003175 break;
3176 }
3177
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003178 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003179 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003180 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003181 }
Calin Juravle52c48962014-12-16 17:02:57 +00003182
Calin Juravle77520bc2015-01-12 18:45:46 +00003183 // Longs are handled in the switch.
3184 if (field_type != Primitive::kPrimLong) {
3185 codegen_->MaybeRecordImplicitNullCheck(instruction);
3186 }
3187
Calin Juravle52c48962014-12-16 17:02:57 +00003188 if (is_volatile) {
3189 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3190 }
3191}
3192
3193void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3194 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3195
3196 LocationSummary* locations =
3197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3198 locations->SetInAt(0, Location::RequiresRegister());
3199 bool is_volatile = field_info.IsVolatile();
3200 Primitive::Type field_type = field_info.GetFieldType();
3201 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3202 || (field_type == Primitive::kPrimByte);
3203
3204 // The register allocator does not support multiple
3205 // inputs that die at entry with one in a specific register.
3206 if (is_byte_type) {
3207 // Ensure the value is in a byte register.
3208 locations->SetInAt(1, Location::RegisterLocation(EAX));
3209 } else {
3210 locations->SetInAt(1, Location::RequiresRegister());
3211 }
3212 // Temporary registers for the write barrier.
3213 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3214 locations->AddTemp(Location::RequiresRegister());
3215 // Ensure the card is in a byte register.
3216 locations->AddTemp(Location::RegisterLocation(ECX));
3217 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3218 // 64bits value can be atomically written to an address with movsd and an XMM register.
3219 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3220 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3221 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3222 // isolated cases when we need this it isn't worth adding the extra complexity.
3223 locations->AddTemp(Location::RequiresFpuRegister());
3224 locations->AddTemp(Location::RequiresFpuRegister());
3225 }
3226}
3227
3228void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3229 const FieldInfo& field_info) {
3230 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3231
3232 LocationSummary* locations = instruction->GetLocations();
3233 Register base = locations->InAt(0).AsRegister<Register>();
3234 Location value = locations->InAt(1);
3235 bool is_volatile = field_info.IsVolatile();
3236 Primitive::Type field_type = field_info.GetFieldType();
3237 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3238
3239 if (is_volatile) {
3240 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3241 }
3242
3243 switch (field_type) {
3244 case Primitive::kPrimBoolean:
3245 case Primitive::kPrimByte: {
3246 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3247 break;
3248 }
3249
3250 case Primitive::kPrimShort:
3251 case Primitive::kPrimChar: {
3252 __ movw(Address(base, offset), value.AsRegister<Register>());
3253 break;
3254 }
3255
3256 case Primitive::kPrimInt:
3257 case Primitive::kPrimNot: {
3258 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003259 break;
3260 }
3261
3262 case Primitive::kPrimLong: {
3263 if (is_volatile) {
3264 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3265 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3266 __ movd(temp1, value.AsRegisterPairLow<Register>());
3267 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3268 __ punpckldq(temp1, temp2);
3269 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003271 } else {
3272 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003274 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3275 }
3276 break;
3277 }
3278
3279 case Primitive::kPrimFloat: {
3280 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3281 break;
3282 }
3283
3284 case Primitive::kPrimDouble: {
3285 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3286 break;
3287 }
3288
3289 case Primitive::kPrimVoid:
3290 LOG(FATAL) << "Unreachable type " << field_type;
3291 UNREACHABLE();
3292 }
3293
Calin Juravle77520bc2015-01-12 18:45:46 +00003294 // Longs are handled in the switch.
3295 if (field_type != Primitive::kPrimLong) {
3296 codegen_->MaybeRecordImplicitNullCheck(instruction);
3297 }
3298
3299 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3300 Register temp = locations->GetTemp(0).AsRegister<Register>();
3301 Register card = locations->GetTemp(1).AsRegister<Register>();
3302 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3303 }
3304
Calin Juravle52c48962014-12-16 17:02:57 +00003305 if (is_volatile) {
3306 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3307 }
3308}
3309
3310void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3311 HandleFieldGet(instruction, instruction->GetFieldInfo());
3312}
3313
3314void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3315 HandleFieldGet(instruction, instruction->GetFieldInfo());
3316}
3317
3318void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3319 HandleFieldSet(instruction, instruction->GetFieldInfo());
3320}
3321
3322void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3323 HandleFieldSet(instruction, instruction->GetFieldInfo());
3324}
3325
3326void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3327 HandleFieldSet(instruction, instruction->GetFieldInfo());
3328}
3329
3330void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3331 HandleFieldSet(instruction, instruction->GetFieldInfo());
3332}
3333
3334void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3335 HandleFieldGet(instruction, instruction->GetFieldInfo());
3336}
3337
3338void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3339 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340}
3341
3342void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003343 LocationSummary* locations =
3344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003345 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3346 ? Location::RequiresRegister()
3347 : Location::Any();
3348 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003349 if (instruction->HasUses()) {
3350 locations->SetOut(Location::SameAsFirstInput());
3351 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003352}
3353
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003354void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003355 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3356 return;
3357 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003358 LocationSummary* locations = instruction->GetLocations();
3359 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003360
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003361 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3362 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3363}
3364
3365void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003366 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003367 codegen_->AddSlowPath(slow_path);
3368
3369 LocationSummary* locations = instruction->GetLocations();
3370 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003371
3372 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003373 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003374 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003375 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003376 } else {
3377 DCHECK(obj.IsConstant()) << obj;
3378 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3379 __ jmp(slow_path->GetEntryLabel());
3380 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381 }
3382 __ j(kEqual, slow_path->GetEntryLabel());
3383}
3384
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003385void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3386 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3387 GenerateImplicitNullCheck(instruction);
3388 } else {
3389 GenerateExplicitNullCheck(instruction);
3390 }
3391}
3392
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003393void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003396 locations->SetInAt(0, Location::RequiresRegister());
3397 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003398 // The output overlaps in case of long: we don't want the low move to overwrite
3399 // the array's location.
3400 locations->SetOut(Location::RequiresRegister(),
3401 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3402 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003403}
3404
3405void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3406 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003407 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003408 Location index = locations->InAt(1);
3409
Calin Juravle77520bc2015-01-12 18:45:46 +00003410 Primitive::Type type = instruction->GetType();
3411 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003412 case Primitive::kPrimBoolean: {
3413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003414 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003415 if (index.IsConstant()) {
3416 __ movzxb(out, Address(obj,
3417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3418 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003420 }
3421 break;
3422 }
3423
3424 case Primitive::kPrimByte: {
3425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003427 if (index.IsConstant()) {
3428 __ movsxb(out, Address(obj,
3429 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3430 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003432 }
3433 break;
3434 }
3435
3436 case Primitive::kPrimShort: {
3437 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003439 if (index.IsConstant()) {
3440 __ movsxw(out, Address(obj,
3441 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3442 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003443 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003444 }
3445 break;
3446 }
3447
3448 case Primitive::kPrimChar: {
3449 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003451 if (index.IsConstant()) {
3452 __ movzxw(out, Address(obj,
3453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3454 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003455 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003456 }
3457 break;
3458 }
3459
3460 case Primitive::kPrimInt:
3461 case Primitive::kPrimNot: {
3462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003464 if (index.IsConstant()) {
3465 __ movl(out, Address(obj,
3466 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3467 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003468 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003469 }
3470 break;
3471 }
3472
3473 case Primitive::kPrimLong: {
3474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003475 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003476 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003477 if (index.IsConstant()) {
3478 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003479 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003480 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003481 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003482 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003483 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003485 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003486 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 }
3489 break;
3490 }
3491
Mark Mendell7c8d0092015-01-26 11:21:33 -05003492 case Primitive::kPrimFloat: {
3493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3494 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3495 if (index.IsConstant()) {
3496 __ movss(out, Address(obj,
3497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3498 } else {
3499 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3500 }
3501 break;
3502 }
3503
3504 case Primitive::kPrimDouble: {
3505 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3506 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3507 if (index.IsConstant()) {
3508 __ movsd(out, Address(obj,
3509 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3510 } else {
3511 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3512 }
3513 break;
3514 }
3515
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003516 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003517 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003518 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003519 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003520
3521 if (type != Primitive::kPrimLong) {
3522 codegen_->MaybeRecordImplicitNullCheck(instruction);
3523 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003524}
3525
3526void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003527 // This location builder might end up asking to up to four registers, which is
3528 // not currently possible for baseline. The situation in which we need four
3529 // registers cannot be met by baseline though, because it has not run any
3530 // optimization.
3531
Nicolas Geoffray39468442014-09-02 15:17:15 +01003532 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003533 bool needs_write_barrier =
3534 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3535
Mark Mendell5f874182015-03-04 15:42:45 -05003536 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003537
Nicolas Geoffray39468442014-09-02 15:17:15 +01003538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3539 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003540 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003541
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003542 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003543 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003544 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3545 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3546 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003547 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003548 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3549 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003550 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003551 // In case of a byte operation, the register allocator does not support multiple
3552 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003553 locations->SetInAt(0, Location::RequiresRegister());
3554 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003555 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003556 // Ensure the value is in a byte register.
3557 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558 } else {
Serguei Katkov55501ce2015-04-08 13:26:09 +06003559 bool is_fp_type = (value_type == Primitive::kPrimFloat)
3560 || (value_type == Primitive::kPrimDouble);
3561 if (is_fp_type) {
3562 locations->SetInAt(2, Location::RequiresFpuRegister());
3563 } else {
3564 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
3565 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003566 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003567 // Temporary registers for the write barrier.
3568 if (needs_write_barrier) {
3569 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003570 // Ensure the card is in a byte register.
3571 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574}
3575
3576void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3577 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003580 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003581 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003582 bool needs_runtime_call = locations->WillCall();
3583 bool needs_write_barrier =
3584 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585
3586 switch (value_type) {
3587 case Primitive::kPrimBoolean:
3588 case Primitive::kPrimByte: {
3589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003590 if (index.IsConstant()) {
3591 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003592 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003593 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003594 } else {
3595 __ movb(Address(obj, offset),
3596 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003598 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003599 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003601 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003602 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003604 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3605 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003607 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 break;
3609 }
3610
3611 case Primitive::kPrimShort:
3612 case Primitive::kPrimChar: {
3613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 if (index.IsConstant()) {
3615 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003616 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003618 } else {
3619 __ movw(Address(obj, offset),
3620 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003623 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3625 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003628 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3629 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003630 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003631 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 break;
3633 }
3634
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003635 case Primitive::kPrimInt:
3636 case Primitive::kPrimNot: {
3637 if (!needs_runtime_call) {
3638 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3639 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003640 size_t offset =
3641 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003642 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003644 } else {
3645 DCHECK(value.IsConstant()) << value;
3646 __ movl(Address(obj, offset),
3647 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3648 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003649 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003650 DCHECK(index.IsRegister()) << index;
3651 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3653 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003654 } else {
3655 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003657 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3658 }
3659 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003660 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003661
3662 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 Register temp = locations->GetTemp(0).AsRegister<Register>();
3664 Register card = locations->GetTemp(1).AsRegister<Register>();
3665 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003668 DCHECK_EQ(value_type, Primitive::kPrimNot);
3669 DCHECK(!codegen_->IsLeafMethod());
3670 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3671 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003672 }
3673 break;
3674 }
3675
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003676 case Primitive::kPrimLong: {
3677 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003678 if (index.IsConstant()) {
3679 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003680 if (value.IsRegisterPair()) {
3681 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003683 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003684 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003685 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003686 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3687 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003688 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003689 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3690 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003692 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003694 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003695 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003697 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003698 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003699 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003700 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003702 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003703 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003705 Immediate(High32Bits(val)));
3706 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 }
3708 break;
3709 }
3710
Mark Mendell7c8d0092015-01-26 11:21:33 -05003711 case Primitive::kPrimFloat: {
3712 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3713 DCHECK(value.IsFpuRegister());
3714 if (index.IsConstant()) {
3715 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3716 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3717 } else {
3718 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3719 value.AsFpuRegister<XmmRegister>());
3720 }
3721 break;
3722 }
3723
3724 case Primitive::kPrimDouble: {
3725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3726 DCHECK(value.IsFpuRegister());
3727 if (index.IsConstant()) {
3728 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3729 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3730 } else {
3731 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3732 value.AsFpuRegister<XmmRegister>());
3733 }
3734 break;
3735 }
3736
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003737 case Primitive::kPrimVoid:
3738 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003739 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 }
3741}
3742
3743void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3744 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003745 locations->SetInAt(0, Location::RequiresRegister());
3746 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747 instruction->SetLocations(locations);
3748}
3749
3750void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3751 LocationSummary* locations = instruction->GetLocations();
3752 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003753 Register obj = locations->InAt(0).AsRegister<Register>();
3754 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003755 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003756 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757}
3758
3759void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003760 LocationSummary* locations =
3761 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003762 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003764 if (instruction->HasUses()) {
3765 locations->SetOut(Location::SameAsFirstInput());
3766 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767}
3768
3769void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3770 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003771 Location index_loc = locations->InAt(0);
3772 Location length_loc = locations->InAt(1);
3773 SlowPathCodeX86* slow_path =
3774 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 codegen_->AddSlowPath(slow_path);
3776
Mark Mendellf60c90b2015-03-04 15:12:59 -05003777 Register length = length_loc.AsRegister<Register>();
3778 if (index_loc.IsConstant()) {
3779 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3780 __ cmpl(length, Immediate(value));
3781 } else {
3782 __ cmpl(length, index_loc.AsRegister<Register>());
3783 }
3784 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785}
3786
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003787void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3788 temp->SetLocations(nullptr);
3789}
3790
3791void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3792 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003793 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003794}
3795
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003796void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003797 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003798 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003799}
3800
3801void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003802 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3803}
3804
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003805void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3807}
3808
3809void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003810 HBasicBlock* block = instruction->GetBlock();
3811 if (block->GetLoopInformation() != nullptr) {
3812 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3813 // The back edge will generate the suspend check.
3814 return;
3815 }
3816 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3817 // The goto will generate the suspend check.
3818 return;
3819 }
3820 GenerateSuspendCheck(instruction, nullptr);
3821}
3822
3823void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3824 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003825 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003826 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003827 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003828 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003829 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003830 if (successor == nullptr) {
3831 __ j(kNotEqual, slow_path->GetEntryLabel());
3832 __ Bind(slow_path->GetReturnLabel());
3833 } else {
3834 __ j(kEqual, codegen_->GetLabelOf(successor));
3835 __ jmp(slow_path->GetEntryLabel());
3836 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003837}
3838
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003839X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3840 return codegen_->GetAssembler();
3841}
3842
Mark Mendell7c8d0092015-01-26 11:21:33 -05003843void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003844 ScratchRegisterScope possible_scratch(
3845 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
3846 int temp = possible_scratch.GetRegister();
3847 if (temp == kNoRegister) {
3848 // Use the stack.
3849 __ pushl(Address(ESP, src));
3850 __ popl(Address(ESP, dst));
3851 } else {
3852 Register temp_reg = static_cast<Register>(temp);
3853 __ movl(temp_reg, Address(ESP, src));
3854 __ movl(Address(ESP, dst), temp_reg);
3855 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003856}
3857
3858void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04003859 ScratchRegisterScope possible_scratch(
3860 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
3861 int temp = possible_scratch.GetRegister();
3862 if (temp == kNoRegister) {
3863 // Use the stack instead.
3864 // Push src low word.
3865 __ pushl(Address(ESP, src));
3866 // Push src high word. Stack offset = 4.
3867 __ pushl(Address(ESP, src + 4 /* offset */ + kX86WordSize /* high */));
3868
3869 // Pop into dst high word. Stack offset = 8.
3870 // Pop with ESP address uses the 'after increment' value of ESP.
3871 __ popl(Address(ESP, dst + 4 /* offset */ + kX86WordSize /* high */));
3872 // Finally dst low word. Stack offset = 4.
3873 __ popl(Address(ESP, dst));
3874 } else {
3875 Register temp_reg = static_cast<Register>(temp);
3876 __ movl(temp_reg, Address(ESP, src));
3877 __ movl(Address(ESP, dst), temp_reg);
3878 __ movl(temp_reg, Address(ESP, src + kX86WordSize));
3879 __ movl(Address(ESP, dst + kX86WordSize), temp_reg);
3880 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003881}
3882
3883void ParallelMoveResolverX86::EmitMove(size_t index) {
3884 MoveOperands* move = moves_.Get(index);
3885 Location source = move->GetSource();
3886 Location destination = move->GetDestination();
3887
3888 if (source.IsRegister()) {
3889 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003891 } else {
3892 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003894 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003895 } else if (source.IsFpuRegister()) {
3896 if (destination.IsFpuRegister()) {
3897 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3898 } else if (destination.IsStackSlot()) {
3899 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3900 } else {
3901 DCHECK(destination.IsDoubleStackSlot());
3902 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3903 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003904 } else if (source.IsStackSlot()) {
3905 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003907 } else if (destination.IsFpuRegister()) {
3908 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003909 } else {
3910 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003911 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3912 }
3913 } else if (source.IsDoubleStackSlot()) {
3914 if (destination.IsFpuRegister()) {
3915 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3916 } else {
3917 DCHECK(destination.IsDoubleStackSlot()) << destination;
3918 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003919 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003920 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003921 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003922 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003923 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003924 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003925 if (value == 0) {
3926 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3927 } else {
3928 __ movl(destination.AsRegister<Register>(), Immediate(value));
3929 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003930 } else {
3931 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003932 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003933 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003934 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003935 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003936 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003937 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003938 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003939 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3940 if (value == 0) {
3941 // Easy handling of 0.0.
3942 __ xorps(dest, dest);
3943 } else {
3944 ScratchRegisterScope ensure_scratch(
Mark Mendella5c19ce2015-04-01 12:51:05 -04003945 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
3946 int temp_reg = ensure_scratch.GetRegister();
3947 if (temp_reg == kNoRegister) {
3948 // Avoid spilling/restoring a scratch register by using the stack.
3949 __ pushl(Immediate(value));
3950 __ movss(dest, Address(ESP, 0));
3951 __ addl(ESP, Immediate(4));
3952 } else {
3953 Register temp = static_cast<Register>(temp_reg);
3954 __ movl(temp, Immediate(value));
3955 __ movd(dest, temp);
3956 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003957 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003958 } else {
3959 DCHECK(destination.IsStackSlot()) << destination;
3960 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3961 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003962 } else if (constant->IsLongConstant()) {
3963 int64_t value = constant->AsLongConstant()->GetValue();
3964 int32_t low_value = Low32Bits(value);
3965 int32_t high_value = High32Bits(value);
3966 Immediate low(low_value);
3967 Immediate high(high_value);
3968 if (destination.IsDoubleStackSlot()) {
3969 __ movl(Address(ESP, destination.GetStackIndex()), low);
3970 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3971 } else {
3972 __ movl(destination.AsRegisterPairLow<Register>(), low);
3973 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3974 }
3975 } else {
3976 DCHECK(constant->IsDoubleConstant());
3977 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00003978 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003979 int32_t low_value = Low32Bits(value);
3980 int32_t high_value = High32Bits(value);
3981 Immediate low(low_value);
3982 Immediate high(high_value);
3983 if (destination.IsFpuRegister()) {
3984 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3985 if (value == 0) {
3986 // Easy handling of 0.0.
3987 __ xorpd(dest, dest);
3988 } else {
3989 __ pushl(high);
3990 __ pushl(low);
3991 __ movsd(dest, Address(ESP, 0));
3992 __ addl(ESP, Immediate(8));
3993 }
3994 } else {
3995 DCHECK(destination.IsDoubleStackSlot()) << destination;
3996 __ movl(Address(ESP, destination.GetStackIndex()), low);
3997 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3998 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003999 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004000 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004001 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004002 }
4003}
4004
Mark Mendella5c19ce2015-04-01 12:51:05 -04004005void ParallelMoveResolverX86::Exchange(Register reg1, Register reg2) {
4006 // Prefer to avoid xchg as it isn't speedy on smaller processors.
4007 ScratchRegisterScope possible_scratch(
4008 this, reg1, codegen_->GetNumberOfCoreRegisters());
4009 int temp_reg = possible_scratch.GetRegister();
4010 if (temp_reg == kNoRegister || temp_reg == reg2) {
4011 __ pushl(reg1);
4012 __ movl(reg1, reg2);
4013 __ popl(reg2);
4014 } else {
4015 Register temp = static_cast<Register>(temp_reg);
4016 __ movl(temp, reg1);
4017 __ movl(reg1, reg2);
4018 __ movl(reg2, temp);
4019 }
4020}
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004021
Mark Mendella5c19ce2015-04-01 12:51:05 -04004022void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
4023 ScratchRegisterScope possible_scratch(
4024 this, reg, codegen_->GetNumberOfCoreRegisters());
4025 int temp_reg = possible_scratch.GetRegister();
4026 if (temp_reg == kNoRegister) {
4027 __ pushl(Address(ESP, mem));
4028 __ movl(Address(ESP, mem + kX86WordSize), reg);
4029 __ popl(reg);
4030 } else {
4031 Register temp = static_cast<Register>(temp_reg);
4032 __ movl(temp, Address(ESP, mem));
4033 __ movl(Address(ESP, mem), reg);
4034 __ movl(reg, temp);
4035 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004036}
4037
Mark Mendell7c8d0092015-01-26 11:21:33 -05004038void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004039 ScratchRegisterScope possible_scratch(
4040 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
4041 int temp_reg = possible_scratch.GetRegister();
4042 if (temp_reg == kNoRegister) {
4043 __ pushl(Address(ESP, mem));
4044 __ movss(Address(ESP, mem + kX86WordSize), reg);
4045 __ movss(reg, Address(ESP, 0));
4046 __ addl(ESP, Immediate(kX86WordSize));
4047 } else {
4048 Register temp = static_cast<Register>(temp_reg);
4049 __ movl(temp, Address(ESP, mem));
4050 __ movss(Address(ESP, mem), reg);
4051 __ movd(reg, temp);
4052 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004053}
4054
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004055void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004056 ScratchRegisterScope possible_scratch1(
4057 this, kNoRegister, codegen_->GetNumberOfCoreRegisters());
4058 int temp_reg1 = possible_scratch1.GetRegister();
4059 if (temp_reg1 == kNoRegister) {
4060 // No free registers. Use the stack.
4061 __ pushl(Address(ESP, mem1));
4062 __ pushl(Address(ESP, mem2 + kX86WordSize));
4063 // Pop with ESP address uses the 'after increment' value of ESP.
4064 __ popl(Address(ESP, mem1 + kX86WordSize));
4065 __ popl(Address(ESP, mem2));
4066 } else {
4067 // Got the first one. Try for a second.
4068 ScratchRegisterScope possible_scratch2(
4069 this, temp_reg1, codegen_->GetNumberOfCoreRegisters());
4070 int temp_reg2 = possible_scratch2.GetRegister();
4071 if (temp_reg2 == kNoRegister) {
4072 Register temp = static_cast<Register>(temp_reg1);
4073 // Bummer. Only have one free register to use.
4074 // Save mem1 on the stack.
4075 __ pushl(Address(ESP, mem1));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004076
Mark Mendella5c19ce2015-04-01 12:51:05 -04004077 // Copy mem2 into mem1.
4078 __ movl(temp, Address(ESP, mem2 + kX86WordSize));
4079 __ movl(Address(ESP, mem1 + kX86WordSize), temp);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004080
Mark Mendella5c19ce2015-04-01 12:51:05 -04004081 // Now pop mem1 into mem2.
4082 // Pop with ESP address uses the 'after increment' value of ESP.
4083 __ popl(Address(ESP, mem2));
4084 } else {
4085 // Great. We have 2 registers to play with.
4086 Register temp1 = static_cast<Register>(temp_reg1);
4087 Register temp2 = static_cast<Register>(temp_reg2);
4088 DCHECK_NE(temp1, temp2);
4089 __ movl(temp1, Address(ESP, mem1));
4090 __ movl(temp2, Address(ESP, mem2));
4091 __ movl(Address(ESP, mem2), temp1);
4092 __ movl(Address(ESP, mem1), temp2);
4093 }
4094 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004095}
4096
4097void ParallelMoveResolverX86::EmitSwap(size_t index) {
4098 MoveOperands* move = moves_.Get(index);
4099 Location source = move->GetSource();
4100 Location destination = move->GetDestination();
4101
4102 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendella5c19ce2015-04-01 12:51:05 -04004103 Exchange(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004104 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004105 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004106 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004108 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4109 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004110 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4111 // Use XOR Swap algorithm to avoid a temporary.
4112 DCHECK_NE(source.reg(), destination.reg());
4113 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4114 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4115 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4116 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4117 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4118 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4119 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004120 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4121 // Take advantage of the 16 bytes in the XMM register.
4122 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4123 Address stack(ESP, destination.GetStackIndex());
4124 // Load the double into the high doubleword.
4125 __ movhpd(reg, stack);
4126
4127 // Store the low double into the destination.
4128 __ movsd(stack, reg);
4129
4130 // Move the high double to the low double.
4131 __ psrldq(reg, Immediate(8));
4132 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4133 // Take advantage of the 16 bytes in the XMM register.
4134 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4135 Address stack(ESP, source.GetStackIndex());
4136 // Load the double into the high doubleword.
4137 __ movhpd(reg, stack);
4138
4139 // Store the low double into the destination.
4140 __ movsd(stack, reg);
4141
4142 // Move the high double to the low double.
4143 __ psrldq(reg, Immediate(8));
4144 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4145 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4146 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004147 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004148 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004149 }
4150}
4151
4152void ParallelMoveResolverX86::SpillScratch(int reg) {
4153 __ pushl(static_cast<Register>(reg));
4154}
4155
4156void ParallelMoveResolverX86::RestoreScratch(int reg) {
4157 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004158}
4159
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004160void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004161 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4162 ? LocationSummary::kCallOnSlowPath
4163 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004164 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004165 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004166 locations->SetOut(Location::RequiresRegister());
4167}
4168
4169void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004170 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004171 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004172 DCHECK(!cls->CanCallRuntime());
4173 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004174 codegen_->LoadCurrentMethod(out);
4175 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4176 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004177 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004178 codegen_->LoadCurrentMethod(out);
4179 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4180 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004181
4182 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4183 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4184 codegen_->AddSlowPath(slow_path);
4185 __ testl(out, out);
4186 __ j(kEqual, slow_path->GetEntryLabel());
4187 if (cls->MustGenerateClinitCheck()) {
4188 GenerateClassInitializationCheck(slow_path, out);
4189 } else {
4190 __ Bind(slow_path->GetExitLabel());
4191 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004192 }
4193}
4194
4195void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4198 locations->SetInAt(0, Location::RequiresRegister());
4199 if (check->HasUses()) {
4200 locations->SetOut(Location::SameAsFirstInput());
4201 }
4202}
4203
4204void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004205 // We assume the class to not be null.
4206 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4207 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004208 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004209 GenerateClassInitializationCheck(slow_path,
4210 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004211}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004212
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004213void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4214 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004215 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4216 Immediate(mirror::Class::kStatusInitialized));
4217 __ j(kLess, slow_path->GetEntryLabel());
4218 __ Bind(slow_path->GetExitLabel());
4219 // No need for memory fence, thanks to the X86 memory model.
4220}
4221
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004222void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4223 LocationSummary* locations =
4224 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4225 locations->SetOut(Location::RequiresRegister());
4226}
4227
4228void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4229 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4230 codegen_->AddSlowPath(slow_path);
4231
Roland Levillain271ab9c2014-11-27 15:23:57 +00004232 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004233 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004234 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4235 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004236 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4237 __ testl(out, out);
4238 __ j(kEqual, slow_path->GetEntryLabel());
4239 __ Bind(slow_path->GetExitLabel());
4240}
4241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004242void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4243 LocationSummary* locations =
4244 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4245 locations->SetOut(Location::RequiresRegister());
4246}
4247
4248void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4249 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004250 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004251 __ fs()->movl(address, Immediate(0));
4252}
4253
4254void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4255 LocationSummary* locations =
4256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4257 InvokeRuntimeCallingConvention calling_convention;
4258 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4259}
4260
4261void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4262 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4263 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4264}
4265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004266void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004267 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4268 ? LocationSummary::kNoCall
4269 : LocationSummary::kCallOnSlowPath;
4270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4271 locations->SetInAt(0, Location::RequiresRegister());
4272 locations->SetInAt(1, Location::Any());
4273 locations->SetOut(Location::RequiresRegister());
4274}
4275
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004276void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004277 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004279 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004280 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4282 Label done, zero;
4283 SlowPathCodeX86* slow_path = nullptr;
4284
4285 // Return 0 if `obj` is null.
4286 // TODO: avoid this check if we know obj is not null.
4287 __ testl(obj, obj);
4288 __ j(kEqual, &zero);
4289 __ movl(out, Address(obj, class_offset));
4290 // Compare the class of `obj` with `cls`.
4291 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004292 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004293 } else {
4294 DCHECK(cls.IsStackSlot()) << cls;
4295 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4296 }
4297
4298 if (instruction->IsClassFinal()) {
4299 // Classes must be equal for the instanceof to succeed.
4300 __ j(kNotEqual, &zero);
4301 __ movl(out, Immediate(1));
4302 __ jmp(&done);
4303 } else {
4304 // If the classes are not equal, we go into a slow path.
4305 DCHECK(locations->OnlyCallsOnSlowPath());
4306 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004307 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004308 codegen_->AddSlowPath(slow_path);
4309 __ j(kNotEqual, slow_path->GetEntryLabel());
4310 __ movl(out, Immediate(1));
4311 __ jmp(&done);
4312 }
4313 __ Bind(&zero);
4314 __ movl(out, Immediate(0));
4315 if (slow_path != nullptr) {
4316 __ Bind(slow_path->GetExitLabel());
4317 }
4318 __ Bind(&done);
4319}
4320
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004321void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4322 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4323 instruction, LocationSummary::kCallOnSlowPath);
4324 locations->SetInAt(0, Location::RequiresRegister());
4325 locations->SetInAt(1, Location::Any());
4326 locations->AddTemp(Location::RequiresRegister());
4327}
4328
4329void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4330 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004331 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004332 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004333 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004334 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4335 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4336 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4337 codegen_->AddSlowPath(slow_path);
4338
4339 // TODO: avoid this check if we know obj is not null.
4340 __ testl(obj, obj);
4341 __ j(kEqual, slow_path->GetExitLabel());
4342 __ movl(temp, Address(obj, class_offset));
4343
4344 // Compare the class of `obj` with `cls`.
4345 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004346 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004347 } else {
4348 DCHECK(cls.IsStackSlot()) << cls;
4349 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4350 }
4351
4352 __ j(kNotEqual, slow_path->GetEntryLabel());
4353 __ Bind(slow_path->GetExitLabel());
4354}
4355
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004356void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4357 LocationSummary* locations =
4358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4359 InvokeRuntimeCallingConvention calling_convention;
4360 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4361}
4362
4363void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4364 __ fs()->call(Address::Absolute(instruction->IsEnter()
4365 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4366 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4367 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4368}
4369
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004370void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4371void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4372void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4373
4374void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4375 LocationSummary* locations =
4376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4377 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4378 || instruction->GetResultType() == Primitive::kPrimLong);
4379 locations->SetInAt(0, Location::RequiresRegister());
4380 locations->SetInAt(1, Location::Any());
4381 locations->SetOut(Location::SameAsFirstInput());
4382}
4383
4384void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4385 HandleBitwiseOperation(instruction);
4386}
4387
4388void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4389 HandleBitwiseOperation(instruction);
4390}
4391
4392void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4393 HandleBitwiseOperation(instruction);
4394}
4395
4396void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4397 LocationSummary* locations = instruction->GetLocations();
4398 Location first = locations->InAt(0);
4399 Location second = locations->InAt(1);
4400 DCHECK(first.Equals(locations->Out()));
4401
4402 if (instruction->GetResultType() == Primitive::kPrimInt) {
4403 if (second.IsRegister()) {
4404 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004406 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004408 } else {
4409 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004411 }
4412 } else if (second.IsConstant()) {
4413 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004414 __ andl(first.AsRegister<Register>(),
4415 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004416 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004417 __ orl(first.AsRegister<Register>(),
4418 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004419 } else {
4420 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004421 __ xorl(first.AsRegister<Register>(),
4422 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004423 }
4424 } else {
4425 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004426 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004427 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004429 } else {
4430 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004432 }
4433 }
4434 } else {
4435 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4436 if (second.IsRegisterPair()) {
4437 if (instruction->IsAnd()) {
4438 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4439 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4440 } else if (instruction->IsOr()) {
4441 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4442 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4443 } else {
4444 DCHECK(instruction->IsXor());
4445 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4446 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4447 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004448 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004449 if (instruction->IsAnd()) {
4450 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4451 __ andl(first.AsRegisterPairHigh<Register>(),
4452 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4453 } else if (instruction->IsOr()) {
4454 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4455 __ orl(first.AsRegisterPairHigh<Register>(),
4456 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4457 } else {
4458 DCHECK(instruction->IsXor());
4459 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4460 __ xorl(first.AsRegisterPairHigh<Register>(),
4461 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4462 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004463 } else {
4464 DCHECK(second.IsConstant()) << second;
4465 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004466 int32_t low_value = Low32Bits(value);
4467 int32_t high_value = High32Bits(value);
4468 Immediate low(low_value);
4469 Immediate high(high_value);
4470 Register first_low = first.AsRegisterPairLow<Register>();
4471 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004472 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004473 if (low_value == 0) {
4474 __ xorl(first_low, first_low);
4475 } else if (low_value != -1) {
4476 __ andl(first_low, low);
4477 }
4478 if (high_value == 0) {
4479 __ xorl(first_high, first_high);
4480 } else if (high_value != -1) {
4481 __ andl(first_high, high);
4482 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004483 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004484 if (low_value != 0) {
4485 __ orl(first_low, low);
4486 }
4487 if (high_value != 0) {
4488 __ orl(first_high, high);
4489 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004490 } else {
4491 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004492 if (low_value != 0) {
4493 __ xorl(first_low, low);
4494 }
4495 if (high_value != 0) {
4496 __ xorl(first_high, high);
4497 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004498 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004499 }
4500 }
4501}
4502
Calin Juravleb1498f62015-02-16 13:13:29 +00004503void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4504 // Nothing to do, this should be removed during prepare for register allocator.
4505 UNUSED(instruction);
4506 LOG(FATAL) << "Unreachable";
4507}
4508
4509void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4510 // Nothing to do, this should be removed during prepare for register allocator.
4511 UNUSED(instruction);
4512 LOG(FATAL) << "Unreachable";
4513}
4514
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004515} // namespace x86
4516} // namespace art