blob: 597d27eb10208cddaef9c7e6a0a8e2a9a853ca33 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
39
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000044static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
47
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010048class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Alexandre Rames2ed20af2015-03-06 13:55:35 +000052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 __ Bind(GetEntryLabel());
54 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070055 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 }
57
58 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010059 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
61};
62
Calin Juravled0d48522014-11-04 16:40:20 +000063class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
64 public:
65 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
66
Alexandre Rames2ed20af2015-03-06 13:55:35 +000067 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000068 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070070 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000071 }
72
73 private:
74 HDivZeroCheck* const instruction_;
75 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
76};
77
Calin Juravlebacfec32014-11-14 15:54:36 +000078class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000080 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000081
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000083 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000084 if (is_div_) {
85 __ negl(reg_);
86 } else {
87 __ movl(reg_, Immediate(0));
88 }
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ jmp(GetExitLabel());
90 }
91
92 private:
93 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000094 bool is_div_;
95 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000096};
97
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010098class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100100 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
101 Location index_location,
102 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000103 : instruction_(instruction),
104 index_location_(index_location),
105 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100109 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000110 // We're moving two locations to locations that could overlap, so we need a parallel
111 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000113 x86_codegen->EmitParallelMoves(
114 index_location_,
115 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100116 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100118 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
119 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100120 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700121 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100122 }
123
124 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 const Location index_location_;
127 const Location length_location_;
128
129 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
130};
131
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000137 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000140 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700142 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 if (successor_ == nullptr) {
145 __ jmp(GetReturnLabel());
146 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 }
150
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100151 Label* GetReturnLabel() {
152 DCHECK(successor_ == nullptr);
153 return &return_label_;
154 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000155
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100156 HBasicBlock* GetSuccessor() const {
157 return successor_;
158 }
159
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 private:
161 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 Label return_label_;
164
165 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
166};
167
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000168class LoadStringSlowPathX86 : public SlowPathCodeX86 {
169 public:
170 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
171
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000173 LocationSummary* locations = instruction_->GetLocations();
174 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
175
176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179
180 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800181 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000184 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000185 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000186
187 __ jmp(GetExitLabel());
188 }
189
190 private:
191 HLoadString* const instruction_;
192
193 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
194};
195
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196class LoadClassSlowPathX86 : public SlowPathCodeX86 {
197 public:
198 LoadClassSlowPathX86(HLoadClass* cls,
199 HInstruction* at,
200 uint32_t dex_pc,
201 bool do_clinit)
202 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
203 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
204 }
205
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 LocationSummary* locations = at_->GetLocations();
208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
209 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211
212 InvokeRuntimeCallingConvention calling_convention;
213 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ fs()->call(Address::Absolute(do_clinit_
215 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
216 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000218
219 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000220 Location out = locations->Out();
221 if (out.IsValid()) {
222 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
223 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000225
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ jmp(GetExitLabel());
228 }
229
230 private:
231 // The class this slow path will load.
232 HLoadClass* const cls_;
233
234 // The instruction where this slow path is happening.
235 // (Might be the load class or an initialization check).
236 HInstruction* const at_;
237
238 // The dex PC of `at_`.
239 const uint32_t dex_pc_;
240
241 // Whether to initialize the class.
242 const bool do_clinit_;
243
244 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
245};
246
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000247class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
248 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000249 TypeCheckSlowPathX86(HInstruction* instruction,
250 Location class_to_check,
251 Location object_class,
252 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000253 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000254 class_to_check_(class_to_check),
255 object_class_(object_class),
256 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000260 DCHECK(instruction_->IsCheckCast()
261 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
263 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266
267 // We're moving two locations to locations that could overlap, so we need a parallel
268 // move resolver.
269 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000270 x86_codegen->EmitParallelMoves(
271 class_to_check_,
272 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100273 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000274 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100275 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
276 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000279 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
280 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 } else {
282 DCHECK(instruction_->IsCheckCast());
283 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 if (instruction_->IsInstanceOf()) {
288 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
289 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 __ jmp(GetExitLabel());
293 }
294
295 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 HInstruction* const instruction_;
297 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
301 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
302};
303
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700304class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
305 public:
306 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
307 : instruction_(instruction) {}
308
309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
310 __ Bind(GetEntryLabel());
311 SaveLiveRegisters(codegen, instruction_->GetLocations());
312 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
313 // No need to restore live registers.
314 DCHECK(instruction_->IsDeoptimize());
315 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
316 uint32_t dex_pc = deoptimize->GetDexPc();
317 codegen->RecordPcInfo(instruction_, dex_pc, this);
318 }
319
320 private:
321 HInstruction* const instruction_;
322 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
323};
324
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100325#undef __
326#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
327
Dave Allison20dfc792014-06-16 20:44:29 -0700328inline Condition X86Condition(IfCondition cond) {
329 switch (cond) {
330 case kCondEQ: return kEqual;
331 case kCondNE: return kNotEqual;
332 case kCondLT: return kLess;
333 case kCondLE: return kLessEqual;
334 case kCondGT: return kGreater;
335 case kCondGE: return kGreaterEqual;
336 default:
337 LOG(FATAL) << "Unknown if condition";
338 }
339 return kEqual;
340}
341
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100343 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344}
345
346void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100347 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100348}
349
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100350size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
351 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
352 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100353}
354
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100355size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
356 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
357 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100358}
359
Mark Mendell7c8d0092015-01-26 11:21:33 -0500360size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
361 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
362 return GetFloatingPointSpillSlotSize();
363}
364
365size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
366 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
367 return GetFloatingPointSpillSlotSize();
368}
369
Mark Mendellfb8d2792015-03-31 22:16:59 -0400370CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
371 const X86InstructionSetFeatures& isa_features,
372 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500373 : CodeGenerator(graph,
374 kNumberOfCpuRegisters,
375 kNumberOfXmmRegisters,
376 kNumberOfRegisterPairs,
377 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
378 arraysize(kCoreCalleeSaves))
379 | (1 << kFakeReturnRegister),
380 0,
381 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100382 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100384 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400385 move_resolver_(graph->GetArena(), this),
386 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000387 // Use a fake return address register to mimic Quick.
388 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100389}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100391Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100392 switch (type) {
393 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100394 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100395 X86ManagedRegister pair =
396 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
400 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100401 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 }
404
405 case Primitive::kPrimByte:
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimChar:
408 case Primitive::kPrimShort:
409 case Primitive::kPrimInt:
410 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
415 X86ManagedRegister current =
416 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
417 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100419 }
420 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422 }
423
424 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100426 return Location::FpuRegisterLocation(
427 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100428 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100429
430 case Primitive::kPrimVoid:
431 LOG(FATAL) << "Unreachable type " << type;
432 }
433
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100434 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435}
436
Mark Mendell5f874182015-03-04 15:42:45 -0500437void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
441 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443
Mark Mendell5f874182015-03-04 15:42:45 -0500444 if (is_baseline) {
445 blocked_core_registers_[EBP] = true;
446 blocked_core_registers_[ESI] = true;
447 blocked_core_registers_[EDI] = true;
448 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100449
450 UpdateBlockedPairRegisters();
451}
452
453void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
454 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
455 X86ManagedRegister current =
456 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
457 if (blocked_core_registers_[current.AsRegisterPairLow()]
458 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
459 blocked_register_pairs_[i] = true;
460 }
461 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462}
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
465 : HGraphVisitor(graph),
466 assembler_(codegen->GetAssembler()),
467 codegen_(codegen) {}
468
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100469static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100470 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100471}
472
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000473void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100474 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000475 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000476 bool skip_overflow_check =
477 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000479
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100481 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100482 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100483 }
484
Mark Mendell5f874182015-03-04 15:42:45 -0500485 if (HasEmptyFrame()) {
486 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000487 }
Mark Mendell5f874182015-03-04 15:42:45 -0500488
489 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
490 Register reg = kCoreCalleeSaves[i];
491 if (allocated_registers_.ContainsCoreRegister(reg)) {
492 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100493 __ cfi().AdjustCFAOffset(kX86WordSize);
494 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500495 }
496 }
497
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100498 int adjust = GetFrameSize() - FrameEntrySpillSize();
499 __ subl(ESP, Immediate(adjust));
500 __ cfi().AdjustCFAOffset(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500501 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000502}
503
504void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100505 __ cfi().RememberState();
506 if (!HasEmptyFrame()) {
507 int adjust = GetFrameSize() - FrameEntrySpillSize();
508 __ addl(ESP, Immediate(adjust));
509 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500510
David Srbeckyc34dc932015-04-12 09:27:43 +0100511 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
512 Register reg = kCoreCalleeSaves[i];
513 if (allocated_registers_.ContainsCoreRegister(reg)) {
514 __ popl(reg);
515 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
516 __ cfi().Restore(DWARFReg(reg));
517 }
Mark Mendell5f874182015-03-04 15:42:45 -0500518 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000519 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100520 __ ret();
521 __ cfi().RestoreState();
522 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000523}
524
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100525void CodeGeneratorX86::Bind(HBasicBlock* block) {
526 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000527}
528
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100529void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000530 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100531 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000532}
533
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
535 switch (load->GetType()) {
536 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539
540 case Primitive::kPrimInt:
541 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100544
545 case Primitive::kPrimBoolean:
546 case Primitive::kPrimByte:
547 case Primitive::kPrimChar:
548 case Primitive::kPrimShort:
549 case Primitive::kPrimVoid:
550 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552 }
553
554 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700555 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100556}
557
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100558Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 switch (type) {
560 case Primitive::kPrimBoolean:
561 case Primitive::kPrimByte:
562 case Primitive::kPrimChar:
563 case Primitive::kPrimShort:
564 case Primitive::kPrimInt:
565 case Primitive::kPrimNot: {
566 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000567 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100569 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000571 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100572 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100573 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100574
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000575 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100576 uint32_t index = gp_index_;
577 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000578 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100579 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
581 calling_convention.GetRegisterPairAt(index));
582 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000584 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
585 }
586 }
587
588 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100589 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000590 stack_index_++;
591 if (index < calling_convention.GetNumberOfFpuRegisters()) {
592 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
593 } else {
594 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
595 }
596 }
597
598 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100599 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000600 stack_index_ += 2;
601 if (index < calling_convention.GetNumberOfFpuRegisters()) {
602 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
603 } else {
604 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100605 }
606 }
607
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 case Primitive::kPrimVoid:
609 LOG(FATAL) << "Unexpected parameter type " << type;
610 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100611 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 return Location();
613}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100614
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615void CodeGeneratorX86::Move32(Location destination, Location source) {
616 if (source.Equals(destination)) {
617 return;
618 }
619 if (destination.IsRegister()) {
620 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000621 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000623 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 } else {
625 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 } else if (destination.IsFpuRegister()) {
629 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000630 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000632 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 } else {
634 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000635 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000638 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000642 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500643 } else if (source.IsConstant()) {
644 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000645 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500646 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 } else {
648 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100649 __ pushl(Address(ESP, source.GetStackIndex()));
650 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 }
652 }
653}
654
655void CodeGeneratorX86::Move64(Location destination, Location source) {
656 if (source.Equals(destination)) {
657 return;
658 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 if (destination.IsRegisterPair()) {
660 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 EmitParallelMoves(
662 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
663 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100664 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000665 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100666 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
667 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 } else if (source.IsFpuRegister()) {
669 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000671 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
674 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
676 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100677 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500678 if (source.IsFpuRegister()) {
679 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
680 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000681 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 } else {
683 LOG(FATAL) << "Unimplemented";
684 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100685 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000686 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100687 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000688 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100691 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000693 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000694 } else if (source.IsConstant()) {
695 HConstant* constant = source.GetConstant();
696 int64_t value;
697 if (constant->IsLongConstant()) {
698 value = constant->AsLongConstant()->GetValue();
699 } else {
700 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000701 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000702 }
703 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
704 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000706 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 EmitParallelMoves(
708 Location::StackSlot(source.GetStackIndex()),
709 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100710 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000711 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100712 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
713 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 }
715 }
716}
717
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100718void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000719 LocationSummary* locations = instruction->GetLocations();
720 if (locations != nullptr && locations->Out().Equals(location)) {
721 return;
722 }
723
724 if (locations != nullptr && locations->Out().IsConstant()) {
725 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000726 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
727 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000728 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000730 } else if (location.IsStackSlot()) {
731 __ movl(Address(ESP, location.GetStackIndex()), imm);
732 } else {
733 DCHECK(location.IsConstant());
734 DCHECK_EQ(location.GetConstant(), const_to_move);
735 }
736 } else if (const_to_move->IsLongConstant()) {
737 int64_t value = const_to_move->AsLongConstant()->GetValue();
738 if (location.IsRegisterPair()) {
739 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
740 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
741 } else if (location.IsDoubleStackSlot()) {
742 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000743 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
744 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000745 } else {
746 DCHECK(location.IsConstant());
747 DCHECK_EQ(location.GetConstant(), instruction);
748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000750 } else if (instruction->IsTemporary()) {
751 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000752 if (temp_location.IsStackSlot()) {
753 Move32(location, temp_location);
754 } else {
755 DCHECK(temp_location.IsDoubleStackSlot());
756 Move64(location, temp_location);
757 }
Roland Levillain476df552014-10-09 17:51:36 +0100758 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 switch (instruction->GetType()) {
761 case Primitive::kPrimBoolean:
762 case Primitive::kPrimByte:
763 case Primitive::kPrimChar:
764 case Primitive::kPrimShort:
765 case Primitive::kPrimInt:
766 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 case Primitive::kPrimFloat:
768 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 break;
770
771 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 case Primitive::kPrimDouble:
773 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 break;
775
776 default:
777 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
778 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100780 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100781 switch (instruction->GetType()) {
782 case Primitive::kPrimBoolean:
783 case Primitive::kPrimByte:
784 case Primitive::kPrimChar:
785 case Primitive::kPrimShort:
786 case Primitive::kPrimInt:
787 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000789 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790 break;
791
792 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000794 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 break;
796
797 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100799 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800 }
801}
802
803void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805}
806
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000807void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000808 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100809 DCHECK(!successor->IsExitBlock());
810
811 HBasicBlock* block = got->GetBlock();
812 HInstruction* previous = got->GetPrevious();
813
814 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000815 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100816 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
817 return;
818 }
819
820 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
821 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
822 }
823 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000825 }
826}
827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000828void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000830}
831
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700833 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000834}
835
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700836void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
837 Label* true_target,
838 Label* false_target,
839 Label* always_true_target) {
840 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 if (cond->IsIntConstant()) {
842 // Constant condition, statically compared against 1.
843 int32_t cond_value = cond->AsIntConstant()->GetValue();
844 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700845 if (always_true_target != nullptr) {
846 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 DCHECK_EQ(cond_value, 0);
851 }
852 } else {
853 bool materialized =
854 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
855 // Moves do not affect the eflags register, so if the condition is
856 // evaluated just before the if, we don't need to evaluate it
857 // again.
858 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700859 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100860 if (materialized) {
861 if (!eflags_set) {
862 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700863 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500865 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100866 } else {
867 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
868 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700869 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700871 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100872 }
873 } else {
874 Location lhs = cond->GetLocations()->InAt(0);
875 Location rhs = cond->GetLocations()->InAt(1);
876 // LHS is guaranteed to be in a register (see
877 // LocationsBuilderX86::VisitCondition).
878 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000879 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100880 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100881 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500882 if (constant == 0) {
883 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
884 } else {
885 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
886 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100889 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700890 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700891 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700893 if (false_target != nullptr) {
894 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895 }
896}
897
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700898void LocationsBuilderX86::VisitIf(HIf* if_instr) {
899 LocationSummary* locations =
900 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
901 HInstruction* cond = if_instr->InputAt(0);
902 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
903 locations->SetInAt(0, Location::Any());
904 }
905}
906
907void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
908 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
909 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
910 Label* always_true_target = true_target;
911 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
912 if_instr->IfTrueSuccessor())) {
913 always_true_target = nullptr;
914 }
915 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
916 if_instr->IfFalseSuccessor())) {
917 false_target = nullptr;
918 }
919 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
920}
921
922void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
923 LocationSummary* locations = new (GetGraph()->GetArena())
924 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
925 HInstruction* cond = deoptimize->InputAt(0);
926 DCHECK(cond->IsCondition());
927 if (cond->AsCondition()->NeedsMaterialization()) {
928 locations->SetInAt(0, Location::Any());
929 }
930}
931
932void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
933 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
934 DeoptimizationSlowPathX86(deoptimize);
935 codegen_->AddSlowPath(slow_path);
936 Label* slow_path_entry = slow_path->GetEntryLabel();
937 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
938}
939
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000940void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000942}
943
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000944void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
945 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000946}
947
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000948void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100949 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000950}
951
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000952void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100953 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700954 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000955}
956
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100958 LocationSummary* locations =
959 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 switch (store->InputAt(1)->GetType()) {
961 case Primitive::kPrimBoolean:
962 case Primitive::kPrimByte:
963 case Primitive::kPrimChar:
964 case Primitive::kPrimShort:
965 case Primitive::kPrimInt:
966 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
969 break;
970
971 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100972 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100973 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
974 break;
975
976 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100977 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 }
979 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000980}
981
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Dave Allison20dfc792014-06-16 20:44:29 -0700986void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100989 locations->SetInAt(0, Location::RequiresRegister());
990 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100991 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500992 // We need a byte register.
993 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100994 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995}
996
Dave Allison20dfc792014-06-16 20:44:29 -0700997void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
998 if (comp->NeedsMaterialization()) {
999 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001001 // Clear register: setcc only sets the low byte.
1002 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001003 Location lhs = locations->InAt(0);
1004 Location rhs = locations->InAt(1);
1005 if (rhs.IsRegister()) {
1006 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1007 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001008 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001009 if (constant == 0) {
1010 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1011 } else {
1012 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1013 }
Dave Allison20dfc792014-06-16 20:44:29 -07001014 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001015 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001016 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001017 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001018 }
Dave Allison20dfc792014-06-16 20:44:29 -07001019}
1020
1021void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1022 VisitCondition(comp);
1023}
1024
1025void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1026 VisitCondition(comp);
1027}
1028
1029void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1030 VisitCondition(comp);
1031}
1032
1033void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1034 VisitCondition(comp);
1035}
1036
1037void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1038 VisitCondition(comp);
1039}
1040
1041void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1042 VisitCondition(comp);
1043}
1044
1045void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1046 VisitCondition(comp);
1047}
1048
1049void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1062 VisitCondition(comp);
1063}
1064
1065void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1066 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067}
1068
1069void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001070 LocationSummary* locations =
1071 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001072 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001073}
1074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001075void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001076 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001077 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001078}
1079
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001080void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1081 LocationSummary* locations =
1082 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1083 locations->SetOut(Location::ConstantLocation(constant));
1084}
1085
1086void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1087 // Will be generated at use site.
1088 UNUSED(constant);
1089}
1090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001092 LocationSummary* locations =
1093 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001094 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095}
1096
1097void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1098 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100}
1101
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001102void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1105 locations->SetOut(Location::ConstantLocation(constant));
1106}
1107
1108void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1109 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001110 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001111}
1112
1113void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1116 locations->SetOut(Location::ConstantLocation(constant));
1117}
1118
1119void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1120 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001121 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001122}
1123
Calin Juravle27df7582015-04-17 19:12:31 +01001124void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1125 memory_barrier->SetLocations(nullptr);
1126}
1127
1128void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1129 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1130}
1131
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001132void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001134}
1135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001136void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001137 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001138 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001139}
1140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001141void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001144 switch (ret->InputAt(0)->GetType()) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte:
1147 case Primitive::kPrimChar:
1148 case Primitive::kPrimShort:
1149 case Primitive::kPrimInt:
1150 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 break;
1153
1154 case Primitive::kPrimLong:
1155 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 break;
1158
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001159 case Primitive::kPrimFloat:
1160 case Primitive::kPrimDouble:
1161 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 break;
1164
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001168}
1169
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001170void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 if (kIsDebugBuild) {
1172 switch (ret->InputAt(0)->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001179 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
1182 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1184 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 break;
1186
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 case Primitive::kPrimFloat:
1188 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001190 break;
1191
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001192 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001193 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001196 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001197}
1198
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001199void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001200 // When we do not run baseline, explicit clinit checks triggered by static
1201 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1202 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001203
Mark Mendellfb8d2792015-03-31 22:16:59 -04001204 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001205 if (intrinsic.TryDispatch(invoke)) {
1206 return;
1207 }
1208
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001209 HandleInvoke(invoke);
1210}
1211
Mark Mendell09ed1a32015-03-25 08:30:06 -04001212static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1213 if (invoke->GetLocations()->Intrinsified()) {
1214 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1215 intrinsic.Dispatch(invoke);
1216 return true;
1217 }
1218 return false;
1219}
1220
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001221void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001222 // When we do not run baseline, explicit clinit checks triggered by static
1223 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1224 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001225
Mark Mendell09ed1a32015-03-25 08:30:06 -04001226 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1227 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001228 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001229
Mark Mendell09ed1a32015-03-25 08:30:06 -04001230 codegen_->GenerateStaticOrDirectCall(
1231 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001232 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001233}
1234
1235void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1236 HandleInvoke(invoke);
1237}
1238
1239void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001240 LocationSummary* locations =
1241 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001242 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001243
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001244 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001245 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001246 HInstruction* input = invoke->InputAt(i);
1247 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1248 }
1249
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 switch (invoke->GetType()) {
1251 case Primitive::kPrimBoolean:
1252 case Primitive::kPrimByte:
1253 case Primitive::kPrimChar:
1254 case Primitive::kPrimShort:
1255 case Primitive::kPrimInt:
1256 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001257 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001258 break;
1259
1260 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001261 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 break;
1263
1264 case Primitive::kPrimVoid:
1265 break;
1266
1267 case Primitive::kPrimDouble:
1268 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001269 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 break;
1271 }
1272
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001273 invoke->SetLocations(locations);
1274}
1275
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001276void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001277 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001278 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1279 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1280 LocationSummary* locations = invoke->GetLocations();
1281 Location receiver = locations->InAt(0);
1282 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1283 // temp = object->GetClass();
1284 if (receiver.IsStackSlot()) {
1285 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1286 __ movl(temp, Address(temp, class_offset));
1287 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001288 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001289 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001290 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001291 // temp = temp->GetMethodAt(method_offset);
1292 __ movl(temp, Address(temp, method_offset));
1293 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001294 __ call(Address(
1295 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001296
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001297 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001299}
1300
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1302 HandleInvoke(invoke);
1303 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001304 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001305}
1306
1307void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1308 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1311 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1312 LocationSummary* locations = invoke->GetLocations();
1313 Location receiver = locations->InAt(0);
1314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1315
1316 // Set the hidden argument.
1317 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001318 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319
1320 // temp = object->GetClass();
1321 if (receiver.IsStackSlot()) {
1322 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1323 __ movl(temp, Address(temp, class_offset));
1324 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001327 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 // temp = temp->GetImtEntryAt(method_offset);
1329 __ movl(temp, Address(temp, method_offset));
1330 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001331 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001332 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001333
1334 DCHECK(!codegen_->IsLeafMethod());
1335 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1336}
1337
Roland Levillain88cb1752014-10-20 16:36:47 +01001338void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1339 LocationSummary* locations =
1340 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1341 switch (neg->GetResultType()) {
1342 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001343 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 locations->SetInAt(0, Location::RequiresRegister());
1345 locations->SetOut(Location::SameAsFirstInput());
1346 break;
1347
Roland Levillain88cb1752014-10-20 16:36:47 +01001348 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001349 locations->SetInAt(0, Location::RequiresFpuRegister());
1350 locations->SetOut(Location::SameAsFirstInput());
1351 locations->AddTemp(Location::RequiresRegister());
1352 locations->AddTemp(Location::RequiresFpuRegister());
1353 break;
1354
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001356 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001357 locations->SetOut(Location::SameAsFirstInput());
1358 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001359 break;
1360
1361 default:
1362 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1363 }
1364}
1365
1366void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1367 LocationSummary* locations = neg->GetLocations();
1368 Location out = locations->Out();
1369 Location in = locations->InAt(0);
1370 switch (neg->GetResultType()) {
1371 case Primitive::kPrimInt:
1372 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001375 break;
1376
1377 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001378 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001379 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001380 __ negl(out.AsRegisterPairLow<Register>());
1381 // Negation is similar to subtraction from zero. The least
1382 // significant byte triggers a borrow when it is different from
1383 // zero; to take it into account, add 1 to the most significant
1384 // byte if the carry flag (CF) is set to 1 after the first NEGL
1385 // operation.
1386 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1387 __ negl(out.AsRegisterPairHigh<Register>());
1388 break;
1389
Roland Levillain5368c212014-11-27 15:03:41 +00001390 case Primitive::kPrimFloat: {
1391 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001392 Register constant = locations->GetTemp(0).AsRegister<Register>();
1393 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001394 // Implement float negation with an exclusive or with value
1395 // 0x80000000 (mask for bit 31, representing the sign of a
1396 // single-precision floating-point number).
1397 __ movl(constant, Immediate(INT32_C(0x80000000)));
1398 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001400 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001401 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001402
Roland Levillain5368c212014-11-27 15:03:41 +00001403 case Primitive::kPrimDouble: {
1404 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001405 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001406 // Implement double negation with an exclusive or with value
1407 // 0x8000000000000000 (mask for bit 63, representing the sign of
1408 // a double-precision floating-point number).
1409 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001410 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001411 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001412 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001413
1414 default:
1415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1416 }
1417}
1418
Roland Levillaindff1f282014-11-05 14:15:05 +00001419void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 Primitive::Type result_type = conversion->GetResultType();
1421 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001422 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001423
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 // The float-to-long and double-to-long type conversions rely on a
1425 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001426 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001427 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1428 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001429 ? LocationSummary::kCall
1430 : LocationSummary::kNoCall;
1431 LocationSummary* locations =
1432 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1433
David Brazdilb2bd1c52015-03-25 11:17:37 +00001434 // The Java language does not allow treating boolean as an integral type but
1435 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001436
Roland Levillaindff1f282014-11-05 14:15:05 +00001437 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001438 case Primitive::kPrimByte:
1439 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001440 case Primitive::kPrimBoolean:
1441 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001442 case Primitive::kPrimShort:
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001445 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001446 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1447 // Make the output overlap to please the register allocator. This greatly simplifies
1448 // the validation of the linear scan implementation
1449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001450 break;
1451
1452 default:
1453 LOG(FATAL) << "Unexpected type conversion from " << input_type
1454 << " to " << result_type;
1455 }
1456 break;
1457
Roland Levillain01a8d712014-11-14 16:27:39 +00001458 case Primitive::kPrimShort:
1459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001460 case Primitive::kPrimBoolean:
1461 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001462 case Primitive::kPrimByte:
1463 case Primitive::kPrimInt:
1464 case Primitive::kPrimChar:
1465 // Processing a Dex `int-to-short' instruction.
1466 locations->SetInAt(0, Location::Any());
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 break;
1469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 }
1474 break;
1475
Roland Levillain946e1432014-11-11 17:35:19 +00001476 case Primitive::kPrimInt:
1477 switch (input_type) {
1478 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001479 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001480 locations->SetInAt(0, Location::Any());
1481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1482 break;
1483
1484 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001485 // Processing a Dex `float-to-int' instruction.
1486 locations->SetInAt(0, Location::RequiresFpuRegister());
1487 locations->SetOut(Location::RequiresRegister());
1488 locations->AddTemp(Location::RequiresFpuRegister());
1489 break;
1490
Roland Levillain946e1432014-11-11 17:35:19 +00001491 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001492 // Processing a Dex `double-to-int' instruction.
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetOut(Location::RequiresRegister());
1495 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001496 break;
1497
1498 default:
1499 LOG(FATAL) << "Unexpected type conversion from " << input_type
1500 << " to " << result_type;
1501 }
1502 break;
1503
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 case Primitive::kPrimLong:
1505 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001506 case Primitive::kPrimBoolean:
1507 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001508 case Primitive::kPrimByte:
1509 case Primitive::kPrimShort:
1510 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001511 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001512 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001513 locations->SetInAt(0, Location::RegisterLocation(EAX));
1514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1515 break;
1516
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001517 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001518 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001519 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001520 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001521 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1522 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1523
Vladimir Marko949c91f2015-01-27 10:48:44 +00001524 // The runtime helper puts the result in EAX, EDX.
1525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001526 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001527 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
1533 break;
1534
Roland Levillain981e4542014-11-14 11:47:14 +00001535 case Primitive::kPrimChar:
1536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001537 case Primitive::kPrimBoolean:
1538 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001539 case Primitive::kPrimByte:
1540 case Primitive::kPrimShort:
1541 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001542 // Processing a Dex `int-to-char' instruction.
1543 locations->SetInAt(0, Location::Any());
1544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551 break;
1552
Roland Levillaindff1f282014-11-05 14:15:05 +00001553 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001554 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001555 case Primitive::kPrimBoolean:
1556 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001557 case Primitive::kPrimByte:
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimInt:
1560 case Primitive::kPrimChar:
1561 // Processing a Dex `int-to-float' instruction.
1562 locations->SetInAt(0, Location::RequiresRegister());
1563 locations->SetOut(Location::RequiresFpuRegister());
1564 break;
1565
1566 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001567 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001568 locations->SetInAt(0, Location::Any());
1569 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001570 break;
1571
Roland Levillaincff13742014-11-17 14:32:17 +00001572 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001573 // Processing a Dex `double-to-float' instruction.
1574 locations->SetInAt(0, Location::RequiresFpuRegister());
1575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001576 break;
1577
1578 default:
1579 LOG(FATAL) << "Unexpected type conversion from " << input_type
1580 << " to " << result_type;
1581 };
1582 break;
1583
Roland Levillaindff1f282014-11-05 14:15:05 +00001584 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001586 case Primitive::kPrimBoolean:
1587 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001588 case Primitive::kPrimByte:
1589 case Primitive::kPrimShort:
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimChar:
1592 // Processing a Dex `int-to-double' instruction.
1593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetOut(Location::RequiresFpuRegister());
1595 break;
1596
1597 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001598 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001599 locations->SetInAt(0, Location::Any());
1600 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001601 break;
1602
Roland Levillaincff13742014-11-17 14:32:17 +00001603 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001604 // Processing a Dex `float-to-double' instruction.
1605 locations->SetInAt(0, Location::RequiresFpuRegister());
1606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001607 break;
1608
1609 default:
1610 LOG(FATAL) << "Unexpected type conversion from " << input_type
1611 << " to " << result_type;
1612 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 }
1619}
1620
1621void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1622 LocationSummary* locations = conversion->GetLocations();
1623 Location out = locations->Out();
1624 Location in = locations->InAt(0);
1625 Primitive::Type result_type = conversion->GetResultType();
1626 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001627 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001628 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001629 case Primitive::kPrimByte:
1630 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001631 case Primitive::kPrimBoolean:
1632 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001633 case Primitive::kPrimShort:
1634 case Primitive::kPrimInt:
1635 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001636 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001637 if (in.IsRegister()) {
1638 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001639 } else {
1640 DCHECK(in.GetConstant()->IsIntConstant());
1641 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1643 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001644 break;
1645
1646 default:
1647 LOG(FATAL) << "Unexpected type conversion from " << input_type
1648 << " to " << result_type;
1649 }
1650 break;
1651
Roland Levillain01a8d712014-11-14 16:27:39 +00001652 case Primitive::kPrimShort:
1653 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001654 case Primitive::kPrimBoolean:
1655 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001656 case Primitive::kPrimByte:
1657 case Primitive::kPrimInt:
1658 case Primitive::kPrimChar:
1659 // Processing a Dex `int-to-short' instruction.
1660 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001662 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001663 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001664 } else {
1665 DCHECK(in.GetConstant()->IsIntConstant());
1666 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001668 }
1669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
1675 break;
1676
Roland Levillain946e1432014-11-11 17:35:19 +00001677 case Primitive::kPrimInt:
1678 switch (input_type) {
1679 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001680 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001681 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001683 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001685 } else {
1686 DCHECK(in.IsConstant());
1687 DCHECK(in.GetConstant()->IsLongConstant());
1688 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001689 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001690 }
1691 break;
1692
Roland Levillain3f8f9362014-12-02 17:45:01 +00001693 case Primitive::kPrimFloat: {
1694 // Processing a Dex `float-to-int' instruction.
1695 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1696 Register output = out.AsRegister<Register>();
1697 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1698 Label done, nan;
1699
1700 __ movl(output, Immediate(kPrimIntMax));
1701 // temp = int-to-float(output)
1702 __ cvtsi2ss(temp, output);
1703 // if input >= temp goto done
1704 __ comiss(input, temp);
1705 __ j(kAboveEqual, &done);
1706 // if input == NaN goto nan
1707 __ j(kUnordered, &nan);
1708 // output = float-to-int-truncate(input)
1709 __ cvttss2si(output, input);
1710 __ jmp(&done);
1711 __ Bind(&nan);
1712 // output = 0
1713 __ xorl(output, output);
1714 __ Bind(&done);
1715 break;
1716 }
1717
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001718 case Primitive::kPrimDouble: {
1719 // Processing a Dex `double-to-int' instruction.
1720 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1721 Register output = out.AsRegister<Register>();
1722 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1723 Label done, nan;
1724
1725 __ movl(output, Immediate(kPrimIntMax));
1726 // temp = int-to-double(output)
1727 __ cvtsi2sd(temp, output);
1728 // if input >= temp goto done
1729 __ comisd(input, temp);
1730 __ j(kAboveEqual, &done);
1731 // if input == NaN goto nan
1732 __ j(kUnordered, &nan);
1733 // output = double-to-int-truncate(input)
1734 __ cvttsd2si(output, input);
1735 __ jmp(&done);
1736 __ Bind(&nan);
1737 // output = 0
1738 __ xorl(output, output);
1739 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001740 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001741 }
Roland Levillain946e1432014-11-11 17:35:19 +00001742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 case Primitive::kPrimLong:
1750 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001751 case Primitive::kPrimBoolean:
1752 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001753 case Primitive::kPrimByte:
1754 case Primitive::kPrimShort:
1755 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001756 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001757 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1759 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 __ cdq();
1762 break;
1763
1764 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001765 // Processing a Dex `float-to-long' instruction.
1766 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001767 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1768 break;
1769
Roland Levillaindff1f282014-11-05 14:15:05 +00001770 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001771 // Processing a Dex `double-to-long' instruction.
1772 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1773 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001774 break;
1775
1776 default:
1777 LOG(FATAL) << "Unexpected type conversion from " << input_type
1778 << " to " << result_type;
1779 }
1780 break;
1781
Roland Levillain981e4542014-11-14 11:47:14 +00001782 case Primitive::kPrimChar:
1783 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001784 case Primitive::kPrimBoolean:
1785 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001786 case Primitive::kPrimByte:
1787 case Primitive::kPrimShort:
1788 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001789 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1790 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001792 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001794 } else {
1795 DCHECK(in.GetConstant()->IsIntConstant());
1796 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001797 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001798 }
1799 break;
1800
1801 default:
1802 LOG(FATAL) << "Unexpected type conversion from " << input_type
1803 << " to " << result_type;
1804 }
1805 break;
1806
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001808 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001809 case Primitive::kPrimBoolean:
1810 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001811 case Primitive::kPrimByte:
1812 case Primitive::kPrimShort:
1813 case Primitive::kPrimInt:
1814 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001815 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001816 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001817 break;
1818
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819 case Primitive::kPrimLong: {
1820 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001821 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822
Roland Levillain232ade02015-04-20 15:14:36 +01001823 // Create stack space for the call to
1824 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1825 // TODO: enhance register allocator to ask for stack temporaries.
1826 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1827 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1828 __ subl(ESP, Immediate(adjustment));
1829 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001830
Roland Levillain232ade02015-04-20 15:14:36 +01001831 // Load the value to the FP stack, using temporaries if needed.
1832 PushOntoFPStack(in, 0, adjustment, false, true);
1833
1834 if (out.IsStackSlot()) {
1835 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1836 } else {
1837 __ fstps(Address(ESP, 0));
1838 Location stack_temp = Location::StackSlot(0);
1839 codegen_->Move32(out, stack_temp);
1840 }
1841
1842 // Remove the temporary stack space we allocated.
1843 if (adjustment != 0) {
1844 __ addl(ESP, Immediate(adjustment));
1845 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001846 break;
1847 }
1848
Roland Levillaincff13742014-11-17 14:32:17 +00001849 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001850 // Processing a Dex `double-to-float' instruction.
1851 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001852 break;
1853
1854 default:
1855 LOG(FATAL) << "Unexpected type conversion from " << input_type
1856 << " to " << result_type;
1857 };
1858 break;
1859
Roland Levillaindff1f282014-11-05 14:15:05 +00001860 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001861 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001862 case Primitive::kPrimBoolean:
1863 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001864 case Primitive::kPrimByte:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001868 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001869 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001870 break;
1871
Roland Levillain647b9ed2014-11-27 12:06:00 +00001872 case Primitive::kPrimLong: {
1873 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001874 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001875
Roland Levillain232ade02015-04-20 15:14:36 +01001876 // Create stack space for the call to
1877 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1878 // TODO: enhance register allocator to ask for stack temporaries.
1879 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1880 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1881 __ subl(ESP, Immediate(adjustment));
1882 }
1883
1884 // Load the value to the FP stack, using temporaries if needed.
1885 PushOntoFPStack(in, 0, adjustment, false, true);
1886
1887 if (out.IsDoubleStackSlot()) {
1888 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1889 } else {
1890 __ fstpl(Address(ESP, 0));
1891 Location stack_temp = Location::DoubleStackSlot(0);
1892 codegen_->Move64(out, stack_temp);
1893 }
1894
1895 // Remove the temporary stack space we allocated.
1896 if (adjustment != 0) {
1897 __ addl(ESP, Immediate(adjustment));
1898 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001899 break;
1900 }
1901
Roland Levillaincff13742014-11-17 14:32:17 +00001902 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001903 // Processing a Dex `float-to-double' instruction.
1904 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001905 break;
1906
1907 default:
1908 LOG(FATAL) << "Unexpected type conversion from " << input_type
1909 << " to " << result_type;
1910 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001911 break;
1912
1913 default:
1914 LOG(FATAL) << "Unexpected type conversion from " << input_type
1915 << " to " << result_type;
1916 }
1917}
1918
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001919void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001922 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001923 case Primitive::kPrimInt: {
1924 locations->SetInAt(0, Location::RequiresRegister());
1925 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1927 break;
1928 }
1929
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetInAt(1, Location::Any());
1933 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 break;
1935 }
1936
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 case Primitive::kPrimFloat:
1938 case Primitive::kPrimDouble: {
1939 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001940 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001946 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1947 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949}
1950
1951void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1952 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 Location first = locations->InAt(0);
1954 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001955 Location out = locations->Out();
1956
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001957 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001960 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1961 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001962 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1963 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001964 } else {
1965 __ leal(out.AsRegister<Register>(), Address(
1966 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1967 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001968 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001969 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1970 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1971 __ addl(out.AsRegister<Register>(), Immediate(value));
1972 } else {
1973 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1974 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001975 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001976 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001978 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001979 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001980 }
1981
1982 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001983 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001984 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1985 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001986 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001987 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1988 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001989 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001990 } else {
1991 DCHECK(second.IsConstant()) << second;
1992 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1993 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1994 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001995 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001996 break;
1997 }
1998
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001999 case Primitive::kPrimFloat: {
2000 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002004 }
2005
2006 case Primitive::kPrimDouble: {
2007 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002008 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 }
2010 break;
2011 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002014 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002015 }
2016}
2017
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002018void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002021 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002022 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::Any());
2026 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002027 break;
2028 }
Calin Juravle11351682014-10-23 15:38:15 +01002029 case Primitive::kPrimFloat:
2030 case Primitive::kPrimDouble: {
2031 locations->SetInAt(0, Location::RequiresFpuRegister());
2032 locations->SetInAt(1, Location::RequiresFpuRegister());
2033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034 break;
Calin Juravle11351682014-10-23 15:38:15 +01002035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002036
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037 default:
Calin Juravle11351682014-10-23 15:38:15 +01002038 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002039 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002040}
2041
2042void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2043 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 Location first = locations->InAt(0);
2045 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002046 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002047 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002048 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002052 __ subl(first.AsRegister<Register>(),
2053 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002054 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002056 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002057 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058 }
2059
2060 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002061 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002062 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2063 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002064 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002065 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002066 __ sbbl(first.AsRegisterPairHigh<Register>(),
2067 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002068 } else {
2069 DCHECK(second.IsConstant()) << second;
2070 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2071 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2072 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002073 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074 break;
2075 }
2076
Calin Juravle11351682014-10-23 15:38:15 +01002077 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002079 break;
Calin Juravle11351682014-10-23 15:38:15 +01002080 }
2081
2082 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002084 break;
2085 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002086
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002087 default:
Calin Juravle11351682014-10-23 15:38:15 +01002088 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002089 }
2090}
2091
Calin Juravle34bacdf2014-10-07 20:23:36 +01002092void LocationsBuilderX86::VisitMul(HMul* mul) {
2093 LocationSummary* locations =
2094 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2095 switch (mul->GetResultType()) {
2096 case Primitive::kPrimInt:
2097 locations->SetInAt(0, Location::RequiresRegister());
2098 locations->SetInAt(1, Location::Any());
2099 locations->SetOut(Location::SameAsFirstInput());
2100 break;
2101 case Primitive::kPrimLong: {
2102 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002103 locations->SetInAt(1, Location::Any());
2104 locations->SetOut(Location::SameAsFirstInput());
2105 // Needed for imul on 32bits with 64bits output.
2106 locations->AddTemp(Location::RegisterLocation(EAX));
2107 locations->AddTemp(Location::RegisterLocation(EDX));
2108 break;
2109 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002110 case Primitive::kPrimFloat:
2111 case Primitive::kPrimDouble: {
2112 locations->SetInAt(0, Location::RequiresFpuRegister());
2113 locations->SetInAt(1, Location::RequiresFpuRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002116 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002117
2118 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002119 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002120 }
2121}
2122
2123void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2124 LocationSummary* locations = mul->GetLocations();
2125 Location first = locations->InAt(0);
2126 Location second = locations->InAt(1);
2127 DCHECK(first.Equals(locations->Out()));
2128
2129 switch (mul->GetResultType()) {
2130 case Primitive::kPrimInt: {
2131 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002133 } else if (second.IsConstant()) {
2134 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136 } else {
2137 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139 }
2140 break;
2141 }
2142
2143 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144 Register in1_hi = first.AsRegisterPairHigh<Register>();
2145 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 Register eax = locations->GetTemp(0).AsRegister<Register>();
2147 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002148
2149 DCHECK_EQ(EAX, eax);
2150 DCHECK_EQ(EDX, edx);
2151
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002152 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002153 // output: in1
2154 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2155 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2156 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002157 if (second.IsConstant()) {
2158 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002159
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002160 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2161 int32_t low_value = Low32Bits(value);
2162 int32_t high_value = High32Bits(value);
2163 Immediate low(low_value);
2164 Immediate high(high_value);
2165
2166 __ movl(eax, high);
2167 // eax <- in1.lo * in2.hi
2168 __ imull(eax, in1_lo);
2169 // in1.hi <- in1.hi * in2.lo
2170 __ imull(in1_hi, low);
2171 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2172 __ addl(in1_hi, eax);
2173 // move in2_lo to eax to prepare for double precision
2174 __ movl(eax, low);
2175 // edx:eax <- in1.lo * in2.lo
2176 __ mull(in1_lo);
2177 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2178 __ addl(in1_hi, edx);
2179 // in1.lo <- (in1.lo * in2.lo)[31:0];
2180 __ movl(in1_lo, eax);
2181 } else if (second.IsRegisterPair()) {
2182 Register in2_hi = second.AsRegisterPairHigh<Register>();
2183 Register in2_lo = second.AsRegisterPairLow<Register>();
2184
2185 __ movl(eax, in2_hi);
2186 // eax <- in1.lo * in2.hi
2187 __ imull(eax, in1_lo);
2188 // in1.hi <- in1.hi * in2.lo
2189 __ imull(in1_hi, in2_lo);
2190 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2191 __ addl(in1_hi, eax);
2192 // move in1_lo to eax to prepare for double precision
2193 __ movl(eax, in1_lo);
2194 // edx:eax <- in1.lo * in2.lo
2195 __ mull(in2_lo);
2196 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2197 __ addl(in1_hi, edx);
2198 // in1.lo <- (in1.lo * in2.lo)[31:0];
2199 __ movl(in1_lo, eax);
2200 } else {
2201 DCHECK(second.IsDoubleStackSlot()) << second;
2202 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2203 Address in2_lo(ESP, second.GetStackIndex());
2204
2205 __ movl(eax, in2_hi);
2206 // eax <- in1.lo * in2.hi
2207 __ imull(eax, in1_lo);
2208 // in1.hi <- in1.hi * in2.lo
2209 __ imull(in1_hi, in2_lo);
2210 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2211 __ addl(in1_hi, eax);
2212 // move in1_lo to eax to prepare for double precision
2213 __ movl(eax, in1_lo);
2214 // edx:eax <- in1.lo * in2.lo
2215 __ mull(in2_lo);
2216 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2217 __ addl(in1_hi, edx);
2218 // in1.lo <- (in1.lo * in2.lo)[31:0];
2219 __ movl(in1_lo, eax);
2220 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002221
2222 break;
2223 }
2224
Calin Juravleb5bfa962014-10-21 18:02:24 +01002225 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002226 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002227 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002228 }
2229
2230 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002231 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002232 break;
2233 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002234
2235 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002236 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002237 }
2238}
2239
Roland Levillain232ade02015-04-20 15:14:36 +01002240void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2241 uint32_t temp_offset,
2242 uint32_t stack_adjustment,
2243 bool is_fp,
2244 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002245 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002246 DCHECK(!is_wide);
2247 if (is_fp) {
2248 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2249 } else {
2250 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2251 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002252 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002253 DCHECK(is_wide);
2254 if (is_fp) {
2255 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2256 } else {
2257 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2258 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002259 } else {
2260 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002261 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002262 Location stack_temp = Location::StackSlot(temp_offset);
2263 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002264 if (is_fp) {
2265 __ flds(Address(ESP, temp_offset));
2266 } else {
2267 __ filds(Address(ESP, temp_offset));
2268 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002269 } else {
2270 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2271 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002272 if (is_fp) {
2273 __ fldl(Address(ESP, temp_offset));
2274 } else {
2275 __ fildl(Address(ESP, temp_offset));
2276 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002277 }
2278 }
2279}
2280
2281void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2282 Primitive::Type type = rem->GetResultType();
2283 bool is_float = type == Primitive::kPrimFloat;
2284 size_t elem_size = Primitive::ComponentSize(type);
2285 LocationSummary* locations = rem->GetLocations();
2286 Location first = locations->InAt(0);
2287 Location second = locations->InAt(1);
2288 Location out = locations->Out();
2289
2290 // Create stack space for 2 elements.
2291 // TODO: enhance register allocator to ask for stack temporaries.
2292 __ subl(ESP, Immediate(2 * elem_size));
2293
2294 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002295 const bool is_wide = !is_float;
2296 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2297 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002298
2299 // Loop doing FPREM until we stabilize.
2300 Label retry;
2301 __ Bind(&retry);
2302 __ fprem();
2303
2304 // Move FP status to AX.
2305 __ fstsw();
2306
2307 // And see if the argument reduction is complete. This is signaled by the
2308 // C2 FPU flag bit set to 0.
2309 __ andl(EAX, Immediate(kC2ConditionMask));
2310 __ j(kNotEqual, &retry);
2311
2312 // We have settled on the final value. Retrieve it into an XMM register.
2313 // Store FP top of stack to real stack.
2314 if (is_float) {
2315 __ fsts(Address(ESP, 0));
2316 } else {
2317 __ fstl(Address(ESP, 0));
2318 }
2319
2320 // Pop the 2 items from the FP stack.
2321 __ fucompp();
2322
2323 // Load the value from the stack into an XMM register.
2324 DCHECK(out.IsFpuRegister()) << out;
2325 if (is_float) {
2326 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2327 } else {
2328 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2329 }
2330
2331 // And remove the temporary stack space we allocated.
2332 __ addl(ESP, Immediate(2 * elem_size));
2333}
2334
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002335
2336void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2337 DCHECK(instruction->IsDiv() || instruction->IsRem());
2338
2339 LocationSummary* locations = instruction->GetLocations();
2340 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002341 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002342
2343 Register out_register = locations->Out().AsRegister<Register>();
2344 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002345 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002346
2347 DCHECK(imm == 1 || imm == -1);
2348
2349 if (instruction->IsRem()) {
2350 __ xorl(out_register, out_register);
2351 } else {
2352 __ movl(out_register, input_register);
2353 if (imm == -1) {
2354 __ negl(out_register);
2355 }
2356 }
2357}
2358
2359
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002360void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002361 LocationSummary* locations = instruction->GetLocations();
2362
2363 Register out_register = locations->Out().AsRegister<Register>();
2364 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002365 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002366
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002367 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002368 Register num = locations->GetTemp(0).AsRegister<Register>();
2369
2370 __ leal(num, Address(input_register, std::abs(imm) - 1));
2371 __ testl(input_register, input_register);
2372 __ cmovl(kGreaterEqual, num, input_register);
2373 int shift = CTZ(imm);
2374 __ sarl(num, Immediate(shift));
2375
2376 if (imm < 0) {
2377 __ negl(num);
2378 }
2379
2380 __ movl(out_register, num);
2381}
2382
2383void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2384 DCHECK(instruction->IsDiv() || instruction->IsRem());
2385
2386 LocationSummary* locations = instruction->GetLocations();
2387 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2388
2389 Register eax = locations->InAt(0).AsRegister<Register>();
2390 Register out = locations->Out().AsRegister<Register>();
2391 Register num;
2392 Register edx;
2393
2394 if (instruction->IsDiv()) {
2395 edx = locations->GetTemp(0).AsRegister<Register>();
2396 num = locations->GetTemp(1).AsRegister<Register>();
2397 } else {
2398 edx = locations->Out().AsRegister<Register>();
2399 num = locations->GetTemp(0).AsRegister<Register>();
2400 }
2401
2402 DCHECK_EQ(EAX, eax);
2403 DCHECK_EQ(EDX, edx);
2404 if (instruction->IsDiv()) {
2405 DCHECK_EQ(EAX, out);
2406 } else {
2407 DCHECK_EQ(EDX, out);
2408 }
2409
2410 int64_t magic;
2411 int shift;
2412 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2413
2414 Label ndiv;
2415 Label end;
2416 // If numerator is 0, the result is 0, no computation needed.
2417 __ testl(eax, eax);
2418 __ j(kNotEqual, &ndiv);
2419
2420 __ xorl(out, out);
2421 __ jmp(&end);
2422
2423 __ Bind(&ndiv);
2424
2425 // Save the numerator.
2426 __ movl(num, eax);
2427
2428 // EAX = magic
2429 __ movl(eax, Immediate(magic));
2430
2431 // EDX:EAX = magic * numerator
2432 __ imull(num);
2433
2434 if (imm > 0 && magic < 0) {
2435 // EDX += num
2436 __ addl(edx, num);
2437 } else if (imm < 0 && magic > 0) {
2438 __ subl(edx, num);
2439 }
2440
2441 // Shift if needed.
2442 if (shift != 0) {
2443 __ sarl(edx, Immediate(shift));
2444 }
2445
2446 // EDX += 1 if EDX < 0
2447 __ movl(eax, edx);
2448 __ shrl(edx, Immediate(31));
2449 __ addl(edx, eax);
2450
2451 if (instruction->IsRem()) {
2452 __ movl(eax, num);
2453 __ imull(edx, Immediate(imm));
2454 __ subl(eax, edx);
2455 __ movl(edx, eax);
2456 } else {
2457 __ movl(eax, edx);
2458 }
2459 __ Bind(&end);
2460}
2461
Calin Juravlebacfec32014-11-14 15:54:36 +00002462void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2463 DCHECK(instruction->IsDiv() || instruction->IsRem());
2464
2465 LocationSummary* locations = instruction->GetLocations();
2466 Location out = locations->Out();
2467 Location first = locations->InAt(0);
2468 Location second = locations->InAt(1);
2469 bool is_div = instruction->IsDiv();
2470
2471 switch (instruction->GetResultType()) {
2472 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 DCHECK_EQ(EAX, first.AsRegister<Register>());
2474 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002475
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002476 if (instruction->InputAt(1)->IsIntConstant()) {
2477 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002478
2479 if (imm == 0) {
2480 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2481 } else if (imm == 1 || imm == -1) {
2482 DivRemOneOrMinusOne(instruction);
2483 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002484 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002485 } else {
2486 DCHECK(imm <= -2 || imm >= 2);
2487 GenerateDivRemWithAnyConstant(instruction);
2488 }
2489 } else {
2490 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002491 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002492 is_div);
2493 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002494
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495 Register second_reg = second.AsRegister<Register>();
2496 // 0x80000000/-1 triggers an arithmetic exception!
2497 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2498 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002499
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002500 __ cmpl(second_reg, Immediate(-1));
2501 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002502
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002503 // edx:eax <- sign-extended of eax
2504 __ cdq();
2505 // eax = quotient, edx = remainder
2506 __ idivl(second_reg);
2507 __ Bind(slow_path->GetExitLabel());
2508 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002509 break;
2510 }
2511
2512 case Primitive::kPrimLong: {
2513 InvokeRuntimeCallingConvention calling_convention;
2514 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2516 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2517 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2518 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2519 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2520
2521 if (is_div) {
2522 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2523 } else {
2524 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2525 }
2526 uint32_t dex_pc = is_div
2527 ? instruction->AsDiv()->GetDexPc()
2528 : instruction->AsRem()->GetDexPc();
2529 codegen_->RecordPcInfo(instruction, dex_pc);
2530
2531 break;
2532 }
2533
2534 default:
2535 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2536 }
2537}
2538
Calin Juravle7c4954d2014-10-28 16:57:40 +00002539void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002540 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002541 ? LocationSummary::kCall
2542 : LocationSummary::kNoCall;
2543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2544
Calin Juravle7c4954d2014-10-28 16:57:40 +00002545 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002546 case Primitive::kPrimInt: {
2547 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002548 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002549 locations->SetOut(Location::SameAsFirstInput());
2550 // Intel uses edx:eax as the dividend.
2551 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002552 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2553 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2554 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002555 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002556 locations->AddTemp(Location::RequiresRegister());
2557 }
Calin Juravled0d48522014-11-04 16:40:20 +00002558 break;
2559 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002560 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002561 InvokeRuntimeCallingConvention calling_convention;
2562 locations->SetInAt(0, Location::RegisterPairLocation(
2563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2564 locations->SetInAt(1, Location::RegisterPairLocation(
2565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2566 // Runtime helper puts the result in EAX, EDX.
2567 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002568 break;
2569 }
2570 case Primitive::kPrimFloat:
2571 case Primitive::kPrimDouble: {
2572 locations->SetInAt(0, Location::RequiresFpuRegister());
2573 locations->SetInAt(1, Location::RequiresFpuRegister());
2574 locations->SetOut(Location::SameAsFirstInput());
2575 break;
2576 }
2577
2578 default:
2579 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2580 }
2581}
2582
2583void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2584 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002585 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002586 Location first = locations->InAt(0);
2587 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002588
2589 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002590 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002591 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002592 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593 break;
2594 }
2595
2596 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002597 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002599 break;
2600 }
2601
2602 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002603 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002604 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002605 break;
2606 }
2607
2608 default:
2609 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2610 }
2611}
2612
Calin Juravlebacfec32014-11-14 15:54:36 +00002613void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002614 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002615
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002616 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2617 ? LocationSummary::kCall
2618 : LocationSummary::kNoCall;
2619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002620
Calin Juravled2ec87d2014-12-08 14:24:46 +00002621 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002622 case Primitive::kPrimInt: {
2623 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002624 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002625 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002626 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2627 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2628 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002629 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002630 locations->AddTemp(Location::RequiresRegister());
2631 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002632 break;
2633 }
2634 case Primitive::kPrimLong: {
2635 InvokeRuntimeCallingConvention calling_convention;
2636 locations->SetInAt(0, Location::RegisterPairLocation(
2637 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2638 locations->SetInAt(1, Location::RegisterPairLocation(
2639 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2640 // Runtime helper puts the result in EAX, EDX.
2641 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2642 break;
2643 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002644 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002645 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002646 locations->SetInAt(0, Location::Any());
2647 locations->SetInAt(1, Location::Any());
2648 locations->SetOut(Location::RequiresFpuRegister());
2649 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002650 break;
2651 }
2652
2653 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002654 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002655 }
2656}
2657
2658void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2659 Primitive::Type type = rem->GetResultType();
2660 switch (type) {
2661 case Primitive::kPrimInt:
2662 case Primitive::kPrimLong: {
2663 GenerateDivRemIntegral(rem);
2664 break;
2665 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002666 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002667 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002668 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002669 break;
2670 }
2671 default:
2672 LOG(FATAL) << "Unexpected rem type " << type;
2673 }
2674}
2675
Calin Juravled0d48522014-11-04 16:40:20 +00002676void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2677 LocationSummary* locations =
2678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002679 switch (instruction->GetType()) {
2680 case Primitive::kPrimInt: {
2681 locations->SetInAt(0, Location::Any());
2682 break;
2683 }
2684 case Primitive::kPrimLong: {
2685 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2686 if (!instruction->IsConstant()) {
2687 locations->AddTemp(Location::RequiresRegister());
2688 }
2689 break;
2690 }
2691 default:
2692 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2693 }
Calin Juravled0d48522014-11-04 16:40:20 +00002694 if (instruction->HasUses()) {
2695 locations->SetOut(Location::SameAsFirstInput());
2696 }
2697}
2698
2699void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2700 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2701 codegen_->AddSlowPath(slow_path);
2702
2703 LocationSummary* locations = instruction->GetLocations();
2704 Location value = locations->InAt(0);
2705
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002706 switch (instruction->GetType()) {
2707 case Primitive::kPrimInt: {
2708 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002710 __ j(kEqual, slow_path->GetEntryLabel());
2711 } else if (value.IsStackSlot()) {
2712 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2713 __ j(kEqual, slow_path->GetEntryLabel());
2714 } else {
2715 DCHECK(value.IsConstant()) << value;
2716 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2717 __ jmp(slow_path->GetEntryLabel());
2718 }
2719 }
2720 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002721 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002722 case Primitive::kPrimLong: {
2723 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002724 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002725 __ movl(temp, value.AsRegisterPairLow<Register>());
2726 __ orl(temp, value.AsRegisterPairHigh<Register>());
2727 __ j(kEqual, slow_path->GetEntryLabel());
2728 } else {
2729 DCHECK(value.IsConstant()) << value;
2730 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2731 __ jmp(slow_path->GetEntryLabel());
2732 }
2733 }
2734 break;
2735 }
2736 default:
2737 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002738 }
Calin Juravled0d48522014-11-04 16:40:20 +00002739}
2740
Calin Juravle9aec02f2014-11-18 23:06:35 +00002741void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2742 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2743
2744 LocationSummary* locations =
2745 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2746
2747 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002748 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002749 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002750 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002751 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002752 // The shift count needs to be in CL or a constant.
2753 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002754 locations->SetOut(Location::SameAsFirstInput());
2755 break;
2756 }
2757 default:
2758 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2759 }
2760}
2761
2762void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2763 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2764
2765 LocationSummary* locations = op->GetLocations();
2766 Location first = locations->InAt(0);
2767 Location second = locations->InAt(1);
2768 DCHECK(first.Equals(locations->Out()));
2769
2770 switch (op->GetResultType()) {
2771 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002772 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002773 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002776 DCHECK_EQ(ECX, second_reg);
2777 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002778 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002780 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002781 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002782 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002783 }
2784 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002785 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2786 if (shift == 0) {
2787 return;
2788 }
2789 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002790 if (op->IsShl()) {
2791 __ shll(first_reg, imm);
2792 } else if (op->IsShr()) {
2793 __ sarl(first_reg, imm);
2794 } else {
2795 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002796 }
2797 }
2798 break;
2799 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002800 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002801 if (second.IsRegister()) {
2802 Register second_reg = second.AsRegister<Register>();
2803 DCHECK_EQ(ECX, second_reg);
2804 if (op->IsShl()) {
2805 GenerateShlLong(first, second_reg);
2806 } else if (op->IsShr()) {
2807 GenerateShrLong(first, second_reg);
2808 } else {
2809 GenerateUShrLong(first, second_reg);
2810 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002811 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002812 // Shift by a constant.
2813 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2814 // Nothing to do if the shift is 0, as the input is already the output.
2815 if (shift != 0) {
2816 if (op->IsShl()) {
2817 GenerateShlLong(first, shift);
2818 } else if (op->IsShr()) {
2819 GenerateShrLong(first, shift);
2820 } else {
2821 GenerateUShrLong(first, shift);
2822 }
2823 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002824 }
2825 break;
2826 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002827 default:
2828 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2829 }
2830}
2831
Mark P Mendell73945692015-04-29 14:56:17 +00002832void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2833 Register low = loc.AsRegisterPairLow<Register>();
2834 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002835 if (shift == 1) {
2836 // This is just an addition.
2837 __ addl(low, low);
2838 __ adcl(high, high);
2839 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002840 // Shift by 32 is easy. High gets low, and low gets 0.
2841 codegen_->EmitParallelMoves(
2842 loc.ToLow(),
2843 loc.ToHigh(),
2844 Primitive::kPrimInt,
2845 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2846 loc.ToLow(),
2847 Primitive::kPrimInt);
2848 } else if (shift > 32) {
2849 // Low part becomes 0. High part is low part << (shift-32).
2850 __ movl(high, low);
2851 __ shll(high, Immediate(shift - 32));
2852 __ xorl(low, low);
2853 } else {
2854 // Between 1 and 31.
2855 __ shld(high, low, Immediate(shift));
2856 __ shll(low, Immediate(shift));
2857 }
2858}
2859
Calin Juravle9aec02f2014-11-18 23:06:35 +00002860void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2861 Label done;
2862 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2863 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2864 __ testl(shifter, Immediate(32));
2865 __ j(kEqual, &done);
2866 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2867 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2868 __ Bind(&done);
2869}
2870
Mark P Mendell73945692015-04-29 14:56:17 +00002871void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2872 Register low = loc.AsRegisterPairLow<Register>();
2873 Register high = loc.AsRegisterPairHigh<Register>();
2874 if (shift == 32) {
2875 // Need to copy the sign.
2876 DCHECK_NE(low, high);
2877 __ movl(low, high);
2878 __ sarl(high, Immediate(31));
2879 } else if (shift > 32) {
2880 DCHECK_NE(low, high);
2881 // High part becomes sign. Low part is shifted by shift - 32.
2882 __ movl(low, high);
2883 __ sarl(high, Immediate(31));
2884 __ sarl(low, Immediate(shift - 32));
2885 } else {
2886 // Between 1 and 31.
2887 __ shrd(low, high, Immediate(shift));
2888 __ sarl(high, Immediate(shift));
2889 }
2890}
2891
Calin Juravle9aec02f2014-11-18 23:06:35 +00002892void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2893 Label done;
2894 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2895 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2896 __ testl(shifter, Immediate(32));
2897 __ j(kEqual, &done);
2898 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2899 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2900 __ Bind(&done);
2901}
2902
Mark P Mendell73945692015-04-29 14:56:17 +00002903void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2904 Register low = loc.AsRegisterPairLow<Register>();
2905 Register high = loc.AsRegisterPairHigh<Register>();
2906 if (shift == 32) {
2907 // Shift by 32 is easy. Low gets high, and high gets 0.
2908 codegen_->EmitParallelMoves(
2909 loc.ToHigh(),
2910 loc.ToLow(),
2911 Primitive::kPrimInt,
2912 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2913 loc.ToHigh(),
2914 Primitive::kPrimInt);
2915 } else if (shift > 32) {
2916 // Low part is high >> (shift - 32). High part becomes 0.
2917 __ movl(low, high);
2918 __ shrl(low, Immediate(shift - 32));
2919 __ xorl(high, high);
2920 } else {
2921 // Between 1 and 31.
2922 __ shrd(low, high, Immediate(shift));
2923 __ shrl(high, Immediate(shift));
2924 }
2925}
2926
Calin Juravle9aec02f2014-11-18 23:06:35 +00002927void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2928 Label done;
2929 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2930 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2931 __ testl(shifter, Immediate(32));
2932 __ j(kEqual, &done);
2933 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2934 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2935 __ Bind(&done);
2936}
2937
2938void LocationsBuilderX86::VisitShl(HShl* shl) {
2939 HandleShift(shl);
2940}
2941
2942void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2943 HandleShift(shl);
2944}
2945
2946void LocationsBuilderX86::VisitShr(HShr* shr) {
2947 HandleShift(shr);
2948}
2949
2950void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2951 HandleShift(shr);
2952}
2953
2954void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2955 HandleShift(ushr);
2956}
2957
2958void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2959 HandleShift(ushr);
2960}
2961
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002962void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002963 LocationSummary* locations =
2964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002965 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002966 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002967 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2968 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002969}
2970
2971void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2972 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002973 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002974 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002975
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002976 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002977
Nicolas Geoffray39468442014-09-02 15:17:15 +01002978 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002979 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002980}
2981
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002982void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2985 locations->SetOut(Location::RegisterLocation(EAX));
2986 InvokeRuntimeCallingConvention calling_convention;
2987 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002988 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002990}
2991
2992void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2993 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002994 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002995 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2996
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002997 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002998
2999 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3000 DCHECK(!codegen_->IsLeafMethod());
3001}
3002
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003003void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003004 LocationSummary* locations =
3005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003006 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3007 if (location.IsStackSlot()) {
3008 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3009 } else if (location.IsDoubleStackSlot()) {
3010 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003011 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003012 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003013}
3014
3015void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003016 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003017}
3018
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003019void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003020 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003021 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003022 locations->SetInAt(0, Location::RequiresRegister());
3023 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003024}
3025
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003026void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3027 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003028 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003029 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003030 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003031 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003032 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003034 break;
3035
3036 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003037 __ notl(out.AsRegisterPairLow<Register>());
3038 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003039 break;
3040
3041 default:
3042 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3043 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003044}
3045
David Brazdil66d126e2015-04-03 16:02:44 +01003046void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3047 LocationSummary* locations =
3048 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3049 locations->SetInAt(0, Location::RequiresRegister());
3050 locations->SetOut(Location::SameAsFirstInput());
3051}
3052
3053void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003054 LocationSummary* locations = bool_not->GetLocations();
3055 Location in = locations->InAt(0);
3056 Location out = locations->Out();
3057 DCHECK(in.Equals(out));
3058 __ xorl(out.AsRegister<Register>(), Immediate(1));
3059}
3060
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003061void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003062 LocationSummary* locations =
3063 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003064 switch (compare->InputAt(0)->GetType()) {
3065 case Primitive::kPrimLong: {
3066 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003067 locations->SetInAt(1, Location::Any());
3068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3069 break;
3070 }
3071 case Primitive::kPrimFloat:
3072 case Primitive::kPrimDouble: {
3073 locations->SetInAt(0, Location::RequiresFpuRegister());
3074 locations->SetInAt(1, Location::RequiresFpuRegister());
3075 locations->SetOut(Location::RequiresRegister());
3076 break;
3077 }
3078 default:
3079 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3080 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003081}
3082
3083void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003084 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003086 Location left = locations->InAt(0);
3087 Location right = locations->InAt(1);
3088
3089 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003090 switch (compare->InputAt(0)->GetType()) {
3091 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003092 Register left_low = left.AsRegisterPairLow<Register>();
3093 Register left_high = left.AsRegisterPairHigh<Register>();
3094 int32_t val_low = 0;
3095 int32_t val_high = 0;
3096 bool right_is_const = false;
3097
3098 if (right.IsConstant()) {
3099 DCHECK(right.GetConstant()->IsLongConstant());
3100 right_is_const = true;
3101 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3102 val_low = Low32Bits(val);
3103 val_high = High32Bits(val);
3104 }
3105
Calin Juravleddb7df22014-11-25 20:56:51 +00003106 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003107 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003108 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003109 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003110 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003111 DCHECK(right_is_const) << right;
3112 if (val_high == 0) {
3113 __ testl(left_high, left_high);
3114 } else {
3115 __ cmpl(left_high, Immediate(val_high));
3116 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003117 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003118 __ j(kLess, &less); // Signed compare.
3119 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003120 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003121 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003122 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003123 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003124 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003125 DCHECK(right_is_const) << right;
3126 if (val_low == 0) {
3127 __ testl(left_low, left_low);
3128 } else {
3129 __ cmpl(left_low, Immediate(val_low));
3130 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003131 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003132 break;
3133 }
3134 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003136 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3137 break;
3138 }
3139 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003141 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003142 break;
3143 }
3144 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003145 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003146 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003147 __ movl(out, Immediate(0));
3148 __ j(kEqual, &done);
3149 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3150
3151 __ Bind(&greater);
3152 __ movl(out, Immediate(1));
3153 __ jmp(&done);
3154
3155 __ Bind(&less);
3156 __ movl(out, Immediate(-1));
3157
3158 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003159}
3160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003161void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003162 LocationSummary* locations =
3163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003164 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3165 locations->SetInAt(i, Location::Any());
3166 }
3167 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003168}
3169
3170void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003171 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003172 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003173}
3174
Calin Juravle52c48962014-12-16 17:02:57 +00003175void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3176 /*
3177 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3178 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3179 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3180 */
3181 switch (kind) {
3182 case MemBarrierKind::kAnyAny: {
3183 __ mfence();
3184 break;
3185 }
3186 case MemBarrierKind::kAnyStore:
3187 case MemBarrierKind::kLoadAny:
3188 case MemBarrierKind::kStoreStore: {
3189 // nop
3190 break;
3191 }
3192 default:
3193 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003194 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003195}
3196
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003197
Mark Mendell09ed1a32015-03-25 08:30:06 -04003198void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3199 Register temp) {
3200 // TODO: Implement all kinds of calls:
3201 // 1) boot -> boot
3202 // 2) app -> boot
3203 // 3) app -> app
3204 //
3205 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003206
3207 if (invoke->IsStringInit()) {
3208 // temp = thread->string_init_entrypoint
3209 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003210 // (temp + offset_of_quick_compiled_code)()
3211 __ call(Address(
3212 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3213 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003214 // temp = method;
3215 LoadCurrentMethod(temp);
3216 if (!invoke->IsRecursive()) {
3217 // temp = temp->dex_cache_resolved_methods_;
3218 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3219 // temp = temp[index_in_cache]
3220 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3221 // (temp + offset_of_quick_compiled_code)()
3222 __ call(Address(temp,
3223 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3224 } else {
3225 __ call(GetFrameEntryLabel());
3226 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003227 }
3228
3229 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003230}
3231
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003232void CodeGeneratorX86::MarkGCCard(Register temp,
3233 Register card,
3234 Register object,
3235 Register value,
3236 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003237 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003238 if (value_can_be_null) {
3239 __ testl(value, value);
3240 __ j(kEqual, &is_null);
3241 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003242 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3243 __ movl(temp, object);
3244 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003245 __ movb(Address(temp, card, TIMES_1, 0),
3246 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003247 if (value_can_be_null) {
3248 __ Bind(&is_null);
3249 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003250}
3251
Calin Juravle52c48962014-12-16 17:02:57 +00003252void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3253 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003256 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003257
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003258 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3259 locations->SetOut(Location::RequiresFpuRegister());
3260 } else {
3261 // The output overlaps in case of long: we don't want the low move to overwrite
3262 // the object's location.
3263 locations->SetOut(Location::RequiresRegister(),
3264 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3265 : Location::kNoOutputOverlap);
3266 }
Calin Juravle52c48962014-12-16 17:02:57 +00003267
3268 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3269 // Long values can be loaded atomically into an XMM using movsd.
3270 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3271 // and then copy the XMM into the output 32bits at a time).
3272 locations->AddTemp(Location::RequiresFpuRegister());
3273 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003274}
3275
Calin Juravle52c48962014-12-16 17:02:57 +00003276void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3277 const FieldInfo& field_info) {
3278 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003279
Calin Juravle52c48962014-12-16 17:02:57 +00003280 LocationSummary* locations = instruction->GetLocations();
3281 Register base = locations->InAt(0).AsRegister<Register>();
3282 Location out = locations->Out();
3283 bool is_volatile = field_info.IsVolatile();
3284 Primitive::Type field_type = field_info.GetFieldType();
3285 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3286
3287 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003288 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003289 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290 break;
3291 }
3292
3293 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003294 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003295 break;
3296 }
3297
3298 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003299 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003300 break;
3301 }
3302
3303 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003304 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305 break;
3306 }
3307
3308 case Primitive::kPrimInt:
3309 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003310 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 break;
3312 }
3313
3314 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 if (is_volatile) {
3316 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3317 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003319 __ movd(out.AsRegisterPairLow<Register>(), temp);
3320 __ psrlq(temp, Immediate(32));
3321 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3322 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003323 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003324 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003326 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3327 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003328 break;
3329 }
3330
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003331 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003332 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003333 break;
3334 }
3335
3336 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003337 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003338 break;
3339 }
3340
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003341 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003342 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003343 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003344 }
Calin Juravle52c48962014-12-16 17:02:57 +00003345
Calin Juravle77520bc2015-01-12 18:45:46 +00003346 // Longs are handled in the switch.
3347 if (field_type != Primitive::kPrimLong) {
3348 codegen_->MaybeRecordImplicitNullCheck(instruction);
3349 }
3350
Calin Juravle52c48962014-12-16 17:02:57 +00003351 if (is_volatile) {
3352 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3353 }
3354}
3355
3356void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3357 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3358
3359 LocationSummary* locations =
3360 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3361 locations->SetInAt(0, Location::RequiresRegister());
3362 bool is_volatile = field_info.IsVolatile();
3363 Primitive::Type field_type = field_info.GetFieldType();
3364 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3365 || (field_type == Primitive::kPrimByte);
3366
3367 // The register allocator does not support multiple
3368 // inputs that die at entry with one in a specific register.
3369 if (is_byte_type) {
3370 // Ensure the value is in a byte register.
3371 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003372 } else if (Primitive::IsFloatingPointType(field_type)) {
3373 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003374 } else {
3375 locations->SetInAt(1, Location::RequiresRegister());
3376 }
3377 // Temporary registers for the write barrier.
3378 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3379 locations->AddTemp(Location::RequiresRegister());
3380 // Ensure the card is in a byte register.
3381 locations->AddTemp(Location::RegisterLocation(ECX));
3382 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3383 // 64bits value can be atomically written to an address with movsd and an XMM register.
3384 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3385 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3386 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3387 // isolated cases when we need this it isn't worth adding the extra complexity.
3388 locations->AddTemp(Location::RequiresFpuRegister());
3389 locations->AddTemp(Location::RequiresFpuRegister());
3390 }
3391}
3392
3393void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003394 const FieldInfo& field_info,
3395 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003396 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3397
3398 LocationSummary* locations = instruction->GetLocations();
3399 Register base = locations->InAt(0).AsRegister<Register>();
3400 Location value = locations->InAt(1);
3401 bool is_volatile = field_info.IsVolatile();
3402 Primitive::Type field_type = field_info.GetFieldType();
3403 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3404
3405 if (is_volatile) {
3406 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3407 }
3408
3409 switch (field_type) {
3410 case Primitive::kPrimBoolean:
3411 case Primitive::kPrimByte: {
3412 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3413 break;
3414 }
3415
3416 case Primitive::kPrimShort:
3417 case Primitive::kPrimChar: {
3418 __ movw(Address(base, offset), value.AsRegister<Register>());
3419 break;
3420 }
3421
3422 case Primitive::kPrimInt:
3423 case Primitive::kPrimNot: {
3424 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003425 break;
3426 }
3427
3428 case Primitive::kPrimLong: {
3429 if (is_volatile) {
3430 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3431 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3432 __ movd(temp1, value.AsRegisterPairLow<Register>());
3433 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3434 __ punpckldq(temp1, temp2);
3435 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003436 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003437 } else {
3438 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003439 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003440 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3441 }
3442 break;
3443 }
3444
3445 case Primitive::kPrimFloat: {
3446 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3447 break;
3448 }
3449
3450 case Primitive::kPrimDouble: {
3451 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3452 break;
3453 }
3454
3455 case Primitive::kPrimVoid:
3456 LOG(FATAL) << "Unreachable type " << field_type;
3457 UNREACHABLE();
3458 }
3459
Calin Juravle77520bc2015-01-12 18:45:46 +00003460 // Longs are handled in the switch.
3461 if (field_type != Primitive::kPrimLong) {
3462 codegen_->MaybeRecordImplicitNullCheck(instruction);
3463 }
3464
3465 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3466 Register temp = locations->GetTemp(0).AsRegister<Register>();
3467 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003468 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003469 }
3470
Calin Juravle52c48962014-12-16 17:02:57 +00003471 if (is_volatile) {
3472 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3473 }
3474}
3475
3476void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3477 HandleFieldGet(instruction, instruction->GetFieldInfo());
3478}
3479
3480void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3481 HandleFieldGet(instruction, instruction->GetFieldInfo());
3482}
3483
3484void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3485 HandleFieldSet(instruction, instruction->GetFieldInfo());
3486}
3487
3488void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003489 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003490}
3491
3492void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3493 HandleFieldSet(instruction, instruction->GetFieldInfo());
3494}
3495
3496void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003497 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003498}
3499
3500void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3501 HandleFieldGet(instruction, instruction->GetFieldInfo());
3502}
3503
3504void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3505 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003506}
3507
3508void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003509 LocationSummary* locations =
3510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003511 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3512 ? Location::RequiresRegister()
3513 : Location::Any();
3514 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003515 if (instruction->HasUses()) {
3516 locations->SetOut(Location::SameAsFirstInput());
3517 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003518}
3519
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003520void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003521 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3522 return;
3523 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003524 LocationSummary* locations = instruction->GetLocations();
3525 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003526
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003527 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3528 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3529}
3530
3531void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003532 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003533 codegen_->AddSlowPath(slow_path);
3534
3535 LocationSummary* locations = instruction->GetLocations();
3536 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003537
3538 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003539 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003540 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003541 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003542 } else {
3543 DCHECK(obj.IsConstant()) << obj;
3544 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3545 __ jmp(slow_path->GetEntryLabel());
3546 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003547 }
3548 __ j(kEqual, slow_path->GetEntryLabel());
3549}
3550
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003551void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3552 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3553 GenerateImplicitNullCheck(instruction);
3554 } else {
3555 GenerateExplicitNullCheck(instruction);
3556 }
3557}
3558
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003559void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003562 locations->SetInAt(0, Location::RequiresRegister());
3563 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003564 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3565 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3566 } else {
3567 // The output overlaps in case of long: we don't want the low move to overwrite
3568 // the array's location.
3569 locations->SetOut(Location::RequiresRegister(),
3570 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3571 : Location::kNoOutputOverlap);
3572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573}
3574
3575void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3576 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 Location index = locations->InAt(1);
3579
Calin Juravle77520bc2015-01-12 18:45:46 +00003580 Primitive::Type type = instruction->GetType();
3581 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003582 case Primitive::kPrimBoolean: {
3583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585 if (index.IsConstant()) {
3586 __ movzxb(out, Address(obj,
3587 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3588 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003589 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003590 }
3591 break;
3592 }
3593
3594 case Primitive::kPrimByte: {
3595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003597 if (index.IsConstant()) {
3598 __ movsxb(out, Address(obj,
3599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3600 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003602 }
3603 break;
3604 }
3605
3606 case Primitive::kPrimShort: {
3607 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609 if (index.IsConstant()) {
3610 __ movsxw(out, Address(obj,
3611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3612 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003613 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 }
3615 break;
3616 }
3617
3618 case Primitive::kPrimChar: {
3619 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 if (index.IsConstant()) {
3622 __ movzxw(out, Address(obj,
3623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3624 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003625 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003626 }
3627 break;
3628 }
3629
3630 case Primitive::kPrimInt:
3631 case Primitive::kPrimNot: {
3632 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 if (index.IsConstant()) {
3635 __ movl(out, Address(obj,
3636 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3637 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003638 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003639 }
3640 break;
3641 }
3642
3643 case Primitive::kPrimLong: {
3644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003645 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003646 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 if (index.IsConstant()) {
3648 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003649 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003650 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003651 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003653 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003655 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003656 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003657 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 }
3659 break;
3660 }
3661
Mark Mendell7c8d0092015-01-26 11:21:33 -05003662 case Primitive::kPrimFloat: {
3663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3664 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3665 if (index.IsConstant()) {
3666 __ movss(out, Address(obj,
3667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3668 } else {
3669 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3670 }
3671 break;
3672 }
3673
3674 case Primitive::kPrimDouble: {
3675 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3676 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3677 if (index.IsConstant()) {
3678 __ movsd(out, Address(obj,
3679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3680 } else {
3681 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3682 }
3683 break;
3684 }
3685
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003686 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003687 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003688 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003689 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003690
3691 if (type != Primitive::kPrimLong) {
3692 codegen_->MaybeRecordImplicitNullCheck(instruction);
3693 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694}
3695
3696void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003697 // This location builder might end up asking to up to four registers, which is
3698 // not currently possible for baseline. The situation in which we need four
3699 // registers cannot be met by baseline though, because it has not run any
3700 // optimization.
3701
Nicolas Geoffray39468442014-09-02 15:17:15 +01003702 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003703 bool needs_write_barrier =
3704 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3705
Mark Mendell5f874182015-03-04 15:42:45 -05003706 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003707
Nicolas Geoffray39468442014-09-02 15:17:15 +01003708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3709 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003710 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003711
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003712 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003713 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003714 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3715 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3716 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003718 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3719 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003720 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003721 // In case of a byte operation, the register allocator does not support multiple
3722 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003723 locations->SetInAt(0, Location::RequiresRegister());
3724 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003725 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003726 // Ensure the value is in a byte register.
3727 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003728 } else if (Primitive::IsFloatingPointType(value_type)) {
3729 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003731 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003732 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003733 // Temporary registers for the write barrier.
3734 if (needs_write_barrier) {
3735 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003736 // Ensure the card is in a byte register.
3737 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740}
3741
3742void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3743 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003744 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003746 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003747 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003748 bool needs_runtime_call = locations->WillCall();
3749 bool needs_write_barrier =
3750 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751
3752 switch (value_type) {
3753 case Primitive::kPrimBoolean:
3754 case Primitive::kPrimByte: {
3755 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 if (index.IsConstant()) {
3757 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003758 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003759 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003760 } else {
3761 __ movb(Address(obj, offset),
3762 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3763 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003765 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003767 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003768 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003770 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3771 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003773 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003774 break;
3775 }
3776
3777 case Primitive::kPrimShort:
3778 case Primitive::kPrimChar: {
3779 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780 if (index.IsConstant()) {
3781 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003784 } else {
3785 __ movw(Address(obj, offset),
3786 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3787 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003789 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3791 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003792 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003793 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003794 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3795 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798 break;
3799 }
3800
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003801 case Primitive::kPrimInt:
3802 case Primitive::kPrimNot: {
3803 if (!needs_runtime_call) {
3804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3805 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003806 size_t offset =
3807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003808 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003810 } else {
3811 DCHECK(value.IsConstant()) << value;
3812 __ movl(Address(obj, offset),
3813 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3814 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003815 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003816 DCHECK(index.IsRegister()) << index;
3817 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003818 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3819 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003820 } else {
3821 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003823 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3824 }
3825 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003826 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003827
3828 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 Register temp = locations->GetTemp(0).AsRegister<Register>();
3830 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003831 codegen_->MarkGCCard(
3832 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003833 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003835 DCHECK_EQ(value_type, Primitive::kPrimNot);
3836 DCHECK(!codegen_->IsLeafMethod());
3837 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3838 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 }
3840 break;
3841 }
3842
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843 case Primitive::kPrimLong: {
3844 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 if (index.IsConstant()) {
3846 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003847 if (value.IsRegisterPair()) {
3848 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003849 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003850 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003851 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003852 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003853 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3854 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003855 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003856 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3857 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003858 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003859 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003860 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003861 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003862 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003863 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003864 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003865 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003866 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003867 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003869 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003872 Immediate(High32Bits(val)));
3873 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874 }
3875 break;
3876 }
3877
Mark Mendell7c8d0092015-01-26 11:21:33 -05003878 case Primitive::kPrimFloat: {
3879 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3880 DCHECK(value.IsFpuRegister());
3881 if (index.IsConstant()) {
3882 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3883 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3884 } else {
3885 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3886 value.AsFpuRegister<XmmRegister>());
3887 }
3888 break;
3889 }
3890
3891 case Primitive::kPrimDouble: {
3892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3893 DCHECK(value.IsFpuRegister());
3894 if (index.IsConstant()) {
3895 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3896 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3897 } else {
3898 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3899 value.AsFpuRegister<XmmRegister>());
3900 }
3901 break;
3902 }
3903
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003904 case Primitive::kPrimVoid:
3905 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003906 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003907 }
3908}
3909
3910void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3911 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003912 locations->SetInAt(0, Location::RequiresRegister());
3913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 instruction->SetLocations(locations);
3915}
3916
3917void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3918 LocationSummary* locations = instruction->GetLocations();
3919 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 Register obj = locations->InAt(0).AsRegister<Register>();
3921 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924}
3925
3926void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003927 LocationSummary* locations =
3928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003929 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003930 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003931 if (instruction->HasUses()) {
3932 locations->SetOut(Location::SameAsFirstInput());
3933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003934}
3935
3936void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3937 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003938 Location index_loc = locations->InAt(0);
3939 Location length_loc = locations->InAt(1);
3940 SlowPathCodeX86* slow_path =
3941 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942
Mark Mendell99dbd682015-04-22 16:18:52 -04003943 if (length_loc.IsConstant()) {
3944 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3945 if (index_loc.IsConstant()) {
3946 // BCE will remove the bounds check if we are guarenteed to pass.
3947 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3948 if (index < 0 || index >= length) {
3949 codegen_->AddSlowPath(slow_path);
3950 __ jmp(slow_path->GetEntryLabel());
3951 } else {
3952 // Some optimization after BCE may have generated this, and we should not
3953 // generate a bounds check if it is a valid range.
3954 }
3955 return;
3956 }
3957
3958 // We have to reverse the jump condition because the length is the constant.
3959 Register index_reg = index_loc.AsRegister<Register>();
3960 __ cmpl(index_reg, Immediate(length));
3961 codegen_->AddSlowPath(slow_path);
3962 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003963 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003964 Register length = length_loc.AsRegister<Register>();
3965 if (index_loc.IsConstant()) {
3966 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3967 __ cmpl(length, Immediate(value));
3968 } else {
3969 __ cmpl(length, index_loc.AsRegister<Register>());
3970 }
3971 codegen_->AddSlowPath(slow_path);
3972 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003973 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003974}
3975
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3977 temp->SetLocations(nullptr);
3978}
3979
3980void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3981 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003982 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003983}
3984
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003985void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003986 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003987 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003988}
3989
3990void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003991 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3992}
3993
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003994void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3996}
3997
3998void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003999 HBasicBlock* block = instruction->GetBlock();
4000 if (block->GetLoopInformation() != nullptr) {
4001 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4002 // The back edge will generate the suspend check.
4003 return;
4004 }
4005 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4006 // The goto will generate the suspend check.
4007 return;
4008 }
4009 GenerateSuspendCheck(instruction, nullptr);
4010}
4011
4012void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4013 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004014 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004015 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4016 if (slow_path == nullptr) {
4017 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4018 instruction->SetSlowPath(slow_path);
4019 codegen_->AddSlowPath(slow_path);
4020 if (successor != nullptr) {
4021 DCHECK(successor->IsLoopHeader());
4022 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4023 }
4024 } else {
4025 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4026 }
4027
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004028 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004029 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004030 if (successor == nullptr) {
4031 __ j(kNotEqual, slow_path->GetEntryLabel());
4032 __ Bind(slow_path->GetReturnLabel());
4033 } else {
4034 __ j(kEqual, codegen_->GetLabelOf(successor));
4035 __ jmp(slow_path->GetEntryLabel());
4036 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004037}
4038
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004039X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4040 return codegen_->GetAssembler();
4041}
4042
Mark Mendell7c8d0092015-01-26 11:21:33 -05004043void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004044 ScratchRegisterScope ensure_scratch(
4045 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4046 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4047 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4048 __ movl(temp_reg, Address(ESP, src + stack_offset));
4049 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004050}
4051
4052void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004053 ScratchRegisterScope ensure_scratch(
4054 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4055 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4056 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4057 __ movl(temp_reg, Address(ESP, src + stack_offset));
4058 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4059 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4060 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004061}
4062
4063void ParallelMoveResolverX86::EmitMove(size_t index) {
4064 MoveOperands* move = moves_.Get(index);
4065 Location source = move->GetSource();
4066 Location destination = move->GetDestination();
4067
4068 if (source.IsRegister()) {
4069 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004071 } else {
4072 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004073 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004074 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004075 } else if (source.IsFpuRegister()) {
4076 if (destination.IsFpuRegister()) {
4077 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4078 } else if (destination.IsStackSlot()) {
4079 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4080 } else {
4081 DCHECK(destination.IsDoubleStackSlot());
4082 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4083 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004084 } else if (source.IsStackSlot()) {
4085 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004087 } else if (destination.IsFpuRegister()) {
4088 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004089 } else {
4090 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004091 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4092 }
4093 } else if (source.IsDoubleStackSlot()) {
4094 if (destination.IsFpuRegister()) {
4095 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4096 } else {
4097 DCHECK(destination.IsDoubleStackSlot()) << destination;
4098 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004099 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004100 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004101 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004102 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004103 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004104 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004105 if (value == 0) {
4106 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4107 } else {
4108 __ movl(destination.AsRegister<Register>(), Immediate(value));
4109 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004110 } else {
4111 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004112 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004113 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004114 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004115 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004116 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004117 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004119 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4120 if (value == 0) {
4121 // Easy handling of 0.0.
4122 __ xorps(dest, dest);
4123 } else {
4124 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004125 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4126 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4127 __ movl(temp, Immediate(value));
4128 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004129 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004130 } else {
4131 DCHECK(destination.IsStackSlot()) << destination;
4132 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4133 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004134 } else if (constant->IsLongConstant()) {
4135 int64_t value = constant->AsLongConstant()->GetValue();
4136 int32_t low_value = Low32Bits(value);
4137 int32_t high_value = High32Bits(value);
4138 Immediate low(low_value);
4139 Immediate high(high_value);
4140 if (destination.IsDoubleStackSlot()) {
4141 __ movl(Address(ESP, destination.GetStackIndex()), low);
4142 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4143 } else {
4144 __ movl(destination.AsRegisterPairLow<Register>(), low);
4145 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4146 }
4147 } else {
4148 DCHECK(constant->IsDoubleConstant());
4149 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004150 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004151 int32_t low_value = Low32Bits(value);
4152 int32_t high_value = High32Bits(value);
4153 Immediate low(low_value);
4154 Immediate high(high_value);
4155 if (destination.IsFpuRegister()) {
4156 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4157 if (value == 0) {
4158 // Easy handling of 0.0.
4159 __ xorpd(dest, dest);
4160 } else {
4161 __ pushl(high);
4162 __ pushl(low);
4163 __ movsd(dest, Address(ESP, 0));
4164 __ addl(ESP, Immediate(8));
4165 }
4166 } else {
4167 DCHECK(destination.IsDoubleStackSlot()) << destination;
4168 __ movl(Address(ESP, destination.GetStackIndex()), low);
4169 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4170 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004171 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004172 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004173 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004174 }
4175}
4176
Mark Mendella5c19ce2015-04-01 12:51:05 -04004177void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004178 Register suggested_scratch = reg == EAX ? EBX : EAX;
4179 ScratchRegisterScope ensure_scratch(
4180 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4181
4182 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4183 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4184 __ movl(Address(ESP, mem + stack_offset), reg);
4185 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004186}
4187
Mark Mendell7c8d0092015-01-26 11:21:33 -05004188void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004189 ScratchRegisterScope ensure_scratch(
4190 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4191
4192 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4193 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4194 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4195 __ movss(Address(ESP, mem + stack_offset), reg);
4196 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004197}
4198
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004199void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004200 ScratchRegisterScope ensure_scratch1(
4201 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004202
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004203 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4204 ScratchRegisterScope ensure_scratch2(
4205 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004206
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004207 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4208 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4209 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4210 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4211 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4212 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004213}
4214
4215void ParallelMoveResolverX86::EmitSwap(size_t index) {
4216 MoveOperands* move = moves_.Get(index);
4217 Location source = move->GetSource();
4218 Location destination = move->GetDestination();
4219
4220 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004221 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004222 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004225 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004226 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4227 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004228 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4229 // Use XOR Swap algorithm to avoid a temporary.
4230 DCHECK_NE(source.reg(), destination.reg());
4231 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4232 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4233 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4234 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4235 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4236 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4237 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004238 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4239 // Take advantage of the 16 bytes in the XMM register.
4240 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4241 Address stack(ESP, destination.GetStackIndex());
4242 // Load the double into the high doubleword.
4243 __ movhpd(reg, stack);
4244
4245 // Store the low double into the destination.
4246 __ movsd(stack, reg);
4247
4248 // Move the high double to the low double.
4249 __ psrldq(reg, Immediate(8));
4250 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4251 // Take advantage of the 16 bytes in the XMM register.
4252 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4253 Address stack(ESP, source.GetStackIndex());
4254 // Load the double into the high doubleword.
4255 __ movhpd(reg, stack);
4256
4257 // Store the low double into the destination.
4258 __ movsd(stack, reg);
4259
4260 // Move the high double to the low double.
4261 __ psrldq(reg, Immediate(8));
4262 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4263 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4264 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004265 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004266 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004267 }
4268}
4269
4270void ParallelMoveResolverX86::SpillScratch(int reg) {
4271 __ pushl(static_cast<Register>(reg));
4272}
4273
4274void ParallelMoveResolverX86::RestoreScratch(int reg) {
4275 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004276}
4277
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004278void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004279 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4280 ? LocationSummary::kCallOnSlowPath
4281 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004282 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004283 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004284 locations->SetOut(Location::RequiresRegister());
4285}
4286
4287void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004289 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004290 DCHECK(!cls->CanCallRuntime());
4291 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004292 codegen_->LoadCurrentMethod(out);
4293 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4294 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004295 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004296 codegen_->LoadCurrentMethod(out);
4297 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
4298 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004299
4300 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4301 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4302 codegen_->AddSlowPath(slow_path);
4303 __ testl(out, out);
4304 __ j(kEqual, slow_path->GetEntryLabel());
4305 if (cls->MustGenerateClinitCheck()) {
4306 GenerateClassInitializationCheck(slow_path, out);
4307 } else {
4308 __ Bind(slow_path->GetExitLabel());
4309 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004310 }
4311}
4312
4313void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4314 LocationSummary* locations =
4315 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4316 locations->SetInAt(0, Location::RequiresRegister());
4317 if (check->HasUses()) {
4318 locations->SetOut(Location::SameAsFirstInput());
4319 }
4320}
4321
4322void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004323 // We assume the class to not be null.
4324 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4325 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004326 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004327 GenerateClassInitializationCheck(slow_path,
4328 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004329}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004330
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004331void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4332 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4334 Immediate(mirror::Class::kStatusInitialized));
4335 __ j(kLess, slow_path->GetEntryLabel());
4336 __ Bind(slow_path->GetExitLabel());
4337 // No need for memory fence, thanks to the X86 memory model.
4338}
4339
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004340void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4341 LocationSummary* locations =
4342 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4343 locations->SetOut(Location::RequiresRegister());
4344}
4345
4346void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4347 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4348 codegen_->AddSlowPath(slow_path);
4349
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004351 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004352 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4353 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004354 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4355 __ testl(out, out);
4356 __ j(kEqual, slow_path->GetEntryLabel());
4357 __ Bind(slow_path->GetExitLabel());
4358}
4359
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004360void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4361 LocationSummary* locations =
4362 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4363 locations->SetOut(Location::RequiresRegister());
4364}
4365
4366void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4367 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004369 __ fs()->movl(address, Immediate(0));
4370}
4371
4372void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4373 LocationSummary* locations =
4374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4375 InvokeRuntimeCallingConvention calling_convention;
4376 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4377}
4378
4379void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4380 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4381 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4382}
4383
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004384void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004385 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4386 ? LocationSummary::kNoCall
4387 : LocationSummary::kCallOnSlowPath;
4388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4389 locations->SetInAt(0, Location::RequiresRegister());
4390 locations->SetInAt(1, Location::Any());
4391 locations->SetOut(Location::RequiresRegister());
4392}
4393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004394void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004395 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004397 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004399 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4400 Label done, zero;
4401 SlowPathCodeX86* slow_path = nullptr;
4402
4403 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004404 // Avoid null check if we know obj is not null.
4405 if (instruction->MustDoNullCheck()) {
4406 __ testl(obj, obj);
4407 __ j(kEqual, &zero);
4408 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 __ movl(out, Address(obj, class_offset));
4410 // Compare the class of `obj` with `cls`.
4411 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004412 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004413 } else {
4414 DCHECK(cls.IsStackSlot()) << cls;
4415 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4416 }
4417
4418 if (instruction->IsClassFinal()) {
4419 // Classes must be equal for the instanceof to succeed.
4420 __ j(kNotEqual, &zero);
4421 __ movl(out, Immediate(1));
4422 __ jmp(&done);
4423 } else {
4424 // If the classes are not equal, we go into a slow path.
4425 DCHECK(locations->OnlyCallsOnSlowPath());
4426 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004427 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004428 codegen_->AddSlowPath(slow_path);
4429 __ j(kNotEqual, slow_path->GetEntryLabel());
4430 __ movl(out, Immediate(1));
4431 __ jmp(&done);
4432 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004433
4434 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4435 __ Bind(&zero);
4436 __ movl(out, Immediate(0));
4437 }
4438
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004439 if (slow_path != nullptr) {
4440 __ Bind(slow_path->GetExitLabel());
4441 }
4442 __ Bind(&done);
4443}
4444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004445void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4447 instruction, LocationSummary::kCallOnSlowPath);
4448 locations->SetInAt(0, Location::RequiresRegister());
4449 locations->SetInAt(1, Location::Any());
4450 locations->AddTemp(Location::RequiresRegister());
4451}
4452
4453void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4454 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004455 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004456 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004457 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004458 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4459 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4460 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4461 codegen_->AddSlowPath(slow_path);
4462
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004463 // Avoid null check if we know obj is not null.
4464 if (instruction->MustDoNullCheck()) {
4465 __ testl(obj, obj);
4466 __ j(kEqual, slow_path->GetExitLabel());
4467 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004468
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004469 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004470 // Compare the class of `obj` with `cls`.
4471 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004472 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004473 } else {
4474 DCHECK(cls.IsStackSlot()) << cls;
4475 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4476 }
4477
4478 __ j(kNotEqual, slow_path->GetEntryLabel());
4479 __ Bind(slow_path->GetExitLabel());
4480}
4481
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004482void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4483 LocationSummary* locations =
4484 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4485 InvokeRuntimeCallingConvention calling_convention;
4486 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4487}
4488
4489void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4490 __ fs()->call(Address::Absolute(instruction->IsEnter()
4491 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4492 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4493 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4494}
4495
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004496void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4497void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4498void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4499
4500void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4501 LocationSummary* locations =
4502 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4503 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4504 || instruction->GetResultType() == Primitive::kPrimLong);
4505 locations->SetInAt(0, Location::RequiresRegister());
4506 locations->SetInAt(1, Location::Any());
4507 locations->SetOut(Location::SameAsFirstInput());
4508}
4509
4510void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4511 HandleBitwiseOperation(instruction);
4512}
4513
4514void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4515 HandleBitwiseOperation(instruction);
4516}
4517
4518void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4519 HandleBitwiseOperation(instruction);
4520}
4521
4522void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4523 LocationSummary* locations = instruction->GetLocations();
4524 Location first = locations->InAt(0);
4525 Location second = locations->InAt(1);
4526 DCHECK(first.Equals(locations->Out()));
4527
4528 if (instruction->GetResultType() == Primitive::kPrimInt) {
4529 if (second.IsRegister()) {
4530 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004531 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004532 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004533 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004534 } else {
4535 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004536 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004537 }
4538 } else if (second.IsConstant()) {
4539 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004540 __ andl(first.AsRegister<Register>(),
4541 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004542 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004543 __ orl(first.AsRegister<Register>(),
4544 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004545 } else {
4546 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004547 __ xorl(first.AsRegister<Register>(),
4548 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004549 }
4550 } else {
4551 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004552 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004553 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004554 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004555 } else {
4556 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004557 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004558 }
4559 }
4560 } else {
4561 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4562 if (second.IsRegisterPair()) {
4563 if (instruction->IsAnd()) {
4564 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4565 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4566 } else if (instruction->IsOr()) {
4567 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4568 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4569 } else {
4570 DCHECK(instruction->IsXor());
4571 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4572 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4573 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004574 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004575 if (instruction->IsAnd()) {
4576 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4577 __ andl(first.AsRegisterPairHigh<Register>(),
4578 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4579 } else if (instruction->IsOr()) {
4580 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4581 __ orl(first.AsRegisterPairHigh<Register>(),
4582 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4583 } else {
4584 DCHECK(instruction->IsXor());
4585 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4586 __ xorl(first.AsRegisterPairHigh<Register>(),
4587 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4588 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004589 } else {
4590 DCHECK(second.IsConstant()) << second;
4591 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004592 int32_t low_value = Low32Bits(value);
4593 int32_t high_value = High32Bits(value);
4594 Immediate low(low_value);
4595 Immediate high(high_value);
4596 Register first_low = first.AsRegisterPairLow<Register>();
4597 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004598 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004599 if (low_value == 0) {
4600 __ xorl(first_low, first_low);
4601 } else if (low_value != -1) {
4602 __ andl(first_low, low);
4603 }
4604 if (high_value == 0) {
4605 __ xorl(first_high, first_high);
4606 } else if (high_value != -1) {
4607 __ andl(first_high, high);
4608 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004609 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004610 if (low_value != 0) {
4611 __ orl(first_low, low);
4612 }
4613 if (high_value != 0) {
4614 __ orl(first_high, high);
4615 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004616 } else {
4617 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004618 if (low_value != 0) {
4619 __ xorl(first_low, low);
4620 }
4621 if (high_value != 0) {
4622 __ xorl(first_high, high);
4623 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004624 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004625 }
4626 }
4627}
4628
Calin Juravleb1498f62015-02-16 13:13:29 +00004629void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4630 // Nothing to do, this should be removed during prepare for register allocator.
4631 UNUSED(instruction);
4632 LOG(FATAL) << "Unreachable";
4633}
4634
4635void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4636 // Nothing to do, this should be removed during prepare for register allocator.
4637 UNUSED(instruction);
4638 LOG(FATAL) << "Unreachable";
4639}
4640
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004641} // namespace x86
4642} // namespace art