blob: 60fd29bf741e31c41c5ab0e7c86df57c1e9808d0 [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
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070027#include "mirror/class-inl.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>();
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001278 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1279 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001280 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(
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001295 temp, 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>();
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001310 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1311 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001312 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 Chartier3d21bdf2015-04-22 13:56:20 -07001331 __ call(Address(temp, 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(
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07003210 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003211 } 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_;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07003216 __ movl(temp, Address(temp, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08003217 // temp = temp[index_in_cache]
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07003218 __ movl(temp, Address(temp,
3219 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
Jeff Hao848f70a2014-01-15 13:49:50 -08003220 // (temp + offset_of_quick_compiled_code)()
3221 __ call(Address(temp,
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07003222 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08003223 } else {
3224 __ call(GetFrameEntryLabel());
3225 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003226 }
3227
3228 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003229}
3230
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003231void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232 Label is_null;
3233 __ testl(value, value);
3234 __ j(kEqual, &is_null);
3235 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3236 __ movl(temp, object);
3237 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003238 __ movb(Address(temp, card, TIMES_1, 0),
3239 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003240 __ Bind(&is_null);
3241}
3242
Calin Juravle52c48962014-12-16 17:02:57 +00003243void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3244 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003245 LocationSummary* locations =
3246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003247 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003248
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003249 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3250 locations->SetOut(Location::RequiresFpuRegister());
3251 } else {
3252 // The output overlaps in case of long: we don't want the low move to overwrite
3253 // the object's location.
3254 locations->SetOut(Location::RequiresRegister(),
3255 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3256 : Location::kNoOutputOverlap);
3257 }
Calin Juravle52c48962014-12-16 17:02:57 +00003258
3259 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3260 // Long values can be loaded atomically into an XMM using movsd.
3261 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3262 // and then copy the XMM into the output 32bits at a time).
3263 locations->AddTemp(Location::RequiresFpuRegister());
3264 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003265}
3266
Calin Juravle52c48962014-12-16 17:02:57 +00003267void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3268 const FieldInfo& field_info) {
3269 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003270
Calin Juravle52c48962014-12-16 17:02:57 +00003271 LocationSummary* locations = instruction->GetLocations();
3272 Register base = locations->InAt(0).AsRegister<Register>();
3273 Location out = locations->Out();
3274 bool is_volatile = field_info.IsVolatile();
3275 Primitive::Type field_type = field_info.GetFieldType();
3276 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3277
3278 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003279 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003280 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003281 break;
3282 }
3283
3284 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003285 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003286 break;
3287 }
3288
3289 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003290 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003291 break;
3292 }
3293
3294 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003295 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003296 break;
3297 }
3298
3299 case Primitive::kPrimInt:
3300 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003301 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003302 break;
3303 }
3304
3305 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003306 if (is_volatile) {
3307 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3308 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003310 __ movd(out.AsRegisterPairLow<Register>(), temp);
3311 __ psrlq(temp, Immediate(32));
3312 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3313 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003314 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003316 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003317 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3318 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319 break;
3320 }
3321
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003322 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003323 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003324 break;
3325 }
3326
3327 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003328 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003329 break;
3330 }
3331
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003332 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003333 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003334 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335 }
Calin Juravle52c48962014-12-16 17:02:57 +00003336
Calin Juravle77520bc2015-01-12 18:45:46 +00003337 // Longs are handled in the switch.
3338 if (field_type != Primitive::kPrimLong) {
3339 codegen_->MaybeRecordImplicitNullCheck(instruction);
3340 }
3341
Calin Juravle52c48962014-12-16 17:02:57 +00003342 if (is_volatile) {
3343 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3344 }
3345}
3346
3347void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3348 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3349
3350 LocationSummary* locations =
3351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3352 locations->SetInAt(0, Location::RequiresRegister());
3353 bool is_volatile = field_info.IsVolatile();
3354 Primitive::Type field_type = field_info.GetFieldType();
3355 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3356 || (field_type == Primitive::kPrimByte);
3357
3358 // The register allocator does not support multiple
3359 // inputs that die at entry with one in a specific register.
3360 if (is_byte_type) {
3361 // Ensure the value is in a byte register.
3362 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003363 } else if (Primitive::IsFloatingPointType(field_type)) {
3364 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003365 } else {
3366 locations->SetInAt(1, Location::RequiresRegister());
3367 }
3368 // Temporary registers for the write barrier.
3369 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3370 locations->AddTemp(Location::RequiresRegister());
3371 // Ensure the card is in a byte register.
3372 locations->AddTemp(Location::RegisterLocation(ECX));
3373 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3374 // 64bits value can be atomically written to an address with movsd and an XMM register.
3375 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3376 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3377 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3378 // isolated cases when we need this it isn't worth adding the extra complexity.
3379 locations->AddTemp(Location::RequiresFpuRegister());
3380 locations->AddTemp(Location::RequiresFpuRegister());
3381 }
3382}
3383
3384void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
3385 const FieldInfo& field_info) {
3386 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3387
3388 LocationSummary* locations = instruction->GetLocations();
3389 Register base = locations->InAt(0).AsRegister<Register>();
3390 Location value = locations->InAt(1);
3391 bool is_volatile = field_info.IsVolatile();
3392 Primitive::Type field_type = field_info.GetFieldType();
3393 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3394
3395 if (is_volatile) {
3396 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3397 }
3398
3399 switch (field_type) {
3400 case Primitive::kPrimBoolean:
3401 case Primitive::kPrimByte: {
3402 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3403 break;
3404 }
3405
3406 case Primitive::kPrimShort:
3407 case Primitive::kPrimChar: {
3408 __ movw(Address(base, offset), value.AsRegister<Register>());
3409 break;
3410 }
3411
3412 case Primitive::kPrimInt:
3413 case Primitive::kPrimNot: {
3414 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003415 break;
3416 }
3417
3418 case Primitive::kPrimLong: {
3419 if (is_volatile) {
3420 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3421 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3422 __ movd(temp1, value.AsRegisterPairLow<Register>());
3423 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3424 __ punpckldq(temp1, temp2);
3425 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003426 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003427 } else {
3428 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003430 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3431 }
3432 break;
3433 }
3434
3435 case Primitive::kPrimFloat: {
3436 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3437 break;
3438 }
3439
3440 case Primitive::kPrimDouble: {
3441 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3442 break;
3443 }
3444
3445 case Primitive::kPrimVoid:
3446 LOG(FATAL) << "Unreachable type " << field_type;
3447 UNREACHABLE();
3448 }
3449
Calin Juravle77520bc2015-01-12 18:45:46 +00003450 // Longs are handled in the switch.
3451 if (field_type != Primitive::kPrimLong) {
3452 codegen_->MaybeRecordImplicitNullCheck(instruction);
3453 }
3454
3455 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3456 Register temp = locations->GetTemp(0).AsRegister<Register>();
3457 Register card = locations->GetTemp(1).AsRegister<Register>();
3458 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3459 }
3460
Calin Juravle52c48962014-12-16 17:02:57 +00003461 if (is_volatile) {
3462 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3463 }
3464}
3465
3466void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3467 HandleFieldGet(instruction, instruction->GetFieldInfo());
3468}
3469
3470void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3471 HandleFieldGet(instruction, instruction->GetFieldInfo());
3472}
3473
3474void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3475 HandleFieldSet(instruction, instruction->GetFieldInfo());
3476}
3477
3478void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3479 HandleFieldSet(instruction, instruction->GetFieldInfo());
3480}
3481
3482void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3483 HandleFieldSet(instruction, instruction->GetFieldInfo());
3484}
3485
3486void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3487 HandleFieldSet(instruction, instruction->GetFieldInfo());
3488}
3489
3490void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3491 HandleFieldGet(instruction, instruction->GetFieldInfo());
3492}
3493
3494void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3495 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003496}
3497
3498void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003499 LocationSummary* locations =
3500 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003501 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3502 ? Location::RequiresRegister()
3503 : Location::Any();
3504 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003505 if (instruction->HasUses()) {
3506 locations->SetOut(Location::SameAsFirstInput());
3507 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003508}
3509
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003510void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003511 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3512 return;
3513 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003514 LocationSummary* locations = instruction->GetLocations();
3515 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003516
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003517 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3518 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3519}
3520
3521void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003522 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003523 codegen_->AddSlowPath(slow_path);
3524
3525 LocationSummary* locations = instruction->GetLocations();
3526 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527
3528 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003529 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003530 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003531 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003532 } else {
3533 DCHECK(obj.IsConstant()) << obj;
3534 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3535 __ jmp(slow_path->GetEntryLabel());
3536 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003537 }
3538 __ j(kEqual, slow_path->GetEntryLabel());
3539}
3540
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003541void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3542 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3543 GenerateImplicitNullCheck(instruction);
3544 } else {
3545 GenerateExplicitNullCheck(instruction);
3546 }
3547}
3548
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003549void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003550 LocationSummary* locations =
3551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003552 locations->SetInAt(0, Location::RequiresRegister());
3553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003554 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3556 } else {
3557 // The output overlaps in case of long: we don't want the low move to overwrite
3558 // the array's location.
3559 locations->SetOut(Location::RequiresRegister(),
3560 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3561 : Location::kNoOutputOverlap);
3562 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003563}
3564
3565void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3566 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003568 Location index = locations->InAt(1);
3569
Calin Juravle77520bc2015-01-12 18:45:46 +00003570 Primitive::Type type = instruction->GetType();
3571 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572 case Primitive::kPrimBoolean: {
3573 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 if (index.IsConstant()) {
3576 __ movzxb(out, Address(obj,
3577 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3578 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003580 }
3581 break;
3582 }
3583
3584 case Primitive::kPrimByte: {
3585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003586 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003587 if (index.IsConstant()) {
3588 __ movsxb(out, Address(obj,
3589 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3590 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 }
3593 break;
3594 }
3595
3596 case Primitive::kPrimShort: {
3597 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599 if (index.IsConstant()) {
3600 __ movsxw(out, Address(obj,
3601 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3602 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 }
3605 break;
3606 }
3607
3608 case Primitive::kPrimChar: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 if (index.IsConstant()) {
3612 __ movzxw(out, Address(obj,
3613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 }
3617 break;
3618 }
3619
3620 case Primitive::kPrimInt:
3621 case Primitive::kPrimNot: {
3622 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624 if (index.IsConstant()) {
3625 __ movl(out, Address(obj,
3626 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3627 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 }
3630 break;
3631 }
3632
3633 case Primitive::kPrimLong: {
3634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003635 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003636 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 if (index.IsConstant()) {
3638 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003639 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003640 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003641 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003642 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003643 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003646 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648 }
3649 break;
3650 }
3651
Mark Mendell7c8d0092015-01-26 11:21:33 -05003652 case Primitive::kPrimFloat: {
3653 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3654 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3655 if (index.IsConstant()) {
3656 __ movss(out, Address(obj,
3657 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3658 } else {
3659 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3660 }
3661 break;
3662 }
3663
3664 case Primitive::kPrimDouble: {
3665 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3666 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3667 if (index.IsConstant()) {
3668 __ movsd(out, Address(obj,
3669 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3670 } else {
3671 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3672 }
3673 break;
3674 }
3675
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003676 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003677 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003678 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003680
3681 if (type != Primitive::kPrimLong) {
3682 codegen_->MaybeRecordImplicitNullCheck(instruction);
3683 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003684}
3685
3686void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003687 // This location builder might end up asking to up to four registers, which is
3688 // not currently possible for baseline. The situation in which we need four
3689 // registers cannot be met by baseline though, because it has not run any
3690 // optimization.
3691
Nicolas Geoffray39468442014-09-02 15:17:15 +01003692 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003693 bool needs_write_barrier =
3694 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3695
Mark Mendell5f874182015-03-04 15:42:45 -05003696 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003697
Nicolas Geoffray39468442014-09-02 15:17:15 +01003698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3699 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003700 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003701
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003702 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003703 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3705 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3706 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003708 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3709 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003710 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003711 // In case of a byte operation, the register allocator does not support multiple
3712 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003713 locations->SetInAt(0, Location::RequiresRegister());
3714 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003715 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003716 // Ensure the value is in a byte register.
3717 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003718 } else if (Primitive::IsFloatingPointType(value_type)) {
3719 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003720 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003721 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003723 // Temporary registers for the write barrier.
3724 if (needs_write_barrier) {
3725 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003726 // Ensure the card is in a byte register.
3727 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003728 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003729 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730}
3731
3732void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3733 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003734 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003736 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003737 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003738 bool needs_runtime_call = locations->WillCall();
3739 bool needs_write_barrier =
3740 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003741
3742 switch (value_type) {
3743 case Primitive::kPrimBoolean:
3744 case Primitive::kPrimByte: {
3745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 if (index.IsConstant()) {
3747 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003748 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003749 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003750 } else {
3751 __ movb(Address(obj, offset),
3752 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3753 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003754 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003755 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003757 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003758 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003760 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3761 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003763 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 break;
3765 }
3766
3767 case Primitive::kPrimShort:
3768 case Primitive::kPrimChar: {
3769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 if (index.IsConstant()) {
3771 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003772 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003773 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003774 } else {
3775 __ movw(Address(obj, offset),
3776 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3777 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003779 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3781 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003784 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3785 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003787 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 break;
3789 }
3790
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003791 case Primitive::kPrimInt:
3792 case Primitive::kPrimNot: {
3793 if (!needs_runtime_call) {
3794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3795 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003796 size_t offset =
3797 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003798 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003799 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003800 } else {
3801 DCHECK(value.IsConstant()) << value;
3802 __ movl(Address(obj, offset),
3803 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3804 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003805 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003806 DCHECK(index.IsRegister()) << index;
3807 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3809 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003810 } else {
3811 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003813 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3814 }
3815 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003817
3818 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 Register temp = locations->GetTemp(0).AsRegister<Register>();
3820 Register card = locations->GetTemp(1).AsRegister<Register>();
3821 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003822 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003824 DCHECK_EQ(value_type, Primitive::kPrimNot);
3825 DCHECK(!codegen_->IsLeafMethod());
3826 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3827 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 }
3829 break;
3830 }
3831
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003832 case Primitive::kPrimLong: {
3833 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 if (index.IsConstant()) {
3835 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003836 if (value.IsRegisterPair()) {
3837 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003839 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003840 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003841 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003842 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3843 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003844 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003845 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003848 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003850 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003851 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003853 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003854 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003855 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003856 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003858 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003859 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003860 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003861 Immediate(High32Bits(val)));
3862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 }
3864 break;
3865 }
3866
Mark Mendell7c8d0092015-01-26 11:21:33 -05003867 case Primitive::kPrimFloat: {
3868 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3869 DCHECK(value.IsFpuRegister());
3870 if (index.IsConstant()) {
3871 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3872 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3873 } else {
3874 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3875 value.AsFpuRegister<XmmRegister>());
3876 }
3877 break;
3878 }
3879
3880 case Primitive::kPrimDouble: {
3881 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3882 DCHECK(value.IsFpuRegister());
3883 if (index.IsConstant()) {
3884 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3885 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3886 } else {
3887 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3888 value.AsFpuRegister<XmmRegister>());
3889 }
3890 break;
3891 }
3892
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003893 case Primitive::kPrimVoid:
3894 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003895 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003896 }
3897}
3898
3899void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003901 locations->SetInAt(0, Location::RequiresRegister());
3902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 instruction->SetLocations(locations);
3904}
3905
3906void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3907 LocationSummary* locations = instruction->GetLocations();
3908 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 Register obj = locations->InAt(0).AsRegister<Register>();
3910 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003912 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913}
3914
3915void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003916 LocationSummary* locations =
3917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003918 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003919 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003920 if (instruction->HasUses()) {
3921 locations->SetOut(Location::SameAsFirstInput());
3922 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923}
3924
3925void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3926 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003927 Location index_loc = locations->InAt(0);
3928 Location length_loc = locations->InAt(1);
3929 SlowPathCodeX86* slow_path =
3930 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931
Mark Mendell99dbd682015-04-22 16:18:52 -04003932 if (length_loc.IsConstant()) {
3933 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3934 if (index_loc.IsConstant()) {
3935 // BCE will remove the bounds check if we are guarenteed to pass.
3936 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3937 if (index < 0 || index >= length) {
3938 codegen_->AddSlowPath(slow_path);
3939 __ jmp(slow_path->GetEntryLabel());
3940 } else {
3941 // Some optimization after BCE may have generated this, and we should not
3942 // generate a bounds check if it is a valid range.
3943 }
3944 return;
3945 }
3946
3947 // We have to reverse the jump condition because the length is the constant.
3948 Register index_reg = index_loc.AsRegister<Register>();
3949 __ cmpl(index_reg, Immediate(length));
3950 codegen_->AddSlowPath(slow_path);
3951 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003952 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003953 Register length = length_loc.AsRegister<Register>();
3954 if (index_loc.IsConstant()) {
3955 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3956 __ cmpl(length, Immediate(value));
3957 } else {
3958 __ cmpl(length, index_loc.AsRegister<Register>());
3959 }
3960 codegen_->AddSlowPath(slow_path);
3961 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003962 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963}
3964
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003965void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3966 temp->SetLocations(nullptr);
3967}
3968
3969void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3970 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003971 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003972}
3973
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003974void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003975 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003976 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003977}
3978
3979void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003980 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3981}
3982
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003983void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3985}
3986
3987void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003988 HBasicBlock* block = instruction->GetBlock();
3989 if (block->GetLoopInformation() != nullptr) {
3990 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3991 // The back edge will generate the suspend check.
3992 return;
3993 }
3994 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3995 // The goto will generate the suspend check.
3996 return;
3997 }
3998 GenerateSuspendCheck(instruction, nullptr);
3999}
4000
4001void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4002 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004003 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004004 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4005 if (slow_path == nullptr) {
4006 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4007 instruction->SetSlowPath(slow_path);
4008 codegen_->AddSlowPath(slow_path);
4009 if (successor != nullptr) {
4010 DCHECK(successor->IsLoopHeader());
4011 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4012 }
4013 } else {
4014 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4015 }
4016
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004017 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004018 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004019 if (successor == nullptr) {
4020 __ j(kNotEqual, slow_path->GetEntryLabel());
4021 __ Bind(slow_path->GetReturnLabel());
4022 } else {
4023 __ j(kEqual, codegen_->GetLabelOf(successor));
4024 __ jmp(slow_path->GetEntryLabel());
4025 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004026}
4027
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004028X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4029 return codegen_->GetAssembler();
4030}
4031
Mark Mendell7c8d0092015-01-26 11:21:33 -05004032void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004033 ScratchRegisterScope ensure_scratch(
4034 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4035 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4036 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4037 __ movl(temp_reg, Address(ESP, src + stack_offset));
4038 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004039}
4040
4041void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004042 ScratchRegisterScope ensure_scratch(
4043 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4044 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4045 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4046 __ movl(temp_reg, Address(ESP, src + stack_offset));
4047 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4048 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4049 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004050}
4051
4052void ParallelMoveResolverX86::EmitMove(size_t index) {
4053 MoveOperands* move = moves_.Get(index);
4054 Location source = move->GetSource();
4055 Location destination = move->GetDestination();
4056
4057 if (source.IsRegister()) {
4058 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004059 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004060 } else {
4061 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004062 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004063 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004064 } else if (source.IsFpuRegister()) {
4065 if (destination.IsFpuRegister()) {
4066 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4067 } else if (destination.IsStackSlot()) {
4068 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4069 } else {
4070 DCHECK(destination.IsDoubleStackSlot());
4071 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4072 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004073 } else if (source.IsStackSlot()) {
4074 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004076 } else if (destination.IsFpuRegister()) {
4077 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004078 } else {
4079 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004080 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4081 }
4082 } else if (source.IsDoubleStackSlot()) {
4083 if (destination.IsFpuRegister()) {
4084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4085 } else {
4086 DCHECK(destination.IsDoubleStackSlot()) << destination;
4087 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004088 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004089 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004090 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004091 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004092 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004093 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004094 if (value == 0) {
4095 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4096 } else {
4097 __ movl(destination.AsRegister<Register>(), Immediate(value));
4098 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004099 } else {
4100 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004101 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004102 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004103 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004104 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004105 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004106 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004107 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004108 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4109 if (value == 0) {
4110 // Easy handling of 0.0.
4111 __ xorps(dest, dest);
4112 } else {
4113 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004114 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4115 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4116 __ movl(temp, Immediate(value));
4117 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004118 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004119 } else {
4120 DCHECK(destination.IsStackSlot()) << destination;
4121 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4122 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004123 } else if (constant->IsLongConstant()) {
4124 int64_t value = constant->AsLongConstant()->GetValue();
4125 int32_t low_value = Low32Bits(value);
4126 int32_t high_value = High32Bits(value);
4127 Immediate low(low_value);
4128 Immediate high(high_value);
4129 if (destination.IsDoubleStackSlot()) {
4130 __ movl(Address(ESP, destination.GetStackIndex()), low);
4131 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4132 } else {
4133 __ movl(destination.AsRegisterPairLow<Register>(), low);
4134 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4135 }
4136 } else {
4137 DCHECK(constant->IsDoubleConstant());
4138 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004139 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004140 int32_t low_value = Low32Bits(value);
4141 int32_t high_value = High32Bits(value);
4142 Immediate low(low_value);
4143 Immediate high(high_value);
4144 if (destination.IsFpuRegister()) {
4145 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4146 if (value == 0) {
4147 // Easy handling of 0.0.
4148 __ xorpd(dest, dest);
4149 } else {
4150 __ pushl(high);
4151 __ pushl(low);
4152 __ movsd(dest, Address(ESP, 0));
4153 __ addl(ESP, Immediate(8));
4154 }
4155 } else {
4156 DCHECK(destination.IsDoubleStackSlot()) << destination;
4157 __ movl(Address(ESP, destination.GetStackIndex()), low);
4158 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4159 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004160 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004161 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004162 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004163 }
4164}
4165
Mark Mendella5c19ce2015-04-01 12:51:05 -04004166void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004167 Register suggested_scratch = reg == EAX ? EBX : EAX;
4168 ScratchRegisterScope ensure_scratch(
4169 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4170
4171 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4172 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4173 __ movl(Address(ESP, mem + stack_offset), reg);
4174 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004175}
4176
Mark Mendell7c8d0092015-01-26 11:21:33 -05004177void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004178 ScratchRegisterScope ensure_scratch(
4179 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4180
4181 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4182 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4183 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4184 __ movss(Address(ESP, mem + stack_offset), reg);
4185 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004186}
4187
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004188void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004189 ScratchRegisterScope ensure_scratch1(
4190 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004191
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004192 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4193 ScratchRegisterScope ensure_scratch2(
4194 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004195
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004196 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4197 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4198 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4199 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4200 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4201 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004202}
4203
4204void ParallelMoveResolverX86::EmitSwap(size_t index) {
4205 MoveOperands* move = moves_.Get(index);
4206 Location source = move->GetSource();
4207 Location destination = move->GetDestination();
4208
4209 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004210 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004211 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004212 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004213 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004214 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004215 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4216 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004217 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4218 // Use XOR Swap algorithm to avoid a temporary.
4219 DCHECK_NE(source.reg(), destination.reg());
4220 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4221 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4222 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4223 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4224 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4225 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4226 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004227 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4228 // Take advantage of the 16 bytes in the XMM register.
4229 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4230 Address stack(ESP, destination.GetStackIndex());
4231 // Load the double into the high doubleword.
4232 __ movhpd(reg, stack);
4233
4234 // Store the low double into the destination.
4235 __ movsd(stack, reg);
4236
4237 // Move the high double to the low double.
4238 __ psrldq(reg, Immediate(8));
4239 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4240 // Take advantage of the 16 bytes in the XMM register.
4241 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4242 Address stack(ESP, source.GetStackIndex());
4243 // Load the double into the high doubleword.
4244 __ movhpd(reg, stack);
4245
4246 // Store the low double into the destination.
4247 __ movsd(stack, reg);
4248
4249 // Move the high double to the low double.
4250 __ psrldq(reg, Immediate(8));
4251 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4252 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4253 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004254 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004255 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004256 }
4257}
4258
4259void ParallelMoveResolverX86::SpillScratch(int reg) {
4260 __ pushl(static_cast<Register>(reg));
4261}
4262
4263void ParallelMoveResolverX86::RestoreScratch(int reg) {
4264 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004265}
4266
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004267void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004268 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4269 ? LocationSummary::kCallOnSlowPath
4270 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004271 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004272 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004273 locations->SetOut(Location::RequiresRegister());
4274}
4275
4276void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004277 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004278 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004279 DCHECK(!cls->CanCallRuntime());
4280 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004281 codegen_->LoadCurrentMethod(out);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07004282 __ movl(out, Address(out, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004283 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004284 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004285 codegen_->LoadCurrentMethod(out);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07004286 __ movl(out, Address(out, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004287 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004288
4289 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4290 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4291 codegen_->AddSlowPath(slow_path);
4292 __ testl(out, out);
4293 __ j(kEqual, slow_path->GetEntryLabel());
4294 if (cls->MustGenerateClinitCheck()) {
4295 GenerateClassInitializationCheck(slow_path, out);
4296 } else {
4297 __ Bind(slow_path->GetExitLabel());
4298 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004299 }
4300}
4301
4302void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4303 LocationSummary* locations =
4304 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4305 locations->SetInAt(0, Location::RequiresRegister());
4306 if (check->HasUses()) {
4307 locations->SetOut(Location::SameAsFirstInput());
4308 }
4309}
4310
4311void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004312 // We assume the class to not be null.
4313 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4314 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004315 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004316 GenerateClassInitializationCheck(slow_path,
4317 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004318}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004319
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004320void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4321 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004322 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4323 Immediate(mirror::Class::kStatusInitialized));
4324 __ j(kLess, slow_path->GetEntryLabel());
4325 __ Bind(slow_path->GetExitLabel());
4326 // No need for memory fence, thanks to the X86 memory model.
4327}
4328
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004329void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4330 LocationSummary* locations =
4331 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4332 locations->SetOut(Location::RequiresRegister());
4333}
4334
4335void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4336 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4337 codegen_->AddSlowPath(slow_path);
4338
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004340 codegen_->LoadCurrentMethod(out);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07004341 __ movl(out, Address(out, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004342 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004343 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4344 __ testl(out, out);
4345 __ j(kEqual, slow_path->GetEntryLabel());
4346 __ Bind(slow_path->GetExitLabel());
4347}
4348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004349void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4350 LocationSummary* locations =
4351 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4352 locations->SetOut(Location::RequiresRegister());
4353}
4354
4355void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4356 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004357 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004358 __ fs()->movl(address, Immediate(0));
4359}
4360
4361void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4362 LocationSummary* locations =
4363 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4364 InvokeRuntimeCallingConvention calling_convention;
4365 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4366}
4367
4368void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4369 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4370 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4371}
4372
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004373void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004374 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4375 ? LocationSummary::kNoCall
4376 : LocationSummary::kCallOnSlowPath;
4377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4378 locations->SetInAt(0, Location::RequiresRegister());
4379 locations->SetInAt(1, Location::Any());
4380 locations->SetOut(Location::RequiresRegister());
4381}
4382
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004383void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004384 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004386 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004387 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004388 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4389 Label done, zero;
4390 SlowPathCodeX86* slow_path = nullptr;
4391
4392 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004393 // Avoid null check if we know obj is not null.
4394 if (instruction->MustDoNullCheck()) {
4395 __ testl(obj, obj);
4396 __ j(kEqual, &zero);
4397 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004398 __ movl(out, Address(obj, class_offset));
4399 // Compare the class of `obj` with `cls`.
4400 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004401 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004402 } else {
4403 DCHECK(cls.IsStackSlot()) << cls;
4404 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4405 }
4406
4407 if (instruction->IsClassFinal()) {
4408 // Classes must be equal for the instanceof to succeed.
4409 __ j(kNotEqual, &zero);
4410 __ movl(out, Immediate(1));
4411 __ jmp(&done);
4412 } else {
4413 // If the classes are not equal, we go into a slow path.
4414 DCHECK(locations->OnlyCallsOnSlowPath());
4415 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004416 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004417 codegen_->AddSlowPath(slow_path);
4418 __ j(kNotEqual, slow_path->GetEntryLabel());
4419 __ movl(out, Immediate(1));
4420 __ jmp(&done);
4421 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004422
4423 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4424 __ Bind(&zero);
4425 __ movl(out, Immediate(0));
4426 }
4427
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004428 if (slow_path != nullptr) {
4429 __ Bind(slow_path->GetExitLabel());
4430 }
4431 __ Bind(&done);
4432}
4433
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004434void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4436 instruction, LocationSummary::kCallOnSlowPath);
4437 locations->SetInAt(0, Location::RequiresRegister());
4438 locations->SetInAt(1, Location::Any());
4439 locations->AddTemp(Location::RequiresRegister());
4440}
4441
4442void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4443 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004444 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004445 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004446 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004447 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4448 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4449 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4450 codegen_->AddSlowPath(slow_path);
4451
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004452 // Avoid null check if we know obj is not null.
4453 if (instruction->MustDoNullCheck()) {
4454 __ testl(obj, obj);
4455 __ j(kEqual, slow_path->GetExitLabel());
4456 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004457
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004458 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004459 // Compare the class of `obj` with `cls`.
4460 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004462 } else {
4463 DCHECK(cls.IsStackSlot()) << cls;
4464 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4465 }
4466
4467 __ j(kNotEqual, slow_path->GetEntryLabel());
4468 __ Bind(slow_path->GetExitLabel());
4469}
4470
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004471void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4472 LocationSummary* locations =
4473 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4474 InvokeRuntimeCallingConvention calling_convention;
4475 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4476}
4477
4478void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4479 __ fs()->call(Address::Absolute(instruction->IsEnter()
4480 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4481 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4482 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4483}
4484
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004485void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4486void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4487void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4488
4489void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4490 LocationSummary* locations =
4491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4492 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4493 || instruction->GetResultType() == Primitive::kPrimLong);
4494 locations->SetInAt(0, Location::RequiresRegister());
4495 locations->SetInAt(1, Location::Any());
4496 locations->SetOut(Location::SameAsFirstInput());
4497}
4498
4499void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4500 HandleBitwiseOperation(instruction);
4501}
4502
4503void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4504 HandleBitwiseOperation(instruction);
4505}
4506
4507void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4508 HandleBitwiseOperation(instruction);
4509}
4510
4511void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4512 LocationSummary* locations = instruction->GetLocations();
4513 Location first = locations->InAt(0);
4514 Location second = locations->InAt(1);
4515 DCHECK(first.Equals(locations->Out()));
4516
4517 if (instruction->GetResultType() == Primitive::kPrimInt) {
4518 if (second.IsRegister()) {
4519 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004520 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004521 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004522 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004523 } else {
4524 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004525 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004526 }
4527 } else if (second.IsConstant()) {
4528 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004529 __ andl(first.AsRegister<Register>(),
4530 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004531 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004532 __ orl(first.AsRegister<Register>(),
4533 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004534 } else {
4535 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004536 __ xorl(first.AsRegister<Register>(),
4537 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004538 }
4539 } else {
4540 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004541 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004542 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004543 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004544 } else {
4545 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004547 }
4548 }
4549 } else {
4550 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4551 if (second.IsRegisterPair()) {
4552 if (instruction->IsAnd()) {
4553 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4554 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4555 } else if (instruction->IsOr()) {
4556 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4557 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4558 } else {
4559 DCHECK(instruction->IsXor());
4560 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4561 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4562 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004563 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004564 if (instruction->IsAnd()) {
4565 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4566 __ andl(first.AsRegisterPairHigh<Register>(),
4567 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4568 } else if (instruction->IsOr()) {
4569 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4570 __ orl(first.AsRegisterPairHigh<Register>(),
4571 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4572 } else {
4573 DCHECK(instruction->IsXor());
4574 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4575 __ xorl(first.AsRegisterPairHigh<Register>(),
4576 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4577 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004578 } else {
4579 DCHECK(second.IsConstant()) << second;
4580 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004581 int32_t low_value = Low32Bits(value);
4582 int32_t high_value = High32Bits(value);
4583 Immediate low(low_value);
4584 Immediate high(high_value);
4585 Register first_low = first.AsRegisterPairLow<Register>();
4586 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004587 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004588 if (low_value == 0) {
4589 __ xorl(first_low, first_low);
4590 } else if (low_value != -1) {
4591 __ andl(first_low, low);
4592 }
4593 if (high_value == 0) {
4594 __ xorl(first_high, first_high);
4595 } else if (high_value != -1) {
4596 __ andl(first_high, high);
4597 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004598 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004599 if (low_value != 0) {
4600 __ orl(first_low, low);
4601 }
4602 if (high_value != 0) {
4603 __ orl(first_high, high);
4604 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004605 } else {
4606 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004607 if (low_value != 0) {
4608 __ xorl(first_low, low);
4609 }
4610 if (high_value != 0) {
4611 __ xorl(first_high, high);
4612 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004613 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004614 }
4615 }
4616}
4617
Calin Juravleb1498f62015-02-16 13:13:29 +00004618void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4619 // Nothing to do, this should be removed during prepare for register allocator.
4620 UNUSED(instruction);
4621 LOG(FATAL) << "Unreachable";
4622}
4623
4624void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4625 // Nothing to do, this should be removed during prepare for register allocator.
4626 UNUSED(instruction);
4627 LOG(FATAL) << "Unreachable";
4628}
4629
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004630} // namespace x86
4631} // namespace art