blob: 4065c443fda77ca6918da5ef5226b9556d663693 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
59 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
62};
63
Calin Juravled0d48522014-11-04 16:40:20 +000064class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
65 public:
66 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
67
Alexandre Rames2ed20af2015-03-06 13:55:35 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000069 __ Bind(GetEntryLabel());
70 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070071 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000072 }
73
74 private:
75 HDivZeroCheck* const instruction_;
76 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
77};
78
Calin Juravlebacfec32014-11-14 15:54:36 +000079class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000081 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000085 if (is_div_) {
86 __ negl(reg_);
87 } else {
88 __ movl(reg_, Immediate(0));
89 }
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ jmp(GetExitLabel());
91 }
92
93 private:
94 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000095 bool is_div_;
96 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000097};
98
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010099class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100100 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100101 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
102 Location index_location,
103 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000104 : instruction_(instruction),
105 index_location_(index_location),
106 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100110 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000111 // We're moving two locations to locations that could overlap, so we need a parallel
112 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000114 x86_codegen->EmitParallelMoves(
115 index_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100117 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000118 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100119 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
120 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700122 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 }
124
125 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100126 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 const Location index_location_;
128 const Location length_location_;
129
130 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000135 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000138 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700143 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 private:
162 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 Label return_label_;
165
166 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
167};
168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169class LoadStringSlowPathX86 : public SlowPathCodeX86 {
170 public:
171 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000174 LocationSummary* locations = instruction_->GetLocations();
175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
176
177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
178 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000185 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000187
188 __ jmp(GetExitLabel());
189 }
190
191 private:
192 HLoadString* const instruction_;
193
194 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 LoadClassSlowPathX86(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
206
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 InvokeRuntimeCallingConvention calling_convention;
214 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ fs()->call(Address::Absolute(do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ jmp(GetExitLabel());
229 }
230
231 private:
232 // The class this slow path will load.
233 HLoadClass* const cls_;
234
235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
246};
247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
249 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 TypeCheckSlowPathX86(HInstruction* instruction,
251 Location class_to_check,
252 Location object_class,
253 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 class_to_check_(class_to_check),
256 object_class_(object_class),
257 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000261 DCHECK(instruction_->IsCheckCast()
262 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
268 // We're moving two locations to locations that could overlap, so we need a parallel
269 // move resolver.
270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000271 x86_codegen->EmitParallelMoves(
272 class_to_check_,
273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100274 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000275 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100276 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
277 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000280 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
281 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 } else {
283 DCHECK(instruction_->IsCheckCast());
284 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
289 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 __ jmp(GetExitLabel());
294 }
295
296 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 HInstruction* const instruction_;
298 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
303};
304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700305class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
306 public:
307 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
308 : instruction_(instruction) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, instruction_->GetLocations());
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
314 // No need to restore live registers.
315 DCHECK(instruction_->IsDeoptimize());
316 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
317 uint32_t dex_pc = deoptimize->GetDexPc();
318 codegen->RecordPcInfo(instruction_, dex_pc, this);
319 }
320
321 private:
322 HInstruction* const instruction_;
323 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100327#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100344 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100348 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Mark Mendell7c8d0092015-01-26 11:21:33 -0500361size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
363 return GetFloatingPointSpillSlotSize();
364}
365
366size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
367 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
368 return GetFloatingPointSpillSlotSize();
369}
370
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
372 const X86InstructionSetFeatures& isa_features,
373 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500374 : CodeGenerator(graph,
375 kNumberOfCpuRegisters,
376 kNumberOfXmmRegisters,
377 kNumberOfRegisterPairs,
378 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
379 arraysize(kCoreCalleeSaves))
380 | (1 << kFakeReturnRegister),
381 0,
382 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100383 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 move_resolver_(graph->GetArena(), this),
387 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000388 // Use a fake return address register to mimic Quick.
389 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100390}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 switch (type) {
394 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 X86ManagedRegister pair =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
399 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
401 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100402 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimByte:
407 case Primitive::kPrimBoolean:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
416 X86ManagedRegister current =
417 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
418 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 }
421 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 return Location::FpuRegisterLocation(
428 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Mark Mendell5f874182015-03-04 15:42:45 -0500438void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
Mark Mendell5f874182015-03-04 15:42:45 -0500445 if (is_baseline) {
446 blocked_core_registers_[EBP] = true;
447 blocked_core_registers_[ESI] = true;
448 blocked_core_registers_[EDI] = true;
449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100450
451 UpdateBlockedPairRegisters();
452}
453
454void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
455 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
456 X86ManagedRegister current =
457 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
458 if (blocked_core_registers_[current.AsRegisterPairLow()]
459 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
460 blocked_register_pairs_[i] = true;
461 }
462 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463}
464
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
466 : HGraphVisitor(graph),
467 assembler_(codegen->GetAssembler()),
468 codegen_(codegen) {}
469
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100471 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472}
473
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100475 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000476 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000477 bool skip_overflow_check =
478 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000480
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 }
485
Mark Mendell5f874182015-03-04 15:42:45 -0500486 if (HasEmptyFrame()) {
487 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000488 }
Mark Mendell5f874182015-03-04 15:42:45 -0500489
490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
491 Register reg = kCoreCalleeSaves[i];
492 if (allocated_registers_.ContainsCoreRegister(reg)) {
493 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().AdjustCFAOffset(kX86WordSize);
495 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500496 }
497 }
498
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 int adjust = GetFrameSize() - FrameEntrySpillSize();
500 __ subl(ESP, Immediate(adjust));
501 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100502 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000503}
504
505void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100506 __ cfi().RememberState();
507 if (!HasEmptyFrame()) {
508 int adjust = GetFrameSize() - FrameEntrySpillSize();
509 __ addl(ESP, Immediate(adjust));
510 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500511
David Srbeckyc34dc932015-04-12 09:27:43 +0100512 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
513 Register reg = kCoreCalleeSaves[i];
514 if (allocated_registers_.ContainsCoreRegister(reg)) {
515 __ popl(reg);
516 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
517 __ cfi().Restore(DWARFReg(reg));
518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100521 __ ret();
522 __ cfi().RestoreState();
523 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528}
529
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100530void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000531 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000533}
534
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
536 switch (load->GetType()) {
537 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545
546 case Primitive::kPrimBoolean:
547 case Primitive::kPrimByte:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimShort:
550 case Primitive::kPrimVoid:
551 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700552 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700556 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557}
558
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100559Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
560 switch (type) {
561 case Primitive::kPrimBoolean:
562 case Primitive::kPrimByte:
563 case Primitive::kPrimChar:
564 case Primitive::kPrimShort:
565 case Primitive::kPrimInt:
566 case Primitive::kPrimNot:
567 return Location::RegisterLocation(EAX);
568
569 case Primitive::kPrimLong:
570 return Location::RegisterPairLocation(EAX, EDX);
571
572 case Primitive::kPrimVoid:
573 return Location::NoLocation();
574
575 case Primitive::kPrimDouble:
576 case Primitive::kPrimFloat:
577 return Location::FpuRegisterLocation(XMM0);
578 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100579
580 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100581}
582
583Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
584 return Location::RegisterLocation(kMethodRegisterArgument);
585}
586
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100587Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100588 switch (type) {
589 case Primitive::kPrimBoolean:
590 case Primitive::kPrimByte:
591 case Primitive::kPrimChar:
592 case Primitive::kPrimShort:
593 case Primitive::kPrimInt:
594 case Primitive::kPrimNot: {
595 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000596 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100598 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000600 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100601 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100602 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000604 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100605 uint32_t index = gp_index_;
606 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000607 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100609 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
610 calling_convention.GetRegisterPairAt(index));
611 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000613 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
614 }
615 }
616
617 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100618 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000619 stack_index_++;
620 if (index < calling_convention.GetNumberOfFpuRegisters()) {
621 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
622 } else {
623 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
624 }
625 }
626
627 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100628 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000629 stack_index_ += 2;
630 if (index < calling_convention.GetNumberOfFpuRegisters()) {
631 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
632 } else {
633 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100634 }
635 }
636
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100637 case Primitive::kPrimVoid:
638 LOG(FATAL) << "Unexpected parameter type " << type;
639 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100640 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100641 return Location();
642}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644void CodeGeneratorX86::Move32(Location destination, Location source) {
645 if (source.Equals(destination)) {
646 return;
647 }
648 if (destination.IsRegister()) {
649 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else {
654 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else if (destination.IsFpuRegister()) {
658 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000659 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000661 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 } else {
663 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000667 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500672 } else if (source.IsConstant()) {
673 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000674 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500675 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 } else {
677 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100678 __ pushl(Address(ESP, source.GetStackIndex()));
679 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 }
681 }
682}
683
684void CodeGeneratorX86::Move64(Location destination, Location source) {
685 if (source.Equals(destination)) {
686 return;
687 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 if (destination.IsRegisterPair()) {
689 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000690 EmitParallelMoves(
691 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
692 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100693 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000694 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100695 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
696 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
698 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000700 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100702 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
703 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
705 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500707 if (source.IsFpuRegister()) {
708 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
709 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else {
712 LOG(FATAL) << "Unimplemented";
713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000715 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000717 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100720 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000723 } else if (source.IsConstant()) {
724 HConstant* constant = source.GetConstant();
725 int64_t value;
726 if (constant->IsLongConstant()) {
727 value = constant->AsLongConstant()->GetValue();
728 } else {
729 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000730 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000731 }
732 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
733 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000735 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000736 EmitParallelMoves(
737 Location::StackSlot(source.GetStackIndex()),
738 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100739 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000740 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100741 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
742 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 }
744 }
745}
746
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100747void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000748 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100749 if (instruction->IsCurrentMethod()) {
750 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
751 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100753 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000755 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
756 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000757 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000758 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 } else if (location.IsStackSlot()) {
760 __ movl(Address(ESP, location.GetStackIndex()), imm);
761 } else {
762 DCHECK(location.IsConstant());
763 DCHECK_EQ(location.GetConstant(), const_to_move);
764 }
765 } else if (const_to_move->IsLongConstant()) {
766 int64_t value = const_to_move->AsLongConstant()->GetValue();
767 if (location.IsRegisterPair()) {
768 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
769 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
770 } else if (location.IsDoubleStackSlot()) {
771 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000772 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
773 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000774 } else {
775 DCHECK(location.IsConstant());
776 DCHECK_EQ(location.GetConstant(), instruction);
777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000779 } else if (instruction->IsTemporary()) {
780 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000781 if (temp_location.IsStackSlot()) {
782 Move32(location, temp_location);
783 } else {
784 DCHECK(temp_location.IsDoubleStackSlot());
785 Move64(location, temp_location);
786 }
Roland Levillain476df552014-10-09 17:51:36 +0100787 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 switch (instruction->GetType()) {
790 case Primitive::kPrimBoolean:
791 case Primitive::kPrimByte:
792 case Primitive::kPrimChar:
793 case Primitive::kPrimShort:
794 case Primitive::kPrimInt:
795 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 case Primitive::kPrimFloat:
797 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 case Primitive::kPrimDouble:
802 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 break;
804
805 default:
806 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
807 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000808 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 switch (instruction->GetType()) {
811 case Primitive::kPrimBoolean:
812 case Primitive::kPrimByte:
813 case Primitive::kPrimChar:
814 case Primitive::kPrimShort:
815 case Primitive::kPrimInt:
816 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 break;
820
821 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 break;
825
826 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829 }
830}
831
832void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834}
835
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000837 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 DCHECK(!successor->IsExitBlock());
839
840 HBasicBlock* block = got->GetBlock();
841 HInstruction* previous = got->GetPrevious();
842
843 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000844 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
846 return;
847 }
848
849 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
850 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
851 }
852 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000854 }
855}
856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000859}
860
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000863}
864
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700865void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
866 Label* true_target,
867 Label* false_target,
868 Label* always_true_target) {
869 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 if (cond->IsIntConstant()) {
871 // Constant condition, statically compared against 1.
872 int32_t cond_value = cond->AsIntConstant()->GetValue();
873 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700874 if (always_true_target != nullptr) {
875 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100876 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 DCHECK_EQ(cond_value, 0);
880 }
881 } else {
882 bool materialized =
883 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
884 // Moves do not affect the eflags register, so if the condition is
885 // evaluated just before the if, we don't need to evaluate it
886 // again.
887 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700888 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100889 if (materialized) {
890 if (!eflags_set) {
891 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100893 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500894 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100895 } else {
896 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
897 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700898 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100899 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700900 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100901 }
902 } else {
903 Location lhs = cond->GetLocations()->InAt(0);
904 Location rhs = cond->GetLocations()->InAt(1);
905 // LHS is guaranteed to be in a register (see
906 // LocationsBuilderX86::VisitCondition).
907 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000908 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100909 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100910 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500911 if (constant == 0) {
912 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
913 } else {
914 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
915 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000917 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100918 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700919 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700920 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700922 if (false_target != nullptr) {
923 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924 }
925}
926
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700927void LocationsBuilderX86::VisitIf(HIf* if_instr) {
928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
930 HInstruction* cond = if_instr->InputAt(0);
931 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
932 locations->SetInAt(0, Location::Any());
933 }
934}
935
936void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
937 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
938 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
939 Label* always_true_target = true_target;
940 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
941 if_instr->IfTrueSuccessor())) {
942 always_true_target = nullptr;
943 }
944 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
945 if_instr->IfFalseSuccessor())) {
946 false_target = nullptr;
947 }
948 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
949}
950
951void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
952 LocationSummary* locations = new (GetGraph()->GetArena())
953 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
954 HInstruction* cond = deoptimize->InputAt(0);
955 DCHECK(cond->IsCondition());
956 if (cond->AsCondition()->NeedsMaterialization()) {
957 locations->SetInAt(0, Location::Any());
958 }
959}
960
961void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
962 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
963 DeoptimizationSlowPathX86(deoptimize);
964 codegen_->AddSlowPath(slow_path);
965 Label* slow_path_entry = slow_path->GetEntryLabel();
966 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
967}
968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000971}
972
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
974 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975}
976
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100978 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100982 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100989 switch (store->InputAt(1)->GetType()) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
998 break;
999
1000 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1003 break;
1004
1005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001008}
1009
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001010void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012}
1013
Roland Levillain0d37cd02015-05-27 16:39:19 +01001014void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001015 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001016 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001017 locations->SetInAt(0, Location::RequiresRegister());
1018 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001019 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001020 // We need a byte register.
1021 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001022 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023}
1024
Roland Levillain0d37cd02015-05-27 16:39:19 +01001025void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1026 if (cond->NeedsMaterialization()) {
1027 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001029 // Clear register: setcc only sets the low byte.
1030 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001031 Location lhs = locations->InAt(0);
1032 Location rhs = locations->InAt(1);
1033 if (rhs.IsRegister()) {
1034 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1035 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001036 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001037 if (constant == 0) {
1038 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1039 } else {
1040 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1041 }
Dave Allison20dfc792014-06-16 20:44:29 -07001042 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001043 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001044 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001045 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046 }
Dave Allison20dfc792014-06-16 20:44:29 -07001047}
1048
1049void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1062 VisitCondition(comp);
1063}
1064
1065void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1066 VisitCondition(comp);
1067}
1068
1069void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1070 VisitCondition(comp);
1071}
1072
1073void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1074 VisitCondition(comp);
1075}
1076
1077void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1078 VisitCondition(comp);
1079}
1080
1081void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1082 VisitCondition(comp);
1083}
1084
1085void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1086 VisitCondition(comp);
1087}
1088
1089void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1090 VisitCondition(comp);
1091}
1092
1093void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1094 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095}
1096
1097void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001100 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101}
1102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001103void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001105 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106}
1107
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001108void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1111 locations->SetOut(Location::ConstantLocation(constant));
1112}
1113
1114void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1115 // Will be generated at use site.
1116 UNUSED(constant);
1117}
1118
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001122 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123}
1124
1125void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128}
1129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001130void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1133 locations->SetOut(Location::ConstantLocation(constant));
1134}
1135
1136void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139}
1140
1141void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1144 locations->SetOut(Location::ConstantLocation(constant));
1145}
1146
1147void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1148 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001150}
1151
Calin Juravle27df7582015-04-17 19:12:31 +01001152void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1153 memory_barrier->SetLocations(nullptr);
1154}
1155
1156void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1157 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1158}
1159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162}
1163
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001164void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001166 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001167}
1168
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001170 LocationSummary* locations =
1171 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 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:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
1182 case Primitive::kPrimLong:
1183 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 0, Location::RegisterPairLocation(EAX, 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:
1189 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 break;
1192
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001198void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 if (kIsDebugBuild) {
1200 switch (ret->InputAt(0)->GetType()) {
1201 case Primitive::kPrimBoolean:
1202 case Primitive::kPrimByte:
1203 case Primitive::kPrimChar:
1204 case Primitive::kPrimShort:
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 break;
1209
1210 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1212 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 break;
1214
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 break;
1219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 }
1223 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001225}
1226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001228 // When we do not run baseline, explicit clinit checks triggered by static
1229 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1230 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001231
Mark Mendellfb8d2792015-03-31 22:16:59 -04001232 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001233 if (intrinsic.TryDispatch(invoke)) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001234 LocationSummary* locations = invoke->GetLocations();
1235 if (locations->CanCall()) {
1236 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::RequiresRegister());
1237 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001238 return;
1239 }
1240
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001241 HandleInvoke(invoke);
1242}
1243
Mark Mendell09ed1a32015-03-25 08:30:06 -04001244static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1245 if (invoke->GetLocations()->Intrinsified()) {
1246 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1247 intrinsic.Dispatch(invoke);
1248 return true;
1249 }
1250 return false;
1251}
1252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001253void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001254 // When we do not run baseline, explicit clinit checks triggered by static
1255 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1256 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001257
Mark Mendell09ed1a32015-03-25 08:30:06 -04001258 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1259 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001260 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001261
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001262 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001263 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001264 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001265 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001266}
1267
1268void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1269 HandleInvoke(invoke);
1270}
1271
1272void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001273 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001274 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001275}
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001279 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1280 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001284 DCHECK(receiver.IsRegister());
1285 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001286 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001287 // temp = temp->GetMethodAt(method_offset);
1288 __ movl(temp, Address(temp, method_offset));
1289 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001290 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001291 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001292
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001293 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001294 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001295}
1296
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001297void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1298 HandleInvoke(invoke);
1299 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001300 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001301}
1302
1303void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1304 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001306 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1307 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308 LocationSummary* locations = invoke->GetLocations();
1309 Location receiver = locations->InAt(0);
1310 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1311
1312 // Set the hidden argument.
1313 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001315
1316 // temp = object->GetClass();
1317 if (receiver.IsStackSlot()) {
1318 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1319 __ movl(temp, Address(temp, class_offset));
1320 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001321 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001322 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001323 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001324 // temp = temp->GetImtEntryAt(method_offset);
1325 __ movl(temp, Address(temp, method_offset));
1326 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001327 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001328 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329
1330 DCHECK(!codegen_->IsLeafMethod());
1331 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1332}
1333
Roland Levillain88cb1752014-10-20 16:36:47 +01001334void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1335 LocationSummary* locations =
1336 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1337 switch (neg->GetResultType()) {
1338 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001339 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001340 locations->SetInAt(0, Location::RequiresRegister());
1341 locations->SetOut(Location::SameAsFirstInput());
1342 break;
1343
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001345 locations->SetInAt(0, Location::RequiresFpuRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 locations->AddTemp(Location::RequiresRegister());
1348 locations->AddTemp(Location::RequiresFpuRegister());
1349 break;
1350
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001352 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001353 locations->SetOut(Location::SameAsFirstInput());
1354 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 break;
1356
1357 default:
1358 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1359 }
1360}
1361
1362void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1363 LocationSummary* locations = neg->GetLocations();
1364 Location out = locations->Out();
1365 Location in = locations->InAt(0);
1366 switch (neg->GetResultType()) {
1367 case Primitive::kPrimInt:
1368 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001371 break;
1372
1373 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001374 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001375 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001376 __ negl(out.AsRegisterPairLow<Register>());
1377 // Negation is similar to subtraction from zero. The least
1378 // significant byte triggers a borrow when it is different from
1379 // zero; to take it into account, add 1 to the most significant
1380 // byte if the carry flag (CF) is set to 1 after the first NEGL
1381 // operation.
1382 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1383 __ negl(out.AsRegisterPairHigh<Register>());
1384 break;
1385
Roland Levillain5368c212014-11-27 15:03:41 +00001386 case Primitive::kPrimFloat: {
1387 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001388 Register constant = locations->GetTemp(0).AsRegister<Register>();
1389 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001390 // Implement float negation with an exclusive or with value
1391 // 0x80000000 (mask for bit 31, representing the sign of a
1392 // single-precision floating-point number).
1393 __ movl(constant, Immediate(INT32_C(0x80000000)));
1394 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001395 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001396 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001397 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001398
Roland Levillain5368c212014-11-27 15:03:41 +00001399 case Primitive::kPrimDouble: {
1400 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001402 // Implement double negation with an exclusive or with value
1403 // 0x8000000000000000 (mask for bit 63, representing the sign of
1404 // a double-precision floating-point number).
1405 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001407 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001408 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001409
1410 default:
1411 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1412 }
1413}
1414
Roland Levillaindff1f282014-11-05 14:15:05 +00001415void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 Primitive::Type result_type = conversion->GetResultType();
1417 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001418 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001419
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001420 // The float-to-long and double-to-long type conversions rely on a
1421 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001422 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001423 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1424 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001425 ? LocationSummary::kCall
1426 : LocationSummary::kNoCall;
1427 LocationSummary* locations =
1428 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1429
David Brazdilb2bd1c52015-03-25 11:17:37 +00001430 // The Java language does not allow treating boolean as an integral type but
1431 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001432
Roland Levillaindff1f282014-11-05 14:15:05 +00001433 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001434 case Primitive::kPrimByte:
1435 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001436 case Primitive::kPrimBoolean:
1437 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001438 case Primitive::kPrimShort:
1439 case Primitive::kPrimInt:
1440 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001441 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001442 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1443 // Make the output overlap to please the register allocator. This greatly simplifies
1444 // the validation of the linear scan implementation
1445 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001446 break;
1447
1448 default:
1449 LOG(FATAL) << "Unexpected type conversion from " << input_type
1450 << " to " << result_type;
1451 }
1452 break;
1453
Roland Levillain01a8d712014-11-14 16:27:39 +00001454 case Primitive::kPrimShort:
1455 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001456 case Primitive::kPrimBoolean:
1457 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001458 case Primitive::kPrimByte:
1459 case Primitive::kPrimInt:
1460 case Primitive::kPrimChar:
1461 // Processing a Dex `int-to-short' instruction.
1462 locations->SetInAt(0, Location::Any());
1463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1464 break;
1465
1466 default:
1467 LOG(FATAL) << "Unexpected type conversion from " << input_type
1468 << " to " << result_type;
1469 }
1470 break;
1471
Roland Levillain946e1432014-11-11 17:35:19 +00001472 case Primitive::kPrimInt:
1473 switch (input_type) {
1474 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001475 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001476 locations->SetInAt(0, Location::Any());
1477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1478 break;
1479
1480 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001481 // Processing a Dex `float-to-int' instruction.
1482 locations->SetInAt(0, Location::RequiresFpuRegister());
1483 locations->SetOut(Location::RequiresRegister());
1484 locations->AddTemp(Location::RequiresFpuRegister());
1485 break;
1486
Roland Levillain946e1432014-11-11 17:35:19 +00001487 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001488 // Processing a Dex `double-to-int' instruction.
1489 locations->SetInAt(0, Location::RequiresFpuRegister());
1490 locations->SetOut(Location::RequiresRegister());
1491 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001492 break;
1493
1494 default:
1495 LOG(FATAL) << "Unexpected type conversion from " << input_type
1496 << " to " << result_type;
1497 }
1498 break;
1499
Roland Levillaindff1f282014-11-05 14:15:05 +00001500 case Primitive::kPrimLong:
1501 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001502 case Primitive::kPrimBoolean:
1503 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 case Primitive::kPrimByte:
1505 case Primitive::kPrimShort:
1506 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001507 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001508 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 locations->SetInAt(0, Location::RegisterLocation(EAX));
1510 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1511 break;
1512
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001513 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001514 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001515 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001516 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001517 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1518 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1519
Vladimir Marko949c91f2015-01-27 10:48:44 +00001520 // The runtime helper puts the result in EAX, EDX.
1521 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001522 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001523 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 }
1529 break;
1530
Roland Levillain981e4542014-11-14 11:47:14 +00001531 case Primitive::kPrimChar:
1532 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001533 case Primitive::kPrimBoolean:
1534 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001535 case Primitive::kPrimByte:
1536 case Primitive::kPrimShort:
1537 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001538 // Processing a Dex `int-to-char' instruction.
1539 locations->SetInAt(0, Location::Any());
1540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1541 break;
1542
1543 default:
1544 LOG(FATAL) << "Unexpected type conversion from " << input_type
1545 << " to " << result_type;
1546 }
1547 break;
1548
Roland Levillaindff1f282014-11-05 14:15:05 +00001549 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001550 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001551 case Primitive::kPrimBoolean:
1552 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001553 case Primitive::kPrimByte:
1554 case Primitive::kPrimShort:
1555 case Primitive::kPrimInt:
1556 case Primitive::kPrimChar:
1557 // Processing a Dex `int-to-float' instruction.
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetOut(Location::RequiresFpuRegister());
1560 break;
1561
1562 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001563 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001564 locations->SetInAt(0, Location::Any());
1565 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001566 break;
1567
Roland Levillaincff13742014-11-17 14:32:17 +00001568 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001569 // Processing a Dex `double-to-float' instruction.
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001572 break;
1573
1574 default:
1575 LOG(FATAL) << "Unexpected type conversion from " << input_type
1576 << " to " << result_type;
1577 };
1578 break;
1579
Roland Levillaindff1f282014-11-05 14:15:05 +00001580 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001581 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001582 case Primitive::kPrimBoolean:
1583 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001584 case Primitive::kPrimByte:
1585 case Primitive::kPrimShort:
1586 case Primitive::kPrimInt:
1587 case Primitive::kPrimChar:
1588 // Processing a Dex `int-to-double' instruction.
1589 locations->SetInAt(0, Location::RequiresRegister());
1590 locations->SetOut(Location::RequiresFpuRegister());
1591 break;
1592
1593 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001594 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001595 locations->SetInAt(0, Location::Any());
1596 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001597 break;
1598
Roland Levillaincff13742014-11-17 14:32:17 +00001599 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001600 // Processing a Dex `float-to-double' instruction.
1601 locations->SetInAt(0, Location::RequiresFpuRegister());
1602 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001603 break;
1604
1605 default:
1606 LOG(FATAL) << "Unexpected type conversion from " << input_type
1607 << " to " << result_type;
1608 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001609 break;
1610
1611 default:
1612 LOG(FATAL) << "Unexpected type conversion from " << input_type
1613 << " to " << result_type;
1614 }
1615}
1616
1617void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1618 LocationSummary* locations = conversion->GetLocations();
1619 Location out = locations->Out();
1620 Location in = locations->InAt(0);
1621 Primitive::Type result_type = conversion->GetResultType();
1622 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001623 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001624 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001625 case Primitive::kPrimByte:
1626 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001627 case Primitive::kPrimBoolean:
1628 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001629 case Primitive::kPrimShort:
1630 case Primitive::kPrimInt:
1631 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001632 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001633 if (in.IsRegister()) {
1634 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001635 } else {
1636 DCHECK(in.GetConstant()->IsIntConstant());
1637 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1638 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1639 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001640 break;
1641
1642 default:
1643 LOG(FATAL) << "Unexpected type conversion from " << input_type
1644 << " to " << result_type;
1645 }
1646 break;
1647
Roland Levillain01a8d712014-11-14 16:27:39 +00001648 case Primitive::kPrimShort:
1649 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001650 case Primitive::kPrimBoolean:
1651 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001652 case Primitive::kPrimByte:
1653 case Primitive::kPrimInt:
1654 case Primitive::kPrimChar:
1655 // Processing a Dex `int-to-short' instruction.
1656 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001657 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001658 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001660 } else {
1661 DCHECK(in.GetConstant()->IsIntConstant());
1662 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001663 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001664 }
1665 break;
1666
1667 default:
1668 LOG(FATAL) << "Unexpected type conversion from " << input_type
1669 << " to " << result_type;
1670 }
1671 break;
1672
Roland Levillain946e1432014-11-11 17:35:19 +00001673 case Primitive::kPrimInt:
1674 switch (input_type) {
1675 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001676 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001677 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001678 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001679 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001681 } else {
1682 DCHECK(in.IsConstant());
1683 DCHECK(in.GetConstant()->IsLongConstant());
1684 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001686 }
1687 break;
1688
Roland Levillain3f8f9362014-12-02 17:45:01 +00001689 case Primitive::kPrimFloat: {
1690 // Processing a Dex `float-to-int' instruction.
1691 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1692 Register output = out.AsRegister<Register>();
1693 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1694 Label done, nan;
1695
1696 __ movl(output, Immediate(kPrimIntMax));
1697 // temp = int-to-float(output)
1698 __ cvtsi2ss(temp, output);
1699 // if input >= temp goto done
1700 __ comiss(input, temp);
1701 __ j(kAboveEqual, &done);
1702 // if input == NaN goto nan
1703 __ j(kUnordered, &nan);
1704 // output = float-to-int-truncate(input)
1705 __ cvttss2si(output, input);
1706 __ jmp(&done);
1707 __ Bind(&nan);
1708 // output = 0
1709 __ xorl(output, output);
1710 __ Bind(&done);
1711 break;
1712 }
1713
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001714 case Primitive::kPrimDouble: {
1715 // Processing a Dex `double-to-int' instruction.
1716 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1717 Register output = out.AsRegister<Register>();
1718 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1719 Label done, nan;
1720
1721 __ movl(output, Immediate(kPrimIntMax));
1722 // temp = int-to-double(output)
1723 __ cvtsi2sd(temp, output);
1724 // if input >= temp goto done
1725 __ comisd(input, temp);
1726 __ j(kAboveEqual, &done);
1727 // if input == NaN goto nan
1728 __ j(kUnordered, &nan);
1729 // output = double-to-int-truncate(input)
1730 __ cvttsd2si(output, input);
1731 __ jmp(&done);
1732 __ Bind(&nan);
1733 // output = 0
1734 __ xorl(output, output);
1735 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001736 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001737 }
Roland Levillain946e1432014-11-11 17:35:19 +00001738
1739 default:
1740 LOG(FATAL) << "Unexpected type conversion from " << input_type
1741 << " to " << result_type;
1742 }
1743 break;
1744
Roland Levillaindff1f282014-11-05 14:15:05 +00001745 case Primitive::kPrimLong:
1746 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001747 case Primitive::kPrimBoolean:
1748 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 case Primitive::kPrimByte:
1750 case Primitive::kPrimShort:
1751 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001752 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001753 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1755 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001757 __ cdq();
1758 break;
1759
1760 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001761 // Processing a Dex `float-to-long' instruction.
1762 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001763 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1764 break;
1765
Roland Levillaindff1f282014-11-05 14:15:05 +00001766 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001767 // Processing a Dex `double-to-long' instruction.
1768 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1769 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001770 break;
1771
1772 default:
1773 LOG(FATAL) << "Unexpected type conversion from " << input_type
1774 << " to " << result_type;
1775 }
1776 break;
1777
Roland Levillain981e4542014-11-14 11:47:14 +00001778 case Primitive::kPrimChar:
1779 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001780 case Primitive::kPrimBoolean:
1781 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001782 case Primitive::kPrimByte:
1783 case Primitive::kPrimShort:
1784 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001785 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1786 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001787 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001788 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001789 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001790 } else {
1791 DCHECK(in.GetConstant()->IsIntConstant());
1792 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001794 }
1795 break;
1796
1797 default:
1798 LOG(FATAL) << "Unexpected type conversion from " << input_type
1799 << " to " << result_type;
1800 }
1801 break;
1802
Roland Levillaindff1f282014-11-05 14:15:05 +00001803 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001804 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001805 case Primitive::kPrimBoolean:
1806 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001807 case Primitive::kPrimByte:
1808 case Primitive::kPrimShort:
1809 case Primitive::kPrimInt:
1810 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001811 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001812 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001813 break;
1814
Roland Levillain6d0e4832014-11-27 18:31:21 +00001815 case Primitive::kPrimLong: {
1816 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001817 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001818
Roland Levillain232ade02015-04-20 15:14:36 +01001819 // Create stack space for the call to
1820 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1821 // TODO: enhance register allocator to ask for stack temporaries.
1822 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1823 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1824 __ subl(ESP, Immediate(adjustment));
1825 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001826
Roland Levillain232ade02015-04-20 15:14:36 +01001827 // Load the value to the FP stack, using temporaries if needed.
1828 PushOntoFPStack(in, 0, adjustment, false, true);
1829
1830 if (out.IsStackSlot()) {
1831 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1832 } else {
1833 __ fstps(Address(ESP, 0));
1834 Location stack_temp = Location::StackSlot(0);
1835 codegen_->Move32(out, stack_temp);
1836 }
1837
1838 // Remove the temporary stack space we allocated.
1839 if (adjustment != 0) {
1840 __ addl(ESP, Immediate(adjustment));
1841 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001842 break;
1843 }
1844
Roland Levillaincff13742014-11-17 14:32:17 +00001845 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001846 // Processing a Dex `double-to-float' instruction.
1847 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001848 break;
1849
1850 default:
1851 LOG(FATAL) << "Unexpected type conversion from " << input_type
1852 << " to " << result_type;
1853 };
1854 break;
1855
Roland Levillaindff1f282014-11-05 14:15:05 +00001856 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001857 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001858 case Primitive::kPrimBoolean:
1859 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001860 case Primitive::kPrimByte:
1861 case Primitive::kPrimShort:
1862 case Primitive::kPrimInt:
1863 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001864 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001865 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001866 break;
1867
Roland Levillain647b9ed2014-11-27 12:06:00 +00001868 case Primitive::kPrimLong: {
1869 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001870 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001871
Roland Levillain232ade02015-04-20 15:14:36 +01001872 // Create stack space for the call to
1873 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1874 // TODO: enhance register allocator to ask for stack temporaries.
1875 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1876 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1877 __ subl(ESP, Immediate(adjustment));
1878 }
1879
1880 // Load the value to the FP stack, using temporaries if needed.
1881 PushOntoFPStack(in, 0, adjustment, false, true);
1882
1883 if (out.IsDoubleStackSlot()) {
1884 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1885 } else {
1886 __ fstpl(Address(ESP, 0));
1887 Location stack_temp = Location::DoubleStackSlot(0);
1888 codegen_->Move64(out, stack_temp);
1889 }
1890
1891 // Remove the temporary stack space we allocated.
1892 if (adjustment != 0) {
1893 __ addl(ESP, Immediate(adjustment));
1894 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001895 break;
1896 }
1897
Roland Levillaincff13742014-11-17 14:32:17 +00001898 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001899 // Processing a Dex `float-to-double' instruction.
1900 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001901 break;
1902
1903 default:
1904 LOG(FATAL) << "Unexpected type conversion from " << input_type
1905 << " to " << result_type;
1906 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001907 break;
1908
1909 default:
1910 LOG(FATAL) << "Unexpected type conversion from " << input_type
1911 << " to " << result_type;
1912 }
1913}
1914
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001915void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001918 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001919 case Primitive::kPrimInt: {
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1923 break;
1924 }
1925
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001927 locations->SetInAt(0, Location::RequiresRegister());
1928 locations->SetInAt(1, Location::Any());
1929 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931 }
1932
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble: {
1935 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001936 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001941 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1943 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001944 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001945}
1946
1947void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1948 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001949 Location first = locations->InAt(0);
1950 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001951 Location out = locations->Out();
1952
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001953 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001955 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001956 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1957 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001958 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1959 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001960 } else {
1961 __ leal(out.AsRegister<Register>(), Address(
1962 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1963 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001965 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1966 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1967 __ addl(out.AsRegister<Register>(), Immediate(value));
1968 } else {
1969 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1970 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001971 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001972 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001975 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 }
1977
1978 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001979 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1981 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001982 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1984 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001985 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001986 } else {
1987 DCHECK(second.IsConstant()) << second;
1988 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1989 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1990 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
1993 }
1994
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995 case Primitive::kPrimFloat: {
1996 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001997 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 }
2001
2002 case Primitive::kPrimDouble: {
2003 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 }
2006 break;
2007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002008
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002010 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002011 }
2012}
2013
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002014void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002018 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::Any());
2022 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002023 break;
2024 }
Calin Juravle11351682014-10-23 15:38:15 +01002025 case Primitive::kPrimFloat:
2026 case Primitive::kPrimDouble: {
2027 locations->SetInAt(0, Location::RequiresFpuRegister());
2028 locations->SetInAt(1, Location::RequiresFpuRegister());
2029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030 break;
Calin Juravle11351682014-10-23 15:38:15 +01002031 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002033 default:
Calin Juravle11351682014-10-23 15:38:15 +01002034 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036}
2037
2038void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2039 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 Location first = locations->InAt(0);
2041 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002042 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002045 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002048 __ subl(first.AsRegister<Register>(),
2049 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002050 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002052 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002053 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002054 }
2055
2056 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002057 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002058 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2059 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002060 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002061 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 __ sbbl(first.AsRegisterPairHigh<Register>(),
2063 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002064 } else {
2065 DCHECK(second.IsConstant()) << second;
2066 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2067 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2068 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 break;
2071 }
2072
Calin Juravle11351682014-10-23 15:38:15 +01002073 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075 break;
Calin Juravle11351682014-10-23 15:38:15 +01002076 }
2077
2078 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002080 break;
2081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002083 default:
Calin Juravle11351682014-10-23 15:38:15 +01002084 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002085 }
2086}
2087
Calin Juravle34bacdf2014-10-07 20:23:36 +01002088void LocationsBuilderX86::VisitMul(HMul* mul) {
2089 LocationSummary* locations =
2090 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2091 switch (mul->GetResultType()) {
2092 case Primitive::kPrimInt:
2093 locations->SetInAt(0, Location::RequiresRegister());
2094 locations->SetInAt(1, Location::Any());
2095 locations->SetOut(Location::SameAsFirstInput());
2096 break;
2097 case Primitive::kPrimLong: {
2098 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002099 locations->SetInAt(1, Location::Any());
2100 locations->SetOut(Location::SameAsFirstInput());
2101 // Needed for imul on 32bits with 64bits output.
2102 locations->AddTemp(Location::RegisterLocation(EAX));
2103 locations->AddTemp(Location::RegisterLocation(EDX));
2104 break;
2105 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002106 case Primitive::kPrimFloat:
2107 case Primitive::kPrimDouble: {
2108 locations->SetInAt(0, Location::RequiresFpuRegister());
2109 locations->SetInAt(1, Location::RequiresFpuRegister());
2110 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002112 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002113
2114 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116 }
2117}
2118
2119void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2120 LocationSummary* locations = mul->GetLocations();
2121 Location first = locations->InAt(0);
2122 Location second = locations->InAt(1);
2123 DCHECK(first.Equals(locations->Out()));
2124
2125 switch (mul->GetResultType()) {
2126 case Primitive::kPrimInt: {
2127 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002129 } else if (second.IsConstant()) {
2130 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 } else {
2133 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 }
2136 break;
2137 }
2138
2139 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 Register in1_hi = first.AsRegisterPairHigh<Register>();
2141 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 Register eax = locations->GetTemp(0).AsRegister<Register>();
2143 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144
2145 DCHECK_EQ(EAX, eax);
2146 DCHECK_EQ(EDX, edx);
2147
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002148 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002149 // output: in1
2150 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2151 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2152 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002153 if (second.IsConstant()) {
2154 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002156 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2157 int32_t low_value = Low32Bits(value);
2158 int32_t high_value = High32Bits(value);
2159 Immediate low(low_value);
2160 Immediate high(high_value);
2161
2162 __ movl(eax, high);
2163 // eax <- in1.lo * in2.hi
2164 __ imull(eax, in1_lo);
2165 // in1.hi <- in1.hi * in2.lo
2166 __ imull(in1_hi, low);
2167 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2168 __ addl(in1_hi, eax);
2169 // move in2_lo to eax to prepare for double precision
2170 __ movl(eax, low);
2171 // edx:eax <- in1.lo * in2.lo
2172 __ mull(in1_lo);
2173 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2174 __ addl(in1_hi, edx);
2175 // in1.lo <- (in1.lo * in2.lo)[31:0];
2176 __ movl(in1_lo, eax);
2177 } else if (second.IsRegisterPair()) {
2178 Register in2_hi = second.AsRegisterPairHigh<Register>();
2179 Register in2_lo = second.AsRegisterPairLow<Register>();
2180
2181 __ movl(eax, in2_hi);
2182 // eax <- in1.lo * in2.hi
2183 __ imull(eax, in1_lo);
2184 // in1.hi <- in1.hi * in2.lo
2185 __ imull(in1_hi, in2_lo);
2186 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2187 __ addl(in1_hi, eax);
2188 // move in1_lo to eax to prepare for double precision
2189 __ movl(eax, in1_lo);
2190 // edx:eax <- in1.lo * in2.lo
2191 __ mull(in2_lo);
2192 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2193 __ addl(in1_hi, edx);
2194 // in1.lo <- (in1.lo * in2.lo)[31:0];
2195 __ movl(in1_lo, eax);
2196 } else {
2197 DCHECK(second.IsDoubleStackSlot()) << second;
2198 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2199 Address in2_lo(ESP, second.GetStackIndex());
2200
2201 __ movl(eax, in2_hi);
2202 // eax <- in1.lo * in2.hi
2203 __ imull(eax, in1_lo);
2204 // in1.hi <- in1.hi * in2.lo
2205 __ imull(in1_hi, in2_lo);
2206 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2207 __ addl(in1_hi, eax);
2208 // move in1_lo to eax to prepare for double precision
2209 __ movl(eax, in1_lo);
2210 // edx:eax <- in1.lo * in2.lo
2211 __ mull(in2_lo);
2212 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2213 __ addl(in1_hi, edx);
2214 // in1.lo <- (in1.lo * in2.lo)[31:0];
2215 __ movl(in1_lo, eax);
2216 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002217
2218 break;
2219 }
2220
Calin Juravleb5bfa962014-10-21 18:02:24 +01002221 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002223 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 }
2225
2226 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002227 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002228 break;
2229 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002230
2231 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002232 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233 }
2234}
2235
Roland Levillain232ade02015-04-20 15:14:36 +01002236void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2237 uint32_t temp_offset,
2238 uint32_t stack_adjustment,
2239 bool is_fp,
2240 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002241 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002242 DCHECK(!is_wide);
2243 if (is_fp) {
2244 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2245 } else {
2246 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2247 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002248 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002249 DCHECK(is_wide);
2250 if (is_fp) {
2251 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2252 } else {
2253 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2254 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002255 } else {
2256 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002257 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 Location stack_temp = Location::StackSlot(temp_offset);
2259 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002260 if (is_fp) {
2261 __ flds(Address(ESP, temp_offset));
2262 } else {
2263 __ filds(Address(ESP, temp_offset));
2264 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002265 } else {
2266 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2267 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002268 if (is_fp) {
2269 __ fldl(Address(ESP, temp_offset));
2270 } else {
2271 __ fildl(Address(ESP, temp_offset));
2272 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002273 }
2274 }
2275}
2276
2277void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2278 Primitive::Type type = rem->GetResultType();
2279 bool is_float = type == Primitive::kPrimFloat;
2280 size_t elem_size = Primitive::ComponentSize(type);
2281 LocationSummary* locations = rem->GetLocations();
2282 Location first = locations->InAt(0);
2283 Location second = locations->InAt(1);
2284 Location out = locations->Out();
2285
2286 // Create stack space for 2 elements.
2287 // TODO: enhance register allocator to ask for stack temporaries.
2288 __ subl(ESP, Immediate(2 * elem_size));
2289
2290 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002291 const bool is_wide = !is_float;
2292 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2293 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002294
2295 // Loop doing FPREM until we stabilize.
2296 Label retry;
2297 __ Bind(&retry);
2298 __ fprem();
2299
2300 // Move FP status to AX.
2301 __ fstsw();
2302
2303 // And see if the argument reduction is complete. This is signaled by the
2304 // C2 FPU flag bit set to 0.
2305 __ andl(EAX, Immediate(kC2ConditionMask));
2306 __ j(kNotEqual, &retry);
2307
2308 // We have settled on the final value. Retrieve it into an XMM register.
2309 // Store FP top of stack to real stack.
2310 if (is_float) {
2311 __ fsts(Address(ESP, 0));
2312 } else {
2313 __ fstl(Address(ESP, 0));
2314 }
2315
2316 // Pop the 2 items from the FP stack.
2317 __ fucompp();
2318
2319 // Load the value from the stack into an XMM register.
2320 DCHECK(out.IsFpuRegister()) << out;
2321 if (is_float) {
2322 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2323 } else {
2324 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2325 }
2326
2327 // And remove the temporary stack space we allocated.
2328 __ addl(ESP, Immediate(2 * elem_size));
2329}
2330
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002331
2332void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2333 DCHECK(instruction->IsDiv() || instruction->IsRem());
2334
2335 LocationSummary* locations = instruction->GetLocations();
2336 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002337 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002338
2339 Register out_register = locations->Out().AsRegister<Register>();
2340 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002341 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002342
2343 DCHECK(imm == 1 || imm == -1);
2344
2345 if (instruction->IsRem()) {
2346 __ xorl(out_register, out_register);
2347 } else {
2348 __ movl(out_register, input_register);
2349 if (imm == -1) {
2350 __ negl(out_register);
2351 }
2352 }
2353}
2354
2355
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002356void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002357 LocationSummary* locations = instruction->GetLocations();
2358
2359 Register out_register = locations->Out().AsRegister<Register>();
2360 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002361 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002362
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002363 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002364 Register num = locations->GetTemp(0).AsRegister<Register>();
2365
2366 __ leal(num, Address(input_register, std::abs(imm) - 1));
2367 __ testl(input_register, input_register);
2368 __ cmovl(kGreaterEqual, num, input_register);
2369 int shift = CTZ(imm);
2370 __ sarl(num, Immediate(shift));
2371
2372 if (imm < 0) {
2373 __ negl(num);
2374 }
2375
2376 __ movl(out_register, num);
2377}
2378
2379void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2380 DCHECK(instruction->IsDiv() || instruction->IsRem());
2381
2382 LocationSummary* locations = instruction->GetLocations();
2383 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2384
2385 Register eax = locations->InAt(0).AsRegister<Register>();
2386 Register out = locations->Out().AsRegister<Register>();
2387 Register num;
2388 Register edx;
2389
2390 if (instruction->IsDiv()) {
2391 edx = locations->GetTemp(0).AsRegister<Register>();
2392 num = locations->GetTemp(1).AsRegister<Register>();
2393 } else {
2394 edx = locations->Out().AsRegister<Register>();
2395 num = locations->GetTemp(0).AsRegister<Register>();
2396 }
2397
2398 DCHECK_EQ(EAX, eax);
2399 DCHECK_EQ(EDX, edx);
2400 if (instruction->IsDiv()) {
2401 DCHECK_EQ(EAX, out);
2402 } else {
2403 DCHECK_EQ(EDX, out);
2404 }
2405
2406 int64_t magic;
2407 int shift;
2408 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2409
2410 Label ndiv;
2411 Label end;
2412 // If numerator is 0, the result is 0, no computation needed.
2413 __ testl(eax, eax);
2414 __ j(kNotEqual, &ndiv);
2415
2416 __ xorl(out, out);
2417 __ jmp(&end);
2418
2419 __ Bind(&ndiv);
2420
2421 // Save the numerator.
2422 __ movl(num, eax);
2423
2424 // EAX = magic
2425 __ movl(eax, Immediate(magic));
2426
2427 // EDX:EAX = magic * numerator
2428 __ imull(num);
2429
2430 if (imm > 0 && magic < 0) {
2431 // EDX += num
2432 __ addl(edx, num);
2433 } else if (imm < 0 && magic > 0) {
2434 __ subl(edx, num);
2435 }
2436
2437 // Shift if needed.
2438 if (shift != 0) {
2439 __ sarl(edx, Immediate(shift));
2440 }
2441
2442 // EDX += 1 if EDX < 0
2443 __ movl(eax, edx);
2444 __ shrl(edx, Immediate(31));
2445 __ addl(edx, eax);
2446
2447 if (instruction->IsRem()) {
2448 __ movl(eax, num);
2449 __ imull(edx, Immediate(imm));
2450 __ subl(eax, edx);
2451 __ movl(edx, eax);
2452 } else {
2453 __ movl(eax, edx);
2454 }
2455 __ Bind(&end);
2456}
2457
Calin Juravlebacfec32014-11-14 15:54:36 +00002458void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2459 DCHECK(instruction->IsDiv() || instruction->IsRem());
2460
2461 LocationSummary* locations = instruction->GetLocations();
2462 Location out = locations->Out();
2463 Location first = locations->InAt(0);
2464 Location second = locations->InAt(1);
2465 bool is_div = instruction->IsDiv();
2466
2467 switch (instruction->GetResultType()) {
2468 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 DCHECK_EQ(EAX, first.AsRegister<Register>());
2470 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002471
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002472 if (instruction->InputAt(1)->IsIntConstant()) {
2473 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002474
2475 if (imm == 0) {
2476 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2477 } else if (imm == 1 || imm == -1) {
2478 DivRemOneOrMinusOne(instruction);
2479 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002480 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002481 } else {
2482 DCHECK(imm <= -2 || imm >= 2);
2483 GenerateDivRemWithAnyConstant(instruction);
2484 }
2485 } else {
2486 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002487 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002488 is_div);
2489 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002490
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002491 Register second_reg = second.AsRegister<Register>();
2492 // 0x80000000/-1 triggers an arithmetic exception!
2493 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2494 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002495
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002496 __ cmpl(second_reg, Immediate(-1));
2497 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002498
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002499 // edx:eax <- sign-extended of eax
2500 __ cdq();
2501 // eax = quotient, edx = remainder
2502 __ idivl(second_reg);
2503 __ Bind(slow_path->GetExitLabel());
2504 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002505 break;
2506 }
2507
2508 case Primitive::kPrimLong: {
2509 InvokeRuntimeCallingConvention calling_convention;
2510 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2511 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2512 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2513 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2514 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2515 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2516
2517 if (is_div) {
2518 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2519 } else {
2520 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2521 }
2522 uint32_t dex_pc = is_div
2523 ? instruction->AsDiv()->GetDexPc()
2524 : instruction->AsRem()->GetDexPc();
2525 codegen_->RecordPcInfo(instruction, dex_pc);
2526
2527 break;
2528 }
2529
2530 default:
2531 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2532 }
2533}
2534
Calin Juravle7c4954d2014-10-28 16:57:40 +00002535void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002536 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002537 ? LocationSummary::kCall
2538 : LocationSummary::kNoCall;
2539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2540
Calin Juravle7c4954d2014-10-28 16:57:40 +00002541 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002542 case Primitive::kPrimInt: {
2543 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002544 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002545 locations->SetOut(Location::SameAsFirstInput());
2546 // Intel uses edx:eax as the dividend.
2547 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002548 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2549 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2550 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002551 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002552 locations->AddTemp(Location::RequiresRegister());
2553 }
Calin Juravled0d48522014-11-04 16:40:20 +00002554 break;
2555 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002556 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002557 InvokeRuntimeCallingConvention calling_convention;
2558 locations->SetInAt(0, Location::RegisterPairLocation(
2559 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2560 locations->SetInAt(1, Location::RegisterPairLocation(
2561 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2562 // Runtime helper puts the result in EAX, EDX.
2563 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002564 break;
2565 }
2566 case Primitive::kPrimFloat:
2567 case Primitive::kPrimDouble: {
2568 locations->SetInAt(0, Location::RequiresFpuRegister());
2569 locations->SetInAt(1, Location::RequiresFpuRegister());
2570 locations->SetOut(Location::SameAsFirstInput());
2571 break;
2572 }
2573
2574 default:
2575 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2576 }
2577}
2578
2579void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2580 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002581 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002582 Location first = locations->InAt(0);
2583 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584
2585 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002586 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002588 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589 break;
2590 }
2591
2592 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002593 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002595 break;
2596 }
2597
2598 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002599 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002600 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002601 break;
2602 }
2603
2604 default:
2605 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2606 }
2607}
2608
Calin Juravlebacfec32014-11-14 15:54:36 +00002609void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002610 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002611
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002612 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2613 ? LocationSummary::kCall
2614 : LocationSummary::kNoCall;
2615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002616
Calin Juravled2ec87d2014-12-08 14:24:46 +00002617 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002618 case Primitive::kPrimInt: {
2619 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002620 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002622 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2623 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2624 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002625 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002626 locations->AddTemp(Location::RequiresRegister());
2627 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002628 break;
2629 }
2630 case Primitive::kPrimLong: {
2631 InvokeRuntimeCallingConvention calling_convention;
2632 locations->SetInAt(0, Location::RegisterPairLocation(
2633 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2634 locations->SetInAt(1, Location::RegisterPairLocation(
2635 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2636 // Runtime helper puts the result in EAX, EDX.
2637 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2638 break;
2639 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002640 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002641 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002642 locations->SetInAt(0, Location::Any());
2643 locations->SetInAt(1, Location::Any());
2644 locations->SetOut(Location::RequiresFpuRegister());
2645 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002646 break;
2647 }
2648
2649 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002650 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002651 }
2652}
2653
2654void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2655 Primitive::Type type = rem->GetResultType();
2656 switch (type) {
2657 case Primitive::kPrimInt:
2658 case Primitive::kPrimLong: {
2659 GenerateDivRemIntegral(rem);
2660 break;
2661 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002662 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002663 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002664 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002665 break;
2666 }
2667 default:
2668 LOG(FATAL) << "Unexpected rem type " << type;
2669 }
2670}
2671
Calin Juravled0d48522014-11-04 16:40:20 +00002672void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2673 LocationSummary* locations =
2674 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002675 switch (instruction->GetType()) {
2676 case Primitive::kPrimInt: {
2677 locations->SetInAt(0, Location::Any());
2678 break;
2679 }
2680 case Primitive::kPrimLong: {
2681 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2682 if (!instruction->IsConstant()) {
2683 locations->AddTemp(Location::RequiresRegister());
2684 }
2685 break;
2686 }
2687 default:
2688 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2689 }
Calin Juravled0d48522014-11-04 16:40:20 +00002690 if (instruction->HasUses()) {
2691 locations->SetOut(Location::SameAsFirstInput());
2692 }
2693}
2694
2695void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2696 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2697 codegen_->AddSlowPath(slow_path);
2698
2699 LocationSummary* locations = instruction->GetLocations();
2700 Location value = locations->InAt(0);
2701
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002702 switch (instruction->GetType()) {
2703 case Primitive::kPrimInt: {
2704 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002706 __ j(kEqual, slow_path->GetEntryLabel());
2707 } else if (value.IsStackSlot()) {
2708 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2709 __ j(kEqual, slow_path->GetEntryLabel());
2710 } else {
2711 DCHECK(value.IsConstant()) << value;
2712 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2713 __ jmp(slow_path->GetEntryLabel());
2714 }
2715 }
2716 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002717 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002718 case Primitive::kPrimLong: {
2719 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002721 __ movl(temp, value.AsRegisterPairLow<Register>());
2722 __ orl(temp, value.AsRegisterPairHigh<Register>());
2723 __ j(kEqual, slow_path->GetEntryLabel());
2724 } else {
2725 DCHECK(value.IsConstant()) << value;
2726 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2727 __ jmp(slow_path->GetEntryLabel());
2728 }
2729 }
2730 break;
2731 }
2732 default:
2733 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002734 }
Calin Juravled0d48522014-11-04 16:40:20 +00002735}
2736
Calin Juravle9aec02f2014-11-18 23:06:35 +00002737void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2738 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2739
2740 LocationSummary* locations =
2741 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2742
2743 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002744 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002745 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002746 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002747 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002748 // The shift count needs to be in CL or a constant.
2749 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002750 locations->SetOut(Location::SameAsFirstInput());
2751 break;
2752 }
2753 default:
2754 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2755 }
2756}
2757
2758void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2759 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2760
2761 LocationSummary* locations = op->GetLocations();
2762 Location first = locations->InAt(0);
2763 Location second = locations->InAt(1);
2764 DCHECK(first.Equals(locations->Out()));
2765
2766 switch (op->GetResultType()) {
2767 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002768 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002769 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002770 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 DCHECK_EQ(ECX, second_reg);
2773 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002774 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002775 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002778 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 }
2780 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002781 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2782 if (shift == 0) {
2783 return;
2784 }
2785 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002786 if (op->IsShl()) {
2787 __ shll(first_reg, imm);
2788 } else if (op->IsShr()) {
2789 __ sarl(first_reg, imm);
2790 } else {
2791 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002792 }
2793 }
2794 break;
2795 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002796 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002797 if (second.IsRegister()) {
2798 Register second_reg = second.AsRegister<Register>();
2799 DCHECK_EQ(ECX, second_reg);
2800 if (op->IsShl()) {
2801 GenerateShlLong(first, second_reg);
2802 } else if (op->IsShr()) {
2803 GenerateShrLong(first, second_reg);
2804 } else {
2805 GenerateUShrLong(first, second_reg);
2806 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002807 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002808 // Shift by a constant.
2809 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2810 // Nothing to do if the shift is 0, as the input is already the output.
2811 if (shift != 0) {
2812 if (op->IsShl()) {
2813 GenerateShlLong(first, shift);
2814 } else if (op->IsShr()) {
2815 GenerateShrLong(first, shift);
2816 } else {
2817 GenerateUShrLong(first, shift);
2818 }
2819 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002820 }
2821 break;
2822 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002823 default:
2824 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2825 }
2826}
2827
Mark P Mendell73945692015-04-29 14:56:17 +00002828void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2829 Register low = loc.AsRegisterPairLow<Register>();
2830 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002831 if (shift == 1) {
2832 // This is just an addition.
2833 __ addl(low, low);
2834 __ adcl(high, high);
2835 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002836 // Shift by 32 is easy. High gets low, and low gets 0.
2837 codegen_->EmitParallelMoves(
2838 loc.ToLow(),
2839 loc.ToHigh(),
2840 Primitive::kPrimInt,
2841 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2842 loc.ToLow(),
2843 Primitive::kPrimInt);
2844 } else if (shift > 32) {
2845 // Low part becomes 0. High part is low part << (shift-32).
2846 __ movl(high, low);
2847 __ shll(high, Immediate(shift - 32));
2848 __ xorl(low, low);
2849 } else {
2850 // Between 1 and 31.
2851 __ shld(high, low, Immediate(shift));
2852 __ shll(low, Immediate(shift));
2853 }
2854}
2855
Calin Juravle9aec02f2014-11-18 23:06:35 +00002856void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2857 Label done;
2858 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2859 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2860 __ testl(shifter, Immediate(32));
2861 __ j(kEqual, &done);
2862 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2863 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2864 __ Bind(&done);
2865}
2866
Mark P Mendell73945692015-04-29 14:56:17 +00002867void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2868 Register low = loc.AsRegisterPairLow<Register>();
2869 Register high = loc.AsRegisterPairHigh<Register>();
2870 if (shift == 32) {
2871 // Need to copy the sign.
2872 DCHECK_NE(low, high);
2873 __ movl(low, high);
2874 __ sarl(high, Immediate(31));
2875 } else if (shift > 32) {
2876 DCHECK_NE(low, high);
2877 // High part becomes sign. Low part is shifted by shift - 32.
2878 __ movl(low, high);
2879 __ sarl(high, Immediate(31));
2880 __ sarl(low, Immediate(shift - 32));
2881 } else {
2882 // Between 1 and 31.
2883 __ shrd(low, high, Immediate(shift));
2884 __ sarl(high, Immediate(shift));
2885 }
2886}
2887
Calin Juravle9aec02f2014-11-18 23:06:35 +00002888void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2889 Label done;
2890 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2891 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2892 __ testl(shifter, Immediate(32));
2893 __ j(kEqual, &done);
2894 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2895 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2896 __ Bind(&done);
2897}
2898
Mark P Mendell73945692015-04-29 14:56:17 +00002899void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2900 Register low = loc.AsRegisterPairLow<Register>();
2901 Register high = loc.AsRegisterPairHigh<Register>();
2902 if (shift == 32) {
2903 // Shift by 32 is easy. Low gets high, and high gets 0.
2904 codegen_->EmitParallelMoves(
2905 loc.ToHigh(),
2906 loc.ToLow(),
2907 Primitive::kPrimInt,
2908 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2909 loc.ToHigh(),
2910 Primitive::kPrimInt);
2911 } else if (shift > 32) {
2912 // Low part is high >> (shift - 32). High part becomes 0.
2913 __ movl(low, high);
2914 __ shrl(low, Immediate(shift - 32));
2915 __ xorl(high, high);
2916 } else {
2917 // Between 1 and 31.
2918 __ shrd(low, high, Immediate(shift));
2919 __ shrl(high, Immediate(shift));
2920 }
2921}
2922
Calin Juravle9aec02f2014-11-18 23:06:35 +00002923void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2924 Label done;
2925 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2926 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2927 __ testl(shifter, Immediate(32));
2928 __ j(kEqual, &done);
2929 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2930 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2931 __ Bind(&done);
2932}
2933
2934void LocationsBuilderX86::VisitShl(HShl* shl) {
2935 HandleShift(shl);
2936}
2937
2938void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2939 HandleShift(shl);
2940}
2941
2942void LocationsBuilderX86::VisitShr(HShr* shr) {
2943 HandleShift(shr);
2944}
2945
2946void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2947 HandleShift(shr);
2948}
2949
2950void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2951 HandleShift(ushr);
2952}
2953
2954void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2955 HandleShift(ushr);
2956}
2957
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002958void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002961 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002962 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002963 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2964 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002965}
2966
2967void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2968 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002969 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002971
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002972 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002973
Nicolas Geoffray39468442014-09-02 15:17:15 +01002974 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002975 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002976}
2977
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002978void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2981 locations->SetOut(Location::RegisterLocation(EAX));
2982 InvokeRuntimeCallingConvention calling_convention;
2983 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002984 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002986}
2987
2988void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2989 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002990 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002991 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2992
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002993 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002994
2995 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2996 DCHECK(!codegen_->IsLeafMethod());
2997}
2998
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002999void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003000 LocationSummary* locations =
3001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003002 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3003 if (location.IsStackSlot()) {
3004 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3005 } else if (location.IsDoubleStackSlot()) {
3006 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003007 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003008 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003009}
3010
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003011void InstructionCodeGeneratorX86::VisitParameterValue(
3012 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3013}
3014
3015void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3016 LocationSummary* locations =
3017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3018 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3019}
3020
3021void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003022}
3023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003024void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003025 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003026 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003029}
3030
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003031void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3032 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003033 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003034 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003035 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003036 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003037 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003039 break;
3040
3041 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003042 __ notl(out.AsRegisterPairLow<Register>());
3043 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003044 break;
3045
3046 default:
3047 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3048 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003049}
3050
David Brazdil66d126e2015-04-03 16:02:44 +01003051void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3052 LocationSummary* locations =
3053 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3054 locations->SetInAt(0, Location::RequiresRegister());
3055 locations->SetOut(Location::SameAsFirstInput());
3056}
3057
3058void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003059 LocationSummary* locations = bool_not->GetLocations();
3060 Location in = locations->InAt(0);
3061 Location out = locations->Out();
3062 DCHECK(in.Equals(out));
3063 __ xorl(out.AsRegister<Register>(), Immediate(1));
3064}
3065
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003066void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003069 switch (compare->InputAt(0)->GetType()) {
3070 case Primitive::kPrimLong: {
3071 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003072 locations->SetInAt(1, Location::Any());
3073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3074 break;
3075 }
3076 case Primitive::kPrimFloat:
3077 case Primitive::kPrimDouble: {
3078 locations->SetInAt(0, Location::RequiresFpuRegister());
3079 locations->SetInAt(1, Location::RequiresFpuRegister());
3080 locations->SetOut(Location::RequiresRegister());
3081 break;
3082 }
3083 default:
3084 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3085 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003086}
3087
3088void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003089 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003091 Location left = locations->InAt(0);
3092 Location right = locations->InAt(1);
3093
3094 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003095 switch (compare->InputAt(0)->GetType()) {
3096 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003097 Register left_low = left.AsRegisterPairLow<Register>();
3098 Register left_high = left.AsRegisterPairHigh<Register>();
3099 int32_t val_low = 0;
3100 int32_t val_high = 0;
3101 bool right_is_const = false;
3102
3103 if (right.IsConstant()) {
3104 DCHECK(right.GetConstant()->IsLongConstant());
3105 right_is_const = true;
3106 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3107 val_low = Low32Bits(val);
3108 val_high = High32Bits(val);
3109 }
3110
Calin Juravleddb7df22014-11-25 20:56:51 +00003111 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003112 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003113 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003114 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003115 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003116 DCHECK(right_is_const) << right;
3117 if (val_high == 0) {
3118 __ testl(left_high, left_high);
3119 } else {
3120 __ cmpl(left_high, Immediate(val_high));
3121 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003122 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003123 __ j(kLess, &less); // Signed compare.
3124 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003125 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003126 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003127 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003128 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003129 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003130 DCHECK(right_is_const) << right;
3131 if (val_low == 0) {
3132 __ testl(left_low, left_low);
3133 } else {
3134 __ cmpl(left_low, Immediate(val_low));
3135 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003136 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003137 break;
3138 }
3139 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003141 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3142 break;
3143 }
3144 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003145 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003146 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003147 break;
3148 }
3149 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003150 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003151 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003152 __ movl(out, Immediate(0));
3153 __ j(kEqual, &done);
3154 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3155
3156 __ Bind(&greater);
3157 __ movl(out, Immediate(1));
3158 __ jmp(&done);
3159
3160 __ Bind(&less);
3161 __ movl(out, Immediate(-1));
3162
3163 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003164}
3165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003166void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003167 LocationSummary* locations =
3168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003169 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3170 locations->SetInAt(i, Location::Any());
3171 }
3172 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003173}
3174
3175void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003176 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003177 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003178}
3179
Calin Juravle52c48962014-12-16 17:02:57 +00003180void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3181 /*
3182 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3183 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3184 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3185 */
3186 switch (kind) {
3187 case MemBarrierKind::kAnyAny: {
3188 __ mfence();
3189 break;
3190 }
3191 case MemBarrierKind::kAnyStore:
3192 case MemBarrierKind::kLoadAny:
3193 case MemBarrierKind::kStoreStore: {
3194 // nop
3195 break;
3196 }
3197 default:
3198 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003199 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003200}
3201
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003202
Mark Mendell09ed1a32015-03-25 08:30:06 -04003203void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003204 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003205 // TODO: Implement all kinds of calls:
3206 // 1) boot -> boot
3207 // 2) app -> boot
3208 // 3) app -> app
3209 //
3210 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003211
3212 if (invoke->IsStringInit()) {
3213 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003214 Register reg = temp.AsRegister<Register>();
3215 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003216 // (temp + offset_of_quick_compiled_code)()
3217 __ call(Address(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003218 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3219 } else if (invoke->IsRecursive()) {
3220 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003221 } else {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003222 Register current_method =
3223 invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex()).AsRegister<Register>();
3224 Register reg = temp.AsRegister<Register>();
3225 // temp = temp->dex_cache_resolved_methods_;
3226 __ movl(reg, Address(
3227 current_method, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3228 // temp = temp[index_in_cache]
3229 __ movl(reg, Address(reg,
3230 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3231 // (temp + offset_of_quick_compiled_code)()
3232 __ call(Address(reg,
3233 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003234 }
3235
3236 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003237}
3238
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003239void CodeGeneratorX86::MarkGCCard(Register temp,
3240 Register card,
3241 Register object,
3242 Register value,
3243 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003244 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003245 if (value_can_be_null) {
3246 __ testl(value, value);
3247 __ j(kEqual, &is_null);
3248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003249 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3250 __ movl(temp, object);
3251 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003252 __ movb(Address(temp, card, TIMES_1, 0),
3253 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003254 if (value_can_be_null) {
3255 __ Bind(&is_null);
3256 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003257}
3258
Calin Juravle52c48962014-12-16 17:02:57 +00003259void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3260 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003261 LocationSummary* locations =
3262 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003263 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003264
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003265 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3266 locations->SetOut(Location::RequiresFpuRegister());
3267 } else {
3268 // The output overlaps in case of long: we don't want the low move to overwrite
3269 // the object's location.
3270 locations->SetOut(Location::RequiresRegister(),
3271 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3272 : Location::kNoOutputOverlap);
3273 }
Calin Juravle52c48962014-12-16 17:02:57 +00003274
3275 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3276 // Long values can be loaded atomically into an XMM using movsd.
3277 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3278 // and then copy the XMM into the output 32bits at a time).
3279 locations->AddTemp(Location::RequiresFpuRegister());
3280 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003281}
3282
Calin Juravle52c48962014-12-16 17:02:57 +00003283void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3284 const FieldInfo& field_info) {
3285 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003286
Calin Juravle52c48962014-12-16 17:02:57 +00003287 LocationSummary* locations = instruction->GetLocations();
3288 Register base = locations->InAt(0).AsRegister<Register>();
3289 Location out = locations->Out();
3290 bool is_volatile = field_info.IsVolatile();
3291 Primitive::Type field_type = field_info.GetFieldType();
3292 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3293
3294 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003295 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003296 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003297 break;
3298 }
3299
3300 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003301 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003302 break;
3303 }
3304
3305 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003306 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003307 break;
3308 }
3309
3310 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003311 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003312 break;
3313 }
3314
3315 case Primitive::kPrimInt:
3316 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003317 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003318 break;
3319 }
3320
3321 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003322 if (is_volatile) {
3323 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3324 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003326 __ movd(out.AsRegisterPairLow<Register>(), temp);
3327 __ psrlq(temp, Immediate(32));
3328 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3329 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003330 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003331 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003332 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003333 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3334 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003335 break;
3336 }
3337
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003338 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003339 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003340 break;
3341 }
3342
3343 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003344 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003345 break;
3346 }
3347
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003348 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003349 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003350 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003351 }
Calin Juravle52c48962014-12-16 17:02:57 +00003352
Calin Juravle77520bc2015-01-12 18:45:46 +00003353 // Longs are handled in the switch.
3354 if (field_type != Primitive::kPrimLong) {
3355 codegen_->MaybeRecordImplicitNullCheck(instruction);
3356 }
3357
Calin Juravle52c48962014-12-16 17:02:57 +00003358 if (is_volatile) {
3359 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3360 }
3361}
3362
3363void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3364 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3365
3366 LocationSummary* locations =
3367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3368 locations->SetInAt(0, Location::RequiresRegister());
3369 bool is_volatile = field_info.IsVolatile();
3370 Primitive::Type field_type = field_info.GetFieldType();
3371 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3372 || (field_type == Primitive::kPrimByte);
3373
3374 // The register allocator does not support multiple
3375 // inputs that die at entry with one in a specific register.
3376 if (is_byte_type) {
3377 // Ensure the value is in a byte register.
3378 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003379 } else if (Primitive::IsFloatingPointType(field_type)) {
3380 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003381 } else {
3382 locations->SetInAt(1, Location::RequiresRegister());
3383 }
3384 // Temporary registers for the write barrier.
3385 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3386 locations->AddTemp(Location::RequiresRegister());
3387 // Ensure the card is in a byte register.
3388 locations->AddTemp(Location::RegisterLocation(ECX));
3389 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3390 // 64bits value can be atomically written to an address with movsd and an XMM register.
3391 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3392 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3393 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3394 // isolated cases when we need this it isn't worth adding the extra complexity.
3395 locations->AddTemp(Location::RequiresFpuRegister());
3396 locations->AddTemp(Location::RequiresFpuRegister());
3397 }
3398}
3399
3400void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003401 const FieldInfo& field_info,
3402 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003403 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3404
3405 LocationSummary* locations = instruction->GetLocations();
3406 Register base = locations->InAt(0).AsRegister<Register>();
3407 Location value = locations->InAt(1);
3408 bool is_volatile = field_info.IsVolatile();
3409 Primitive::Type field_type = field_info.GetFieldType();
3410 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3411
3412 if (is_volatile) {
3413 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3414 }
3415
3416 switch (field_type) {
3417 case Primitive::kPrimBoolean:
3418 case Primitive::kPrimByte: {
3419 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3420 break;
3421 }
3422
3423 case Primitive::kPrimShort:
3424 case Primitive::kPrimChar: {
3425 __ movw(Address(base, offset), value.AsRegister<Register>());
3426 break;
3427 }
3428
3429 case Primitive::kPrimInt:
3430 case Primitive::kPrimNot: {
3431 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003432 break;
3433 }
3434
3435 case Primitive::kPrimLong: {
3436 if (is_volatile) {
3437 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3438 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3439 __ movd(temp1, value.AsRegisterPairLow<Register>());
3440 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3441 __ punpckldq(temp1, temp2);
3442 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003443 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003444 } else {
3445 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003447 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3448 }
3449 break;
3450 }
3451
3452 case Primitive::kPrimFloat: {
3453 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3454 break;
3455 }
3456
3457 case Primitive::kPrimDouble: {
3458 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3459 break;
3460 }
3461
3462 case Primitive::kPrimVoid:
3463 LOG(FATAL) << "Unreachable type " << field_type;
3464 UNREACHABLE();
3465 }
3466
Calin Juravle77520bc2015-01-12 18:45:46 +00003467 // Longs are handled in the switch.
3468 if (field_type != Primitive::kPrimLong) {
3469 codegen_->MaybeRecordImplicitNullCheck(instruction);
3470 }
3471
3472 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3473 Register temp = locations->GetTemp(0).AsRegister<Register>();
3474 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003475 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003476 }
3477
Calin Juravle52c48962014-12-16 17:02:57 +00003478 if (is_volatile) {
3479 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3480 }
3481}
3482
3483void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3484 HandleFieldGet(instruction, instruction->GetFieldInfo());
3485}
3486
3487void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3488 HandleFieldGet(instruction, instruction->GetFieldInfo());
3489}
3490
3491void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3492 HandleFieldSet(instruction, instruction->GetFieldInfo());
3493}
3494
3495void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003496 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003497}
3498
3499void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3500 HandleFieldSet(instruction, instruction->GetFieldInfo());
3501}
3502
3503void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003504 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003505}
3506
3507void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3508 HandleFieldGet(instruction, instruction->GetFieldInfo());
3509}
3510
3511void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3512 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003513}
3514
3515void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003516 LocationSummary* locations =
3517 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003518 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3519 ? Location::RequiresRegister()
3520 : Location::Any();
3521 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003522 if (instruction->HasUses()) {
3523 locations->SetOut(Location::SameAsFirstInput());
3524 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003525}
3526
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003527void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003528 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3529 return;
3530 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003531 LocationSummary* locations = instruction->GetLocations();
3532 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003533
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003534 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3535 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3536}
3537
3538void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003539 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540 codegen_->AddSlowPath(slow_path);
3541
3542 LocationSummary* locations = instruction->GetLocations();
3543 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544
3545 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003546 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003547 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003548 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003549 } else {
3550 DCHECK(obj.IsConstant()) << obj;
3551 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3552 __ jmp(slow_path->GetEntryLabel());
3553 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003554 }
3555 __ j(kEqual, slow_path->GetEntryLabel());
3556}
3557
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003558void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3559 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3560 GenerateImplicitNullCheck(instruction);
3561 } else {
3562 GenerateExplicitNullCheck(instruction);
3563 }
3564}
3565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003566void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003567 LocationSummary* locations =
3568 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003569 locations->SetInAt(0, Location::RequiresRegister());
3570 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003571 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3572 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3573 } else {
3574 // The output overlaps in case of long: we don't want the low move to overwrite
3575 // the array's location.
3576 locations->SetOut(Location::RequiresRegister(),
3577 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3578 : Location::kNoOutputOverlap);
3579 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003580}
3581
3582void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3583 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585 Location index = locations->InAt(1);
3586
Calin Juravle77520bc2015-01-12 18:45:46 +00003587 Primitive::Type type = instruction->GetType();
3588 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 case Primitive::kPrimBoolean: {
3590 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 if (index.IsConstant()) {
3593 __ movzxb(out, Address(obj,
3594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3595 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003597 }
3598 break;
3599 }
3600
3601 case Primitive::kPrimByte: {
3602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 if (index.IsConstant()) {
3605 __ movsxb(out, Address(obj,
3606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3607 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003609 }
3610 break;
3611 }
3612
3613 case Primitive::kPrimShort: {
3614 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 if (index.IsConstant()) {
3617 __ movsxw(out, Address(obj,
3618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3619 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 }
3622 break;
3623 }
3624
3625 case Primitive::kPrimChar: {
3626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 if (index.IsConstant()) {
3629 __ movzxw(out, Address(obj,
3630 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3631 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633 }
3634 break;
3635 }
3636
3637 case Primitive::kPrimInt:
3638 case Primitive::kPrimNot: {
3639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003641 if (index.IsConstant()) {
3642 __ movl(out, Address(obj,
3643 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3644 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003646 }
3647 break;
3648 }
3649
3650 case Primitive::kPrimLong: {
3651 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003652 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003653 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003654 if (index.IsConstant()) {
3655 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003656 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003658 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003659 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003660 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003661 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003663 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003664 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003665 }
3666 break;
3667 }
3668
Mark Mendell7c8d0092015-01-26 11:21:33 -05003669 case Primitive::kPrimFloat: {
3670 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3671 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3672 if (index.IsConstant()) {
3673 __ movss(out, Address(obj,
3674 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3675 } else {
3676 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3677 }
3678 break;
3679 }
3680
3681 case Primitive::kPrimDouble: {
3682 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3683 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3684 if (index.IsConstant()) {
3685 __ movsd(out, Address(obj,
3686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3687 } else {
3688 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3689 }
3690 break;
3691 }
3692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003693 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003694 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003695 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003696 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003697
3698 if (type != Primitive::kPrimLong) {
3699 codegen_->MaybeRecordImplicitNullCheck(instruction);
3700 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003701}
3702
3703void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003704 // This location builder might end up asking to up to four registers, which is
3705 // not currently possible for baseline. The situation in which we need four
3706 // registers cannot be met by baseline though, because it has not run any
3707 // optimization.
3708
Nicolas Geoffray39468442014-09-02 15:17:15 +01003709 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003710 bool needs_write_barrier =
3711 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3712
Mark Mendell5f874182015-03-04 15:42:45 -05003713 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003714
Nicolas Geoffray39468442014-09-02 15:17:15 +01003715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3716 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003717 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003718
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003719 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003720 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003721 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3722 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3723 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003724 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003725 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3726 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003727 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003728 // In case of a byte operation, the register allocator does not support multiple
3729 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003730 locations->SetInAt(0, Location::RequiresRegister());
3731 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003732 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003733 // Ensure the value is in a byte register.
3734 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003735 } else if (Primitive::IsFloatingPointType(value_type)) {
3736 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003737 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003738 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003740 // Temporary registers for the write barrier.
3741 if (needs_write_barrier) {
3742 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003743 // Ensure the card is in a byte register.
3744 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747}
3748
3749void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3750 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003753 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003754 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003755 bool needs_runtime_call = locations->WillCall();
3756 bool needs_write_barrier =
3757 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758
3759 switch (value_type) {
3760 case Primitive::kPrimBoolean:
3761 case Primitive::kPrimByte: {
3762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 if (index.IsConstant()) {
3764 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003765 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003766 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003767 } else {
3768 __ movb(Address(obj, offset),
3769 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003772 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003773 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003774 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003775 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003777 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3778 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003779 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003780 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003781 break;
3782 }
3783
3784 case Primitive::kPrimShort:
3785 case Primitive::kPrimChar: {
3786 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 if (index.IsConstant()) {
3788 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003789 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003791 } else {
3792 __ movw(Address(obj, offset),
3793 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3794 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003795 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003796 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003797 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3798 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003799 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003800 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003801 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003803 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 break;
3806 }
3807
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003808 case Primitive::kPrimInt:
3809 case Primitive::kPrimNot: {
3810 if (!needs_runtime_call) {
3811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3812 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003813 size_t offset =
3814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003815 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003816 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003817 } else {
3818 DCHECK(value.IsConstant()) << value;
3819 __ movl(Address(obj, offset),
3820 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3821 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003822 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003823 DCHECK(index.IsRegister()) << index;
3824 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003825 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3826 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003827 } else {
3828 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003830 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3831 }
3832 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003834
3835 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003836 Register temp = locations->GetTemp(0).AsRegister<Register>();
3837 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003838 codegen_->MarkGCCard(
3839 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003840 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003841 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003842 DCHECK_EQ(value_type, Primitive::kPrimNot);
3843 DCHECK(!codegen_->IsLeafMethod());
3844 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3845 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003846 }
3847 break;
3848 }
3849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003850 case Primitive::kPrimLong: {
3851 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003852 if (index.IsConstant()) {
3853 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003854 if (value.IsRegisterPair()) {
3855 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003856 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003857 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003858 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003859 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003860 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3861 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003862 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003863 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3864 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003866 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003868 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003871 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003872 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003873 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003874 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003875 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003876 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003877 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003879 Immediate(High32Bits(val)));
3880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003881 }
3882 break;
3883 }
3884
Mark Mendell7c8d0092015-01-26 11:21:33 -05003885 case Primitive::kPrimFloat: {
3886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3887 DCHECK(value.IsFpuRegister());
3888 if (index.IsConstant()) {
3889 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3890 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3891 } else {
3892 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3893 value.AsFpuRegister<XmmRegister>());
3894 }
3895 break;
3896 }
3897
3898 case Primitive::kPrimDouble: {
3899 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3900 DCHECK(value.IsFpuRegister());
3901 if (index.IsConstant()) {
3902 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3903 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3904 } else {
3905 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3906 value.AsFpuRegister<XmmRegister>());
3907 }
3908 break;
3909 }
3910
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 case Primitive::kPrimVoid:
3912 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003913 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 }
3915}
3916
3917void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003919 locations->SetInAt(0, Location::RequiresRegister());
3920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921}
3922
3923void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3924 LocationSummary* locations = instruction->GetLocations();
3925 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 Register obj = locations->InAt(0).AsRegister<Register>();
3927 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003929 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930}
3931
3932void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003933 LocationSummary* locations =
3934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003935 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003936 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003937 if (instruction->HasUses()) {
3938 locations->SetOut(Location::SameAsFirstInput());
3939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003940}
3941
3942void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3943 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003944 Location index_loc = locations->InAt(0);
3945 Location length_loc = locations->InAt(1);
3946 SlowPathCodeX86* slow_path =
3947 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003948
Mark Mendell99dbd682015-04-22 16:18:52 -04003949 if (length_loc.IsConstant()) {
3950 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3951 if (index_loc.IsConstant()) {
3952 // BCE will remove the bounds check if we are guarenteed to pass.
3953 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3954 if (index < 0 || index >= length) {
3955 codegen_->AddSlowPath(slow_path);
3956 __ jmp(slow_path->GetEntryLabel());
3957 } else {
3958 // Some optimization after BCE may have generated this, and we should not
3959 // generate a bounds check if it is a valid range.
3960 }
3961 return;
3962 }
3963
3964 // We have to reverse the jump condition because the length is the constant.
3965 Register index_reg = index_loc.AsRegister<Register>();
3966 __ cmpl(index_reg, Immediate(length));
3967 codegen_->AddSlowPath(slow_path);
3968 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003969 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003970 Register length = length_loc.AsRegister<Register>();
3971 if (index_loc.IsConstant()) {
3972 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3973 __ cmpl(length, Immediate(value));
3974 } else {
3975 __ cmpl(length, index_loc.AsRegister<Register>());
3976 }
3977 codegen_->AddSlowPath(slow_path);
3978 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003979 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980}
3981
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003982void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3983 temp->SetLocations(nullptr);
3984}
3985
3986void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3987 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003988 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989}
3990
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003991void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003992 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003993 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003994}
3995
3996void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003997 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3998}
3999
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004000void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4002}
4003
4004void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004005 HBasicBlock* block = instruction->GetBlock();
4006 if (block->GetLoopInformation() != nullptr) {
4007 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4008 // The back edge will generate the suspend check.
4009 return;
4010 }
4011 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4012 // The goto will generate the suspend check.
4013 return;
4014 }
4015 GenerateSuspendCheck(instruction, nullptr);
4016}
4017
4018void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4019 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004020 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004021 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4022 if (slow_path == nullptr) {
4023 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4024 instruction->SetSlowPath(slow_path);
4025 codegen_->AddSlowPath(slow_path);
4026 if (successor != nullptr) {
4027 DCHECK(successor->IsLoopHeader());
4028 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4029 }
4030 } else {
4031 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4032 }
4033
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004034 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004035 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004036 if (successor == nullptr) {
4037 __ j(kNotEqual, slow_path->GetEntryLabel());
4038 __ Bind(slow_path->GetReturnLabel());
4039 } else {
4040 __ j(kEqual, codegen_->GetLabelOf(successor));
4041 __ jmp(slow_path->GetEntryLabel());
4042 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004043}
4044
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004045X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4046 return codegen_->GetAssembler();
4047}
4048
Mark Mendell7c8d0092015-01-26 11:21:33 -05004049void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004050 ScratchRegisterScope ensure_scratch(
4051 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4052 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4053 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4054 __ movl(temp_reg, Address(ESP, src + stack_offset));
4055 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004056}
4057
4058void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004059 ScratchRegisterScope ensure_scratch(
4060 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4061 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4062 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4063 __ movl(temp_reg, Address(ESP, src + stack_offset));
4064 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4065 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4066 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004067}
4068
4069void ParallelMoveResolverX86::EmitMove(size_t index) {
4070 MoveOperands* move = moves_.Get(index);
4071 Location source = move->GetSource();
4072 Location destination = move->GetDestination();
4073
4074 if (source.IsRegister()) {
4075 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004077 } else {
4078 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004080 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004081 } else if (source.IsFpuRegister()) {
4082 if (destination.IsFpuRegister()) {
4083 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4084 } else if (destination.IsStackSlot()) {
4085 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4086 } else {
4087 DCHECK(destination.IsDoubleStackSlot());
4088 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4089 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004090 } else if (source.IsStackSlot()) {
4091 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004093 } else if (destination.IsFpuRegister()) {
4094 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004095 } else {
4096 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004097 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4098 }
4099 } else if (source.IsDoubleStackSlot()) {
4100 if (destination.IsFpuRegister()) {
4101 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4102 } else {
4103 DCHECK(destination.IsDoubleStackSlot()) << destination;
4104 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004105 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004106 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004107 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004108 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004109 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004110 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004111 if (value == 0) {
4112 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4113 } else {
4114 __ movl(destination.AsRegister<Register>(), Immediate(value));
4115 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004116 } else {
4117 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004118 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004119 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004120 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004121 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004122 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004123 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004124 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004125 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4126 if (value == 0) {
4127 // Easy handling of 0.0.
4128 __ xorps(dest, dest);
4129 } else {
4130 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004131 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4132 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4133 __ movl(temp, Immediate(value));
4134 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004135 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004136 } else {
4137 DCHECK(destination.IsStackSlot()) << destination;
4138 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4139 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004140 } else if (constant->IsLongConstant()) {
4141 int64_t value = constant->AsLongConstant()->GetValue();
4142 int32_t low_value = Low32Bits(value);
4143 int32_t high_value = High32Bits(value);
4144 Immediate low(low_value);
4145 Immediate high(high_value);
4146 if (destination.IsDoubleStackSlot()) {
4147 __ movl(Address(ESP, destination.GetStackIndex()), low);
4148 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4149 } else {
4150 __ movl(destination.AsRegisterPairLow<Register>(), low);
4151 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4152 }
4153 } else {
4154 DCHECK(constant->IsDoubleConstant());
4155 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004156 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004157 int32_t low_value = Low32Bits(value);
4158 int32_t high_value = High32Bits(value);
4159 Immediate low(low_value);
4160 Immediate high(high_value);
4161 if (destination.IsFpuRegister()) {
4162 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4163 if (value == 0) {
4164 // Easy handling of 0.0.
4165 __ xorpd(dest, dest);
4166 } else {
4167 __ pushl(high);
4168 __ pushl(low);
4169 __ movsd(dest, Address(ESP, 0));
4170 __ addl(ESP, Immediate(8));
4171 }
4172 } else {
4173 DCHECK(destination.IsDoubleStackSlot()) << destination;
4174 __ movl(Address(ESP, destination.GetStackIndex()), low);
4175 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4176 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004177 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004178 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004179 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004180 }
4181}
4182
Mark Mendella5c19ce2015-04-01 12:51:05 -04004183void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004184 Register suggested_scratch = reg == EAX ? EBX : EAX;
4185 ScratchRegisterScope ensure_scratch(
4186 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4187
4188 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4189 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4190 __ movl(Address(ESP, mem + stack_offset), reg);
4191 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004192}
4193
Mark Mendell7c8d0092015-01-26 11:21:33 -05004194void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004195 ScratchRegisterScope ensure_scratch(
4196 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4197
4198 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4199 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4200 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4201 __ movss(Address(ESP, mem + stack_offset), reg);
4202 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004203}
4204
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004205void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004206 ScratchRegisterScope ensure_scratch1(
4207 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004208
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004209 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4210 ScratchRegisterScope ensure_scratch2(
4211 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004212
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004213 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4214 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4215 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4216 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4217 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4218 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004219}
4220
4221void ParallelMoveResolverX86::EmitSwap(size_t index) {
4222 MoveOperands* move = moves_.Get(index);
4223 Location source = move->GetSource();
4224 Location destination = move->GetDestination();
4225
4226 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004227 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004228 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004229 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004230 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004231 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004232 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4233 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004234 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4235 // Use XOR Swap algorithm to avoid a temporary.
4236 DCHECK_NE(source.reg(), destination.reg());
4237 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4238 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4239 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4240 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4241 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4242 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4243 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004244 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4245 // Take advantage of the 16 bytes in the XMM register.
4246 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4247 Address stack(ESP, destination.GetStackIndex());
4248 // Load the double into the high doubleword.
4249 __ movhpd(reg, stack);
4250
4251 // Store the low double into the destination.
4252 __ movsd(stack, reg);
4253
4254 // Move the high double to the low double.
4255 __ psrldq(reg, Immediate(8));
4256 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4257 // Take advantage of the 16 bytes in the XMM register.
4258 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4259 Address stack(ESP, source.GetStackIndex());
4260 // Load the double into the high doubleword.
4261 __ movhpd(reg, stack);
4262
4263 // Store the low double into the destination.
4264 __ movsd(stack, reg);
4265
4266 // Move the high double to the low double.
4267 __ psrldq(reg, Immediate(8));
4268 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4269 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4270 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004271 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004272 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004273 }
4274}
4275
4276void ParallelMoveResolverX86::SpillScratch(int reg) {
4277 __ pushl(static_cast<Register>(reg));
4278}
4279
4280void ParallelMoveResolverX86::RestoreScratch(int reg) {
4281 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004282}
4283
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004284void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004285 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4286 ? LocationSummary::kCallOnSlowPath
4287 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004288 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004289 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004290 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004291 locations->SetOut(Location::RequiresRegister());
4292}
4293
4294void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004295 LocationSummary* locations = cls->GetLocations();
4296 Register out = locations->Out().AsRegister<Register>();
4297 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004298 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004299 DCHECK(!cls->CanCallRuntime());
4300 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004301 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004302 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004303 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004304 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004305 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004306 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004307
4308 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4309 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4310 codegen_->AddSlowPath(slow_path);
4311 __ testl(out, out);
4312 __ j(kEqual, slow_path->GetEntryLabel());
4313 if (cls->MustGenerateClinitCheck()) {
4314 GenerateClassInitializationCheck(slow_path, out);
4315 } else {
4316 __ Bind(slow_path->GetExitLabel());
4317 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004318 }
4319}
4320
4321void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4322 LocationSummary* locations =
4323 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4324 locations->SetInAt(0, Location::RequiresRegister());
4325 if (check->HasUses()) {
4326 locations->SetOut(Location::SameAsFirstInput());
4327 }
4328}
4329
4330void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004331 // We assume the class to not be null.
4332 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4333 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004334 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004335 GenerateClassInitializationCheck(slow_path,
4336 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004337}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004338
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004339void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4340 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004341 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4342 Immediate(mirror::Class::kStatusInitialized));
4343 __ j(kLess, slow_path->GetEntryLabel());
4344 __ Bind(slow_path->GetExitLabel());
4345 // No need for memory fence, thanks to the X86 memory model.
4346}
4347
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004348void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4349 LocationSummary* locations =
4350 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004351 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004352 locations->SetOut(Location::RequiresRegister());
4353}
4354
4355void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4356 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4357 codegen_->AddSlowPath(slow_path);
4358
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004359 LocationSummary* locations = load->GetLocations();
4360 Register out = locations->Out().AsRegister<Register>();
4361 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004362 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004363 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004364 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4365 __ testl(out, out);
4366 __ j(kEqual, slow_path->GetEntryLabel());
4367 __ Bind(slow_path->GetExitLabel());
4368}
4369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004370void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4371 LocationSummary* locations =
4372 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4373 locations->SetOut(Location::RequiresRegister());
4374}
4375
4376void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4377 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004378 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004379 __ fs()->movl(address, Immediate(0));
4380}
4381
4382void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4383 LocationSummary* locations =
4384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4385 InvokeRuntimeCallingConvention calling_convention;
4386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4387}
4388
4389void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4390 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4391 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4392}
4393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004394void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004395 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4396 ? LocationSummary::kNoCall
4397 : LocationSummary::kCallOnSlowPath;
4398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4399 locations->SetInAt(0, Location::RequiresRegister());
4400 locations->SetInAt(1, Location::Any());
4401 locations->SetOut(Location::RequiresRegister());
4402}
4403
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004404void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004405 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004406 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004407 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4410 Label done, zero;
4411 SlowPathCodeX86* slow_path = nullptr;
4412
4413 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004414 // Avoid null check if we know obj is not null.
4415 if (instruction->MustDoNullCheck()) {
4416 __ testl(obj, obj);
4417 __ j(kEqual, &zero);
4418 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004419 __ movl(out, Address(obj, class_offset));
4420 // Compare the class of `obj` with `cls`.
4421 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004422 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004423 } else {
4424 DCHECK(cls.IsStackSlot()) << cls;
4425 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4426 }
4427
4428 if (instruction->IsClassFinal()) {
4429 // Classes must be equal for the instanceof to succeed.
4430 __ j(kNotEqual, &zero);
4431 __ movl(out, Immediate(1));
4432 __ jmp(&done);
4433 } else {
4434 // If the classes are not equal, we go into a slow path.
4435 DCHECK(locations->OnlyCallsOnSlowPath());
4436 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004437 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004438 codegen_->AddSlowPath(slow_path);
4439 __ j(kNotEqual, slow_path->GetEntryLabel());
4440 __ movl(out, Immediate(1));
4441 __ jmp(&done);
4442 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004443
4444 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4445 __ Bind(&zero);
4446 __ movl(out, Immediate(0));
4447 }
4448
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004449 if (slow_path != nullptr) {
4450 __ Bind(slow_path->GetExitLabel());
4451 }
4452 __ Bind(&done);
4453}
4454
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004455void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4457 instruction, LocationSummary::kCallOnSlowPath);
4458 locations->SetInAt(0, Location::RequiresRegister());
4459 locations->SetInAt(1, Location::Any());
4460 locations->AddTemp(Location::RequiresRegister());
4461}
4462
4463void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4464 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004465 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004466 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004467 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004468 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4469 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4470 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4471 codegen_->AddSlowPath(slow_path);
4472
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004473 // Avoid null check if we know obj is not null.
4474 if (instruction->MustDoNullCheck()) {
4475 __ testl(obj, obj);
4476 __ j(kEqual, slow_path->GetExitLabel());
4477 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004478
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004479 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004480 // Compare the class of `obj` with `cls`.
4481 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004482 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004483 } else {
4484 DCHECK(cls.IsStackSlot()) << cls;
4485 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4486 }
4487
4488 __ j(kNotEqual, slow_path->GetEntryLabel());
4489 __ Bind(slow_path->GetExitLabel());
4490}
4491
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004492void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4493 LocationSummary* locations =
4494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4495 InvokeRuntimeCallingConvention calling_convention;
4496 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4497}
4498
4499void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4500 __ fs()->call(Address::Absolute(instruction->IsEnter()
4501 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4502 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4503 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4504}
4505
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004506void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4507void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4508void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4509
4510void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4511 LocationSummary* locations =
4512 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4513 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4514 || instruction->GetResultType() == Primitive::kPrimLong);
4515 locations->SetInAt(0, Location::RequiresRegister());
4516 locations->SetInAt(1, Location::Any());
4517 locations->SetOut(Location::SameAsFirstInput());
4518}
4519
4520void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4521 HandleBitwiseOperation(instruction);
4522}
4523
4524void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4525 HandleBitwiseOperation(instruction);
4526}
4527
4528void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4529 HandleBitwiseOperation(instruction);
4530}
4531
4532void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4533 LocationSummary* locations = instruction->GetLocations();
4534 Location first = locations->InAt(0);
4535 Location second = locations->InAt(1);
4536 DCHECK(first.Equals(locations->Out()));
4537
4538 if (instruction->GetResultType() == Primitive::kPrimInt) {
4539 if (second.IsRegister()) {
4540 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004541 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004542 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004543 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004544 } else {
4545 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004547 }
4548 } else if (second.IsConstant()) {
4549 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004550 __ andl(first.AsRegister<Register>(),
4551 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004552 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004553 __ orl(first.AsRegister<Register>(),
4554 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004555 } else {
4556 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004557 __ xorl(first.AsRegister<Register>(),
4558 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559 }
4560 } else {
4561 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004562 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004563 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004565 } else {
4566 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004567 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004568 }
4569 }
4570 } else {
4571 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4572 if (second.IsRegisterPair()) {
4573 if (instruction->IsAnd()) {
4574 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4575 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4576 } else if (instruction->IsOr()) {
4577 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4578 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4579 } else {
4580 DCHECK(instruction->IsXor());
4581 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4582 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4583 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004584 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004585 if (instruction->IsAnd()) {
4586 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4587 __ andl(first.AsRegisterPairHigh<Register>(),
4588 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4589 } else if (instruction->IsOr()) {
4590 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4591 __ orl(first.AsRegisterPairHigh<Register>(),
4592 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4593 } else {
4594 DCHECK(instruction->IsXor());
4595 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4596 __ xorl(first.AsRegisterPairHigh<Register>(),
4597 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4598 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004599 } else {
4600 DCHECK(second.IsConstant()) << second;
4601 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004602 int32_t low_value = Low32Bits(value);
4603 int32_t high_value = High32Bits(value);
4604 Immediate low(low_value);
4605 Immediate high(high_value);
4606 Register first_low = first.AsRegisterPairLow<Register>();
4607 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004608 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004609 if (low_value == 0) {
4610 __ xorl(first_low, first_low);
4611 } else if (low_value != -1) {
4612 __ andl(first_low, low);
4613 }
4614 if (high_value == 0) {
4615 __ xorl(first_high, first_high);
4616 } else if (high_value != -1) {
4617 __ andl(first_high, high);
4618 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004619 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004620 if (low_value != 0) {
4621 __ orl(first_low, low);
4622 }
4623 if (high_value != 0) {
4624 __ orl(first_high, high);
4625 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004626 } else {
4627 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004628 if (low_value != 0) {
4629 __ xorl(first_low, low);
4630 }
4631 if (high_value != 0) {
4632 __ xorl(first_high, high);
4633 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004634 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004635 }
4636 }
4637}
4638
Calin Juravleb1498f62015-02-16 13:13:29 +00004639void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4640 // Nothing to do, this should be removed during prepare for register allocator.
4641 UNUSED(instruction);
4642 LOG(FATAL) << "Unreachable";
4643}
4644
4645void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4646 // Nothing to do, this should be removed during prepare for register allocator.
4647 UNUSED(instruction);
4648 LOG(FATAL) << "Unreachable";
4649}
4650
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004651} // namespace x86
4652} // namespace art