blob: 8a7b52e5496bd3cba59192b508f38d0c68e38950 [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)) {
1234 return;
1235 }
1236
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001237 HandleInvoke(invoke);
1238}
1239
Mark Mendell09ed1a32015-03-25 08:30:06 -04001240static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1241 if (invoke->GetLocations()->Intrinsified()) {
1242 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1243 intrinsic.Dispatch(invoke);
1244 return true;
1245 }
1246 return false;
1247}
1248
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001249void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001250 // When we do not run baseline, explicit clinit checks triggered by static
1251 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1252 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001253
Mark Mendell09ed1a32015-03-25 08:30:06 -04001254 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1255 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001256 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001257
Mark Mendell09ed1a32015-03-25 08:30:06 -04001258 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00001259 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001260 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001261}
1262
1263void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1264 HandleInvoke(invoke);
1265}
1266
1267void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001268 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001269 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270}
1271
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001272void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001274 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1275 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001276 LocationSummary* locations = invoke->GetLocations();
1277 Location receiver = locations->InAt(0);
1278 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00001279 // temp = object->GetClass();
1280 if (receiver.IsStackSlot()) {
1281 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1282 __ movl(temp, Address(temp, class_offset));
1283 } else {
1284 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
1285 }
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 Geoffrayc345f142015-06-04 17:17:32 +00003204 Register 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 Geoffrayc345f142015-06-04 17:17:32 +00003214 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003215 // (temp + offset_of_quick_compiled_code)()
3216 __ call(Address(
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003217 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003218 } else {
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003219 // temp = method;
3220 LoadCurrentMethod(temp);
3221 if (!invoke->IsRecursive()) {
3222 // temp = temp->dex_cache_resolved_methods_;
3223 __ movl(temp, Address(temp, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3224 // temp = temp[index_in_cache]
3225 __ movl(temp, Address(temp,
3226 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3227 // (temp + offset_of_quick_compiled_code)()
3228 __ call(Address(temp,
3229 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3230 } else {
3231 __ call(GetFrameEntryLabel());
3232 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003233 }
3234
3235 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003236}
3237
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003238void CodeGeneratorX86::MarkGCCard(Register temp,
3239 Register card,
3240 Register object,
3241 Register value,
3242 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003244 if (value_can_be_null) {
3245 __ testl(value, value);
3246 __ j(kEqual, &is_null);
3247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3249 __ movl(temp, object);
3250 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003251 __ movb(Address(temp, card, TIMES_1, 0),
3252 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003253 if (value_can_be_null) {
3254 __ Bind(&is_null);
3255 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003256}
3257
Calin Juravle52c48962014-12-16 17:02:57 +00003258void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3259 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003260 LocationSummary* locations =
3261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003262 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003263
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003264 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3265 locations->SetOut(Location::RequiresFpuRegister());
3266 } else {
3267 // The output overlaps in case of long: we don't want the low move to overwrite
3268 // the object's location.
3269 locations->SetOut(Location::RequiresRegister(),
3270 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3271 : Location::kNoOutputOverlap);
3272 }
Calin Juravle52c48962014-12-16 17:02:57 +00003273
3274 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3275 // Long values can be loaded atomically into an XMM using movsd.
3276 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3277 // and then copy the XMM into the output 32bits at a time).
3278 locations->AddTemp(Location::RequiresFpuRegister());
3279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280}
3281
Calin Juravle52c48962014-12-16 17:02:57 +00003282void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3283 const FieldInfo& field_info) {
3284 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003285
Calin Juravle52c48962014-12-16 17:02:57 +00003286 LocationSummary* locations = instruction->GetLocations();
3287 Register base = locations->InAt(0).AsRegister<Register>();
3288 Location out = locations->Out();
3289 bool is_volatile = field_info.IsVolatile();
3290 Primitive::Type field_type = field_info.GetFieldType();
3291 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3292
3293 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003295 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003296 break;
3297 }
3298
3299 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003300 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 break;
3302 }
3303
3304 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003305 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003306 break;
3307 }
3308
3309 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003310 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 break;
3312 }
3313
3314 case Primitive::kPrimInt:
3315 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003316 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003317 break;
3318 }
3319
3320 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 if (is_volatile) {
3322 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3323 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003325 __ movd(out.AsRegisterPairLow<Register>(), temp);
3326 __ psrlq(temp, Immediate(32));
3327 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3328 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003329 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003330 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003332 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3333 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003334 break;
3335 }
3336
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003337 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003338 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003339 break;
3340 }
3341
3342 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003343 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003344 break;
3345 }
3346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003347 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003348 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003349 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003350 }
Calin Juravle52c48962014-12-16 17:02:57 +00003351
Calin Juravle77520bc2015-01-12 18:45:46 +00003352 // Longs are handled in the switch.
3353 if (field_type != Primitive::kPrimLong) {
3354 codegen_->MaybeRecordImplicitNullCheck(instruction);
3355 }
3356
Calin Juravle52c48962014-12-16 17:02:57 +00003357 if (is_volatile) {
3358 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3359 }
3360}
3361
3362void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3363 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3364
3365 LocationSummary* locations =
3366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3367 locations->SetInAt(0, Location::RequiresRegister());
3368 bool is_volatile = field_info.IsVolatile();
3369 Primitive::Type field_type = field_info.GetFieldType();
3370 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3371 || (field_type == Primitive::kPrimByte);
3372
3373 // The register allocator does not support multiple
3374 // inputs that die at entry with one in a specific register.
3375 if (is_byte_type) {
3376 // Ensure the value is in a byte register.
3377 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003378 } else if (Primitive::IsFloatingPointType(field_type)) {
3379 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003380 } else {
3381 locations->SetInAt(1, Location::RequiresRegister());
3382 }
3383 // Temporary registers for the write barrier.
3384 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3385 locations->AddTemp(Location::RequiresRegister());
3386 // Ensure the card is in a byte register.
3387 locations->AddTemp(Location::RegisterLocation(ECX));
3388 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3389 // 64bits value can be atomically written to an address with movsd and an XMM register.
3390 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3391 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3392 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3393 // isolated cases when we need this it isn't worth adding the extra complexity.
3394 locations->AddTemp(Location::RequiresFpuRegister());
3395 locations->AddTemp(Location::RequiresFpuRegister());
3396 }
3397}
3398
3399void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003400 const FieldInfo& field_info,
3401 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003402 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3403
3404 LocationSummary* locations = instruction->GetLocations();
3405 Register base = locations->InAt(0).AsRegister<Register>();
3406 Location value = locations->InAt(1);
3407 bool is_volatile = field_info.IsVolatile();
3408 Primitive::Type field_type = field_info.GetFieldType();
3409 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3410
3411 if (is_volatile) {
3412 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3413 }
3414
3415 switch (field_type) {
3416 case Primitive::kPrimBoolean:
3417 case Primitive::kPrimByte: {
3418 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3419 break;
3420 }
3421
3422 case Primitive::kPrimShort:
3423 case Primitive::kPrimChar: {
3424 __ movw(Address(base, offset), value.AsRegister<Register>());
3425 break;
3426 }
3427
3428 case Primitive::kPrimInt:
3429 case Primitive::kPrimNot: {
3430 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003431 break;
3432 }
3433
3434 case Primitive::kPrimLong: {
3435 if (is_volatile) {
3436 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3437 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3438 __ movd(temp1, value.AsRegisterPairLow<Register>());
3439 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3440 __ punpckldq(temp1, temp2);
3441 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003442 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003443 } else {
3444 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003445 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003446 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3447 }
3448 break;
3449 }
3450
3451 case Primitive::kPrimFloat: {
3452 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3453 break;
3454 }
3455
3456 case Primitive::kPrimDouble: {
3457 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3458 break;
3459 }
3460
3461 case Primitive::kPrimVoid:
3462 LOG(FATAL) << "Unreachable type " << field_type;
3463 UNREACHABLE();
3464 }
3465
Calin Juravle77520bc2015-01-12 18:45:46 +00003466 // Longs are handled in the switch.
3467 if (field_type != Primitive::kPrimLong) {
3468 codegen_->MaybeRecordImplicitNullCheck(instruction);
3469 }
3470
3471 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3472 Register temp = locations->GetTemp(0).AsRegister<Register>();
3473 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003474 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003475 }
3476
Calin Juravle52c48962014-12-16 17:02:57 +00003477 if (is_volatile) {
3478 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3479 }
3480}
3481
3482void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3483 HandleFieldGet(instruction, instruction->GetFieldInfo());
3484}
3485
3486void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3487 HandleFieldGet(instruction, instruction->GetFieldInfo());
3488}
3489
3490void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3491 HandleFieldSet(instruction, instruction->GetFieldInfo());
3492}
3493
3494void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003495 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003496}
3497
3498void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3499 HandleFieldSet(instruction, instruction->GetFieldInfo());
3500}
3501
3502void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003503 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003504}
3505
3506void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3507 HandleFieldGet(instruction, instruction->GetFieldInfo());
3508}
3509
3510void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3511 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003512}
3513
3514void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003517 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3518 ? Location::RequiresRegister()
3519 : Location::Any();
3520 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003521 if (instruction->HasUses()) {
3522 locations->SetOut(Location::SameAsFirstInput());
3523 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003524}
3525
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003526void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003527 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3528 return;
3529 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003530 LocationSummary* locations = instruction->GetLocations();
3531 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003532
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003533 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3534 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3535}
3536
3537void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003538 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003539 codegen_->AddSlowPath(slow_path);
3540
3541 LocationSummary* locations = instruction->GetLocations();
3542 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003543
3544 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003545 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003546 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003547 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003548 } else {
3549 DCHECK(obj.IsConstant()) << obj;
3550 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3551 __ jmp(slow_path->GetEntryLabel());
3552 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003553 }
3554 __ j(kEqual, slow_path->GetEntryLabel());
3555}
3556
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003557void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3558 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3559 GenerateImplicitNullCheck(instruction);
3560 } else {
3561 GenerateExplicitNullCheck(instruction);
3562 }
3563}
3564
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003566 LocationSummary* locations =
3567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003568 locations->SetInAt(0, Location::RequiresRegister());
3569 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003570 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3571 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3572 } else {
3573 // The output overlaps in case of long: we don't want the low move to overwrite
3574 // the array's location.
3575 locations->SetOut(Location::RequiresRegister(),
3576 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3577 : Location::kNoOutputOverlap);
3578 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579}
3580
3581void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3582 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 Location index = locations->InAt(1);
3585
Calin Juravle77520bc2015-01-12 18:45:46 +00003586 Primitive::Type type = instruction->GetType();
3587 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003588 case Primitive::kPrimBoolean: {
3589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003591 if (index.IsConstant()) {
3592 __ movzxb(out, Address(obj,
3593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 }
3597 break;
3598 }
3599
3600 case Primitive::kPrimByte: {
3601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 if (index.IsConstant()) {
3604 __ movsxb(out, Address(obj,
3605 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3606 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 }
3609 break;
3610 }
3611
3612 case Primitive::kPrimShort: {
3613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 if (index.IsConstant()) {
3616 __ movsxw(out, Address(obj,
3617 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3618 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 }
3621 break;
3622 }
3623
3624 case Primitive::kPrimChar: {
3625 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003626 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003627 if (index.IsConstant()) {
3628 __ movzxw(out, Address(obj,
3629 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3630 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 }
3633 break;
3634 }
3635
3636 case Primitive::kPrimInt:
3637 case Primitive::kPrimNot: {
3638 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640 if (index.IsConstant()) {
3641 __ movl(out, Address(obj,
3642 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3643 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 }
3646 break;
3647 }
3648
3649 case Primitive::kPrimLong: {
3650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003651 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003652 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 if (index.IsConstant()) {
3654 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003655 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003656 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003657 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003659 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003661 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003662 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003664 }
3665 break;
3666 }
3667
Mark Mendell7c8d0092015-01-26 11:21:33 -05003668 case Primitive::kPrimFloat: {
3669 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3670 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3671 if (index.IsConstant()) {
3672 __ movss(out, Address(obj,
3673 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3674 } else {
3675 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3676 }
3677 break;
3678 }
3679
3680 case Primitive::kPrimDouble: {
3681 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3682 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3683 if (index.IsConstant()) {
3684 __ movsd(out, Address(obj,
3685 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3686 } else {
3687 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3688 }
3689 break;
3690 }
3691
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003692 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003693 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003694 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003696
3697 if (type != Primitive::kPrimLong) {
3698 codegen_->MaybeRecordImplicitNullCheck(instruction);
3699 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700}
3701
3702void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003703 // This location builder might end up asking to up to four registers, which is
3704 // not currently possible for baseline. The situation in which we need four
3705 // registers cannot be met by baseline though, because it has not run any
3706 // optimization.
3707
Nicolas Geoffray39468442014-09-02 15:17:15 +01003708 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 bool needs_write_barrier =
3710 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3711
Mark Mendell5f874182015-03-04 15:42:45 -05003712 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003713
Nicolas Geoffray39468442014-09-02 15:17:15 +01003714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3715 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003716 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003717
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003719 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003720 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3721 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3722 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003723 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003724 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3725 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003726 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003727 // In case of a byte operation, the register allocator does not support multiple
3728 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003729 locations->SetInAt(0, Location::RequiresRegister());
3730 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003731 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003732 // Ensure the value is in a byte register.
3733 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003734 } else if (Primitive::IsFloatingPointType(value_type)) {
3735 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003737 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003739 // Temporary registers for the write barrier.
3740 if (needs_write_barrier) {
3741 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003742 // Ensure the card is in a byte register.
3743 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746}
3747
3748void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3749 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003752 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003753 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003754 bool needs_runtime_call = locations->WillCall();
3755 bool needs_write_barrier =
3756 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757
3758 switch (value_type) {
3759 case Primitive::kPrimBoolean:
3760 case Primitive::kPrimByte: {
3761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 if (index.IsConstant()) {
3763 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003764 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003765 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003766 } else {
3767 __ movb(Address(obj, offset),
3768 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3769 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003771 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003773 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003774 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003776 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3777 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780 break;
3781 }
3782
3783 case Primitive::kPrimShort:
3784 case Primitive::kPrimChar: {
3785 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 if (index.IsConstant()) {
3787 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003788 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 } else {
3791 __ movw(Address(obj, offset),
3792 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3793 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003795 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3797 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003798 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003799 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3801 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 break;
3805 }
3806
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003807 case Primitive::kPrimInt:
3808 case Primitive::kPrimNot: {
3809 if (!needs_runtime_call) {
3810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3811 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003812 size_t offset =
3813 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003814 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003816 } else {
3817 DCHECK(value.IsConstant()) << value;
3818 __ movl(Address(obj, offset),
3819 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3820 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003821 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003822 DCHECK(index.IsRegister()) << index;
3823 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003824 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3825 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003826 } else {
3827 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003829 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3830 }
3831 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003833
3834 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 Register temp = locations->GetTemp(0).AsRegister<Register>();
3836 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003837 codegen_->MarkGCCard(
3838 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003839 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003840 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003841 DCHECK_EQ(value_type, Primitive::kPrimNot);
3842 DCHECK(!codegen_->IsLeafMethod());
3843 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3844 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 }
3846 break;
3847 }
3848
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003849 case Primitive::kPrimLong: {
3850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003851 if (index.IsConstant()) {
3852 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003853 if (value.IsRegisterPair()) {
3854 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003855 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003856 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003857 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003858 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003859 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3860 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003861 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003862 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3863 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003864 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003865 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003867 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003868 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003870 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003871 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003872 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003873 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003875 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003878 Immediate(High32Bits(val)));
3879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 }
3881 break;
3882 }
3883
Mark Mendell7c8d0092015-01-26 11:21:33 -05003884 case Primitive::kPrimFloat: {
3885 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3886 DCHECK(value.IsFpuRegister());
3887 if (index.IsConstant()) {
3888 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3889 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3890 } else {
3891 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3892 value.AsFpuRegister<XmmRegister>());
3893 }
3894 break;
3895 }
3896
3897 case Primitive::kPrimDouble: {
3898 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3899 DCHECK(value.IsFpuRegister());
3900 if (index.IsConstant()) {
3901 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3902 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3903 } else {
3904 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3905 value.AsFpuRegister<XmmRegister>());
3906 }
3907 break;
3908 }
3909
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 case Primitive::kPrimVoid:
3911 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003912 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913 }
3914}
3915
3916void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003918 locations->SetInAt(0, Location::RequiresRegister());
3919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003920}
3921
3922void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3923 LocationSummary* locations = instruction->GetLocations();
3924 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003925 Register obj = locations->InAt(0).AsRegister<Register>();
3926 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003928 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003929}
3930
3931void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003932 LocationSummary* locations =
3933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003934 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003935 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003936 if (instruction->HasUses()) {
3937 locations->SetOut(Location::SameAsFirstInput());
3938 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003939}
3940
3941void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3942 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003943 Location index_loc = locations->InAt(0);
3944 Location length_loc = locations->InAt(1);
3945 SlowPathCodeX86* slow_path =
3946 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947
Mark Mendell99dbd682015-04-22 16:18:52 -04003948 if (length_loc.IsConstant()) {
3949 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3950 if (index_loc.IsConstant()) {
3951 // BCE will remove the bounds check if we are guarenteed to pass.
3952 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3953 if (index < 0 || index >= length) {
3954 codegen_->AddSlowPath(slow_path);
3955 __ jmp(slow_path->GetEntryLabel());
3956 } else {
3957 // Some optimization after BCE may have generated this, and we should not
3958 // generate a bounds check if it is a valid range.
3959 }
3960 return;
3961 }
3962
3963 // We have to reverse the jump condition because the length is the constant.
3964 Register index_reg = index_loc.AsRegister<Register>();
3965 __ cmpl(index_reg, Immediate(length));
3966 codegen_->AddSlowPath(slow_path);
3967 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003968 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003969 Register length = length_loc.AsRegister<Register>();
3970 if (index_loc.IsConstant()) {
3971 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3972 __ cmpl(length, Immediate(value));
3973 } else {
3974 __ cmpl(length, index_loc.AsRegister<Register>());
3975 }
3976 codegen_->AddSlowPath(slow_path);
3977 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979}
3980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003981void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3982 temp->SetLocations(nullptr);
3983}
3984
3985void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3986 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003987 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003988}
3989
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003990void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003991 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003992 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003993}
3994
3995void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003996 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3997}
3998
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003999void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4001}
4002
4003void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004004 HBasicBlock* block = instruction->GetBlock();
4005 if (block->GetLoopInformation() != nullptr) {
4006 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4007 // The back edge will generate the suspend check.
4008 return;
4009 }
4010 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4011 // The goto will generate the suspend check.
4012 return;
4013 }
4014 GenerateSuspendCheck(instruction, nullptr);
4015}
4016
4017void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4018 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004019 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004020 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4021 if (slow_path == nullptr) {
4022 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4023 instruction->SetSlowPath(slow_path);
4024 codegen_->AddSlowPath(slow_path);
4025 if (successor != nullptr) {
4026 DCHECK(successor->IsLoopHeader());
4027 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4028 }
4029 } else {
4030 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4031 }
4032
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004033 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004034 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004035 if (successor == nullptr) {
4036 __ j(kNotEqual, slow_path->GetEntryLabel());
4037 __ Bind(slow_path->GetReturnLabel());
4038 } else {
4039 __ j(kEqual, codegen_->GetLabelOf(successor));
4040 __ jmp(slow_path->GetEntryLabel());
4041 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004042}
4043
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004044X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4045 return codegen_->GetAssembler();
4046}
4047
Mark Mendell7c8d0092015-01-26 11:21:33 -05004048void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004049 ScratchRegisterScope ensure_scratch(
4050 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4051 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4052 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4053 __ movl(temp_reg, Address(ESP, src + stack_offset));
4054 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004055}
4056
4057void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004058 ScratchRegisterScope ensure_scratch(
4059 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4060 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4061 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4062 __ movl(temp_reg, Address(ESP, src + stack_offset));
4063 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4064 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4065 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004066}
4067
4068void ParallelMoveResolverX86::EmitMove(size_t index) {
4069 MoveOperands* move = moves_.Get(index);
4070 Location source = move->GetSource();
4071 Location destination = move->GetDestination();
4072
4073 if (source.IsRegister()) {
4074 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004076 } else {
4077 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004079 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004080 } else if (source.IsFpuRegister()) {
4081 if (destination.IsFpuRegister()) {
4082 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4083 } else if (destination.IsStackSlot()) {
4084 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4085 } else {
4086 DCHECK(destination.IsDoubleStackSlot());
4087 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4088 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004089 } else if (source.IsStackSlot()) {
4090 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004092 } else if (destination.IsFpuRegister()) {
4093 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004094 } else {
4095 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004096 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4097 }
4098 } else if (source.IsDoubleStackSlot()) {
4099 if (destination.IsFpuRegister()) {
4100 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4101 } else {
4102 DCHECK(destination.IsDoubleStackSlot()) << destination;
4103 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004104 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004105 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004106 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004107 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004108 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004109 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004110 if (value == 0) {
4111 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4112 } else {
4113 __ movl(destination.AsRegister<Register>(), Immediate(value));
4114 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004115 } else {
4116 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004117 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004119 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004120 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004121 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004122 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004123 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004124 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4125 if (value == 0) {
4126 // Easy handling of 0.0.
4127 __ xorps(dest, dest);
4128 } else {
4129 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004130 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4131 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4132 __ movl(temp, Immediate(value));
4133 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004134 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004135 } else {
4136 DCHECK(destination.IsStackSlot()) << destination;
4137 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4138 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004139 } else if (constant->IsLongConstant()) {
4140 int64_t value = constant->AsLongConstant()->GetValue();
4141 int32_t low_value = Low32Bits(value);
4142 int32_t high_value = High32Bits(value);
4143 Immediate low(low_value);
4144 Immediate high(high_value);
4145 if (destination.IsDoubleStackSlot()) {
4146 __ movl(Address(ESP, destination.GetStackIndex()), low);
4147 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4148 } else {
4149 __ movl(destination.AsRegisterPairLow<Register>(), low);
4150 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4151 }
4152 } else {
4153 DCHECK(constant->IsDoubleConstant());
4154 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004155 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004156 int32_t low_value = Low32Bits(value);
4157 int32_t high_value = High32Bits(value);
4158 Immediate low(low_value);
4159 Immediate high(high_value);
4160 if (destination.IsFpuRegister()) {
4161 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4162 if (value == 0) {
4163 // Easy handling of 0.0.
4164 __ xorpd(dest, dest);
4165 } else {
4166 __ pushl(high);
4167 __ pushl(low);
4168 __ movsd(dest, Address(ESP, 0));
4169 __ addl(ESP, Immediate(8));
4170 }
4171 } else {
4172 DCHECK(destination.IsDoubleStackSlot()) << destination;
4173 __ movl(Address(ESP, destination.GetStackIndex()), low);
4174 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4175 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004176 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004177 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004178 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004179 }
4180}
4181
Mark Mendella5c19ce2015-04-01 12:51:05 -04004182void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004183 Register suggested_scratch = reg == EAX ? EBX : EAX;
4184 ScratchRegisterScope ensure_scratch(
4185 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4186
4187 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4188 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4189 __ movl(Address(ESP, mem + stack_offset), reg);
4190 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004191}
4192
Mark Mendell7c8d0092015-01-26 11:21:33 -05004193void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004194 ScratchRegisterScope ensure_scratch(
4195 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4196
4197 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4198 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4199 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4200 __ movss(Address(ESP, mem + stack_offset), reg);
4201 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004202}
4203
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004204void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004205 ScratchRegisterScope ensure_scratch1(
4206 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004207
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004208 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4209 ScratchRegisterScope ensure_scratch2(
4210 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004211
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004212 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4213 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4214 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4215 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4216 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4217 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004218}
4219
4220void ParallelMoveResolverX86::EmitSwap(size_t index) {
4221 MoveOperands* move = moves_.Get(index);
4222 Location source = move->GetSource();
4223 Location destination = move->GetDestination();
4224
4225 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004226 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004227 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004228 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004229 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004230 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004231 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4232 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004233 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4234 // Use XOR Swap algorithm to avoid a temporary.
4235 DCHECK_NE(source.reg(), destination.reg());
4236 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4237 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4238 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4239 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4240 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4241 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4242 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004243 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4244 // Take advantage of the 16 bytes in the XMM register.
4245 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4246 Address stack(ESP, destination.GetStackIndex());
4247 // Load the double into the high doubleword.
4248 __ movhpd(reg, stack);
4249
4250 // Store the low double into the destination.
4251 __ movsd(stack, reg);
4252
4253 // Move the high double to the low double.
4254 __ psrldq(reg, Immediate(8));
4255 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4256 // Take advantage of the 16 bytes in the XMM register.
4257 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4258 Address stack(ESP, source.GetStackIndex());
4259 // Load the double into the high doubleword.
4260 __ movhpd(reg, stack);
4261
4262 // Store the low double into the destination.
4263 __ movsd(stack, reg);
4264
4265 // Move the high double to the low double.
4266 __ psrldq(reg, Immediate(8));
4267 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4268 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4269 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004270 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004271 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004272 }
4273}
4274
4275void ParallelMoveResolverX86::SpillScratch(int reg) {
4276 __ pushl(static_cast<Register>(reg));
4277}
4278
4279void ParallelMoveResolverX86::RestoreScratch(int reg) {
4280 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004281}
4282
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004283void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004284 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4285 ? LocationSummary::kCallOnSlowPath
4286 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004287 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004288 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004289 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004290 locations->SetOut(Location::RequiresRegister());
4291}
4292
4293void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004294 LocationSummary* locations = cls->GetLocations();
4295 Register out = locations->Out().AsRegister<Register>();
4296 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004297 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004298 DCHECK(!cls->CanCallRuntime());
4299 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004300 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004301 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004302 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004303 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004304 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004305 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004306
4307 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4308 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4309 codegen_->AddSlowPath(slow_path);
4310 __ testl(out, out);
4311 __ j(kEqual, slow_path->GetEntryLabel());
4312 if (cls->MustGenerateClinitCheck()) {
4313 GenerateClassInitializationCheck(slow_path, out);
4314 } else {
4315 __ Bind(slow_path->GetExitLabel());
4316 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004317 }
4318}
4319
4320void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4321 LocationSummary* locations =
4322 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4323 locations->SetInAt(0, Location::RequiresRegister());
4324 if (check->HasUses()) {
4325 locations->SetOut(Location::SameAsFirstInput());
4326 }
4327}
4328
4329void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004330 // We assume the class to not be null.
4331 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4332 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004334 GenerateClassInitializationCheck(slow_path,
4335 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004336}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004337
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004338void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4339 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004340 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4341 Immediate(mirror::Class::kStatusInitialized));
4342 __ j(kLess, slow_path->GetEntryLabel());
4343 __ Bind(slow_path->GetExitLabel());
4344 // No need for memory fence, thanks to the X86 memory model.
4345}
4346
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004347void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4348 LocationSummary* locations =
4349 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004350 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004351 locations->SetOut(Location::RequiresRegister());
4352}
4353
4354void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4355 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4356 codegen_->AddSlowPath(slow_path);
4357
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004358 LocationSummary* locations = load->GetLocations();
4359 Register out = locations->Out().AsRegister<Register>();
4360 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004361 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004362 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004363 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4364 __ testl(out, out);
4365 __ j(kEqual, slow_path->GetEntryLabel());
4366 __ Bind(slow_path->GetExitLabel());
4367}
4368
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004369void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4370 LocationSummary* locations =
4371 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4372 locations->SetOut(Location::RequiresRegister());
4373}
4374
4375void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4376 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004378 __ fs()->movl(address, Immediate(0));
4379}
4380
4381void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4382 LocationSummary* locations =
4383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4384 InvokeRuntimeCallingConvention calling_convention;
4385 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4386}
4387
4388void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4389 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4390 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4391}
4392
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004393void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004394 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4395 ? LocationSummary::kNoCall
4396 : LocationSummary::kCallOnSlowPath;
4397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4398 locations->SetInAt(0, Location::RequiresRegister());
4399 locations->SetInAt(1, Location::Any());
4400 locations->SetOut(Location::RequiresRegister());
4401}
4402
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004403void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004404 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004406 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004408 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4409 Label done, zero;
4410 SlowPathCodeX86* slow_path = nullptr;
4411
4412 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004413 // Avoid null check if we know obj is not null.
4414 if (instruction->MustDoNullCheck()) {
4415 __ testl(obj, obj);
4416 __ j(kEqual, &zero);
4417 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004418 __ movl(out, Address(obj, class_offset));
4419 // Compare the class of `obj` with `cls`.
4420 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004422 } else {
4423 DCHECK(cls.IsStackSlot()) << cls;
4424 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4425 }
4426
4427 if (instruction->IsClassFinal()) {
4428 // Classes must be equal for the instanceof to succeed.
4429 __ j(kNotEqual, &zero);
4430 __ movl(out, Immediate(1));
4431 __ jmp(&done);
4432 } else {
4433 // If the classes are not equal, we go into a slow path.
4434 DCHECK(locations->OnlyCallsOnSlowPath());
4435 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004436 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004437 codegen_->AddSlowPath(slow_path);
4438 __ j(kNotEqual, slow_path->GetEntryLabel());
4439 __ movl(out, Immediate(1));
4440 __ jmp(&done);
4441 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004442
4443 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4444 __ Bind(&zero);
4445 __ movl(out, Immediate(0));
4446 }
4447
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004448 if (slow_path != nullptr) {
4449 __ Bind(slow_path->GetExitLabel());
4450 }
4451 __ Bind(&done);
4452}
4453
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004454void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4456 instruction, LocationSummary::kCallOnSlowPath);
4457 locations->SetInAt(0, Location::RequiresRegister());
4458 locations->SetInAt(1, Location::Any());
4459 locations->AddTemp(Location::RequiresRegister());
4460}
4461
4462void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4463 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004464 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004465 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004467 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4468 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4469 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4470 codegen_->AddSlowPath(slow_path);
4471
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004472 // Avoid null check if we know obj is not null.
4473 if (instruction->MustDoNullCheck()) {
4474 __ testl(obj, obj);
4475 __ j(kEqual, slow_path->GetExitLabel());
4476 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004477
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004478 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004479 // Compare the class of `obj` with `cls`.
4480 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004481 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004482 } else {
4483 DCHECK(cls.IsStackSlot()) << cls;
4484 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4485 }
4486
4487 __ j(kNotEqual, slow_path->GetEntryLabel());
4488 __ Bind(slow_path->GetExitLabel());
4489}
4490
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004491void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4492 LocationSummary* locations =
4493 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4494 InvokeRuntimeCallingConvention calling_convention;
4495 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4496}
4497
4498void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4499 __ fs()->call(Address::Absolute(instruction->IsEnter()
4500 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4501 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4502 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4503}
4504
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004505void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4506void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4507void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4508
4509void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4510 LocationSummary* locations =
4511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4512 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4513 || instruction->GetResultType() == Primitive::kPrimLong);
4514 locations->SetInAt(0, Location::RequiresRegister());
4515 locations->SetInAt(1, Location::Any());
4516 locations->SetOut(Location::SameAsFirstInput());
4517}
4518
4519void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4520 HandleBitwiseOperation(instruction);
4521}
4522
4523void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4524 HandleBitwiseOperation(instruction);
4525}
4526
4527void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4528 HandleBitwiseOperation(instruction);
4529}
4530
4531void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4532 LocationSummary* locations = instruction->GetLocations();
4533 Location first = locations->InAt(0);
4534 Location second = locations->InAt(1);
4535 DCHECK(first.Equals(locations->Out()));
4536
4537 if (instruction->GetResultType() == Primitive::kPrimInt) {
4538 if (second.IsRegister()) {
4539 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004540 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004541 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004542 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004543 } else {
4544 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004545 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004546 }
4547 } else if (second.IsConstant()) {
4548 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004549 __ andl(first.AsRegister<Register>(),
4550 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004551 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004552 __ orl(first.AsRegister<Register>(),
4553 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004554 } else {
4555 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004556 __ xorl(first.AsRegister<Register>(),
4557 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004558 }
4559 } else {
4560 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004562 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004564 } else {
4565 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004566 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004567 }
4568 }
4569 } else {
4570 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4571 if (second.IsRegisterPair()) {
4572 if (instruction->IsAnd()) {
4573 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4574 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4575 } else if (instruction->IsOr()) {
4576 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4577 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4578 } else {
4579 DCHECK(instruction->IsXor());
4580 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4581 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4582 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004583 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004584 if (instruction->IsAnd()) {
4585 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4586 __ andl(first.AsRegisterPairHigh<Register>(),
4587 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4588 } else if (instruction->IsOr()) {
4589 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4590 __ orl(first.AsRegisterPairHigh<Register>(),
4591 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4592 } else {
4593 DCHECK(instruction->IsXor());
4594 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4595 __ xorl(first.AsRegisterPairHigh<Register>(),
4596 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4597 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004598 } else {
4599 DCHECK(second.IsConstant()) << second;
4600 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004601 int32_t low_value = Low32Bits(value);
4602 int32_t high_value = High32Bits(value);
4603 Immediate low(low_value);
4604 Immediate high(high_value);
4605 Register first_low = first.AsRegisterPairLow<Register>();
4606 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004607 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004608 if (low_value == 0) {
4609 __ xorl(first_low, first_low);
4610 } else if (low_value != -1) {
4611 __ andl(first_low, low);
4612 }
4613 if (high_value == 0) {
4614 __ xorl(first_high, first_high);
4615 } else if (high_value != -1) {
4616 __ andl(first_high, high);
4617 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004618 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004619 if (low_value != 0) {
4620 __ orl(first_low, low);
4621 }
4622 if (high_value != 0) {
4623 __ orl(first_high, high);
4624 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004625 } else {
4626 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004627 if (low_value != 0) {
4628 __ xorl(first_low, low);
4629 }
4630 if (high_value != 0) {
4631 __ xorl(first_high, high);
4632 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004633 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004634 }
4635 }
4636}
4637
Calin Juravleb1498f62015-02-16 13:13:29 +00004638void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4639 // Nothing to do, this should be removed during prepare for register allocator.
4640 UNUSED(instruction);
4641 LOG(FATAL) << "Unreachable";
4642}
4643
4644void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4645 // Nothing to do, this should be removed during prepare for register allocator.
4646 UNUSED(instruction);
4647 LOG(FATAL) << "Unreachable";
4648}
4649
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004650} // namespace x86
4651} // namespace art