blob: 2848a48a642e180bbd07adf46451c957d0adf366 [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)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100156 HBasicBlock* GetSuccessor() const {
157 return successor_;
158 }
159
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 private:
161 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 Label return_label_;
164
165 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
166};
167
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000168class LoadStringSlowPathX86 : public SlowPathCodeX86 {
169 public:
170 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
171
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000173 LocationSummary* locations = instruction_->GetLocations();
174 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
175
176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179
180 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800181 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000184 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000185 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000186
187 __ jmp(GetExitLabel());
188 }
189
190 private:
191 HLoadString* const instruction_;
192
193 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
194};
195
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196class LoadClassSlowPathX86 : public SlowPathCodeX86 {
197 public:
198 LoadClassSlowPathX86(HLoadClass* cls,
199 HInstruction* at,
200 uint32_t dex_pc,
201 bool do_clinit)
202 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
203 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
204 }
205
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 LocationSummary* locations = at_->GetLocations();
208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
209 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211
212 InvokeRuntimeCallingConvention calling_convention;
213 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ fs()->call(Address::Absolute(do_clinit_
215 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
216 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218
219 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
221 if (out.IsValid()) {
222 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
223 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000225
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ jmp(GetExitLabel());
228 }
229
230 private:
231 // The class this slow path will load.
232 HLoadClass* const cls_;
233
234 // The instruction where this slow path is happening.
235 // (Might be the load class or an initialization check).
236 HInstruction* const at_;
237
238 // The dex PC of `at_`.
239 const uint32_t dex_pc_;
240
241 // Whether to initialize the class.
242 const bool do_clinit_;
243
244 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
245};
246
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000247class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
248 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000249 TypeCheckSlowPathX86(HInstruction* instruction,
250 Location class_to_check,
251 Location object_class,
252 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000254 class_to_check_(class_to_check),
255 object_class_(object_class),
256 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000260 DCHECK(instruction_->IsCheckCast()
261 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
263 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266
267 // We're moving two locations to locations that could overlap, so we need a parallel
268 // move resolver.
269 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000270 x86_codegen->EmitParallelMoves(
271 class_to_check_,
272 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100273 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000274 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100275 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
276 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000279 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
280 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 } else {
282 DCHECK(instruction_->IsCheckCast());
283 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 if (instruction_->IsInstanceOf()) {
288 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
289 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 __ jmp(GetExitLabel());
293 }
294
295 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 HInstruction* const instruction_;
297 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
301 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
302};
303
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700304class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
305 public:
306 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
307 : instruction_(instruction) {}
308
309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
310 __ Bind(GetEntryLabel());
311 SaveLiveRegisters(codegen, instruction_->GetLocations());
312 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
313 // No need to restore live registers.
314 DCHECK(instruction_->IsDeoptimize());
315 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
316 uint32_t dex_pc = deoptimize->GetDexPc();
317 codegen->RecordPcInfo(instruction_, dex_pc, this);
318 }
319
320 private:
321 HInstruction* const instruction_;
322 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
323};
324
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100325#undef __
326#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
327
Dave Allison20dfc792014-06-16 20:44:29 -0700328inline Condition X86Condition(IfCondition cond) {
329 switch (cond) {
330 case kCondEQ: return kEqual;
331 case kCondNE: return kNotEqual;
332 case kCondLT: return kLess;
333 case kCondLE: return kLessEqual;
334 case kCondGT: return kGreater;
335 case kCondGE: return kGreaterEqual;
336 default:
337 LOG(FATAL) << "Unknown if condition";
338 }
339 return kEqual;
340}
341
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
343 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
344}
345
346void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
347 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
348}
349
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100350size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
351 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
352 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100353}
354
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100355size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
356 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
357 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100358}
359
Mark Mendell7c8d0092015-01-26 11:21:33 -0500360size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
361 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
362 return GetFloatingPointSpillSlotSize();
363}
364
365size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
366 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
367 return GetFloatingPointSpillSlotSize();
368}
369
Mark Mendellfb8d2792015-03-31 22:16:59 -0400370CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
371 const X86InstructionSetFeatures& isa_features,
372 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500373 : CodeGenerator(graph,
374 kNumberOfCpuRegisters,
375 kNumberOfXmmRegisters,
376 kNumberOfRegisterPairs,
377 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
378 arraysize(kCoreCalleeSaves))
379 | (1 << kFakeReturnRegister),
380 0,
381 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100382 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100384 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400385 move_resolver_(graph->GetArena(), this),
386 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000387 // Use a fake return address register to mimic Quick.
388 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100389}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100391Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100392 switch (type) {
393 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100394 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100395 X86ManagedRegister pair =
396 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
400 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100401 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 }
404
405 case Primitive::kPrimByte:
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimChar:
408 case Primitive::kPrimShort:
409 case Primitive::kPrimInt:
410 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
415 X86ManagedRegister current =
416 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
417 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100419 }
420 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422 }
423
424 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100426 return Location::FpuRegisterLocation(
427 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100428 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100429
430 case Primitive::kPrimVoid:
431 LOG(FATAL) << "Unreachable type " << type;
432 }
433
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435}
436
Mark Mendell5f874182015-03-04 15:42:45 -0500437void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
441 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443
Mark Mendell5f874182015-03-04 15:42:45 -0500444 if (is_baseline) {
445 blocked_core_registers_[EBP] = true;
446 blocked_core_registers_[ESI] = true;
447 blocked_core_registers_[EDI] = true;
448 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100449
450 UpdateBlockedPairRegisters();
451}
452
453void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
454 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
455 X86ManagedRegister current =
456 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
457 if (blocked_core_registers_[current.AsRegisterPairLow()]
458 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
459 blocked_register_pairs_[i] = true;
460 }
461 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462}
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
465 : HGraphVisitor(graph),
466 assembler_(codegen->GetAssembler()),
467 codegen_(codegen) {}
468
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100469static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100470 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100471}
472
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000473void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100474 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000475 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000476 bool skip_overflow_check =
477 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000479
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100481 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100483 }
484
Mark Mendell5f874182015-03-04 15:42:45 -0500485 if (HasEmptyFrame()) {
486 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000487 }
Mark Mendell5f874182015-03-04 15:42:45 -0500488
489 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
490 Register reg = kCoreCalleeSaves[i];
491 if (allocated_registers_.ContainsCoreRegister(reg)) {
492 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100493 __ cfi().AdjustCFAOffset(kX86WordSize);
494 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500495 }
496 }
497
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100498 int adjust = GetFrameSize() - FrameEntrySpillSize();
499 __ subl(ESP, Immediate(adjust));
500 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500501 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000502}
503
504void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100505 __ cfi().RememberState();
506 if (!HasEmptyFrame()) {
507 int adjust = GetFrameSize() - FrameEntrySpillSize();
508 __ addl(ESP, Immediate(adjust));
509 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500510
David Srbeckyc34dc932015-04-12 09:27:43 +0100511 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
512 Register reg = kCoreCalleeSaves[i];
513 if (allocated_registers_.ContainsCoreRegister(reg)) {
514 __ popl(reg);
515 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
516 __ cfi().Restore(DWARFReg(reg));
517 }
Mark Mendell5f874182015-03-04 15:42:45 -0500518 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000519 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100520 __ ret();
521 __ cfi().RestoreState();
522 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000523}
524
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100525void CodeGeneratorX86::Bind(HBasicBlock* block) {
526 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000527}
528
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100529void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000530 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100531 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000532}
533
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
535 switch (load->GetType()) {
536 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539
540 case Primitive::kPrimInt:
541 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100544
545 case Primitive::kPrimBoolean:
546 case Primitive::kPrimByte:
547 case Primitive::kPrimChar:
548 case Primitive::kPrimShort:
549 case Primitive::kPrimVoid:
550 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552 }
553
554 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700555 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100556}
557
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100558Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 switch (type) {
560 case Primitive::kPrimBoolean:
561 case Primitive::kPrimByte:
562 case Primitive::kPrimChar:
563 case Primitive::kPrimShort:
564 case Primitive::kPrimInt:
565 case Primitive::kPrimNot: {
566 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000567 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100569 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000571 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100572 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100573 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000575 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100576 uint32_t index = gp_index_;
577 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000578 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100579 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
581 calling_convention.GetRegisterPairAt(index));
582 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000584 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
585 }
586 }
587
588 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100589 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000590 stack_index_++;
591 if (index < calling_convention.GetNumberOfFpuRegisters()) {
592 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
593 } else {
594 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
595 }
596 }
597
598 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100599 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000600 stack_index_ += 2;
601 if (index < calling_convention.GetNumberOfFpuRegisters()) {
602 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
603 } else {
604 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100605 }
606 }
607
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 case Primitive::kPrimVoid:
609 LOG(FATAL) << "Unexpected parameter type " << type;
610 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100611 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 return Location();
613}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100614
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615void CodeGeneratorX86::Move32(Location destination, Location source) {
616 if (source.Equals(destination)) {
617 return;
618 }
619 if (destination.IsRegister()) {
620 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 } else {
625 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 } else if (destination.IsFpuRegister()) {
629 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 } else {
634 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000635 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000638 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000642 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500643 } else if (source.IsConstant()) {
644 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000645 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500646 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 } else {
648 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100649 __ pushl(Address(ESP, source.GetStackIndex()));
650 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 }
652 }
653}
654
655void CodeGeneratorX86::Move64(Location destination, Location source) {
656 if (source.Equals(destination)) {
657 return;
658 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 if (destination.IsRegisterPair()) {
660 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 EmitParallelMoves(
662 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
663 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100664 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000665 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100666 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
667 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 } else if (source.IsFpuRegister()) {
669 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000671 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
674 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
676 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100677 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500678 if (source.IsFpuRegister()) {
679 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
680 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000681 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 } else {
683 LOG(FATAL) << "Unimplemented";
684 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100685 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000686 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000688 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100691 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000693 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000694 } else if (source.IsConstant()) {
695 HConstant* constant = source.GetConstant();
696 int64_t value;
697 if (constant->IsLongConstant()) {
698 value = constant->AsLongConstant()->GetValue();
699 } else {
700 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000701 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000702 }
703 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
704 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000706 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 EmitParallelMoves(
708 Location::StackSlot(source.GetStackIndex()),
709 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100710 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000711 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100712 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
713 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 }
715 }
716}
717
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100718void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000719 LocationSummary* locations = instruction->GetLocations();
720 if (locations != nullptr && locations->Out().Equals(location)) {
721 return;
722 }
723
724 if (locations != nullptr && locations->Out().IsConstant()) {
725 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000726 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
727 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000728 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000730 } else if (location.IsStackSlot()) {
731 __ movl(Address(ESP, location.GetStackIndex()), imm);
732 } else {
733 DCHECK(location.IsConstant());
734 DCHECK_EQ(location.GetConstant(), const_to_move);
735 }
736 } else if (const_to_move->IsLongConstant()) {
737 int64_t value = const_to_move->AsLongConstant()->GetValue();
738 if (location.IsRegisterPair()) {
739 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
740 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
741 } else if (location.IsDoubleStackSlot()) {
742 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000743 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
744 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000745 } else {
746 DCHECK(location.IsConstant());
747 DCHECK_EQ(location.GetConstant(), instruction);
748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000750 } else if (instruction->IsTemporary()) {
751 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000752 if (temp_location.IsStackSlot()) {
753 Move32(location, temp_location);
754 } else {
755 DCHECK(temp_location.IsDoubleStackSlot());
756 Move64(location, temp_location);
757 }
Roland Levillain476df552014-10-09 17:51:36 +0100758 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 switch (instruction->GetType()) {
761 case Primitive::kPrimBoolean:
762 case Primitive::kPrimByte:
763 case Primitive::kPrimChar:
764 case Primitive::kPrimShort:
765 case Primitive::kPrimInt:
766 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 case Primitive::kPrimFloat:
768 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 break;
770
771 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 case Primitive::kPrimDouble:
773 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 break;
775
776 default:
777 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
778 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100780 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100781 switch (instruction->GetType()) {
782 case Primitive::kPrimBoolean:
783 case Primitive::kPrimByte:
784 case Primitive::kPrimChar:
785 case Primitive::kPrimShort:
786 case Primitive::kPrimInt:
787 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000789 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 break;
791
792 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000794 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 break;
796
797 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100799 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800 }
801}
802
803void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805}
806
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000807void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000808 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100809 DCHECK(!successor->IsExitBlock());
810
811 HBasicBlock* block = got->GetBlock();
812 HInstruction* previous = got->GetPrevious();
813
814 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000815 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100816 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
817 return;
818 }
819
820 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
821 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
822 }
823 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000825 }
826}
827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000828void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000830}
831
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700833 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000834}
835
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
837 Label* true_target,
838 Label* false_target,
839 Label* always_true_target) {
840 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 if (cond->IsIntConstant()) {
842 // Constant condition, statically compared against 1.
843 int32_t cond_value = cond->AsIntConstant()->GetValue();
844 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700845 if (always_true_target != nullptr) {
846 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 DCHECK_EQ(cond_value, 0);
851 }
852 } else {
853 bool materialized =
854 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
855 // Moves do not affect the eflags register, so if the condition is
856 // evaluated just before the if, we don't need to evaluate it
857 // again.
858 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700859 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100860 if (materialized) {
861 if (!eflags_set) {
862 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700863 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500865 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100866 } else {
867 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
868 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700869 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700871 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100872 }
873 } else {
874 Location lhs = cond->GetLocations()->InAt(0);
875 Location rhs = cond->GetLocations()->InAt(1);
876 // LHS is guaranteed to be in a register (see
877 // LocationsBuilderX86::VisitCondition).
878 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000879 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100880 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100881 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500882 if (constant == 0) {
883 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
884 } else {
885 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
886 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100889 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700890 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700891 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700893 if (false_target != nullptr) {
894 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895 }
896}
897
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700898void LocationsBuilderX86::VisitIf(HIf* if_instr) {
899 LocationSummary* locations =
900 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
901 HInstruction* cond = if_instr->InputAt(0);
902 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
903 locations->SetInAt(0, Location::Any());
904 }
905}
906
907void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
908 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
909 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
910 Label* always_true_target = true_target;
911 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
912 if_instr->IfTrueSuccessor())) {
913 always_true_target = nullptr;
914 }
915 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
916 if_instr->IfFalseSuccessor())) {
917 false_target = nullptr;
918 }
919 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
920}
921
922void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
923 LocationSummary* locations = new (GetGraph()->GetArena())
924 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
925 HInstruction* cond = deoptimize->InputAt(0);
926 DCHECK(cond->IsCondition());
927 if (cond->AsCondition()->NeedsMaterialization()) {
928 locations->SetInAt(0, Location::Any());
929 }
930}
931
932void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
933 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
934 DeoptimizationSlowPathX86(deoptimize);
935 codegen_->AddSlowPath(slow_path);
936 Label* slow_path_entry = slow_path->GetEntryLabel();
937 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
938}
939
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000940void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000942}
943
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000944void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
945 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000946}
947
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000948void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100949 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000950}
951
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000952void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100953 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700954 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000955}
956
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100958 LocationSummary* locations =
959 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 switch (store->InputAt(1)->GetType()) {
961 case Primitive::kPrimBoolean:
962 case Primitive::kPrimByte:
963 case Primitive::kPrimChar:
964 case Primitive::kPrimShort:
965 case Primitive::kPrimInt:
966 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
969 break;
970
971 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100972 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100973 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
974 break;
975
976 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100977 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 }
979 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000980}
981
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Dave Allison20dfc792014-06-16 20:44:29 -0700986void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100989 locations->SetInAt(0, Location::RequiresRegister());
990 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500992 // We need a byte register.
993 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100994 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995}
996
Dave Allison20dfc792014-06-16 20:44:29 -0700997void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
998 if (comp->NeedsMaterialization()) {
999 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001001 // Clear register: setcc only sets the low byte.
1002 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001003 Location lhs = locations->InAt(0);
1004 Location rhs = locations->InAt(1);
1005 if (rhs.IsRegister()) {
1006 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1007 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001008 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001009 if (constant == 0) {
1010 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1011 } else {
1012 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1013 }
Dave Allison20dfc792014-06-16 20:44:29 -07001014 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001015 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001016 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001017 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001018 }
Dave Allison20dfc792014-06-16 20:44:29 -07001019}
1020
1021void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1022 VisitCondition(comp);
1023}
1024
1025void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1026 VisitCondition(comp);
1027}
1028
1029void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1030 VisitCondition(comp);
1031}
1032
1033void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1034 VisitCondition(comp);
1035}
1036
1037void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1038 VisitCondition(comp);
1039}
1040
1041void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1042 VisitCondition(comp);
1043}
1044
1045void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1046 VisitCondition(comp);
1047}
1048
1049void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1062 VisitCondition(comp);
1063}
1064
1065void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1066 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067}
1068
1069void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001070 LocationSummary* locations =
1071 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001072 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001073}
1074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001075void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001076 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001077 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001078}
1079
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001080void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1081 LocationSummary* locations =
1082 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1083 locations->SetOut(Location::ConstantLocation(constant));
1084}
1085
1086void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1087 // Will be generated at use site.
1088 UNUSED(constant);
1089}
1090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001092 LocationSummary* locations =
1093 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001094 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095}
1096
1097void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1098 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100}
1101
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001102void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1105 locations->SetOut(Location::ConstantLocation(constant));
1106}
1107
1108void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1109 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001111}
1112
1113void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1116 locations->SetOut(Location::ConstantLocation(constant));
1117}
1118
1119void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1120 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001122}
1123
Calin Juravle27df7582015-04-17 19:12:31 +01001124void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1125 memory_barrier->SetLocations(nullptr);
1126}
1127
1128void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1129 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1130}
1131
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001134}
1135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001136void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001137 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001138 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001139}
1140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001141void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001144 switch (ret->InputAt(0)->GetType()) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte:
1147 case Primitive::kPrimChar:
1148 case Primitive::kPrimShort:
1149 case Primitive::kPrimInt:
1150 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 break;
1153
1154 case Primitive::kPrimLong:
1155 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 break;
1158
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001159 case Primitive::kPrimFloat:
1160 case Primitive::kPrimDouble:
1161 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 break;
1164
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001168}
1169
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001170void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 if (kIsDebugBuild) {
1172 switch (ret->InputAt(0)->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001179 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
1182 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1184 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 break;
1186
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 case Primitive::kPrimFloat:
1188 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 break;
1191
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001192 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001196 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001197}
1198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001199void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001200 // When we do not run baseline, explicit clinit checks triggered by static
1201 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1202 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001203
Mark Mendellfb8d2792015-03-31 22:16:59 -04001204 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001205 if (intrinsic.TryDispatch(invoke)) {
1206 return;
1207 }
1208
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001209 HandleInvoke(invoke);
1210}
1211
Mark Mendell09ed1a32015-03-25 08:30:06 -04001212static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1213 if (invoke->GetLocations()->Intrinsified()) {
1214 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1215 intrinsic.Dispatch(invoke);
1216 return true;
1217 }
1218 return false;
1219}
1220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001221void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001222 // When we do not run baseline, explicit clinit checks triggered by static
1223 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1224 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001225
Mark Mendell09ed1a32015-03-25 08:30:06 -04001226 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1227 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001228 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229
Mark Mendell09ed1a32015-03-25 08:30:06 -04001230 codegen_->GenerateStaticOrDirectCall(
1231 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001232 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001233}
1234
1235void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1236 HandleInvoke(invoke);
1237}
1238
1239void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001240 LocationSummary* locations =
1241 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001242 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001243
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001244 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001245 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001246 HInstruction* input = invoke->InputAt(i);
1247 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1248 }
1249
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 switch (invoke->GetType()) {
1251 case Primitive::kPrimBoolean:
1252 case Primitive::kPrimByte:
1253 case Primitive::kPrimChar:
1254 case Primitive::kPrimShort:
1255 case Primitive::kPrimInt:
1256 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001257 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001258 break;
1259
1260 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001261 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 break;
1263
1264 case Primitive::kPrimVoid:
1265 break;
1266
1267 case Primitive::kPrimDouble:
1268 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001269 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 break;
1271 }
1272
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001273 invoke->SetLocations(locations);
1274}
1275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001276void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001277 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001278 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1279 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1280 LocationSummary* locations = invoke->GetLocations();
1281 Location receiver = locations->InAt(0);
1282 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1283 // temp = object->GetClass();
1284 if (receiver.IsStackSlot()) {
1285 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1286 __ movl(temp, Address(temp, class_offset));
1287 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001288 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001289 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001290 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001291 // temp = temp->GetMethodAt(method_offset);
1292 __ movl(temp, Address(temp, method_offset));
1293 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001294 __ call(Address(
1295 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001296
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001297 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001299}
1300
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1302 HandleInvoke(invoke);
1303 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001304 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001305}
1306
1307void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1308 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1311 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1312 LocationSummary* locations = invoke->GetLocations();
1313 Location receiver = locations->InAt(0);
1314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1315
1316 // Set the hidden argument.
1317 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001318 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319
1320 // temp = object->GetClass();
1321 if (receiver.IsStackSlot()) {
1322 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1323 __ movl(temp, Address(temp, class_offset));
1324 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001327 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 // temp = temp->GetImtEntryAt(method_offset);
1329 __ movl(temp, Address(temp, method_offset));
1330 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001331 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001332 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001333
1334 DCHECK(!codegen_->IsLeafMethod());
1335 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1336}
1337
Roland Levillain88cb1752014-10-20 16:36:47 +01001338void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1339 LocationSummary* locations =
1340 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1341 switch (neg->GetResultType()) {
1342 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001343 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 locations->SetInAt(0, Location::RequiresRegister());
1345 locations->SetOut(Location::SameAsFirstInput());
1346 break;
1347
Roland Levillain88cb1752014-10-20 16:36:47 +01001348 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001349 locations->SetInAt(0, Location::RequiresFpuRegister());
1350 locations->SetOut(Location::SameAsFirstInput());
1351 locations->AddTemp(Location::RequiresRegister());
1352 locations->AddTemp(Location::RequiresFpuRegister());
1353 break;
1354
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001356 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001357 locations->SetOut(Location::SameAsFirstInput());
1358 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001359 break;
1360
1361 default:
1362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1363 }
1364}
1365
1366void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1367 LocationSummary* locations = neg->GetLocations();
1368 Location out = locations->Out();
1369 Location in = locations->InAt(0);
1370 switch (neg->GetResultType()) {
1371 case Primitive::kPrimInt:
1372 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001375 break;
1376
1377 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001378 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001379 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001380 __ negl(out.AsRegisterPairLow<Register>());
1381 // Negation is similar to subtraction from zero. The least
1382 // significant byte triggers a borrow when it is different from
1383 // zero; to take it into account, add 1 to the most significant
1384 // byte if the carry flag (CF) is set to 1 after the first NEGL
1385 // operation.
1386 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1387 __ negl(out.AsRegisterPairHigh<Register>());
1388 break;
1389
Roland Levillain5368c212014-11-27 15:03:41 +00001390 case Primitive::kPrimFloat: {
1391 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001392 Register constant = locations->GetTemp(0).AsRegister<Register>();
1393 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001394 // Implement float negation with an exclusive or with value
1395 // 0x80000000 (mask for bit 31, representing the sign of a
1396 // single-precision floating-point number).
1397 __ movl(constant, Immediate(INT32_C(0x80000000)));
1398 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001400 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001401 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001402
Roland Levillain5368c212014-11-27 15:03:41 +00001403 case Primitive::kPrimDouble: {
1404 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001405 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001406 // Implement double negation with an exclusive or with value
1407 // 0x8000000000000000 (mask for bit 63, representing the sign of
1408 // a double-precision floating-point number).
1409 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001410 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001411 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001412 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001413
1414 default:
1415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1416 }
1417}
1418
Roland Levillaindff1f282014-11-05 14:15:05 +00001419void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 Primitive::Type result_type = conversion->GetResultType();
1421 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001422 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001423
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 // The float-to-long and double-to-long type conversions rely on a
1425 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001426 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001427 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1428 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001429 ? LocationSummary::kCall
1430 : LocationSummary::kNoCall;
1431 LocationSummary* locations =
1432 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1433
David Brazdilb2bd1c52015-03-25 11:17:37 +00001434 // The Java language does not allow treating boolean as an integral type but
1435 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001436
Roland Levillaindff1f282014-11-05 14:15:05 +00001437 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001438 case Primitive::kPrimByte:
1439 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001440 case Primitive::kPrimBoolean:
1441 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001442 case Primitive::kPrimShort:
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001445 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001446 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1447 // Make the output overlap to please the register allocator. This greatly simplifies
1448 // the validation of the linear scan implementation
1449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001450 break;
1451
1452 default:
1453 LOG(FATAL) << "Unexpected type conversion from " << input_type
1454 << " to " << result_type;
1455 }
1456 break;
1457
Roland Levillain01a8d712014-11-14 16:27:39 +00001458 case Primitive::kPrimShort:
1459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001460 case Primitive::kPrimBoolean:
1461 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001462 case Primitive::kPrimByte:
1463 case Primitive::kPrimInt:
1464 case Primitive::kPrimChar:
1465 // Processing a Dex `int-to-short' instruction.
1466 locations->SetInAt(0, Location::Any());
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 break;
1469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 }
1474 break;
1475
Roland Levillain946e1432014-11-11 17:35:19 +00001476 case Primitive::kPrimInt:
1477 switch (input_type) {
1478 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001479 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001480 locations->SetInAt(0, Location::Any());
1481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1482 break;
1483
1484 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001485 // Processing a Dex `float-to-int' instruction.
1486 locations->SetInAt(0, Location::RequiresFpuRegister());
1487 locations->SetOut(Location::RequiresRegister());
1488 locations->AddTemp(Location::RequiresFpuRegister());
1489 break;
1490
Roland Levillain946e1432014-11-11 17:35:19 +00001491 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001492 // Processing a Dex `double-to-int' instruction.
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetOut(Location::RequiresRegister());
1495 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001496 break;
1497
1498 default:
1499 LOG(FATAL) << "Unexpected type conversion from " << input_type
1500 << " to " << result_type;
1501 }
1502 break;
1503
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 case Primitive::kPrimLong:
1505 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001506 case Primitive::kPrimBoolean:
1507 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001508 case Primitive::kPrimByte:
1509 case Primitive::kPrimShort:
1510 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001511 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001512 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001513 locations->SetInAt(0, Location::RegisterLocation(EAX));
1514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1515 break;
1516
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001517 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001518 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001519 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001520 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001521 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1522 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1523
Vladimir Marko949c91f2015-01-27 10:48:44 +00001524 // The runtime helper puts the result in EAX, EDX.
1525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001526 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001527 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
1533 break;
1534
Roland Levillain981e4542014-11-14 11:47:14 +00001535 case Primitive::kPrimChar:
1536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001537 case Primitive::kPrimBoolean:
1538 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001539 case Primitive::kPrimByte:
1540 case Primitive::kPrimShort:
1541 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001542 // Processing a Dex `int-to-char' instruction.
1543 locations->SetInAt(0, Location::Any());
1544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551 break;
1552
Roland Levillaindff1f282014-11-05 14:15:05 +00001553 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001554 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001555 case Primitive::kPrimBoolean:
1556 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001557 case Primitive::kPrimByte:
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimInt:
1560 case Primitive::kPrimChar:
1561 // Processing a Dex `int-to-float' instruction.
1562 locations->SetInAt(0, Location::RequiresRegister());
1563 locations->SetOut(Location::RequiresFpuRegister());
1564 break;
1565
1566 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001567 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001568 locations->SetInAt(0, Location::Any());
1569 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001570 break;
1571
Roland Levillaincff13742014-11-17 14:32:17 +00001572 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001573 // Processing a Dex `double-to-float' instruction.
1574 locations->SetInAt(0, Location::RequiresFpuRegister());
1575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001576 break;
1577
1578 default:
1579 LOG(FATAL) << "Unexpected type conversion from " << input_type
1580 << " to " << result_type;
1581 };
1582 break;
1583
Roland Levillaindff1f282014-11-05 14:15:05 +00001584 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001586 case Primitive::kPrimBoolean:
1587 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001588 case Primitive::kPrimByte:
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimChar:
1592 // Processing a Dex `int-to-double' instruction.
1593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetOut(Location::RequiresFpuRegister());
1595 break;
1596
1597 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001598 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001599 locations->SetInAt(0, Location::Any());
1600 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001601 break;
1602
Roland Levillaincff13742014-11-17 14:32:17 +00001603 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001604 // Processing a Dex `float-to-double' instruction.
1605 locations->SetInAt(0, Location::RequiresFpuRegister());
1606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001607 break;
1608
1609 default:
1610 LOG(FATAL) << "Unexpected type conversion from " << input_type
1611 << " to " << result_type;
1612 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 }
1619}
1620
1621void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1622 LocationSummary* locations = conversion->GetLocations();
1623 Location out = locations->Out();
1624 Location in = locations->InAt(0);
1625 Primitive::Type result_type = conversion->GetResultType();
1626 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001627 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001628 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001629 case Primitive::kPrimByte:
1630 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001631 case Primitive::kPrimBoolean:
1632 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001633 case Primitive::kPrimShort:
1634 case Primitive::kPrimInt:
1635 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001636 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001637 if (in.IsRegister()) {
1638 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001639 } else {
1640 DCHECK(in.GetConstant()->IsIntConstant());
1641 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1643 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001644 break;
1645
1646 default:
1647 LOG(FATAL) << "Unexpected type conversion from " << input_type
1648 << " to " << result_type;
1649 }
1650 break;
1651
Roland Levillain01a8d712014-11-14 16:27:39 +00001652 case Primitive::kPrimShort:
1653 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001654 case Primitive::kPrimBoolean:
1655 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001656 case Primitive::kPrimByte:
1657 case Primitive::kPrimInt:
1658 case Primitive::kPrimChar:
1659 // Processing a Dex `int-to-short' instruction.
1660 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001662 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001663 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001664 } else {
1665 DCHECK(in.GetConstant()->IsIntConstant());
1666 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001668 }
1669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
1675 break;
1676
Roland Levillain946e1432014-11-11 17:35:19 +00001677 case Primitive::kPrimInt:
1678 switch (input_type) {
1679 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001680 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001681 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001683 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001685 } else {
1686 DCHECK(in.IsConstant());
1687 DCHECK(in.GetConstant()->IsLongConstant());
1688 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001689 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001690 }
1691 break;
1692
Roland Levillain3f8f9362014-12-02 17:45:01 +00001693 case Primitive::kPrimFloat: {
1694 // Processing a Dex `float-to-int' instruction.
1695 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1696 Register output = out.AsRegister<Register>();
1697 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1698 Label done, nan;
1699
1700 __ movl(output, Immediate(kPrimIntMax));
1701 // temp = int-to-float(output)
1702 __ cvtsi2ss(temp, output);
1703 // if input >= temp goto done
1704 __ comiss(input, temp);
1705 __ j(kAboveEqual, &done);
1706 // if input == NaN goto nan
1707 __ j(kUnordered, &nan);
1708 // output = float-to-int-truncate(input)
1709 __ cvttss2si(output, input);
1710 __ jmp(&done);
1711 __ Bind(&nan);
1712 // output = 0
1713 __ xorl(output, output);
1714 __ Bind(&done);
1715 break;
1716 }
1717
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001718 case Primitive::kPrimDouble: {
1719 // Processing a Dex `double-to-int' instruction.
1720 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1721 Register output = out.AsRegister<Register>();
1722 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1723 Label done, nan;
1724
1725 __ movl(output, Immediate(kPrimIntMax));
1726 // temp = int-to-double(output)
1727 __ cvtsi2sd(temp, output);
1728 // if input >= temp goto done
1729 __ comisd(input, temp);
1730 __ j(kAboveEqual, &done);
1731 // if input == NaN goto nan
1732 __ j(kUnordered, &nan);
1733 // output = double-to-int-truncate(input)
1734 __ cvttsd2si(output, input);
1735 __ jmp(&done);
1736 __ Bind(&nan);
1737 // output = 0
1738 __ xorl(output, output);
1739 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001740 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001741 }
Roland Levillain946e1432014-11-11 17:35:19 +00001742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 case Primitive::kPrimLong:
1750 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001751 case Primitive::kPrimBoolean:
1752 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001753 case Primitive::kPrimByte:
1754 case Primitive::kPrimShort:
1755 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001756 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001757 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1759 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 __ cdq();
1762 break;
1763
1764 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001765 // Processing a Dex `float-to-long' instruction.
1766 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001767 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1768 break;
1769
Roland Levillaindff1f282014-11-05 14:15:05 +00001770 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001771 // Processing a Dex `double-to-long' instruction.
1772 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1773 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001774 break;
1775
1776 default:
1777 LOG(FATAL) << "Unexpected type conversion from " << input_type
1778 << " to " << result_type;
1779 }
1780 break;
1781
Roland Levillain981e4542014-11-14 11:47:14 +00001782 case Primitive::kPrimChar:
1783 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001784 case Primitive::kPrimBoolean:
1785 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001786 case Primitive::kPrimByte:
1787 case Primitive::kPrimShort:
1788 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001789 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1790 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001792 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001794 } else {
1795 DCHECK(in.GetConstant()->IsIntConstant());
1796 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001797 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001798 }
1799 break;
1800
1801 default:
1802 LOG(FATAL) << "Unexpected type conversion from " << input_type
1803 << " to " << result_type;
1804 }
1805 break;
1806
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001808 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001809 case Primitive::kPrimBoolean:
1810 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001811 case Primitive::kPrimByte:
1812 case Primitive::kPrimShort:
1813 case Primitive::kPrimInt:
1814 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001815 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001816 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001817 break;
1818
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819 case Primitive::kPrimLong: {
1820 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001821 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822
Roland Levillain232ade02015-04-20 15:14:36 +01001823 // Create stack space for the call to
1824 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1825 // TODO: enhance register allocator to ask for stack temporaries.
1826 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1827 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1828 __ subl(ESP, Immediate(adjustment));
1829 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001830
Roland Levillain232ade02015-04-20 15:14:36 +01001831 // Load the value to the FP stack, using temporaries if needed.
1832 PushOntoFPStack(in, 0, adjustment, false, true);
1833
1834 if (out.IsStackSlot()) {
1835 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1836 } else {
1837 __ fstps(Address(ESP, 0));
1838 Location stack_temp = Location::StackSlot(0);
1839 codegen_->Move32(out, stack_temp);
1840 }
1841
1842 // Remove the temporary stack space we allocated.
1843 if (adjustment != 0) {
1844 __ addl(ESP, Immediate(adjustment));
1845 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001846 break;
1847 }
1848
Roland Levillaincff13742014-11-17 14:32:17 +00001849 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001850 // Processing a Dex `double-to-float' instruction.
1851 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001852 break;
1853
1854 default:
1855 LOG(FATAL) << "Unexpected type conversion from " << input_type
1856 << " to " << result_type;
1857 };
1858 break;
1859
Roland Levillaindff1f282014-11-05 14:15:05 +00001860 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001861 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001862 case Primitive::kPrimBoolean:
1863 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001864 case Primitive::kPrimByte:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001868 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001869 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001870 break;
1871
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872 case Primitive::kPrimLong: {
1873 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001874 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001875
Roland Levillain232ade02015-04-20 15:14:36 +01001876 // Create stack space for the call to
1877 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1878 // TODO: enhance register allocator to ask for stack temporaries.
1879 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1880 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1881 __ subl(ESP, Immediate(adjustment));
1882 }
1883
1884 // Load the value to the FP stack, using temporaries if needed.
1885 PushOntoFPStack(in, 0, adjustment, false, true);
1886
1887 if (out.IsDoubleStackSlot()) {
1888 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1889 } else {
1890 __ fstpl(Address(ESP, 0));
1891 Location stack_temp = Location::DoubleStackSlot(0);
1892 codegen_->Move64(out, stack_temp);
1893 }
1894
1895 // Remove the temporary stack space we allocated.
1896 if (adjustment != 0) {
1897 __ addl(ESP, Immediate(adjustment));
1898 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001899 break;
1900 }
1901
Roland Levillaincff13742014-11-17 14:32:17 +00001902 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001903 // Processing a Dex `float-to-double' instruction.
1904 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001905 break;
1906
1907 default:
1908 LOG(FATAL) << "Unexpected type conversion from " << input_type
1909 << " to " << result_type;
1910 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001911 break;
1912
1913 default:
1914 LOG(FATAL) << "Unexpected type conversion from " << input_type
1915 << " to " << result_type;
1916 }
1917}
1918
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001919void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001922 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001923 case Primitive::kPrimInt: {
1924 locations->SetInAt(0, Location::RequiresRegister());
1925 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1927 break;
1928 }
1929
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetInAt(1, Location::Any());
1933 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 break;
1935 }
1936
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 case Primitive::kPrimFloat:
1938 case Primitive::kPrimDouble: {
1939 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001940 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001946 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1947 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949}
1950
1951void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1952 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 Location first = locations->InAt(0);
1954 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001955 Location out = locations->Out();
1956
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001957 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001960 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1961 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1962 } else {
1963 __ leal(out.AsRegister<Register>(), Address(
1964 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1965 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001967 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1968 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1969 __ addl(out.AsRegister<Register>(), Immediate(value));
1970 } else {
1971 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1972 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001973 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001974 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001975 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001976 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001977 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001978 }
1979
1980 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001981 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1983 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001984 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001985 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1986 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001988 } else {
1989 DCHECK(second.IsConstant()) << second;
1990 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1991 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1992 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001993 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001994 break;
1995 }
1996
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 case Primitive::kPrimFloat: {
1998 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 }
2003
2004 case Primitive::kPrimDouble: {
2005 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002006 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002007 }
2008 break;
2009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002011 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002012 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 }
2014}
2015
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002016void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 LocationSummary* locations =
2018 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::Any());
2024 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025 break;
2026 }
Calin Juravle11351682014-10-23 15:38:15 +01002027 case Primitive::kPrimFloat:
2028 case Primitive::kPrimDouble: {
2029 locations->SetInAt(0, Location::RequiresFpuRegister());
2030 locations->SetInAt(1, Location::RequiresFpuRegister());
2031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032 break;
Calin Juravle11351682014-10-23 15:38:15 +01002033 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035 default:
Calin Juravle11351682014-10-23 15:38:15 +01002036 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038}
2039
2040void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2041 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002042 Location first = locations->InAt(0);
2043 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002044 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002050 __ subl(first.AsRegister<Register>(),
2051 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002052 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002054 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002055 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002056 }
2057
2058 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002059 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002060 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2061 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002062 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002063 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002064 __ sbbl(first.AsRegisterPairHigh<Register>(),
2065 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002066 } else {
2067 DCHECK(second.IsConstant()) << second;
2068 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2069 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2070 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002071 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072 break;
2073 }
2074
Calin Juravle11351682014-10-23 15:38:15 +01002075 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 break;
Calin Juravle11351682014-10-23 15:38:15 +01002078 }
2079
2080 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002082 break;
2083 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002085 default:
Calin Juravle11351682014-10-23 15:38:15 +01002086 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002087 }
2088}
2089
Calin Juravle34bacdf2014-10-07 20:23:36 +01002090void LocationsBuilderX86::VisitMul(HMul* mul) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2093 switch (mul->GetResultType()) {
2094 case Primitive::kPrimInt:
2095 locations->SetInAt(0, Location::RequiresRegister());
2096 locations->SetInAt(1, Location::Any());
2097 locations->SetOut(Location::SameAsFirstInput());
2098 break;
2099 case Primitive::kPrimLong: {
2100 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002101 locations->SetInAt(1, Location::Any());
2102 locations->SetOut(Location::SameAsFirstInput());
2103 // Needed for imul on 32bits with 64bits output.
2104 locations->AddTemp(Location::RegisterLocation(EAX));
2105 locations->AddTemp(Location::RegisterLocation(EDX));
2106 break;
2107 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002108 case Primitive::kPrimFloat:
2109 case Primitive::kPrimDouble: {
2110 locations->SetInAt(0, Location::RequiresFpuRegister());
2111 locations->SetInAt(1, Location::RequiresFpuRegister());
2112 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002113 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002114 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115
2116 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002117 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118 }
2119}
2120
2121void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2122 LocationSummary* locations = mul->GetLocations();
2123 Location first = locations->InAt(0);
2124 Location second = locations->InAt(1);
2125 DCHECK(first.Equals(locations->Out()));
2126
2127 switch (mul->GetResultType()) {
2128 case Primitive::kPrimInt: {
2129 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 } else if (second.IsConstant()) {
2132 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 } else {
2135 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002137 }
2138 break;
2139 }
2140
2141 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002142 Register in1_hi = first.AsRegisterPairHigh<Register>();
2143 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 Register eax = locations->GetTemp(0).AsRegister<Register>();
2145 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002146
2147 DCHECK_EQ(EAX, eax);
2148 DCHECK_EQ(EDX, edx);
2149
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002150 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002151 // output: in1
2152 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2153 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2154 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002155 if (second.IsConstant()) {
2156 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002157
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002158 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2159 int32_t low_value = Low32Bits(value);
2160 int32_t high_value = High32Bits(value);
2161 Immediate low(low_value);
2162 Immediate high(high_value);
2163
2164 __ movl(eax, high);
2165 // eax <- in1.lo * in2.hi
2166 __ imull(eax, in1_lo);
2167 // in1.hi <- in1.hi * in2.lo
2168 __ imull(in1_hi, low);
2169 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2170 __ addl(in1_hi, eax);
2171 // move in2_lo to eax to prepare for double precision
2172 __ movl(eax, low);
2173 // edx:eax <- in1.lo * in2.lo
2174 __ mull(in1_lo);
2175 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2176 __ addl(in1_hi, edx);
2177 // in1.lo <- (in1.lo * in2.lo)[31:0];
2178 __ movl(in1_lo, eax);
2179 } else if (second.IsRegisterPair()) {
2180 Register in2_hi = second.AsRegisterPairHigh<Register>();
2181 Register in2_lo = second.AsRegisterPairLow<Register>();
2182
2183 __ movl(eax, in2_hi);
2184 // eax <- in1.lo * in2.hi
2185 __ imull(eax, in1_lo);
2186 // in1.hi <- in1.hi * in2.lo
2187 __ imull(in1_hi, in2_lo);
2188 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2189 __ addl(in1_hi, eax);
2190 // move in1_lo to eax to prepare for double precision
2191 __ movl(eax, in1_lo);
2192 // edx:eax <- in1.lo * in2.lo
2193 __ mull(in2_lo);
2194 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2195 __ addl(in1_hi, edx);
2196 // in1.lo <- (in1.lo * in2.lo)[31:0];
2197 __ movl(in1_lo, eax);
2198 } else {
2199 DCHECK(second.IsDoubleStackSlot()) << second;
2200 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2201 Address in2_lo(ESP, second.GetStackIndex());
2202
2203 __ movl(eax, in2_hi);
2204 // eax <- in1.lo * in2.hi
2205 __ imull(eax, in1_lo);
2206 // in1.hi <- in1.hi * in2.lo
2207 __ imull(in1_hi, in2_lo);
2208 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2209 __ addl(in1_hi, eax);
2210 // move in1_lo to eax to prepare for double precision
2211 __ movl(eax, in1_lo);
2212 // edx:eax <- in1.lo * in2.lo
2213 __ mull(in2_lo);
2214 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2215 __ addl(in1_hi, edx);
2216 // in1.lo <- (in1.lo * in2.lo)[31:0];
2217 __ movl(in1_lo, eax);
2218 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002219
2220 break;
2221 }
2222
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002224 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002225 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002226 }
2227
2228 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002230 break;
2231 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002232
2233 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002234 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002235 }
2236}
2237
Roland Levillain232ade02015-04-20 15:14:36 +01002238void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2239 uint32_t temp_offset,
2240 uint32_t stack_adjustment,
2241 bool is_fp,
2242 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002243 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002244 DCHECK(!is_wide);
2245 if (is_fp) {
2246 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2247 } else {
2248 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2249 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002250 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002251 DCHECK(is_wide);
2252 if (is_fp) {
2253 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2254 } else {
2255 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2256 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002257 } else {
2258 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002259 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002260 Location stack_temp = Location::StackSlot(temp_offset);
2261 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002262 if (is_fp) {
2263 __ flds(Address(ESP, temp_offset));
2264 } else {
2265 __ filds(Address(ESP, temp_offset));
2266 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002267 } else {
2268 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2269 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002270 if (is_fp) {
2271 __ fldl(Address(ESP, temp_offset));
2272 } else {
2273 __ fildl(Address(ESP, temp_offset));
2274 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002275 }
2276 }
2277}
2278
2279void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2280 Primitive::Type type = rem->GetResultType();
2281 bool is_float = type == Primitive::kPrimFloat;
2282 size_t elem_size = Primitive::ComponentSize(type);
2283 LocationSummary* locations = rem->GetLocations();
2284 Location first = locations->InAt(0);
2285 Location second = locations->InAt(1);
2286 Location out = locations->Out();
2287
2288 // Create stack space for 2 elements.
2289 // TODO: enhance register allocator to ask for stack temporaries.
2290 __ subl(ESP, Immediate(2 * elem_size));
2291
2292 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002293 const bool is_wide = !is_float;
2294 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2295 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002296
2297 // Loop doing FPREM until we stabilize.
2298 Label retry;
2299 __ Bind(&retry);
2300 __ fprem();
2301
2302 // Move FP status to AX.
2303 __ fstsw();
2304
2305 // And see if the argument reduction is complete. This is signaled by the
2306 // C2 FPU flag bit set to 0.
2307 __ andl(EAX, Immediate(kC2ConditionMask));
2308 __ j(kNotEqual, &retry);
2309
2310 // We have settled on the final value. Retrieve it into an XMM register.
2311 // Store FP top of stack to real stack.
2312 if (is_float) {
2313 __ fsts(Address(ESP, 0));
2314 } else {
2315 __ fstl(Address(ESP, 0));
2316 }
2317
2318 // Pop the 2 items from the FP stack.
2319 __ fucompp();
2320
2321 // Load the value from the stack into an XMM register.
2322 DCHECK(out.IsFpuRegister()) << out;
2323 if (is_float) {
2324 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2325 } else {
2326 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2327 }
2328
2329 // And remove the temporary stack space we allocated.
2330 __ addl(ESP, Immediate(2 * elem_size));
2331}
2332
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002333
2334void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2335 DCHECK(instruction->IsDiv() || instruction->IsRem());
2336
2337 LocationSummary* locations = instruction->GetLocations();
2338 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002339 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002340
2341 Register out_register = locations->Out().AsRegister<Register>();
2342 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002343 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002344
2345 DCHECK(imm == 1 || imm == -1);
2346
2347 if (instruction->IsRem()) {
2348 __ xorl(out_register, out_register);
2349 } else {
2350 __ movl(out_register, input_register);
2351 if (imm == -1) {
2352 __ negl(out_register);
2353 }
2354 }
2355}
2356
2357
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002358void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002359 LocationSummary* locations = instruction->GetLocations();
2360
2361 Register out_register = locations->Out().AsRegister<Register>();
2362 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002363 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002364
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002365 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002366 Register num = locations->GetTemp(0).AsRegister<Register>();
2367
2368 __ leal(num, Address(input_register, std::abs(imm) - 1));
2369 __ testl(input_register, input_register);
2370 __ cmovl(kGreaterEqual, num, input_register);
2371 int shift = CTZ(imm);
2372 __ sarl(num, Immediate(shift));
2373
2374 if (imm < 0) {
2375 __ negl(num);
2376 }
2377
2378 __ movl(out_register, num);
2379}
2380
2381void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2382 DCHECK(instruction->IsDiv() || instruction->IsRem());
2383
2384 LocationSummary* locations = instruction->GetLocations();
2385 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2386
2387 Register eax = locations->InAt(0).AsRegister<Register>();
2388 Register out = locations->Out().AsRegister<Register>();
2389 Register num;
2390 Register edx;
2391
2392 if (instruction->IsDiv()) {
2393 edx = locations->GetTemp(0).AsRegister<Register>();
2394 num = locations->GetTemp(1).AsRegister<Register>();
2395 } else {
2396 edx = locations->Out().AsRegister<Register>();
2397 num = locations->GetTemp(0).AsRegister<Register>();
2398 }
2399
2400 DCHECK_EQ(EAX, eax);
2401 DCHECK_EQ(EDX, edx);
2402 if (instruction->IsDiv()) {
2403 DCHECK_EQ(EAX, out);
2404 } else {
2405 DCHECK_EQ(EDX, out);
2406 }
2407
2408 int64_t magic;
2409 int shift;
2410 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2411
2412 Label ndiv;
2413 Label end;
2414 // If numerator is 0, the result is 0, no computation needed.
2415 __ testl(eax, eax);
2416 __ j(kNotEqual, &ndiv);
2417
2418 __ xorl(out, out);
2419 __ jmp(&end);
2420
2421 __ Bind(&ndiv);
2422
2423 // Save the numerator.
2424 __ movl(num, eax);
2425
2426 // EAX = magic
2427 __ movl(eax, Immediate(magic));
2428
2429 // EDX:EAX = magic * numerator
2430 __ imull(num);
2431
2432 if (imm > 0 && magic < 0) {
2433 // EDX += num
2434 __ addl(edx, num);
2435 } else if (imm < 0 && magic > 0) {
2436 __ subl(edx, num);
2437 }
2438
2439 // Shift if needed.
2440 if (shift != 0) {
2441 __ sarl(edx, Immediate(shift));
2442 }
2443
2444 // EDX += 1 if EDX < 0
2445 __ movl(eax, edx);
2446 __ shrl(edx, Immediate(31));
2447 __ addl(edx, eax);
2448
2449 if (instruction->IsRem()) {
2450 __ movl(eax, num);
2451 __ imull(edx, Immediate(imm));
2452 __ subl(eax, edx);
2453 __ movl(edx, eax);
2454 } else {
2455 __ movl(eax, edx);
2456 }
2457 __ Bind(&end);
2458}
2459
Calin Juravlebacfec32014-11-14 15:54:36 +00002460void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2461 DCHECK(instruction->IsDiv() || instruction->IsRem());
2462
2463 LocationSummary* locations = instruction->GetLocations();
2464 Location out = locations->Out();
2465 Location first = locations->InAt(0);
2466 Location second = locations->InAt(1);
2467 bool is_div = instruction->IsDiv();
2468
2469 switch (instruction->GetResultType()) {
2470 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 DCHECK_EQ(EAX, first.AsRegister<Register>());
2472 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002473
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002474 if (instruction->InputAt(1)->IsIntConstant()) {
2475 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002476
2477 if (imm == 0) {
2478 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2479 } else if (imm == 1 || imm == -1) {
2480 DivRemOneOrMinusOne(instruction);
2481 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002482 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002483 } else {
2484 DCHECK(imm <= -2 || imm >= 2);
2485 GenerateDivRemWithAnyConstant(instruction);
2486 }
2487 } else {
2488 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002489 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002490 is_div);
2491 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002492
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002493 Register second_reg = second.AsRegister<Register>();
2494 // 0x80000000/-1 triggers an arithmetic exception!
2495 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2496 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 __ cmpl(second_reg, Immediate(-1));
2499 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501 // edx:eax <- sign-extended of eax
2502 __ cdq();
2503 // eax = quotient, edx = remainder
2504 __ idivl(second_reg);
2505 __ Bind(slow_path->GetExitLabel());
2506 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002507 break;
2508 }
2509
2510 case Primitive::kPrimLong: {
2511 InvokeRuntimeCallingConvention calling_convention;
2512 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2513 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2514 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2516 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2517 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2518
2519 if (is_div) {
2520 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2521 } else {
2522 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2523 }
2524 uint32_t dex_pc = is_div
2525 ? instruction->AsDiv()->GetDexPc()
2526 : instruction->AsRem()->GetDexPc();
2527 codegen_->RecordPcInfo(instruction, dex_pc);
2528
2529 break;
2530 }
2531
2532 default:
2533 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2534 }
2535}
2536
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002538 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002539 ? LocationSummary::kCall
2540 : LocationSummary::kNoCall;
2541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2542
Calin Juravle7c4954d2014-10-28 16:57:40 +00002543 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002544 case Primitive::kPrimInt: {
2545 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002546 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002547 locations->SetOut(Location::SameAsFirstInput());
2548 // Intel uses edx:eax as the dividend.
2549 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002550 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2551 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2552 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002553 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002554 locations->AddTemp(Location::RequiresRegister());
2555 }
Calin Juravled0d48522014-11-04 16:40:20 +00002556 break;
2557 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002558 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002559 InvokeRuntimeCallingConvention calling_convention;
2560 locations->SetInAt(0, Location::RegisterPairLocation(
2561 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2562 locations->SetInAt(1, Location::RegisterPairLocation(
2563 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2564 // Runtime helper puts the result in EAX, EDX.
2565 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002566 break;
2567 }
2568 case Primitive::kPrimFloat:
2569 case Primitive::kPrimDouble: {
2570 locations->SetInAt(0, Location::RequiresFpuRegister());
2571 locations->SetInAt(1, Location::RequiresFpuRegister());
2572 locations->SetOut(Location::SameAsFirstInput());
2573 break;
2574 }
2575
2576 default:
2577 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2578 }
2579}
2580
2581void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2582 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002583 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 Location first = locations->InAt(0);
2585 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002586
2587 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002588 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002590 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002591 break;
2592 }
2593
2594 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002595 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002597 break;
2598 }
2599
2600 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002601 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002603 break;
2604 }
2605
2606 default:
2607 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2608 }
2609}
2610
Calin Juravlebacfec32014-11-14 15:54:36 +00002611void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002612 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002613
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002614 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2615 ? LocationSummary::kCall
2616 : LocationSummary::kNoCall;
2617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002618
Calin Juravled2ec87d2014-12-08 14:24:46 +00002619 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002620 case Primitive::kPrimInt: {
2621 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002622 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002624 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2625 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2626 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002627 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002628 locations->AddTemp(Location::RequiresRegister());
2629 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002630 break;
2631 }
2632 case Primitive::kPrimLong: {
2633 InvokeRuntimeCallingConvention calling_convention;
2634 locations->SetInAt(0, Location::RegisterPairLocation(
2635 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2636 locations->SetInAt(1, Location::RegisterPairLocation(
2637 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2638 // Runtime helper puts the result in EAX, EDX.
2639 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2640 break;
2641 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002642 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002643 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002644 locations->SetInAt(0, Location::Any());
2645 locations->SetInAt(1, Location::Any());
2646 locations->SetOut(Location::RequiresFpuRegister());
2647 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002648 break;
2649 }
2650
2651 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002652 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002653 }
2654}
2655
2656void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2657 Primitive::Type type = rem->GetResultType();
2658 switch (type) {
2659 case Primitive::kPrimInt:
2660 case Primitive::kPrimLong: {
2661 GenerateDivRemIntegral(rem);
2662 break;
2663 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002664 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002665 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002666 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002667 break;
2668 }
2669 default:
2670 LOG(FATAL) << "Unexpected rem type " << type;
2671 }
2672}
2673
Calin Juravled0d48522014-11-04 16:40:20 +00002674void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002677 switch (instruction->GetType()) {
2678 case Primitive::kPrimInt: {
2679 locations->SetInAt(0, Location::Any());
2680 break;
2681 }
2682 case Primitive::kPrimLong: {
2683 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2684 if (!instruction->IsConstant()) {
2685 locations->AddTemp(Location::RequiresRegister());
2686 }
2687 break;
2688 }
2689 default:
2690 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2691 }
Calin Juravled0d48522014-11-04 16:40:20 +00002692 if (instruction->HasUses()) {
2693 locations->SetOut(Location::SameAsFirstInput());
2694 }
2695}
2696
2697void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2698 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2699 codegen_->AddSlowPath(slow_path);
2700
2701 LocationSummary* locations = instruction->GetLocations();
2702 Location value = locations->InAt(0);
2703
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002704 switch (instruction->GetType()) {
2705 case Primitive::kPrimInt: {
2706 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002708 __ j(kEqual, slow_path->GetEntryLabel());
2709 } else if (value.IsStackSlot()) {
2710 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2711 __ j(kEqual, slow_path->GetEntryLabel());
2712 } else {
2713 DCHECK(value.IsConstant()) << value;
2714 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2715 __ jmp(slow_path->GetEntryLabel());
2716 }
2717 }
2718 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002719 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002720 case Primitive::kPrimLong: {
2721 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002722 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002723 __ movl(temp, value.AsRegisterPairLow<Register>());
2724 __ orl(temp, value.AsRegisterPairHigh<Register>());
2725 __ j(kEqual, slow_path->GetEntryLabel());
2726 } else {
2727 DCHECK(value.IsConstant()) << value;
2728 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2729 __ jmp(slow_path->GetEntryLabel());
2730 }
2731 }
2732 break;
2733 }
2734 default:
2735 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002736 }
Calin Juravled0d48522014-11-04 16:40:20 +00002737}
2738
Calin Juravle9aec02f2014-11-18 23:06:35 +00002739void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2740 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2741
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2744
2745 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002746 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002747 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002748 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002749 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002750 // The shift count needs to be in CL or a constant.
2751 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002752 locations->SetOut(Location::SameAsFirstInput());
2753 break;
2754 }
2755 default:
2756 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2757 }
2758}
2759
2760void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2761 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2762
2763 LocationSummary* locations = op->GetLocations();
2764 Location first = locations->InAt(0);
2765 Location second = locations->InAt(1);
2766 DCHECK(first.Equals(locations->Out()));
2767
2768 switch (op->GetResultType()) {
2769 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002770 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002771 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 DCHECK_EQ(ECX, second_reg);
2775 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002778 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002780 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002781 }
2782 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002783 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2784 if (shift == 0) {
2785 return;
2786 }
2787 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002788 if (op->IsShl()) {
2789 __ shll(first_reg, imm);
2790 } else if (op->IsShr()) {
2791 __ sarl(first_reg, imm);
2792 } else {
2793 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002794 }
2795 }
2796 break;
2797 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002798 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002799 if (second.IsRegister()) {
2800 Register second_reg = second.AsRegister<Register>();
2801 DCHECK_EQ(ECX, second_reg);
2802 if (op->IsShl()) {
2803 GenerateShlLong(first, second_reg);
2804 } else if (op->IsShr()) {
2805 GenerateShrLong(first, second_reg);
2806 } else {
2807 GenerateUShrLong(first, second_reg);
2808 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002809 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002810 // Shift by a constant.
2811 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2812 // Nothing to do if the shift is 0, as the input is already the output.
2813 if (shift != 0) {
2814 if (op->IsShl()) {
2815 GenerateShlLong(first, shift);
2816 } else if (op->IsShr()) {
2817 GenerateShrLong(first, shift);
2818 } else {
2819 GenerateUShrLong(first, shift);
2820 }
2821 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002822 }
2823 break;
2824 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002825 default:
2826 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2827 }
2828}
2829
Mark P Mendell73945692015-04-29 14:56:17 +00002830void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2831 Register low = loc.AsRegisterPairLow<Register>();
2832 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002833 if (shift == 1) {
2834 // This is just an addition.
2835 __ addl(low, low);
2836 __ adcl(high, high);
2837 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002838 // Shift by 32 is easy. High gets low, and low gets 0.
2839 codegen_->EmitParallelMoves(
2840 loc.ToLow(),
2841 loc.ToHigh(),
2842 Primitive::kPrimInt,
2843 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2844 loc.ToLow(),
2845 Primitive::kPrimInt);
2846 } else if (shift > 32) {
2847 // Low part becomes 0. High part is low part << (shift-32).
2848 __ movl(high, low);
2849 __ shll(high, Immediate(shift - 32));
2850 __ xorl(low, low);
2851 } else {
2852 // Between 1 and 31.
2853 __ shld(high, low, Immediate(shift));
2854 __ shll(low, Immediate(shift));
2855 }
2856}
2857
Calin Juravle9aec02f2014-11-18 23:06:35 +00002858void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2859 Label done;
2860 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2861 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2862 __ testl(shifter, Immediate(32));
2863 __ j(kEqual, &done);
2864 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2865 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2866 __ Bind(&done);
2867}
2868
Mark P Mendell73945692015-04-29 14:56:17 +00002869void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2870 Register low = loc.AsRegisterPairLow<Register>();
2871 Register high = loc.AsRegisterPairHigh<Register>();
2872 if (shift == 32) {
2873 // Need to copy the sign.
2874 DCHECK_NE(low, high);
2875 __ movl(low, high);
2876 __ sarl(high, Immediate(31));
2877 } else if (shift > 32) {
2878 DCHECK_NE(low, high);
2879 // High part becomes sign. Low part is shifted by shift - 32.
2880 __ movl(low, high);
2881 __ sarl(high, Immediate(31));
2882 __ sarl(low, Immediate(shift - 32));
2883 } else {
2884 // Between 1 and 31.
2885 __ shrd(low, high, Immediate(shift));
2886 __ sarl(high, Immediate(shift));
2887 }
2888}
2889
Calin Juravle9aec02f2014-11-18 23:06:35 +00002890void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2891 Label done;
2892 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2893 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2894 __ testl(shifter, Immediate(32));
2895 __ j(kEqual, &done);
2896 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2897 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2898 __ Bind(&done);
2899}
2900
Mark P Mendell73945692015-04-29 14:56:17 +00002901void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2902 Register low = loc.AsRegisterPairLow<Register>();
2903 Register high = loc.AsRegisterPairHigh<Register>();
2904 if (shift == 32) {
2905 // Shift by 32 is easy. Low gets high, and high gets 0.
2906 codegen_->EmitParallelMoves(
2907 loc.ToHigh(),
2908 loc.ToLow(),
2909 Primitive::kPrimInt,
2910 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2911 loc.ToHigh(),
2912 Primitive::kPrimInt);
2913 } else if (shift > 32) {
2914 // Low part is high >> (shift - 32). High part becomes 0.
2915 __ movl(low, high);
2916 __ shrl(low, Immediate(shift - 32));
2917 __ xorl(high, high);
2918 } else {
2919 // Between 1 and 31.
2920 __ shrd(low, high, Immediate(shift));
2921 __ shrl(high, Immediate(shift));
2922 }
2923}
2924
Calin Juravle9aec02f2014-11-18 23:06:35 +00002925void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2926 Label done;
2927 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2928 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2929 __ testl(shifter, Immediate(32));
2930 __ j(kEqual, &done);
2931 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2932 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2933 __ Bind(&done);
2934}
2935
2936void LocationsBuilderX86::VisitShl(HShl* shl) {
2937 HandleShift(shl);
2938}
2939
2940void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2941 HandleShift(shl);
2942}
2943
2944void LocationsBuilderX86::VisitShr(HShr* shr) {
2945 HandleShift(shr);
2946}
2947
2948void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2949 HandleShift(shr);
2950}
2951
2952void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2953 HandleShift(ushr);
2954}
2955
2956void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2957 HandleShift(ushr);
2958}
2959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002960void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002963 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002964 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002965 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2966 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002967}
2968
2969void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2970 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002971 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002972 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002973
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002974 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002975
Nicolas Geoffray39468442014-09-02 15:17:15 +01002976 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002977 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002978}
2979
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002980void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2981 LocationSummary* locations =
2982 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2983 locations->SetOut(Location::RegisterLocation(EAX));
2984 InvokeRuntimeCallingConvention calling_convention;
2985 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002986 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2987 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002988}
2989
2990void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2991 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002992 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002993 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2994
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002995 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002996
2997 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2998 DCHECK(!codegen_->IsLeafMethod());
2999}
3000
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003001void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003002 LocationSummary* locations =
3003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003004 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3005 if (location.IsStackSlot()) {
3006 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3007 } else if (location.IsDoubleStackSlot()) {
3008 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003009 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003010 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003011}
3012
3013void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003014 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003015}
3016
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003017void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003018 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003019 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 locations->SetInAt(0, Location::RequiresRegister());
3021 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003022}
3023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003024void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3025 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003026 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003027 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003028 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003029 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003030 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003032 break;
3033
3034 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003035 __ notl(out.AsRegisterPairLow<Register>());
3036 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003037 break;
3038
3039 default:
3040 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3041 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003042}
3043
David Brazdil66d126e2015-04-03 16:02:44 +01003044void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3045 LocationSummary* locations =
3046 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3047 locations->SetInAt(0, Location::RequiresRegister());
3048 locations->SetOut(Location::SameAsFirstInput());
3049}
3050
3051void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003052 LocationSummary* locations = bool_not->GetLocations();
3053 Location in = locations->InAt(0);
3054 Location out = locations->Out();
3055 DCHECK(in.Equals(out));
3056 __ xorl(out.AsRegister<Register>(), Immediate(1));
3057}
3058
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003059void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003060 LocationSummary* locations =
3061 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003062 switch (compare->InputAt(0)->GetType()) {
3063 case Primitive::kPrimLong: {
3064 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003065 locations->SetInAt(1, Location::Any());
3066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3067 break;
3068 }
3069 case Primitive::kPrimFloat:
3070 case Primitive::kPrimDouble: {
3071 locations->SetInAt(0, Location::RequiresFpuRegister());
3072 locations->SetInAt(1, Location::RequiresFpuRegister());
3073 locations->SetOut(Location::RequiresRegister());
3074 break;
3075 }
3076 default:
3077 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3078 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003079}
3080
3081void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003082 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003083 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003084 Location left = locations->InAt(0);
3085 Location right = locations->InAt(1);
3086
3087 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003088 switch (compare->InputAt(0)->GetType()) {
3089 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003090 Register left_low = left.AsRegisterPairLow<Register>();
3091 Register left_high = left.AsRegisterPairHigh<Register>();
3092 int32_t val_low = 0;
3093 int32_t val_high = 0;
3094 bool right_is_const = false;
3095
3096 if (right.IsConstant()) {
3097 DCHECK(right.GetConstant()->IsLongConstant());
3098 right_is_const = true;
3099 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3100 val_low = Low32Bits(val);
3101 val_high = High32Bits(val);
3102 }
3103
Calin Juravleddb7df22014-11-25 20:56:51 +00003104 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003105 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003106 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003107 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003108 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003109 DCHECK(right_is_const) << right;
3110 if (val_high == 0) {
3111 __ testl(left_high, left_high);
3112 } else {
3113 __ cmpl(left_high, Immediate(val_high));
3114 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003115 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003116 __ j(kLess, &less); // Signed compare.
3117 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003118 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003119 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003120 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003121 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003122 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003123 DCHECK(right_is_const) << right;
3124 if (val_low == 0) {
3125 __ testl(left_low, left_low);
3126 } else {
3127 __ cmpl(left_low, Immediate(val_low));
3128 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003130 break;
3131 }
3132 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003134 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3135 break;
3136 }
3137 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003138 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003139 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003140 break;
3141 }
3142 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003143 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003144 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003145 __ movl(out, Immediate(0));
3146 __ j(kEqual, &done);
3147 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3148
3149 __ Bind(&greater);
3150 __ movl(out, Immediate(1));
3151 __ jmp(&done);
3152
3153 __ Bind(&less);
3154 __ movl(out, Immediate(-1));
3155
3156 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003157}
3158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003159void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003160 LocationSummary* locations =
3161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003162 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3163 locations->SetInAt(i, Location::Any());
3164 }
3165 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003166}
3167
3168void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003169 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003170 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003171}
3172
Calin Juravle52c48962014-12-16 17:02:57 +00003173void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3174 /*
3175 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3176 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3177 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3178 */
3179 switch (kind) {
3180 case MemBarrierKind::kAnyAny: {
3181 __ mfence();
3182 break;
3183 }
3184 case MemBarrierKind::kAnyStore:
3185 case MemBarrierKind::kLoadAny:
3186 case MemBarrierKind::kStoreStore: {
3187 // nop
3188 break;
3189 }
3190 default:
3191 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003192 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003193}
3194
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003195
Mark Mendell09ed1a32015-03-25 08:30:06 -04003196void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3197 Register temp) {
3198 // TODO: Implement all kinds of calls:
3199 // 1) boot -> boot
3200 // 2) app -> boot
3201 // 3) app -> app
3202 //
3203 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003204
3205 if (invoke->IsStringInit()) {
3206 // temp = thread->string_init_entrypoint
3207 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003208 // (temp + offset_of_quick_compiled_code)()
3209 __ call(Address(
3210 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3211 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003212 // temp = method;
3213 LoadCurrentMethod(temp);
3214 if (!invoke->IsRecursive()) {
3215 // temp = temp->dex_cache_resolved_methods_;
3216 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3217 // temp = temp[index_in_cache]
3218 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3219 // (temp + offset_of_quick_compiled_code)()
3220 __ call(Address(temp,
3221 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3222 } else {
3223 __ call(GetFrameEntryLabel());
3224 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003225 }
3226
3227 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003228}
3229
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003230void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003231 Label is_null;
3232 __ testl(value, value);
3233 __ j(kEqual, &is_null);
3234 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3235 __ movl(temp, object);
3236 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003237 __ movb(Address(temp, card, TIMES_1, 0),
3238 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 __ Bind(&is_null);
3240}
3241
Calin Juravle52c48962014-12-16 17:02:57 +00003242void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3243 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003244 LocationSummary* locations =
3245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003246 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003247
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003248 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3249 locations->SetOut(Location::RequiresFpuRegister());
3250 } else {
3251 // The output overlaps in case of long: we don't want the low move to overwrite
3252 // the object's location.
3253 locations->SetOut(Location::RequiresRegister(),
3254 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3255 : Location::kNoOutputOverlap);
3256 }
Calin Juravle52c48962014-12-16 17:02:57 +00003257
3258 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3259 // Long values can be loaded atomically into an XMM using movsd.
3260 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3261 // and then copy the XMM into the output 32bits at a time).
3262 locations->AddTemp(Location::RequiresFpuRegister());
3263 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003264}
3265
Calin Juravle52c48962014-12-16 17:02:57 +00003266void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3267 const FieldInfo& field_info) {
3268 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003269
Calin Juravle52c48962014-12-16 17:02:57 +00003270 LocationSummary* locations = instruction->GetLocations();
3271 Register base = locations->InAt(0).AsRegister<Register>();
3272 Location out = locations->Out();
3273 bool is_volatile = field_info.IsVolatile();
3274 Primitive::Type field_type = field_info.GetFieldType();
3275 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3276
3277 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003279 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280 break;
3281 }
3282
3283 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003284 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003285 break;
3286 }
3287
3288 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003289 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290 break;
3291 }
3292
3293 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003294 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003295 break;
3296 }
3297
3298 case Primitive::kPrimInt:
3299 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003300 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 break;
3302 }
3303
3304 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003305 if (is_volatile) {
3306 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3307 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003309 __ movd(out.AsRegisterPairLow<Register>(), temp);
3310 __ psrlq(temp, Immediate(32));
3311 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3312 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003313 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003314 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003315 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003316 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3317 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003318 break;
3319 }
3320
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003321 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003322 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003323 break;
3324 }
3325
3326 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003327 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003328 break;
3329 }
3330
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003331 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003332 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003333 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003334 }
Calin Juravle52c48962014-12-16 17:02:57 +00003335
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 // Longs are handled in the switch.
3337 if (field_type != Primitive::kPrimLong) {
3338 codegen_->MaybeRecordImplicitNullCheck(instruction);
3339 }
3340
Calin Juravle52c48962014-12-16 17:02:57 +00003341 if (is_volatile) {
3342 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3343 }
3344}
3345
3346void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3347 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3348
3349 LocationSummary* locations =
3350 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3351 locations->SetInAt(0, Location::RequiresRegister());
3352 bool is_volatile = field_info.IsVolatile();
3353 Primitive::Type field_type = field_info.GetFieldType();
3354 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3355 || (field_type == Primitive::kPrimByte);
3356
3357 // The register allocator does not support multiple
3358 // inputs that die at entry with one in a specific register.
3359 if (is_byte_type) {
3360 // Ensure the value is in a byte register.
3361 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003362 } else if (Primitive::IsFloatingPointType(field_type)) {
3363 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003364 } else {
3365 locations->SetInAt(1, Location::RequiresRegister());
3366 }
3367 // Temporary registers for the write barrier.
3368 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3369 locations->AddTemp(Location::RequiresRegister());
3370 // Ensure the card is in a byte register.
3371 locations->AddTemp(Location::RegisterLocation(ECX));
3372 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3373 // 64bits value can be atomically written to an address with movsd and an XMM register.
3374 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3375 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3376 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3377 // isolated cases when we need this it isn't worth adding the extra complexity.
3378 locations->AddTemp(Location::RequiresFpuRegister());
3379 locations->AddTemp(Location::RequiresFpuRegister());
3380 }
3381}
3382
3383void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3384 const FieldInfo& field_info) {
3385 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3386
3387 LocationSummary* locations = instruction->GetLocations();
3388 Register base = locations->InAt(0).AsRegister<Register>();
3389 Location value = locations->InAt(1);
3390 bool is_volatile = field_info.IsVolatile();
3391 Primitive::Type field_type = field_info.GetFieldType();
3392 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3393
3394 if (is_volatile) {
3395 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3396 }
3397
3398 switch (field_type) {
3399 case Primitive::kPrimBoolean:
3400 case Primitive::kPrimByte: {
3401 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3402 break;
3403 }
3404
3405 case Primitive::kPrimShort:
3406 case Primitive::kPrimChar: {
3407 __ movw(Address(base, offset), value.AsRegister<Register>());
3408 break;
3409 }
3410
3411 case Primitive::kPrimInt:
3412 case Primitive::kPrimNot: {
3413 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003414 break;
3415 }
3416
3417 case Primitive::kPrimLong: {
3418 if (is_volatile) {
3419 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3420 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3421 __ movd(temp1, value.AsRegisterPairLow<Register>());
3422 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3423 __ punpckldq(temp1, temp2);
3424 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003426 } else {
3427 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003429 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3430 }
3431 break;
3432 }
3433
3434 case Primitive::kPrimFloat: {
3435 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3436 break;
3437 }
3438
3439 case Primitive::kPrimDouble: {
3440 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3441 break;
3442 }
3443
3444 case Primitive::kPrimVoid:
3445 LOG(FATAL) << "Unreachable type " << field_type;
3446 UNREACHABLE();
3447 }
3448
Calin Juravle77520bc2015-01-12 18:45:46 +00003449 // Longs are handled in the switch.
3450 if (field_type != Primitive::kPrimLong) {
3451 codegen_->MaybeRecordImplicitNullCheck(instruction);
3452 }
3453
3454 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3455 Register temp = locations->GetTemp(0).AsRegister<Register>();
3456 Register card = locations->GetTemp(1).AsRegister<Register>();
3457 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3458 }
3459
Calin Juravle52c48962014-12-16 17:02:57 +00003460 if (is_volatile) {
3461 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3462 }
3463}
3464
3465void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3466 HandleFieldGet(instruction, instruction->GetFieldInfo());
3467}
3468
3469void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3470 HandleFieldGet(instruction, instruction->GetFieldInfo());
3471}
3472
3473void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3474 HandleFieldSet(instruction, instruction->GetFieldInfo());
3475}
3476
3477void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3478 HandleFieldSet(instruction, instruction->GetFieldInfo());
3479}
3480
3481void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3482 HandleFieldSet(instruction, instruction->GetFieldInfo());
3483}
3484
3485void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3486 HandleFieldSet(instruction, instruction->GetFieldInfo());
3487}
3488
3489void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3490 HandleFieldGet(instruction, instruction->GetFieldInfo());
3491}
3492
3493void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3494 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003495}
3496
3497void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003498 LocationSummary* locations =
3499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003500 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3501 ? Location::RequiresRegister()
3502 : Location::Any();
3503 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003504 if (instruction->HasUses()) {
3505 locations->SetOut(Location::SameAsFirstInput());
3506 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003507}
3508
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003509void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003510 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3511 return;
3512 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003513 LocationSummary* locations = instruction->GetLocations();
3514 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003515
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003516 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3517 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3518}
3519
3520void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003521 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003522 codegen_->AddSlowPath(slow_path);
3523
3524 LocationSummary* locations = instruction->GetLocations();
3525 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003526
3527 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003528 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003529 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003530 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003531 } else {
3532 DCHECK(obj.IsConstant()) << obj;
3533 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3534 __ jmp(slow_path->GetEntryLabel());
3535 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003536 }
3537 __ j(kEqual, slow_path->GetEntryLabel());
3538}
3539
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003540void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3541 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3542 GenerateImplicitNullCheck(instruction);
3543 } else {
3544 GenerateExplicitNullCheck(instruction);
3545 }
3546}
3547
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003548void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003549 LocationSummary* locations =
3550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003551 locations->SetInAt(0, Location::RequiresRegister());
3552 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003553 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3554 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3555 } else {
3556 // The output overlaps in case of long: we don't want the low move to overwrite
3557 // the array's location.
3558 locations->SetOut(Location::RequiresRegister(),
3559 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3560 : Location::kNoOutputOverlap);
3561 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003562}
3563
3564void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3565 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567 Location index = locations->InAt(1);
3568
Calin Juravle77520bc2015-01-12 18:45:46 +00003569 Primitive::Type type = instruction->GetType();
3570 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003571 case Primitive::kPrimBoolean: {
3572 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003574 if (index.IsConstant()) {
3575 __ movzxb(out, Address(obj,
3576 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 }
3580 break;
3581 }
3582
3583 case Primitive::kPrimByte: {
3584 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003585 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586 if (index.IsConstant()) {
3587 __ movsxb(out, Address(obj,
3588 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3589 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003591 }
3592 break;
3593 }
3594
3595 case Primitive::kPrimShort: {
3596 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003598 if (index.IsConstant()) {
3599 __ movsxw(out, Address(obj,
3600 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3601 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 }
3604 break;
3605 }
3606
3607 case Primitive::kPrimChar: {
3608 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 if (index.IsConstant()) {
3611 __ movzxw(out, Address(obj,
3612 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3613 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 }
3616 break;
3617 }
3618
3619 case Primitive::kPrimInt:
3620 case Primitive::kPrimNot: {
3621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623 if (index.IsConstant()) {
3624 __ movl(out, Address(obj,
3625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 }
3629 break;
3630 }
3631
3632 case Primitive::kPrimLong: {
3633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003634 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003635 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003636 if (index.IsConstant()) {
3637 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003638 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003639 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003640 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003641 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003642 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003645 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 }
3648 break;
3649 }
3650
Mark Mendell7c8d0092015-01-26 11:21:33 -05003651 case Primitive::kPrimFloat: {
3652 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3653 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3654 if (index.IsConstant()) {
3655 __ movss(out, Address(obj,
3656 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3657 } else {
3658 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3659 }
3660 break;
3661 }
3662
3663 case Primitive::kPrimDouble: {
3664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3665 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3666 if (index.IsConstant()) {
3667 __ movsd(out, Address(obj,
3668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3669 } else {
3670 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3671 }
3672 break;
3673 }
3674
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003675 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003676 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003677 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003678 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003679
3680 if (type != Primitive::kPrimLong) {
3681 codegen_->MaybeRecordImplicitNullCheck(instruction);
3682 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003683}
3684
3685void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003686 // This location builder might end up asking to up to four registers, which is
3687 // not currently possible for baseline. The situation in which we need four
3688 // registers cannot be met by baseline though, because it has not run any
3689 // optimization.
3690
Nicolas Geoffray39468442014-09-02 15:17:15 +01003691 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003692 bool needs_write_barrier =
3693 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3694
Mark Mendell5f874182015-03-04 15:42:45 -05003695 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003696
Nicolas Geoffray39468442014-09-02 15:17:15 +01003697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3698 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003700
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003701 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003702 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003703 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3704 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3705 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003706 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003707 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3708 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003709 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003710 // In case of a byte operation, the register allocator does not support multiple
3711 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003712 locations->SetInAt(0, Location::RequiresRegister());
3713 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003714 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003715 // Ensure the value is in a byte register.
3716 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003717 } else if (Primitive::IsFloatingPointType(value_type)) {
3718 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003719 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003720 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003722 // Temporary registers for the write barrier.
3723 if (needs_write_barrier) {
3724 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003725 // Ensure the card is in a byte register.
3726 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003727 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003729}
3730
3731void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3732 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003734 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003735 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003736 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003737 bool needs_runtime_call = locations->WillCall();
3738 bool needs_write_barrier =
3739 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740
3741 switch (value_type) {
3742 case Primitive::kPrimBoolean:
3743 case Primitive::kPrimByte: {
3744 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 if (index.IsConstant()) {
3746 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003747 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003748 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003749 } else {
3750 __ movb(Address(obj, offset),
3751 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3752 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003754 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003756 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003759 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 break;
3764 }
3765
3766 case Primitive::kPrimShort:
3767 case Primitive::kPrimChar: {
3768 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769 if (index.IsConstant()) {
3770 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003771 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003773 } else {
3774 __ movw(Address(obj, offset),
3775 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3776 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003777 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003778 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3780 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003781 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003783 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3784 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003786 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 break;
3788 }
3789
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003790 case Primitive::kPrimInt:
3791 case Primitive::kPrimNot: {
3792 if (!needs_runtime_call) {
3793 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3794 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003795 size_t offset =
3796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003797 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003799 } else {
3800 DCHECK(value.IsConstant()) << value;
3801 __ movl(Address(obj, offset),
3802 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3803 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003804 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003805 DCHECK(index.IsRegister()) << index;
3806 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003807 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3808 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003809 } else {
3810 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003812 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3813 }
3814 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003816
3817 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003818 Register temp = locations->GetTemp(0).AsRegister<Register>();
3819 Register card = locations->GetTemp(1).AsRegister<Register>();
3820 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003821 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003823 DCHECK_EQ(value_type, Primitive::kPrimNot);
3824 DCHECK(!codegen_->IsLeafMethod());
3825 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3826 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003827 }
3828 break;
3829 }
3830
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003831 case Primitive::kPrimLong: {
3832 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003833 if (index.IsConstant()) {
3834 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003835 if (value.IsRegisterPair()) {
3836 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003837 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003838 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003839 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003840 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003841 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3842 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003844 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3845 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003846 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003847 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003848 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003849 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003852 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003853 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003854 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003855 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003857 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003858 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003859 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003860 Immediate(High32Bits(val)));
3861 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003862 }
3863 break;
3864 }
3865
Mark Mendell7c8d0092015-01-26 11:21:33 -05003866 case Primitive::kPrimFloat: {
3867 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3868 DCHECK(value.IsFpuRegister());
3869 if (index.IsConstant()) {
3870 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3871 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3872 } else {
3873 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3874 value.AsFpuRegister<XmmRegister>());
3875 }
3876 break;
3877 }
3878
3879 case Primitive::kPrimDouble: {
3880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3881 DCHECK(value.IsFpuRegister());
3882 if (index.IsConstant()) {
3883 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3884 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3885 } else {
3886 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3887 value.AsFpuRegister<XmmRegister>());
3888 }
3889 break;
3890 }
3891
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003892 case Primitive::kPrimVoid:
3893 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003894 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895 }
3896}
3897
3898void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003900 locations->SetInAt(0, Location::RequiresRegister());
3901 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902 instruction->SetLocations(locations);
3903}
3904
3905void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3906 LocationSummary* locations = instruction->GetLocations();
3907 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003908 Register obj = locations->InAt(0).AsRegister<Register>();
3909 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003911 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003912}
3913
3914void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003915 LocationSummary* locations =
3916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003917 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003918 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003919 if (instruction->HasUses()) {
3920 locations->SetOut(Location::SameAsFirstInput());
3921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922}
3923
3924void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3925 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003926 Location index_loc = locations->InAt(0);
3927 Location length_loc = locations->InAt(1);
3928 SlowPathCodeX86* slow_path =
3929 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930
Mark Mendell99dbd682015-04-22 16:18:52 -04003931 if (length_loc.IsConstant()) {
3932 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3933 if (index_loc.IsConstant()) {
3934 // BCE will remove the bounds check if we are guarenteed to pass.
3935 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3936 if (index < 0 || index >= length) {
3937 codegen_->AddSlowPath(slow_path);
3938 __ jmp(slow_path->GetEntryLabel());
3939 } else {
3940 // Some optimization after BCE may have generated this, and we should not
3941 // generate a bounds check if it is a valid range.
3942 }
3943 return;
3944 }
3945
3946 // We have to reverse the jump condition because the length is the constant.
3947 Register index_reg = index_loc.AsRegister<Register>();
3948 __ cmpl(index_reg, Immediate(length));
3949 codegen_->AddSlowPath(slow_path);
3950 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003951 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003952 Register length = length_loc.AsRegister<Register>();
3953 if (index_loc.IsConstant()) {
3954 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3955 __ cmpl(length, Immediate(value));
3956 } else {
3957 __ cmpl(length, index_loc.AsRegister<Register>());
3958 }
3959 codegen_->AddSlowPath(slow_path);
3960 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003961 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962}
3963
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003964void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3965 temp->SetLocations(nullptr);
3966}
3967
3968void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3969 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003970 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971}
3972
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003973void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003974 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003975 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003976}
3977
3978void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003979 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3980}
3981
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003982void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3984}
3985
3986void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003987 HBasicBlock* block = instruction->GetBlock();
3988 if (block->GetLoopInformation() != nullptr) {
3989 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3990 // The back edge will generate the suspend check.
3991 return;
3992 }
3993 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3994 // The goto will generate the suspend check.
3995 return;
3996 }
3997 GenerateSuspendCheck(instruction, nullptr);
3998}
3999
4000void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4001 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004002 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004003 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4004 if (slow_path == nullptr) {
4005 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4006 instruction->SetSlowPath(slow_path);
4007 codegen_->AddSlowPath(slow_path);
4008 if (successor != nullptr) {
4009 DCHECK(successor->IsLoopHeader());
4010 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4011 }
4012 } else {
4013 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4014 }
4015
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004016 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004017 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004018 if (successor == nullptr) {
4019 __ j(kNotEqual, slow_path->GetEntryLabel());
4020 __ Bind(slow_path->GetReturnLabel());
4021 } else {
4022 __ j(kEqual, codegen_->GetLabelOf(successor));
4023 __ jmp(slow_path->GetEntryLabel());
4024 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004025}
4026
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004027X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4028 return codegen_->GetAssembler();
4029}
4030
Mark Mendell7c8d0092015-01-26 11:21:33 -05004031void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004032 ScratchRegisterScope ensure_scratch(
4033 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4034 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4035 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4036 __ movl(temp_reg, Address(ESP, src + stack_offset));
4037 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004038}
4039
4040void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004041 ScratchRegisterScope ensure_scratch(
4042 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4043 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4044 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4045 __ movl(temp_reg, Address(ESP, src + stack_offset));
4046 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4047 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4048 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004049}
4050
4051void ParallelMoveResolverX86::EmitMove(size_t index) {
4052 MoveOperands* move = moves_.Get(index);
4053 Location source = move->GetSource();
4054 Location destination = move->GetDestination();
4055
4056 if (source.IsRegister()) {
4057 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004058 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004059 } else {
4060 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004061 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004062 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004063 } else if (source.IsFpuRegister()) {
4064 if (destination.IsFpuRegister()) {
4065 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4066 } else if (destination.IsStackSlot()) {
4067 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4068 } else {
4069 DCHECK(destination.IsDoubleStackSlot());
4070 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4071 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004072 } else if (source.IsStackSlot()) {
4073 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004075 } else if (destination.IsFpuRegister()) {
4076 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004077 } else {
4078 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004079 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4080 }
4081 } else if (source.IsDoubleStackSlot()) {
4082 if (destination.IsFpuRegister()) {
4083 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4084 } else {
4085 DCHECK(destination.IsDoubleStackSlot()) << destination;
4086 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004087 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004088 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004089 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004090 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004091 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004092 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004093 if (value == 0) {
4094 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4095 } else {
4096 __ movl(destination.AsRegister<Register>(), Immediate(value));
4097 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004098 } else {
4099 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004100 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004101 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004102 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004103 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004104 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004105 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004106 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004107 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4108 if (value == 0) {
4109 // Easy handling of 0.0.
4110 __ xorps(dest, dest);
4111 } else {
4112 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004113 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4114 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4115 __ movl(temp, Immediate(value));
4116 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004117 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 } else {
4119 DCHECK(destination.IsStackSlot()) << destination;
4120 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4121 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004122 } else if (constant->IsLongConstant()) {
4123 int64_t value = constant->AsLongConstant()->GetValue();
4124 int32_t low_value = Low32Bits(value);
4125 int32_t high_value = High32Bits(value);
4126 Immediate low(low_value);
4127 Immediate high(high_value);
4128 if (destination.IsDoubleStackSlot()) {
4129 __ movl(Address(ESP, destination.GetStackIndex()), low);
4130 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4131 } else {
4132 __ movl(destination.AsRegisterPairLow<Register>(), low);
4133 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4134 }
4135 } else {
4136 DCHECK(constant->IsDoubleConstant());
4137 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004138 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004139 int32_t low_value = Low32Bits(value);
4140 int32_t high_value = High32Bits(value);
4141 Immediate low(low_value);
4142 Immediate high(high_value);
4143 if (destination.IsFpuRegister()) {
4144 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4145 if (value == 0) {
4146 // Easy handling of 0.0.
4147 __ xorpd(dest, dest);
4148 } else {
4149 __ pushl(high);
4150 __ pushl(low);
4151 __ movsd(dest, Address(ESP, 0));
4152 __ addl(ESP, Immediate(8));
4153 }
4154 } else {
4155 DCHECK(destination.IsDoubleStackSlot()) << destination;
4156 __ movl(Address(ESP, destination.GetStackIndex()), low);
4157 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4158 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004159 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004160 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004161 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004162 }
4163}
4164
Mark Mendella5c19ce2015-04-01 12:51:05 -04004165void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004166 Register suggested_scratch = reg == EAX ? EBX : EAX;
4167 ScratchRegisterScope ensure_scratch(
4168 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4169
4170 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4171 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4172 __ movl(Address(ESP, mem + stack_offset), reg);
4173 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004174}
4175
Mark Mendell7c8d0092015-01-26 11:21:33 -05004176void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004177 ScratchRegisterScope ensure_scratch(
4178 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4179
4180 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4181 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4182 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4183 __ movss(Address(ESP, mem + stack_offset), reg);
4184 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004185}
4186
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004187void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004188 ScratchRegisterScope ensure_scratch1(
4189 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004190
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004191 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4192 ScratchRegisterScope ensure_scratch2(
4193 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004194
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004195 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4196 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4197 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4198 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4199 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4200 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004201}
4202
4203void ParallelMoveResolverX86::EmitSwap(size_t index) {
4204 MoveOperands* move = moves_.Get(index);
4205 Location source = move->GetSource();
4206 Location destination = move->GetDestination();
4207
4208 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004209 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004210 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004211 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004212 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004213 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004214 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4215 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004216 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4217 // Use XOR Swap algorithm to avoid a temporary.
4218 DCHECK_NE(source.reg(), destination.reg());
4219 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4220 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4221 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4222 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4223 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4224 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4225 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004226 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4227 // Take advantage of the 16 bytes in the XMM register.
4228 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4229 Address stack(ESP, destination.GetStackIndex());
4230 // Load the double into the high doubleword.
4231 __ movhpd(reg, stack);
4232
4233 // Store the low double into the destination.
4234 __ movsd(stack, reg);
4235
4236 // Move the high double to the low double.
4237 __ psrldq(reg, Immediate(8));
4238 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4239 // Take advantage of the 16 bytes in the XMM register.
4240 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4241 Address stack(ESP, source.GetStackIndex());
4242 // Load the double into the high doubleword.
4243 __ movhpd(reg, stack);
4244
4245 // Store the low double into the destination.
4246 __ movsd(stack, reg);
4247
4248 // Move the high double to the low double.
4249 __ psrldq(reg, Immediate(8));
4250 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4251 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4252 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004253 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004254 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004255 }
4256}
4257
4258void ParallelMoveResolverX86::SpillScratch(int reg) {
4259 __ pushl(static_cast<Register>(reg));
4260}
4261
4262void ParallelMoveResolverX86::RestoreScratch(int reg) {
4263 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004264}
4265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004266void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004267 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4268 ? LocationSummary::kCallOnSlowPath
4269 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004270 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004271 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004272 locations->SetOut(Location::RequiresRegister());
4273}
4274
4275void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004276 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004277 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004278 DCHECK(!cls->CanCallRuntime());
4279 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280 codegen_->LoadCurrentMethod(out);
4281 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4282 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004283 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004284 codegen_->LoadCurrentMethod(out);
4285 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4286 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004287
4288 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4289 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4290 codegen_->AddSlowPath(slow_path);
4291 __ testl(out, out);
4292 __ j(kEqual, slow_path->GetEntryLabel());
4293 if (cls->MustGenerateClinitCheck()) {
4294 GenerateClassInitializationCheck(slow_path, out);
4295 } else {
4296 __ Bind(slow_path->GetExitLabel());
4297 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004298 }
4299}
4300
4301void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4302 LocationSummary* locations =
4303 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4304 locations->SetInAt(0, Location::RequiresRegister());
4305 if (check->HasUses()) {
4306 locations->SetOut(Location::SameAsFirstInput());
4307 }
4308}
4309
4310void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004311 // We assume the class to not be null.
4312 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4313 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004314 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004315 GenerateClassInitializationCheck(slow_path,
4316 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004317}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004318
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004319void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4320 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004321 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4322 Immediate(mirror::Class::kStatusInitialized));
4323 __ j(kLess, slow_path->GetEntryLabel());
4324 __ Bind(slow_path->GetExitLabel());
4325 // No need for memory fence, thanks to the X86 memory model.
4326}
4327
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004328void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4329 LocationSummary* locations =
4330 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4331 locations->SetOut(Location::RequiresRegister());
4332}
4333
4334void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4335 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4336 codegen_->AddSlowPath(slow_path);
4337
Roland Levillain271ab9c2014-11-27 15:23:57 +00004338 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004339 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004340 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4341 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004342 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4343 __ testl(out, out);
4344 __ j(kEqual, slow_path->GetEntryLabel());
4345 __ Bind(slow_path->GetExitLabel());
4346}
4347
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004348void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4349 LocationSummary* locations =
4350 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4351 locations->SetOut(Location::RequiresRegister());
4352}
4353
4354void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4355 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004357 __ fs()->movl(address, Immediate(0));
4358}
4359
4360void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4361 LocationSummary* locations =
4362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4363 InvokeRuntimeCallingConvention calling_convention;
4364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4365}
4366
4367void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4368 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4369 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4370}
4371
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004372void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004373 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4374 ? LocationSummary::kNoCall
4375 : LocationSummary::kCallOnSlowPath;
4376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 locations->SetInAt(1, Location::Any());
4379 locations->SetOut(Location::RequiresRegister());
4380}
4381
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004382void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004383 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004384 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004385 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004386 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004387 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4388 Label done, zero;
4389 SlowPathCodeX86* slow_path = nullptr;
4390
4391 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004392 // Avoid null check if we know obj is not null.
4393 if (instruction->MustDoNullCheck()) {
4394 __ testl(obj, obj);
4395 __ j(kEqual, &zero);
4396 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004397 __ movl(out, Address(obj, class_offset));
4398 // Compare the class of `obj` with `cls`.
4399 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004400 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004401 } else {
4402 DCHECK(cls.IsStackSlot()) << cls;
4403 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4404 }
4405
4406 if (instruction->IsClassFinal()) {
4407 // Classes must be equal for the instanceof to succeed.
4408 __ j(kNotEqual, &zero);
4409 __ movl(out, Immediate(1));
4410 __ jmp(&done);
4411 } else {
4412 // If the classes are not equal, we go into a slow path.
4413 DCHECK(locations->OnlyCallsOnSlowPath());
4414 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004415 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004416 codegen_->AddSlowPath(slow_path);
4417 __ j(kNotEqual, slow_path->GetEntryLabel());
4418 __ movl(out, Immediate(1));
4419 __ jmp(&done);
4420 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004421
4422 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4423 __ Bind(&zero);
4424 __ movl(out, Immediate(0));
4425 }
4426
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004427 if (slow_path != nullptr) {
4428 __ Bind(slow_path->GetExitLabel());
4429 }
4430 __ Bind(&done);
4431}
4432
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004433void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4435 instruction, LocationSummary::kCallOnSlowPath);
4436 locations->SetInAt(0, Location::RequiresRegister());
4437 locations->SetInAt(1, Location::Any());
4438 locations->AddTemp(Location::RequiresRegister());
4439}
4440
4441void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4442 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004444 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004445 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004446 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4447 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4448 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4449 codegen_->AddSlowPath(slow_path);
4450
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004451 // Avoid null check if we know obj is not null.
4452 if (instruction->MustDoNullCheck()) {
4453 __ testl(obj, obj);
4454 __ j(kEqual, slow_path->GetExitLabel());
4455 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004456
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004457 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004458 // Compare the class of `obj` with `cls`.
4459 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004460 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004461 } else {
4462 DCHECK(cls.IsStackSlot()) << cls;
4463 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4464 }
4465
4466 __ j(kNotEqual, slow_path->GetEntryLabel());
4467 __ Bind(slow_path->GetExitLabel());
4468}
4469
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004470void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4471 LocationSummary* locations =
4472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4473 InvokeRuntimeCallingConvention calling_convention;
4474 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4475}
4476
4477void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4478 __ fs()->call(Address::Absolute(instruction->IsEnter()
4479 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4480 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4481 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4482}
4483
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004484void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4485void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4486void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4487
4488void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4489 LocationSummary* locations =
4490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4491 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4492 || instruction->GetResultType() == Primitive::kPrimLong);
4493 locations->SetInAt(0, Location::RequiresRegister());
4494 locations->SetInAt(1, Location::Any());
4495 locations->SetOut(Location::SameAsFirstInput());
4496}
4497
4498void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4499 HandleBitwiseOperation(instruction);
4500}
4501
4502void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4503 HandleBitwiseOperation(instruction);
4504}
4505
4506void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4507 HandleBitwiseOperation(instruction);
4508}
4509
4510void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4511 LocationSummary* locations = instruction->GetLocations();
4512 Location first = locations->InAt(0);
4513 Location second = locations->InAt(1);
4514 DCHECK(first.Equals(locations->Out()));
4515
4516 if (instruction->GetResultType() == Primitive::kPrimInt) {
4517 if (second.IsRegister()) {
4518 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004519 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004520 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004521 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004522 } else {
4523 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004524 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004525 }
4526 } else if (second.IsConstant()) {
4527 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004528 __ andl(first.AsRegister<Register>(),
4529 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004530 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004531 __ orl(first.AsRegister<Register>(),
4532 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004533 } else {
4534 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004535 __ xorl(first.AsRegister<Register>(),
4536 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004537 }
4538 } else {
4539 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004540 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004541 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004542 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004543 } else {
4544 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004545 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004546 }
4547 }
4548 } else {
4549 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4550 if (second.IsRegisterPair()) {
4551 if (instruction->IsAnd()) {
4552 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4553 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4554 } else if (instruction->IsOr()) {
4555 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4556 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4557 } else {
4558 DCHECK(instruction->IsXor());
4559 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4560 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4561 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004562 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004563 if (instruction->IsAnd()) {
4564 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4565 __ andl(first.AsRegisterPairHigh<Register>(),
4566 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4567 } else if (instruction->IsOr()) {
4568 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4569 __ orl(first.AsRegisterPairHigh<Register>(),
4570 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4571 } else {
4572 DCHECK(instruction->IsXor());
4573 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4574 __ xorl(first.AsRegisterPairHigh<Register>(),
4575 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4576 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004577 } else {
4578 DCHECK(second.IsConstant()) << second;
4579 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004580 int32_t low_value = Low32Bits(value);
4581 int32_t high_value = High32Bits(value);
4582 Immediate low(low_value);
4583 Immediate high(high_value);
4584 Register first_low = first.AsRegisterPairLow<Register>();
4585 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004586 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004587 if (low_value == 0) {
4588 __ xorl(first_low, first_low);
4589 } else if (low_value != -1) {
4590 __ andl(first_low, low);
4591 }
4592 if (high_value == 0) {
4593 __ xorl(first_high, first_high);
4594 } else if (high_value != -1) {
4595 __ andl(first_high, high);
4596 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004597 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004598 if (low_value != 0) {
4599 __ orl(first_low, low);
4600 }
4601 if (high_value != 0) {
4602 __ orl(first_high, high);
4603 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004604 } else {
4605 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004606 if (low_value != 0) {
4607 __ xorl(first_low, low);
4608 }
4609 if (high_value != 0) {
4610 __ xorl(first_high, high);
4611 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004612 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004613 }
4614 }
4615}
4616
Calin Juravleb1498f62015-02-16 13:13:29 +00004617void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4618 // Nothing to do, this should be removed during prepare for register allocator.
4619 UNUSED(instruction);
4620 LOG(FATAL) << "Unreachable";
4621}
4622
4623void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4624 // Nothing to do, this should be removed during prepare for register allocator.
4625 UNUSED(instruction);
4626 LOG(FATAL) << "Unreachable";
4627}
4628
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004629} // namespace x86
4630} // namespace art