blob: 3c5efa436f946688798e3893c75165c84b86b664 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010019#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010020#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000021#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010022#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040023#include "intrinsics.h"
24#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070025#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010026#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010027#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
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
Nicolas Geoffraye5038322014-07-04 09:41:32 +010047#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
48
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 __
327#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
328
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
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100559Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 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 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000568 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100569 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100571 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000572 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100573 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 uint32_t index = gp_index_;
578 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000579 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100580 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
582 calling_convention.GetRegisterPairAt(index));
583 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100584 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000585 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
586 }
587 }
588
589 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100590 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000591 stack_index_++;
592 if (index < calling_convention.GetNumberOfFpuRegisters()) {
593 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
594 } else {
595 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
596 }
597 }
598
599 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100600 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000601 stack_index_ += 2;
602 if (index < calling_convention.GetNumberOfFpuRegisters()) {
603 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
604 } else {
605 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 }
607 }
608
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100609 case Primitive::kPrimVoid:
610 LOG(FATAL) << "Unexpected parameter type " << type;
611 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100612 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100613 return Location();
614}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100615
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616void CodeGeneratorX86::Move32(Location destination, Location source) {
617 if (source.Equals(destination)) {
618 return;
619 }
620 if (destination.IsRegister()) {
621 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
626 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (destination.IsFpuRegister()) {
630 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 } else {
635 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000639 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000641 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100642 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000643 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500644 } else if (source.IsConstant()) {
645 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500647 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 } else {
649 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100650 __ pushl(Address(ESP, source.GetStackIndex()));
651 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 }
653 }
654}
655
656void CodeGeneratorX86::Move64(Location destination, Location source) {
657 if (source.Equals(destination)) {
658 return;
659 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100660 if (destination.IsRegisterPair()) {
661 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000662 EmitParallelMoves(
663 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
664 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100665 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000666 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100667 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
668 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else if (source.IsFpuRegister()) {
670 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000672 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100674 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
675 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
677 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500679 if (source.IsFpuRegister()) {
680 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
681 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 } else {
684 LOG(FATAL) << "Unimplemented";
685 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000687 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000689 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100690 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100692 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000695 } else if (source.IsConstant()) {
696 HConstant* constant = source.GetConstant();
697 int64_t value;
698 if (constant->IsLongConstant()) {
699 value = constant->AsLongConstant()->GetValue();
700 } else {
701 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000702 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000703 }
704 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
705 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000707 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000708 EmitParallelMoves(
709 Location::StackSlot(source.GetStackIndex()),
710 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100711 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000712 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100713 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
714 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 }
716 }
717}
718
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100719void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000720 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100721 if (instruction->IsCurrentMethod()) {
722 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
723 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100725 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000727 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
728 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000729 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000730 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000731 } else if (location.IsStackSlot()) {
732 __ movl(Address(ESP, location.GetStackIndex()), imm);
733 } else {
734 DCHECK(location.IsConstant());
735 DCHECK_EQ(location.GetConstant(), const_to_move);
736 }
737 } else if (const_to_move->IsLongConstant()) {
738 int64_t value = const_to_move->AsLongConstant()->GetValue();
739 if (location.IsRegisterPair()) {
740 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
741 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
742 } else if (location.IsDoubleStackSlot()) {
743 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000744 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
745 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000746 } else {
747 DCHECK(location.IsConstant());
748 DCHECK_EQ(location.GetConstant(), instruction);
749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000751 } else if (instruction->IsTemporary()) {
752 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000753 if (temp_location.IsStackSlot()) {
754 Move32(location, temp_location);
755 } else {
756 DCHECK(temp_location.IsDoubleStackSlot());
757 Move64(location, temp_location);
758 }
Roland Levillain476df552014-10-09 17:51:36 +0100759 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 switch (instruction->GetType()) {
762 case Primitive::kPrimBoolean:
763 case Primitive::kPrimByte:
764 case Primitive::kPrimChar:
765 case Primitive::kPrimShort:
766 case Primitive::kPrimInt:
767 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 case Primitive::kPrimFloat:
769 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 break;
771
772 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100773 case Primitive::kPrimDouble:
774 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 break;
776
777 default:
778 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
779 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100781 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100782 switch (instruction->GetType()) {
783 case Primitive::kPrimBoolean:
784 case Primitive::kPrimByte:
785 case Primitive::kPrimChar:
786 case Primitive::kPrimShort:
787 case Primitive::kPrimInt:
788 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000790 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 break;
792
793 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000795 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796 break;
797
798 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100799 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801 }
802}
803
804void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000806}
807
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000808void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000809 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100810 DCHECK(!successor->IsExitBlock());
811
812 HBasicBlock* block = got->GetBlock();
813 HInstruction* previous = got->GetPrevious();
814
815 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000816 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100817 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
818 return;
819 }
820
821 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
822 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
823 }
824 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000826 }
827}
828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000831}
832
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700834 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000835}
836
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700837void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
838 Label* true_target,
839 Label* false_target,
840 Label* always_true_target) {
841 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 if (cond->IsIntConstant()) {
843 // Constant condition, statically compared against 1.
844 int32_t cond_value = cond->AsIntConstant()->GetValue();
845 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846 if (always_true_target != nullptr) {
847 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100851 DCHECK_EQ(cond_value, 0);
852 }
853 } else {
854 bool materialized =
855 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
856 // Moves do not affect the eflags register, so if the condition is
857 // evaluated just before the if, we don't need to evaluate it
858 // again.
859 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 if (materialized) {
862 if (!eflags_set) {
863 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700864 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500866 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
868 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
869 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700870 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700872 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100873 }
874 } else {
875 Location lhs = cond->GetLocations()->InAt(0);
876 Location rhs = cond->GetLocations()->InAt(1);
877 // LHS is guaranteed to be in a register (see
878 // LocationsBuilderX86::VisitCondition).
879 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100881 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100882 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500883 if (constant == 0) {
884 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
885 } else {
886 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
887 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100890 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700891 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700892 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700894 if (false_target != nullptr) {
895 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896 }
897}
898
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700899void LocationsBuilderX86::VisitIf(HIf* if_instr) {
900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
902 HInstruction* cond = if_instr->InputAt(0);
903 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
904 locations->SetInAt(0, Location::Any());
905 }
906}
907
908void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
909 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
910 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
911 Label* always_true_target = true_target;
912 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
913 if_instr->IfTrueSuccessor())) {
914 always_true_target = nullptr;
915 }
916 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
917 if_instr->IfFalseSuccessor())) {
918 false_target = nullptr;
919 }
920 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
921}
922
923void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
924 LocationSummary* locations = new (GetGraph()->GetArena())
925 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
926 HInstruction* cond = deoptimize->InputAt(0);
927 DCHECK(cond->IsCondition());
928 if (cond->AsCondition()->NeedsMaterialization()) {
929 locations->SetInAt(0, Location::Any());
930 }
931}
932
933void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
934 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
935 DeoptimizationSlowPathX86(deoptimize);
936 codegen_->AddSlowPath(slow_path);
937 Label* slow_path_entry = slow_path->GetEntryLabel();
938 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
939}
940
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000941void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000942 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000943}
944
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000945void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
946 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000947}
948
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100950 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000951}
952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100954 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700955 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000956}
957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100958void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 LocationSummary* locations =
960 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100961 switch (store->InputAt(1)->GetType()) {
962 case Primitive::kPrimBoolean:
963 case Primitive::kPrimByte:
964 case Primitive::kPrimChar:
965 case Primitive::kPrimShort:
966 case Primitive::kPrimInt:
967 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100969 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
970 break;
971
972 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100974 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
975 break;
976
977 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100979 }
980 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981}
982
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000985}
986
Dave Allison20dfc792014-06-16 20:44:29 -0700987void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100988 LocationSummary* locations =
989 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100990 locations->SetInAt(0, Location::RequiresRegister());
991 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100992 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500993 // We need a byte register.
994 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100995 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000996}
997
Dave Allison20dfc792014-06-16 20:44:29 -0700998void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
999 if (comp->NeedsMaterialization()) {
1000 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001001 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001002 // Clear register: setcc only sets the low byte.
1003 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001004 Location lhs = locations->InAt(0);
1005 Location rhs = locations->InAt(1);
1006 if (rhs.IsRegister()) {
1007 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1008 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001009 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001010 if (constant == 0) {
1011 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1012 } else {
1013 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1014 }
Dave Allison20dfc792014-06-16 20:44:29 -07001015 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001016 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001017 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001018 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001019 }
Dave Allison20dfc792014-06-16 20:44:29 -07001020}
1021
1022void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1047 VisitCondition(comp);
1048}
1049
1050void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1051 VisitCondition(comp);
1052}
1053
1054void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1055 VisitCondition(comp);
1056}
1057
1058void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1059 VisitCondition(comp);
1060}
1061
1062void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1063 VisitCondition(comp);
1064}
1065
1066void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1067 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001068}
1069
1070void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 LocationSummary* locations =
1072 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001073 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001074}
1075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001076void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001078 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001079}
1080
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001081void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1082 LocationSummary* locations =
1083 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1084 locations->SetOut(Location::ConstantLocation(constant));
1085}
1086
1087void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1088 // Will be generated at use site.
1089 UNUSED(constant);
1090}
1091
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001093 LocationSummary* locations =
1094 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001095 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096}
1097
1098void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1099 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001100 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101}
1102
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001103void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1104 LocationSummary* locations =
1105 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1106 locations->SetOut(Location::ConstantLocation(constant));
1107}
1108
1109void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1110 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001111 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001112}
1113
1114void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1117 locations->SetOut(Location::ConstantLocation(constant));
1118}
1119
1120void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1121 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123}
1124
Calin Juravle27df7582015-04-17 19:12:31 +01001125void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1126 memory_barrier->SetLocations(nullptr);
1127}
1128
1129void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1130 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1131}
1132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001134 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135}
1136
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001139 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001140}
1141
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001142void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001143 LocationSummary* locations =
1144 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001145 switch (ret->InputAt(0)->GetType()) {
1146 case Primitive::kPrimBoolean:
1147 case Primitive::kPrimByte:
1148 case Primitive::kPrimChar:
1149 case Primitive::kPrimShort:
1150 case Primitive::kPrimInt:
1151 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154
1155 case Primitive::kPrimLong:
1156 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 break;
1159
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 case Primitive::kPrimFloat:
1161 case Primitive::kPrimDouble:
1162 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 break;
1165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169}
1170
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001171void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 if (kIsDebugBuild) {
1173 switch (ret->InputAt(0)->GetType()) {
1174 case Primitive::kPrimBoolean:
1175 case Primitive::kPrimByte:
1176 case Primitive::kPrimChar:
1177 case Primitive::kPrimShort:
1178 case Primitive::kPrimInt:
1179 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001180 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1185 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 break;
1187
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001188 case Primitive::kPrimFloat:
1189 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), 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 }
1196 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001200void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001201 // When we do not run baseline, explicit clinit checks triggered by static
1202 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1203 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001204
Mark Mendellfb8d2792015-03-31 22:16:59 -04001205 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001206 if (intrinsic.TryDispatch(invoke)) {
1207 return;
1208 }
1209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 HandleInvoke(invoke);
1211}
1212
Mark Mendell09ed1a32015-03-25 08:30:06 -04001213static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1214 if (invoke->GetLocations()->Intrinsified()) {
1215 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1216 intrinsic.Dispatch(invoke);
1217 return true;
1218 }
1219 return false;
1220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001223 // When we do not run baseline, explicit clinit checks triggered by static
1224 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1225 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1228 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001229 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230
Mark Mendell09ed1a32015-03-25 08:30:06 -04001231 codegen_->GenerateStaticOrDirectCall(
1232 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001233 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234}
1235
1236void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1237 HandleInvoke(invoke);
1238}
1239
1240void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001243 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001244
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001245 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001246 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001247 HInstruction* input = invoke->InputAt(i);
1248 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1249 }
1250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 switch (invoke->GetType()) {
1252 case Primitive::kPrimBoolean:
1253 case Primitive::kPrimByte:
1254 case Primitive::kPrimChar:
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001262 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 break;
1264
1265 case Primitive::kPrimVoid:
1266 break;
1267
1268 case Primitive::kPrimDouble:
1269 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001270 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 break;
1272 }
1273
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001274 invoke->SetLocations(locations);
1275}
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1280 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
1286 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
1288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 // temp = temp->GetMethodAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001295 __ call(Address(
1296 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001300}
1301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1303 HandleInvoke(invoke);
1304 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001305 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001306}
1307
1308void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001311 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1312 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1313 LocationSummary* locations = invoke->GetLocations();
1314 Location receiver = locations->InAt(0);
1315 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1316
1317 // Set the hidden argument.
1318 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
1323 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
1325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 // temp = temp->GetImtEntryAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001332 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001334
1335 DCHECK(!codegen_->IsLeafMethod());
1336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1337}
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001344 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 break;
1348
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001350 locations->SetInAt(0, Location::RequiresFpuRegister());
1351 locations->SetOut(Location::SameAsFirstInput());
1352 locations->AddTemp(Location::RequiresRegister());
1353 locations->AddTemp(Location::RequiresFpuRegister());
1354 break;
1355
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001357 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001358 locations->SetOut(Location::SameAsFirstInput());
1359 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1364 }
1365}
1366
1367void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1368 LocationSummary* locations = neg->GetLocations();
1369 Location out = locations->Out();
1370 Location in = locations->InAt(0);
1371 switch (neg->GetResultType()) {
1372 case Primitive::kPrimInt:
1373 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001374 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001376 break;
1377
1378 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001379 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 __ negl(out.AsRegisterPairLow<Register>());
1382 // Negation is similar to subtraction from zero. The least
1383 // significant byte triggers a borrow when it is different from
1384 // zero; to take it into account, add 1 to the most significant
1385 // byte if the carry flag (CF) is set to 1 after the first NEGL
1386 // operation.
1387 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1388 __ negl(out.AsRegisterPairHigh<Register>());
1389 break;
1390
Roland Levillain5368c212014-11-27 15:03:41 +00001391 case Primitive::kPrimFloat: {
1392 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 Register constant = locations->GetTemp(0).AsRegister<Register>();
1394 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001395 // Implement float negation with an exclusive or with value
1396 // 0x80000000 (mask for bit 31, representing the sign of a
1397 // single-precision floating-point number).
1398 __ movl(constant, Immediate(INT32_C(0x80000000)));
1399 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001402 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403
Roland Levillain5368c212014-11-27 15:03:41 +00001404 case Primitive::kPrimDouble: {
1405 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001407 // Implement double negation with an exclusive or with value
1408 // 0x8000000000000000 (mask for bit 63, representing the sign of
1409 // a double-precision floating-point number).
1410 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001411 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001412 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001413 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001414
1415 default:
1416 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1417 }
1418}
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001421 Primitive::Type result_type = conversion->GetResultType();
1422 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001423 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001424
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001425 // The float-to-long and double-to-long type conversions rely on a
1426 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001427 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1429 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001430 ? LocationSummary::kCall
1431 : LocationSummary::kNoCall;
1432 LocationSummary* locations =
1433 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1434
David Brazdilb2bd1c52015-03-25 11:17:37 +00001435 // The Java language does not allow treating boolean as an integral type but
1436 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001437
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimByte:
1440 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001441 case Primitive::kPrimBoolean:
1442 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1448 // Make the output overlap to please the register allocator. This greatly simplifies
1449 // the validation of the linear scan implementation
1450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unexpected type conversion from " << input_type
1455 << " to " << result_type;
1456 }
1457 break;
1458
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimShort:
1460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001461 case Primitive::kPrimBoolean:
1462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001463 case Primitive::kPrimByte:
1464 case Primitive::kPrimInt:
1465 case Primitive::kPrimChar:
1466 // Processing a Dex `int-to-short' instruction.
1467 locations->SetInAt(0, Location::Any());
1468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillain946e1432014-11-11 17:35:19 +00001477 case Primitive::kPrimInt:
1478 switch (input_type) {
1479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001481 locations->SetInAt(0, Location::Any());
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 break;
1484
1485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001486 // Processing a Dex `float-to-int' instruction.
1487 locations->SetInAt(0, Location::RequiresFpuRegister());
1488 locations->SetOut(Location::RequiresRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 break;
1491
Roland Levillain946e1432014-11-11 17:35:19 +00001492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001493 // Processing a Dex `double-to-int' instruction.
1494 locations->SetInAt(0, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::RequiresRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503 break;
1504
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimLong:
1506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001507 case Primitive::kPrimBoolean:
1508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 case Primitive::kPrimByte:
1510 case Primitive::kPrimShort:
1511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 locations->SetInAt(0, Location::RegisterLocation(EAX));
1515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1516 break;
1517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1524
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 // The runtime helper puts the result in EAX, EDX.
1526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
1534 break;
1535
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimChar:
1537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001538 case Primitive::kPrimBoolean:
1539 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001540 case Primitive::kPrimByte:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001543 // Processing a Dex `int-to-char' instruction.
1544 locations->SetInAt(0, Location::Any());
1545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1546 break;
1547
1548 default:
1549 LOG(FATAL) << "Unexpected type conversion from " << input_type
1550 << " to " << result_type;
1551 }
1552 break;
1553
Roland Levillaindff1f282014-11-05 14:15:05 +00001554 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001555 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001556 case Primitive::kPrimBoolean:
1557 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001558 case Primitive::kPrimByte:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimChar:
1562 // Processing a Dex `int-to-float' instruction.
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetOut(Location::RequiresFpuRegister());
1565 break;
1566
1567 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001568 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001569 locations->SetInAt(0, Location::Any());
1570 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001571 break;
1572
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001574 // Processing a Dex `double-to-float' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 };
1583 break;
1584
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001587 case Primitive::kPrimBoolean:
1588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimByte:
1590 case Primitive::kPrimShort:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-double' instruction.
1594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetOut(Location::RequiresFpuRegister());
1596 break;
1597
1598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001600 locations->SetInAt(0, Location::Any());
1601 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001602 break;
1603
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001605 // Processing a Dex `float-to-double' instruction.
1606 locations->SetInAt(0, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620}
1621
1622void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1623 LocationSummary* locations = conversion->GetLocations();
1624 Location out = locations->Out();
1625 Location in = locations->InAt(0);
1626 Primitive::Type result_type = conversion->GetResultType();
1627 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001628 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimByte:
1631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001632 case Primitive::kPrimBoolean:
1633 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001637 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001638 if (in.IsRegister()) {
1639 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 } else {
1641 DCHECK(in.GetConstant()->IsIntConstant());
1642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1644 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001645 break;
1646
1647 default:
1648 LOG(FATAL) << "Unexpected type conversion from " << input_type
1649 << " to " << result_type;
1650 }
1651 break;
1652
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimShort:
1654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001655 case Primitive::kPrimBoolean:
1656 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001657 case Primitive::kPrimByte:
1658 case Primitive::kPrimInt:
1659 case Primitive::kPrimChar:
1660 // Processing a Dex `int-to-short' instruction.
1661 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else {
1666 DCHECK(in.GetConstant()->IsIntConstant());
1667 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001669 }
1670 break;
1671
1672 default:
1673 LOG(FATAL) << "Unexpected type conversion from " << input_type
1674 << " to " << result_type;
1675 }
1676 break;
1677
Roland Levillain946e1432014-11-11 17:35:19 +00001678 case Primitive::kPrimInt:
1679 switch (input_type) {
1680 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001681 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001682 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001684 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else {
1687 DCHECK(in.IsConstant());
1688 DCHECK(in.GetConstant()->IsLongConstant());
1689 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001691 }
1692 break;
1693
Roland Levillain3f8f9362014-12-02 17:45:01 +00001694 case Primitive::kPrimFloat: {
1695 // Processing a Dex `float-to-int' instruction.
1696 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1697 Register output = out.AsRegister<Register>();
1698 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1699 Label done, nan;
1700
1701 __ movl(output, Immediate(kPrimIntMax));
1702 // temp = int-to-float(output)
1703 __ cvtsi2ss(temp, output);
1704 // if input >= temp goto done
1705 __ comiss(input, temp);
1706 __ j(kAboveEqual, &done);
1707 // if input == NaN goto nan
1708 __ j(kUnordered, &nan);
1709 // output = float-to-int-truncate(input)
1710 __ cvttss2si(output, input);
1711 __ jmp(&done);
1712 __ Bind(&nan);
1713 // output = 0
1714 __ xorl(output, output);
1715 __ Bind(&done);
1716 break;
1717 }
1718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 case Primitive::kPrimDouble: {
1720 // Processing a Dex `double-to-int' instruction.
1721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1722 Register output = out.AsRegister<Register>();
1723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1724 Label done, nan;
1725
1726 __ movl(output, Immediate(kPrimIntMax));
1727 // temp = int-to-double(output)
1728 __ cvtsi2sd(temp, output);
1729 // if input >= temp goto done
1730 __ comisd(input, temp);
1731 __ j(kAboveEqual, &done);
1732 // if input == NaN goto nan
1733 __ j(kUnordered, &nan);
1734 // output = double-to-int-truncate(input)
1735 __ cvttsd2si(output, input);
1736 __ jmp(&done);
1737 __ Bind(&nan);
1738 // output = 0
1739 __ xorl(output, output);
1740 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001741 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001742 }
Roland Levillain946e1432014-11-11 17:35:19 +00001743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimLong:
1751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001752 case Primitive::kPrimBoolean:
1753 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Primitive::kPrimByte:
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001757 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001758 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001759 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1760 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 __ cdq();
1763 break;
1764
1765 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001766 // Processing a Dex `float-to-long' instruction.
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001768 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1769 break;
1770
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001772 // Processing a Dex `double-to-long' instruction.
1773 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1774 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 break;
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type conversion from " << input_type
1779 << " to " << result_type;
1780 }
1781 break;
1782
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimChar:
1784 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001785 case Primitive::kPrimBoolean:
1786 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimByte:
1788 case Primitive::kPrimShort:
1789 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1791 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001792 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001793 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else {
1796 DCHECK(in.GetConstant()->IsIntConstant());
1797 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001799 }
1800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
Roland Levillain6d0e4832014-11-27 18:31:21 +00001820 case Primitive::kPrimLong: {
1821 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001822 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001823
Roland Levillain232ade02015-04-20 15:14:36 +01001824 // Create stack space for the call to
1825 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1826 // TODO: enhance register allocator to ask for stack temporaries.
1827 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1828 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1829 __ subl(ESP, Immediate(adjustment));
1830 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831
Roland Levillain232ade02015-04-20 15:14:36 +01001832 // Load the value to the FP stack, using temporaries if needed.
1833 PushOntoFPStack(in, 0, adjustment, false, true);
1834
1835 if (out.IsStackSlot()) {
1836 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1837 } else {
1838 __ fstps(Address(ESP, 0));
1839 Location stack_temp = Location::StackSlot(0);
1840 codegen_->Move32(out, stack_temp);
1841 }
1842
1843 // Remove the temporary stack space we allocated.
1844 if (adjustment != 0) {
1845 __ addl(ESP, Immediate(adjustment));
1846 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 break;
1848 }
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 };
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001869 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001871 break;
1872
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 case Primitive::kPrimLong: {
1874 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001875 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876
Roland Levillain232ade02015-04-20 15:14:36 +01001877 // Create stack space for the call to
1878 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1879 // TODO: enhance register allocator to ask for stack temporaries.
1880 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1881 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1882 __ subl(ESP, Immediate(adjustment));
1883 }
1884
1885 // Load the value to the FP stack, using temporaries if needed.
1886 PushOntoFPStack(in, 0, adjustment, false, true);
1887
1888 if (out.IsDoubleStackSlot()) {
1889 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1890 } else {
1891 __ fstpl(Address(ESP, 0));
1892 Location stack_temp = Location::DoubleStackSlot(0);
1893 codegen_->Move64(out, stack_temp);
1894 }
1895
1896 // Remove the temporary stack space we allocated.
1897 if (adjustment != 0) {
1898 __ addl(ESP, Immediate(adjustment));
1899 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001900 break;
1901 }
1902
Roland Levillaincff13742014-11-17 14:32:17 +00001903 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001904 // Processing a Dex `float-to-double' instruction.
1905 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918}
1919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001923 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001924 case Primitive::kPrimInt: {
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1928 break;
1929 }
1930
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetInAt(1, Location::Any());
1934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936 }
1937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 case Primitive::kPrimFloat:
1939 case Primitive::kPrimDouble: {
1940 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001941 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1948 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950}
1951
1952void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1953 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 Location first = locations->InAt(0);
1955 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001956 Location out = locations->Out();
1957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001958 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1962 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1963 } else {
1964 __ leal(out.AsRegister<Register>(), Address(
1965 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1966 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001968 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1969 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1970 __ addl(out.AsRegister<Register>(), Immediate(value));
1971 } else {
1972 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1973 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001975 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001976 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001977 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001978 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001979 }
1980
1981 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001982 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1984 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001985 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1987 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001988 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001989 } else {
1990 DCHECK(second.IsConstant()) << second;
1991 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1992 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1993 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001994 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001995 break;
1996 }
1997
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001998 case Primitive::kPrimFloat: {
1999 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002002 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 }
2004
2005 case Primitive::kPrimDouble: {
2006 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002008 }
2009 break;
2010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002013 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 }
2015}
2016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002018 LocationSummary* locations =
2019 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002020 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002021 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002022 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetInAt(1, Location::Any());
2025 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026 break;
2027 }
Calin Juravle11351682014-10-23 15:38:15 +01002028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble: {
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002033 break;
Calin Juravle11351682014-10-23 15:38:15 +01002034 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036 default:
Calin Juravle11351682014-10-23 15:38:15 +01002037 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002039}
2040
2041void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2042 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002043 Location first = locations->InAt(0);
2044 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002045 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002051 __ subl(first.AsRegister<Register>(),
2052 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002055 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002056 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002057 }
2058
2059 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002060 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002061 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2062 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002063 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002064 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 __ sbbl(first.AsRegisterPairHigh<Register>(),
2066 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002067 } else {
2068 DCHECK(second.IsConstant()) << second;
2069 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2070 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2071 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002073 break;
2074 }
2075
Calin Juravle11351682014-10-23 15:38:15 +01002076 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002078 break;
Calin Juravle11351682014-10-23 15:38:15 +01002079 }
2080
2081 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002083 break;
2084 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002086 default:
Calin Juravle11351682014-10-23 15:38:15 +01002087 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002088 }
2089}
2090
Calin Juravle34bacdf2014-10-07 20:23:36 +01002091void LocationsBuilderX86::VisitMul(HMul* mul) {
2092 LocationSummary* locations =
2093 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2094 switch (mul->GetResultType()) {
2095 case Primitive::kPrimInt:
2096 locations->SetInAt(0, Location::RequiresRegister());
2097 locations->SetInAt(1, Location::Any());
2098 locations->SetOut(Location::SameAsFirstInput());
2099 break;
2100 case Primitive::kPrimLong: {
2101 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002102 locations->SetInAt(1, Location::Any());
2103 locations->SetOut(Location::SameAsFirstInput());
2104 // Needed for imul on 32bits with 64bits output.
2105 locations->AddTemp(Location::RegisterLocation(EAX));
2106 locations->AddTemp(Location::RegisterLocation(EDX));
2107 break;
2108 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002109 case Primitive::kPrimFloat:
2110 case Primitive::kPrimDouble: {
2111 locations->SetInAt(0, Location::RequiresFpuRegister());
2112 locations->SetInAt(1, Location::RequiresFpuRegister());
2113 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002115 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116
2117 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002119 }
2120}
2121
2122void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2123 LocationSummary* locations = mul->GetLocations();
2124 Location first = locations->InAt(0);
2125 Location second = locations->InAt(1);
2126 DCHECK(first.Equals(locations->Out()));
2127
2128 switch (mul->GetResultType()) {
2129 case Primitive::kPrimInt: {
2130 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002132 } else if (second.IsConstant()) {
2133 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 } else {
2136 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002138 }
2139 break;
2140 }
2141
2142 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143 Register in1_hi = first.AsRegisterPairHigh<Register>();
2144 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 Register eax = locations->GetTemp(0).AsRegister<Register>();
2146 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147
2148 DCHECK_EQ(EAX, eax);
2149 DCHECK_EQ(EDX, edx);
2150
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002151 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002152 // output: in1
2153 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2154 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2155 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002156 if (second.IsConstant()) {
2157 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002158
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002159 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2160 int32_t low_value = Low32Bits(value);
2161 int32_t high_value = High32Bits(value);
2162 Immediate low(low_value);
2163 Immediate high(high_value);
2164
2165 __ movl(eax, high);
2166 // eax <- in1.lo * in2.hi
2167 __ imull(eax, in1_lo);
2168 // in1.hi <- in1.hi * in2.lo
2169 __ imull(in1_hi, low);
2170 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2171 __ addl(in1_hi, eax);
2172 // move in2_lo to eax to prepare for double precision
2173 __ movl(eax, low);
2174 // edx:eax <- in1.lo * in2.lo
2175 __ mull(in1_lo);
2176 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2177 __ addl(in1_hi, edx);
2178 // in1.lo <- (in1.lo * in2.lo)[31:0];
2179 __ movl(in1_lo, eax);
2180 } else if (second.IsRegisterPair()) {
2181 Register in2_hi = second.AsRegisterPairHigh<Register>();
2182 Register in2_lo = second.AsRegisterPairLow<Register>();
2183
2184 __ movl(eax, in2_hi);
2185 // eax <- in1.lo * in2.hi
2186 __ imull(eax, in1_lo);
2187 // in1.hi <- in1.hi * in2.lo
2188 __ imull(in1_hi, in2_lo);
2189 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2190 __ addl(in1_hi, eax);
2191 // move in1_lo to eax to prepare for double precision
2192 __ movl(eax, in1_lo);
2193 // edx:eax <- in1.lo * in2.lo
2194 __ mull(in2_lo);
2195 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2196 __ addl(in1_hi, edx);
2197 // in1.lo <- (in1.lo * in2.lo)[31:0];
2198 __ movl(in1_lo, eax);
2199 } else {
2200 DCHECK(second.IsDoubleStackSlot()) << second;
2201 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2202 Address in2_lo(ESP, second.GetStackIndex());
2203
2204 __ movl(eax, in2_hi);
2205 // eax <- in1.lo * in2.hi
2206 __ imull(eax, in1_lo);
2207 // in1.hi <- in1.hi * in2.lo
2208 __ imull(in1_hi, in2_lo);
2209 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2210 __ addl(in1_hi, eax);
2211 // move in1_lo to eax to prepare for double precision
2212 __ movl(eax, in1_lo);
2213 // edx:eax <- in1.lo * in2.lo
2214 __ mull(in2_lo);
2215 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2216 __ addl(in1_hi, edx);
2217 // in1.lo <- (in1.lo * in2.lo)[31:0];
2218 __ movl(in1_lo, eax);
2219 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002220
2221 break;
2222 }
2223
Calin Juravleb5bfa962014-10-21 18:02:24 +01002224 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002225 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002226 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 }
2228
2229 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 break;
2232 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002233
2234 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002236 }
2237}
2238
Roland Levillain232ade02015-04-20 15:14:36 +01002239void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2240 uint32_t temp_offset,
2241 uint32_t stack_adjustment,
2242 bool is_fp,
2243 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002244 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002245 DCHECK(!is_wide);
2246 if (is_fp) {
2247 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2248 } else {
2249 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2250 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002251 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002252 DCHECK(is_wide);
2253 if (is_fp) {
2254 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2255 } else {
2256 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2257 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 } else {
2259 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002260 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 Location stack_temp = Location::StackSlot(temp_offset);
2262 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002263 if (is_fp) {
2264 __ flds(Address(ESP, temp_offset));
2265 } else {
2266 __ filds(Address(ESP, temp_offset));
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 } else {
2269 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2270 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002271 if (is_fp) {
2272 __ fldl(Address(ESP, temp_offset));
2273 } else {
2274 __ fildl(Address(ESP, temp_offset));
2275 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002276 }
2277 }
2278}
2279
2280void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2281 Primitive::Type type = rem->GetResultType();
2282 bool is_float = type == Primitive::kPrimFloat;
2283 size_t elem_size = Primitive::ComponentSize(type);
2284 LocationSummary* locations = rem->GetLocations();
2285 Location first = locations->InAt(0);
2286 Location second = locations->InAt(1);
2287 Location out = locations->Out();
2288
2289 // Create stack space for 2 elements.
2290 // TODO: enhance register allocator to ask for stack temporaries.
2291 __ subl(ESP, Immediate(2 * elem_size));
2292
2293 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 const bool is_wide = !is_float;
2295 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2296 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002297
2298 // Loop doing FPREM until we stabilize.
2299 Label retry;
2300 __ Bind(&retry);
2301 __ fprem();
2302
2303 // Move FP status to AX.
2304 __ fstsw();
2305
2306 // And see if the argument reduction is complete. This is signaled by the
2307 // C2 FPU flag bit set to 0.
2308 __ andl(EAX, Immediate(kC2ConditionMask));
2309 __ j(kNotEqual, &retry);
2310
2311 // We have settled on the final value. Retrieve it into an XMM register.
2312 // Store FP top of stack to real stack.
2313 if (is_float) {
2314 __ fsts(Address(ESP, 0));
2315 } else {
2316 __ fstl(Address(ESP, 0));
2317 }
2318
2319 // Pop the 2 items from the FP stack.
2320 __ fucompp();
2321
2322 // Load the value from the stack into an XMM register.
2323 DCHECK(out.IsFpuRegister()) << out;
2324 if (is_float) {
2325 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2326 } else {
2327 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2328 }
2329
2330 // And remove the temporary stack space we allocated.
2331 __ addl(ESP, Immediate(2 * elem_size));
2332}
2333
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002334
2335void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2336 DCHECK(instruction->IsDiv() || instruction->IsRem());
2337
2338 LocationSummary* locations = instruction->GetLocations();
2339 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002340 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002341
2342 Register out_register = locations->Out().AsRegister<Register>();
2343 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002344 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002345
2346 DCHECK(imm == 1 || imm == -1);
2347
2348 if (instruction->IsRem()) {
2349 __ xorl(out_register, out_register);
2350 } else {
2351 __ movl(out_register, input_register);
2352 if (imm == -1) {
2353 __ negl(out_register);
2354 }
2355 }
2356}
2357
2358
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002359void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002360 LocationSummary* locations = instruction->GetLocations();
2361
2362 Register out_register = locations->Out().AsRegister<Register>();
2363 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002364 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002365
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002366 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002367 Register num = locations->GetTemp(0).AsRegister<Register>();
2368
2369 __ leal(num, Address(input_register, std::abs(imm) - 1));
2370 __ testl(input_register, input_register);
2371 __ cmovl(kGreaterEqual, num, input_register);
2372 int shift = CTZ(imm);
2373 __ sarl(num, Immediate(shift));
2374
2375 if (imm < 0) {
2376 __ negl(num);
2377 }
2378
2379 __ movl(out_register, num);
2380}
2381
2382void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2383 DCHECK(instruction->IsDiv() || instruction->IsRem());
2384
2385 LocationSummary* locations = instruction->GetLocations();
2386 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2387
2388 Register eax = locations->InAt(0).AsRegister<Register>();
2389 Register out = locations->Out().AsRegister<Register>();
2390 Register num;
2391 Register edx;
2392
2393 if (instruction->IsDiv()) {
2394 edx = locations->GetTemp(0).AsRegister<Register>();
2395 num = locations->GetTemp(1).AsRegister<Register>();
2396 } else {
2397 edx = locations->Out().AsRegister<Register>();
2398 num = locations->GetTemp(0).AsRegister<Register>();
2399 }
2400
2401 DCHECK_EQ(EAX, eax);
2402 DCHECK_EQ(EDX, edx);
2403 if (instruction->IsDiv()) {
2404 DCHECK_EQ(EAX, out);
2405 } else {
2406 DCHECK_EQ(EDX, out);
2407 }
2408
2409 int64_t magic;
2410 int shift;
2411 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2412
2413 Label ndiv;
2414 Label end;
2415 // If numerator is 0, the result is 0, no computation needed.
2416 __ testl(eax, eax);
2417 __ j(kNotEqual, &ndiv);
2418
2419 __ xorl(out, out);
2420 __ jmp(&end);
2421
2422 __ Bind(&ndiv);
2423
2424 // Save the numerator.
2425 __ movl(num, eax);
2426
2427 // EAX = magic
2428 __ movl(eax, Immediate(magic));
2429
2430 // EDX:EAX = magic * numerator
2431 __ imull(num);
2432
2433 if (imm > 0 && magic < 0) {
2434 // EDX += num
2435 __ addl(edx, num);
2436 } else if (imm < 0 && magic > 0) {
2437 __ subl(edx, num);
2438 }
2439
2440 // Shift if needed.
2441 if (shift != 0) {
2442 __ sarl(edx, Immediate(shift));
2443 }
2444
2445 // EDX += 1 if EDX < 0
2446 __ movl(eax, edx);
2447 __ shrl(edx, Immediate(31));
2448 __ addl(edx, eax);
2449
2450 if (instruction->IsRem()) {
2451 __ movl(eax, num);
2452 __ imull(edx, Immediate(imm));
2453 __ subl(eax, edx);
2454 __ movl(edx, eax);
2455 } else {
2456 __ movl(eax, edx);
2457 }
2458 __ Bind(&end);
2459}
2460
Calin Juravlebacfec32014-11-14 15:54:36 +00002461void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2462 DCHECK(instruction->IsDiv() || instruction->IsRem());
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location out = locations->Out();
2466 Location first = locations->InAt(0);
2467 Location second = locations->InAt(1);
2468 bool is_div = instruction->IsDiv();
2469
2470 switch (instruction->GetResultType()) {
2471 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 DCHECK_EQ(EAX, first.AsRegister<Register>());
2473 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002474
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002475 if (instruction->InputAt(1)->IsIntConstant()) {
2476 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002477
2478 if (imm == 0) {
2479 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2480 } else if (imm == 1 || imm == -1) {
2481 DivRemOneOrMinusOne(instruction);
2482 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002483 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002484 } else {
2485 DCHECK(imm <= -2 || imm >= 2);
2486 GenerateDivRemWithAnyConstant(instruction);
2487 }
2488 } else {
2489 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002490 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002491 is_div);
2492 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002493
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494 Register second_reg = second.AsRegister<Register>();
2495 // 0x80000000/-1 triggers an arithmetic exception!
2496 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2497 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002498
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002499 __ cmpl(second_reg, Immediate(-1));
2500 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002501
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002502 // edx:eax <- sign-extended of eax
2503 __ cdq();
2504 // eax = quotient, edx = remainder
2505 __ idivl(second_reg);
2506 __ Bind(slow_path->GetExitLabel());
2507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002508 break;
2509 }
2510
2511 case Primitive::kPrimLong: {
2512 InvokeRuntimeCallingConvention calling_convention;
2513 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2514 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2516 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2517 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2518 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2519
2520 if (is_div) {
2521 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2522 } else {
2523 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2524 }
2525 uint32_t dex_pc = is_div
2526 ? instruction->AsDiv()->GetDexPc()
2527 : instruction->AsRem()->GetDexPc();
2528 codegen_->RecordPcInfo(instruction, dex_pc);
2529
2530 break;
2531 }
2532
2533 default:
2534 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2535 }
2536}
2537
Calin Juravle7c4954d2014-10-28 16:57:40 +00002538void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002539 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002540 ? LocationSummary::kCall
2541 : LocationSummary::kNoCall;
2542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2543
Calin Juravle7c4954d2014-10-28 16:57:40 +00002544 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002545 case Primitive::kPrimInt: {
2546 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002548 locations->SetOut(Location::SameAsFirstInput());
2549 // Intel uses edx:eax as the dividend.
2550 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2552 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2553 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002554 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 locations->AddTemp(Location::RequiresRegister());
2556 }
Calin Juravled0d48522014-11-04 16:40:20 +00002557 break;
2558 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002560 InvokeRuntimeCallingConvention calling_convention;
2561 locations->SetInAt(0, Location::RegisterPairLocation(
2562 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2563 locations->SetInAt(1, Location::RegisterPairLocation(
2564 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2565 // Runtime helper puts the result in EAX, EDX.
2566 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 break;
2568 }
2569 case Primitive::kPrimFloat:
2570 case Primitive::kPrimDouble: {
2571 locations->SetInAt(0, Location::RequiresFpuRegister());
2572 locations->SetInAt(1, Location::RequiresFpuRegister());
2573 locations->SetOut(Location::SameAsFirstInput());
2574 break;
2575 }
2576
2577 default:
2578 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2579 }
2580}
2581
2582void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2583 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002584 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002585 Location first = locations->InAt(0);
2586 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587
2588 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002589 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 break;
2593 }
2594
2595 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 break;
2599 }
2600
2601 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002602 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604 break;
2605 }
2606
2607 default:
2608 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2609 }
2610}
2611
Calin Juravlebacfec32014-11-14 15:54:36 +00002612void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002613 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002614
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002615 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2616 ? LocationSummary::kCall
2617 : LocationSummary::kNoCall;
2618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002619
Calin Juravled2ec87d2014-12-08 14:24:46 +00002620 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002621 case Primitive::kPrimInt: {
2622 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002623 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002624 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2626 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2627 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002628 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002629 locations->AddTemp(Location::RequiresRegister());
2630 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002631 break;
2632 }
2633 case Primitive::kPrimLong: {
2634 InvokeRuntimeCallingConvention calling_convention;
2635 locations->SetInAt(0, Location::RegisterPairLocation(
2636 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2637 locations->SetInAt(1, Location::RegisterPairLocation(
2638 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2639 // Runtime helper puts the result in EAX, EDX.
2640 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2641 break;
2642 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002643 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002644 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 locations->SetInAt(0, Location::Any());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::RequiresFpuRegister());
2648 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002649 break;
2650 }
2651
2652 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002653 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002654 }
2655}
2656
2657void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2658 Primitive::Type type = rem->GetResultType();
2659 switch (type) {
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimLong: {
2662 GenerateDivRemIntegral(rem);
2663 break;
2664 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002665 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002666 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002667 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002668 break;
2669 }
2670 default:
2671 LOG(FATAL) << "Unexpected rem type " << type;
2672 }
2673}
2674
Calin Juravled0d48522014-11-04 16:40:20 +00002675void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 LocationSummary* locations =
2677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002678 switch (instruction->GetType()) {
2679 case Primitive::kPrimInt: {
2680 locations->SetInAt(0, Location::Any());
2681 break;
2682 }
2683 case Primitive::kPrimLong: {
2684 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2685 if (!instruction->IsConstant()) {
2686 locations->AddTemp(Location::RequiresRegister());
2687 }
2688 break;
2689 }
2690 default:
2691 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2692 }
Calin Juravled0d48522014-11-04 16:40:20 +00002693 if (instruction->HasUses()) {
2694 locations->SetOut(Location::SameAsFirstInput());
2695 }
2696}
2697
2698void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2699 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2700 codegen_->AddSlowPath(slow_path);
2701
2702 LocationSummary* locations = instruction->GetLocations();
2703 Location value = locations->InAt(0);
2704
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002705 switch (instruction->GetType()) {
2706 case Primitive::kPrimInt: {
2707 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002709 __ j(kEqual, slow_path->GetEntryLabel());
2710 } else if (value.IsStackSlot()) {
2711 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2712 __ j(kEqual, slow_path->GetEntryLabel());
2713 } else {
2714 DCHECK(value.IsConstant()) << value;
2715 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2716 __ jmp(slow_path->GetEntryLabel());
2717 }
2718 }
2719 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002720 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002721 case Primitive::kPrimLong: {
2722 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002724 __ movl(temp, value.AsRegisterPairLow<Register>());
2725 __ orl(temp, value.AsRegisterPairHigh<Register>());
2726 __ j(kEqual, slow_path->GetEntryLabel());
2727 } else {
2728 DCHECK(value.IsConstant()) << value;
2729 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2730 __ jmp(slow_path->GetEntryLabel());
2731 }
2732 }
2733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002737 }
Calin Juravled0d48522014-11-04 16:40:20 +00002738}
2739
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2741 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2742
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2745
2746 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002747 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002748 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002749 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002750 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002751 // The shift count needs to be in CL or a constant.
2752 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002753 locations->SetOut(Location::SameAsFirstInput());
2754 break;
2755 }
2756 default:
2757 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2758 }
2759}
2760
2761void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2762 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2763
2764 LocationSummary* locations = op->GetLocations();
2765 Location first = locations->InAt(0);
2766 Location second = locations->InAt(1);
2767 DCHECK(first.Equals(locations->Out()));
2768
2769 switch (op->GetResultType()) {
2770 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002771 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002772 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002773 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002775 DCHECK_EQ(ECX, second_reg);
2776 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002777 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002778 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002779 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002780 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 }
2783 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002784 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2785 if (shift == 0) {
2786 return;
2787 }
2788 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002789 if (op->IsShl()) {
2790 __ shll(first_reg, imm);
2791 } else if (op->IsShr()) {
2792 __ sarl(first_reg, imm);
2793 } else {
2794 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795 }
2796 }
2797 break;
2798 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002799 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002800 if (second.IsRegister()) {
2801 Register second_reg = second.AsRegister<Register>();
2802 DCHECK_EQ(ECX, second_reg);
2803 if (op->IsShl()) {
2804 GenerateShlLong(first, second_reg);
2805 } else if (op->IsShr()) {
2806 GenerateShrLong(first, second_reg);
2807 } else {
2808 GenerateUShrLong(first, second_reg);
2809 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002810 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002811 // Shift by a constant.
2812 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2813 // Nothing to do if the shift is 0, as the input is already the output.
2814 if (shift != 0) {
2815 if (op->IsShl()) {
2816 GenerateShlLong(first, shift);
2817 } else if (op->IsShr()) {
2818 GenerateShrLong(first, shift);
2819 } else {
2820 GenerateUShrLong(first, shift);
2821 }
2822 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002823 }
2824 break;
2825 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002826 default:
2827 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2828 }
2829}
2830
Mark P Mendell73945692015-04-29 14:56:17 +00002831void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2832 Register low = loc.AsRegisterPairLow<Register>();
2833 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002834 if (shift == 1) {
2835 // This is just an addition.
2836 __ addl(low, low);
2837 __ adcl(high, high);
2838 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002839 // Shift by 32 is easy. High gets low, and low gets 0.
2840 codegen_->EmitParallelMoves(
2841 loc.ToLow(),
2842 loc.ToHigh(),
2843 Primitive::kPrimInt,
2844 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2845 loc.ToLow(),
2846 Primitive::kPrimInt);
2847 } else if (shift > 32) {
2848 // Low part becomes 0. High part is low part << (shift-32).
2849 __ movl(high, low);
2850 __ shll(high, Immediate(shift - 32));
2851 __ xorl(low, low);
2852 } else {
2853 // Between 1 and 31.
2854 __ shld(high, low, Immediate(shift));
2855 __ shll(low, Immediate(shift));
2856 }
2857}
2858
Calin Juravle9aec02f2014-11-18 23:06:35 +00002859void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2860 Label done;
2861 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2862 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2863 __ testl(shifter, Immediate(32));
2864 __ j(kEqual, &done);
2865 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2866 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2867 __ Bind(&done);
2868}
2869
Mark P Mendell73945692015-04-29 14:56:17 +00002870void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2871 Register low = loc.AsRegisterPairLow<Register>();
2872 Register high = loc.AsRegisterPairHigh<Register>();
2873 if (shift == 32) {
2874 // Need to copy the sign.
2875 DCHECK_NE(low, high);
2876 __ movl(low, high);
2877 __ sarl(high, Immediate(31));
2878 } else if (shift > 32) {
2879 DCHECK_NE(low, high);
2880 // High part becomes sign. Low part is shifted by shift - 32.
2881 __ movl(low, high);
2882 __ sarl(high, Immediate(31));
2883 __ sarl(low, Immediate(shift - 32));
2884 } else {
2885 // Between 1 and 31.
2886 __ shrd(low, high, Immediate(shift));
2887 __ sarl(high, Immediate(shift));
2888 }
2889}
2890
Calin Juravle9aec02f2014-11-18 23:06:35 +00002891void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2892 Label done;
2893 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2894 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2895 __ testl(shifter, Immediate(32));
2896 __ j(kEqual, &done);
2897 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2898 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2899 __ Bind(&done);
2900}
2901
Mark P Mendell73945692015-04-29 14:56:17 +00002902void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2903 Register low = loc.AsRegisterPairLow<Register>();
2904 Register high = loc.AsRegisterPairHigh<Register>();
2905 if (shift == 32) {
2906 // Shift by 32 is easy. Low gets high, and high gets 0.
2907 codegen_->EmitParallelMoves(
2908 loc.ToHigh(),
2909 loc.ToLow(),
2910 Primitive::kPrimInt,
2911 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2912 loc.ToHigh(),
2913 Primitive::kPrimInt);
2914 } else if (shift > 32) {
2915 // Low part is high >> (shift - 32). High part becomes 0.
2916 __ movl(low, high);
2917 __ shrl(low, Immediate(shift - 32));
2918 __ xorl(high, high);
2919 } else {
2920 // Between 1 and 31.
2921 __ shrd(low, high, Immediate(shift));
2922 __ shrl(high, Immediate(shift));
2923 }
2924}
2925
Calin Juravle9aec02f2014-11-18 23:06:35 +00002926void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2927 Label done;
2928 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2929 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2930 __ testl(shifter, Immediate(32));
2931 __ j(kEqual, &done);
2932 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2933 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2934 __ Bind(&done);
2935}
2936
2937void LocationsBuilderX86::VisitShl(HShl* shl) {
2938 HandleShift(shl);
2939}
2940
2941void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2942 HandleShift(shl);
2943}
2944
2945void LocationsBuilderX86::VisitShr(HShr* shr) {
2946 HandleShift(shr);
2947}
2948
2949void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2950 HandleShift(shr);
2951}
2952
2953void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2954 HandleShift(ushr);
2955}
2956
2957void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2958 HandleShift(ushr);
2959}
2960
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002961void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002964 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002965 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002966 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2967 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002968}
2969
2970void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2971 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002972 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002973 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002974
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002975 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002976
Nicolas Geoffray39468442014-09-02 15:17:15 +01002977 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002978 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002979}
2980
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002981void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2982 LocationSummary* locations =
2983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2984 locations->SetOut(Location::RegisterLocation(EAX));
2985 InvokeRuntimeCallingConvention calling_convention;
2986 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002987 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2988 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002989}
2990
2991void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2992 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002993 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002994 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2995
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002996 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002997
2998 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2999 DCHECK(!codegen_->IsLeafMethod());
3000}
3001
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003002void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003 LocationSummary* locations =
3004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003005 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3006 if (location.IsStackSlot()) {
3007 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3008 } else if (location.IsDoubleStackSlot()) {
3009 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003010 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003011 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003012}
3013
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003014void InstructionCodeGeneratorX86::VisitParameterValue(
3015 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3016}
3017
3018void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3021 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3022}
3023
3024void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003025}
3026
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003027void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003028 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003029 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003032}
3033
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003034void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3035 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003036 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003037 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003038 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003039 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003040 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003042 break;
3043
3044 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003045 __ notl(out.AsRegisterPairLow<Register>());
3046 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003047 break;
3048
3049 default:
3050 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3051 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003052}
3053
David Brazdil66d126e2015-04-03 16:02:44 +01003054void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3057 locations->SetInAt(0, Location::RequiresRegister());
3058 locations->SetOut(Location::SameAsFirstInput());
3059}
3060
3061void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003062 LocationSummary* locations = bool_not->GetLocations();
3063 Location in = locations->InAt(0);
3064 Location out = locations->Out();
3065 DCHECK(in.Equals(out));
3066 __ xorl(out.AsRegister<Register>(), Immediate(1));
3067}
3068
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003069void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003070 LocationSummary* locations =
3071 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003072 switch (compare->InputAt(0)->GetType()) {
3073 case Primitive::kPrimLong: {
3074 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003075 locations->SetInAt(1, Location::Any());
3076 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3077 break;
3078 }
3079 case Primitive::kPrimFloat:
3080 case Primitive::kPrimDouble: {
3081 locations->SetInAt(0, Location::RequiresFpuRegister());
3082 locations->SetInAt(1, Location::RequiresFpuRegister());
3083 locations->SetOut(Location::RequiresRegister());
3084 break;
3085 }
3086 default:
3087 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3088 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003089}
3090
3091void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003092 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003094 Location left = locations->InAt(0);
3095 Location right = locations->InAt(1);
3096
3097 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003098 switch (compare->InputAt(0)->GetType()) {
3099 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003100 Register left_low = left.AsRegisterPairLow<Register>();
3101 Register left_high = left.AsRegisterPairHigh<Register>();
3102 int32_t val_low = 0;
3103 int32_t val_high = 0;
3104 bool right_is_const = false;
3105
3106 if (right.IsConstant()) {
3107 DCHECK(right.GetConstant()->IsLongConstant());
3108 right_is_const = true;
3109 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3110 val_low = Low32Bits(val);
3111 val_high = High32Bits(val);
3112 }
3113
Calin Juravleddb7df22014-11-25 20:56:51 +00003114 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003115 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003116 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003117 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003118 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003119 DCHECK(right_is_const) << right;
3120 if (val_high == 0) {
3121 __ testl(left_high, left_high);
3122 } else {
3123 __ cmpl(left_high, Immediate(val_high));
3124 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003125 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003126 __ j(kLess, &less); // Signed compare.
3127 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003128 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003129 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003130 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003131 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003132 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003133 DCHECK(right_is_const) << right;
3134 if (val_low == 0) {
3135 __ testl(left_low, left_low);
3136 } else {
3137 __ cmpl(left_low, Immediate(val_low));
3138 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003139 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003140 break;
3141 }
3142 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003143 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003144 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3145 break;
3146 }
3147 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003149 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003150 break;
3151 }
3152 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003153 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003154 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003155 __ movl(out, Immediate(0));
3156 __ j(kEqual, &done);
3157 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3158
3159 __ Bind(&greater);
3160 __ movl(out, Immediate(1));
3161 __ jmp(&done);
3162
3163 __ Bind(&less);
3164 __ movl(out, Immediate(-1));
3165
3166 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003167}
3168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003169void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003170 LocationSummary* locations =
3171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003172 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3173 locations->SetInAt(i, Location::Any());
3174 }
3175 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003176}
3177
3178void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003179 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003180 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003181}
3182
Calin Juravle52c48962014-12-16 17:02:57 +00003183void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3184 /*
3185 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3186 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3187 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3188 */
3189 switch (kind) {
3190 case MemBarrierKind::kAnyAny: {
3191 __ mfence();
3192 break;
3193 }
3194 case MemBarrierKind::kAnyStore:
3195 case MemBarrierKind::kLoadAny:
3196 case MemBarrierKind::kStoreStore: {
3197 // nop
3198 break;
3199 }
3200 default:
3201 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003202 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003203}
3204
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205
Mark Mendell09ed1a32015-03-25 08:30:06 -04003206void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3207 Register temp) {
3208 // TODO: Implement all kinds of calls:
3209 // 1) boot -> boot
3210 // 2) app -> boot
3211 // 3) app -> app
3212 //
3213 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003214
3215 if (invoke->IsStringInit()) {
3216 // temp = thread->string_init_entrypoint
3217 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003218 // (temp + offset_of_quick_compiled_code)()
3219 __ call(Address(
3220 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3221 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003222 // temp = method;
3223 LoadCurrentMethod(temp);
3224 if (!invoke->IsRecursive()) {
3225 // temp = temp->dex_cache_resolved_methods_;
3226 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3227 // temp = temp[index_in_cache]
3228 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
3229 // (temp + offset_of_quick_compiled_code)()
3230 __ call(Address(temp,
3231 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3232 } else {
3233 __ call(GetFrameEntryLabel());
3234 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003235 }
3236
3237 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003238}
3239
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003240void CodeGeneratorX86::MarkGCCard(Register temp,
3241 Register card,
3242 Register object,
3243 Register value,
3244 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003245 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003246 if (value_can_be_null) {
3247 __ testl(value, value);
3248 __ j(kEqual, &is_null);
3249 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003250 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3251 __ movl(temp, object);
3252 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003253 __ movb(Address(temp, card, TIMES_1, 0),
3254 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003255 if (value_can_be_null) {
3256 __ Bind(&is_null);
3257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258}
3259
Calin Juravle52c48962014-12-16 17:02:57 +00003260void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3261 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003264 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003265
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003266 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3267 locations->SetOut(Location::RequiresFpuRegister());
3268 } else {
3269 // The output overlaps in case of long: we don't want the low move to overwrite
3270 // the object's location.
3271 locations->SetOut(Location::RequiresRegister(),
3272 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3273 : Location::kNoOutputOverlap);
3274 }
Calin Juravle52c48962014-12-16 17:02:57 +00003275
3276 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3277 // Long values can be loaded atomically into an XMM using movsd.
3278 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3279 // and then copy the XMM into the output 32bits at a time).
3280 locations->AddTemp(Location::RequiresFpuRegister());
3281 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003282}
3283
Calin Juravle52c48962014-12-16 17:02:57 +00003284void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3285 const FieldInfo& field_info) {
3286 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003287
Calin Juravle52c48962014-12-16 17:02:57 +00003288 LocationSummary* locations = instruction->GetLocations();
3289 Register base = locations->InAt(0).AsRegister<Register>();
3290 Location out = locations->Out();
3291 bool is_volatile = field_info.IsVolatile();
3292 Primitive::Type field_type = field_info.GetFieldType();
3293 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3294
3295 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003296 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003297 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003298 break;
3299 }
3300
3301 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003302 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003303 break;
3304 }
3305
3306 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003307 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003308 break;
3309 }
3310
3311 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003312 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003313 break;
3314 }
3315
3316 case Primitive::kPrimInt:
3317 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003318 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319 break;
3320 }
3321
3322 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003323 if (is_volatile) {
3324 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3325 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003326 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003327 __ movd(out.AsRegisterPairLow<Register>(), temp);
3328 __ psrlq(temp, Immediate(32));
3329 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3330 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003331 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003332 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003333 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003334 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3335 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003336 break;
3337 }
3338
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003339 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003340 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003341 break;
3342 }
3343
3344 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003345 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003346 break;
3347 }
3348
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003349 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003350 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003351 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003352 }
Calin Juravle52c48962014-12-16 17:02:57 +00003353
Calin Juravle77520bc2015-01-12 18:45:46 +00003354 // Longs are handled in the switch.
3355 if (field_type != Primitive::kPrimLong) {
3356 codegen_->MaybeRecordImplicitNullCheck(instruction);
3357 }
3358
Calin Juravle52c48962014-12-16 17:02:57 +00003359 if (is_volatile) {
3360 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3361 }
3362}
3363
3364void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3365 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3366
3367 LocationSummary* locations =
3368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3369 locations->SetInAt(0, Location::RequiresRegister());
3370 bool is_volatile = field_info.IsVolatile();
3371 Primitive::Type field_type = field_info.GetFieldType();
3372 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3373 || (field_type == Primitive::kPrimByte);
3374
3375 // The register allocator does not support multiple
3376 // inputs that die at entry with one in a specific register.
3377 if (is_byte_type) {
3378 // Ensure the value is in a byte register.
3379 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003380 } else if (Primitive::IsFloatingPointType(field_type)) {
3381 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003382 } else {
3383 locations->SetInAt(1, Location::RequiresRegister());
3384 }
3385 // Temporary registers for the write barrier.
3386 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3387 locations->AddTemp(Location::RequiresRegister());
3388 // Ensure the card is in a byte register.
3389 locations->AddTemp(Location::RegisterLocation(ECX));
3390 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3391 // 64bits value can be atomically written to an address with movsd and an XMM register.
3392 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3393 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3394 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3395 // isolated cases when we need this it isn't worth adding the extra complexity.
3396 locations->AddTemp(Location::RequiresFpuRegister());
3397 locations->AddTemp(Location::RequiresFpuRegister());
3398 }
3399}
3400
3401void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003402 const FieldInfo& field_info,
3403 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003404 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3405
3406 LocationSummary* locations = instruction->GetLocations();
3407 Register base = locations->InAt(0).AsRegister<Register>();
3408 Location value = locations->InAt(1);
3409 bool is_volatile = field_info.IsVolatile();
3410 Primitive::Type field_type = field_info.GetFieldType();
3411 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3412
3413 if (is_volatile) {
3414 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3415 }
3416
3417 switch (field_type) {
3418 case Primitive::kPrimBoolean:
3419 case Primitive::kPrimByte: {
3420 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3421 break;
3422 }
3423
3424 case Primitive::kPrimShort:
3425 case Primitive::kPrimChar: {
3426 __ movw(Address(base, offset), value.AsRegister<Register>());
3427 break;
3428 }
3429
3430 case Primitive::kPrimInt:
3431 case Primitive::kPrimNot: {
3432 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003433 break;
3434 }
3435
3436 case Primitive::kPrimLong: {
3437 if (is_volatile) {
3438 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3439 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3440 __ movd(temp1, value.AsRegisterPairLow<Register>());
3441 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3442 __ punpckldq(temp1, temp2);
3443 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003444 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003445 } else {
3446 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003447 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003448 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3449 }
3450 break;
3451 }
3452
3453 case Primitive::kPrimFloat: {
3454 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3455 break;
3456 }
3457
3458 case Primitive::kPrimDouble: {
3459 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3460 break;
3461 }
3462
3463 case Primitive::kPrimVoid:
3464 LOG(FATAL) << "Unreachable type " << field_type;
3465 UNREACHABLE();
3466 }
3467
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 // Longs are handled in the switch.
3469 if (field_type != Primitive::kPrimLong) {
3470 codegen_->MaybeRecordImplicitNullCheck(instruction);
3471 }
3472
3473 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3474 Register temp = locations->GetTemp(0).AsRegister<Register>();
3475 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003476 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003477 }
3478
Calin Juravle52c48962014-12-16 17:02:57 +00003479 if (is_volatile) {
3480 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3481 }
3482}
3483
3484void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3485 HandleFieldGet(instruction, instruction->GetFieldInfo());
3486}
3487
3488void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3489 HandleFieldGet(instruction, instruction->GetFieldInfo());
3490}
3491
3492void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3493 HandleFieldSet(instruction, instruction->GetFieldInfo());
3494}
3495
3496void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003497 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003498}
3499
3500void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3501 HandleFieldSet(instruction, instruction->GetFieldInfo());
3502}
3503
3504void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003505 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003506}
3507
3508void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3509 HandleFieldGet(instruction, instruction->GetFieldInfo());
3510}
3511
3512void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3513 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003514}
3515
3516void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003517 LocationSummary* locations =
3518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003519 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3520 ? Location::RequiresRegister()
3521 : Location::Any();
3522 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003523 if (instruction->HasUses()) {
3524 locations->SetOut(Location::SameAsFirstInput());
3525 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003526}
3527
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003528void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3530 return;
3531 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003532 LocationSummary* locations = instruction->GetLocations();
3533 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003534
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003535 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3536 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3537}
3538
3539void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003540 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003541 codegen_->AddSlowPath(slow_path);
3542
3543 LocationSummary* locations = instruction->GetLocations();
3544 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003545
3546 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003547 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003548 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003549 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003550 } else {
3551 DCHECK(obj.IsConstant()) << obj;
3552 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3553 __ jmp(slow_path->GetEntryLabel());
3554 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003555 }
3556 __ j(kEqual, slow_path->GetEntryLabel());
3557}
3558
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003559void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3560 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3561 GenerateImplicitNullCheck(instruction);
3562 } else {
3563 GenerateExplicitNullCheck(instruction);
3564 }
3565}
3566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003570 locations->SetInAt(0, Location::RequiresRegister());
3571 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003572 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3573 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3574 } else {
3575 // The output overlaps in case of long: we don't want the low move to overwrite
3576 // the array's location.
3577 locations->SetOut(Location::RequiresRegister(),
3578 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3579 : Location::kNoOutputOverlap);
3580 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003581}
3582
3583void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3584 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003585 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586 Location index = locations->InAt(1);
3587
Calin Juravle77520bc2015-01-12 18:45:46 +00003588 Primitive::Type type = instruction->GetType();
3589 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003590 case Primitive::kPrimBoolean: {
3591 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003592 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 if (index.IsConstant()) {
3594 __ movzxb(out, Address(obj,
3595 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003598 }
3599 break;
3600 }
3601
3602 case Primitive::kPrimByte: {
3603 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003604 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003605 if (index.IsConstant()) {
3606 __ movsxb(out, Address(obj,
3607 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3608 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003610 }
3611 break;
3612 }
3613
3614 case Primitive::kPrimShort: {
3615 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003617 if (index.IsConstant()) {
3618 __ movsxw(out, Address(obj,
3619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3620 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003622 }
3623 break;
3624 }
3625
3626 case Primitive::kPrimChar: {
3627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 if (index.IsConstant()) {
3630 __ movzxw(out, Address(obj,
3631 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3632 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 }
3635 break;
3636 }
3637
3638 case Primitive::kPrimInt:
3639 case Primitive::kPrimNot: {
3640 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003641 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003642 if (index.IsConstant()) {
3643 __ movl(out, Address(obj,
3644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3645 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 }
3648 break;
3649 }
3650
3651 case Primitive::kPrimLong: {
3652 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003653 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003654 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003655 if (index.IsConstant()) {
3656 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003657 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003658 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003659 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003660 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003661 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003662 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003663 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003664 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 }
3667 break;
3668 }
3669
Mark Mendell7c8d0092015-01-26 11:21:33 -05003670 case Primitive::kPrimFloat: {
3671 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3672 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3673 if (index.IsConstant()) {
3674 __ movss(out, Address(obj,
3675 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3676 } else {
3677 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3678 }
3679 break;
3680 }
3681
3682 case Primitive::kPrimDouble: {
3683 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3684 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3685 if (index.IsConstant()) {
3686 __ movsd(out, Address(obj,
3687 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3688 } else {
3689 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3690 }
3691 break;
3692 }
3693
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003695 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003696 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003697 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003698
3699 if (type != Primitive::kPrimLong) {
3700 codegen_->MaybeRecordImplicitNullCheck(instruction);
3701 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003702}
3703
3704void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003705 // This location builder might end up asking to up to four registers, which is
3706 // not currently possible for baseline. The situation in which we need four
3707 // registers cannot be met by baseline though, because it has not run any
3708 // optimization.
3709
Nicolas Geoffray39468442014-09-02 15:17:15 +01003710 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003711 bool needs_write_barrier =
3712 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3713
Mark Mendell5f874182015-03-04 15:42:45 -05003714 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003715
Nicolas Geoffray39468442014-09-02 15:17:15 +01003716 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3717 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003720 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003721 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003722 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3723 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3724 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003726 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3727 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003728 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003729 // In case of a byte operation, the register allocator does not support multiple
3730 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003731 locations->SetInAt(0, Location::RequiresRegister());
3732 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003733 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003734 // Ensure the value is in a byte register.
3735 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003736 } else if (Primitive::IsFloatingPointType(value_type)) {
3737 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003739 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003741 // Temporary registers for the write barrier.
3742 if (needs_write_barrier) {
3743 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003744 // Ensure the card is in a byte register.
3745 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003746 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748}
3749
3750void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3751 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003752 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003754 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003755 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003756 bool needs_runtime_call = locations->WillCall();
3757 bool needs_write_barrier =
3758 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759
3760 switch (value_type) {
3761 case Primitive::kPrimBoolean:
3762 case Primitive::kPrimByte: {
3763 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 if (index.IsConstant()) {
3765 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003766 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003767 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003768 } else {
3769 __ movb(Address(obj, offset),
3770 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3771 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003773 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003775 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003776 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003778 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3779 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003780 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 break;
3783 }
3784
3785 case Primitive::kPrimShort:
3786 case Primitive::kPrimChar: {
3787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 if (index.IsConstant()) {
3789 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003792 } else {
3793 __ movw(Address(obj, offset),
3794 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3795 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003797 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3799 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003802 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3803 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003804 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003805 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 break;
3807 }
3808
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003809 case Primitive::kPrimInt:
3810 case Primitive::kPrimNot: {
3811 if (!needs_runtime_call) {
3812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3813 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003814 size_t offset =
3815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003816 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003818 } else {
3819 DCHECK(value.IsConstant()) << value;
3820 __ movl(Address(obj, offset),
3821 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3822 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003823 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003824 DCHECK(index.IsRegister()) << index;
3825 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003826 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3827 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003828 } else {
3829 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003830 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003831 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3832 }
3833 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003835
3836 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003837 Register temp = locations->GetTemp(0).AsRegister<Register>();
3838 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003839 codegen_->MarkGCCard(
3840 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003843 DCHECK_EQ(value_type, Primitive::kPrimNot);
3844 DCHECK(!codegen_->IsLeafMethod());
3845 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3846 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 }
3848 break;
3849 }
3850
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003851 case Primitive::kPrimLong: {
3852 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853 if (index.IsConstant()) {
3854 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003855 if (value.IsRegisterPair()) {
3856 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003857 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003858 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003859 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003860 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003861 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3862 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003864 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3865 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003866 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003867 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003869 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003872 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003873 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003874 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003875 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003877 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003878 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003880 Immediate(High32Bits(val)));
3881 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 }
3883 break;
3884 }
3885
Mark Mendell7c8d0092015-01-26 11:21:33 -05003886 case Primitive::kPrimFloat: {
3887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3888 DCHECK(value.IsFpuRegister());
3889 if (index.IsConstant()) {
3890 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3891 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3892 } else {
3893 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3894 value.AsFpuRegister<XmmRegister>());
3895 }
3896 break;
3897 }
3898
3899 case Primitive::kPrimDouble: {
3900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3901 DCHECK(value.IsFpuRegister());
3902 if (index.IsConstant()) {
3903 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3904 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3905 } else {
3906 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3907 value.AsFpuRegister<XmmRegister>());
3908 }
3909 break;
3910 }
3911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003912 case Primitive::kPrimVoid:
3913 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003914 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003915 }
3916}
3917
3918void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003920 locations->SetInAt(0, Location::RequiresRegister());
3921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922 instruction->SetLocations(locations);
3923}
3924
3925void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3926 LocationSummary* locations = instruction->GetLocations();
3927 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003928 Register obj = locations->InAt(0).AsRegister<Register>();
3929 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932}
3933
3934void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003935 LocationSummary* locations =
3936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003937 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003938 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003939 if (instruction->HasUses()) {
3940 locations->SetOut(Location::SameAsFirstInput());
3941 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942}
3943
3944void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3945 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003946 Location index_loc = locations->InAt(0);
3947 Location length_loc = locations->InAt(1);
3948 SlowPathCodeX86* slow_path =
3949 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003950
Mark Mendell99dbd682015-04-22 16:18:52 -04003951 if (length_loc.IsConstant()) {
3952 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3953 if (index_loc.IsConstant()) {
3954 // BCE will remove the bounds check if we are guarenteed to pass.
3955 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3956 if (index < 0 || index >= length) {
3957 codegen_->AddSlowPath(slow_path);
3958 __ jmp(slow_path->GetEntryLabel());
3959 } else {
3960 // Some optimization after BCE may have generated this, and we should not
3961 // generate a bounds check if it is a valid range.
3962 }
3963 return;
3964 }
3965
3966 // We have to reverse the jump condition because the length is the constant.
3967 Register index_reg = index_loc.AsRegister<Register>();
3968 __ cmpl(index_reg, Immediate(length));
3969 codegen_->AddSlowPath(slow_path);
3970 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003971 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003972 Register length = length_loc.AsRegister<Register>();
3973 if (index_loc.IsConstant()) {
3974 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3975 __ cmpl(length, Immediate(value));
3976 } else {
3977 __ cmpl(length, index_loc.AsRegister<Register>());
3978 }
3979 codegen_->AddSlowPath(slow_path);
3980 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003981 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003982}
3983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3985 temp->SetLocations(nullptr);
3986}
3987
3988void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3989 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003990 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003991}
3992
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003993void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003994 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003995 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003996}
3997
3998void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003999 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4000}
4001
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004002void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4004}
4005
4006void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004007 HBasicBlock* block = instruction->GetBlock();
4008 if (block->GetLoopInformation() != nullptr) {
4009 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4010 // The back edge will generate the suspend check.
4011 return;
4012 }
4013 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4014 // The goto will generate the suspend check.
4015 return;
4016 }
4017 GenerateSuspendCheck(instruction, nullptr);
4018}
4019
4020void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4021 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004022 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004023 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4024 if (slow_path == nullptr) {
4025 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4026 instruction->SetSlowPath(slow_path);
4027 codegen_->AddSlowPath(slow_path);
4028 if (successor != nullptr) {
4029 DCHECK(successor->IsLoopHeader());
4030 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4031 }
4032 } else {
4033 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4034 }
4035
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004036 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004037 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004038 if (successor == nullptr) {
4039 __ j(kNotEqual, slow_path->GetEntryLabel());
4040 __ Bind(slow_path->GetReturnLabel());
4041 } else {
4042 __ j(kEqual, codegen_->GetLabelOf(successor));
4043 __ jmp(slow_path->GetEntryLabel());
4044 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004045}
4046
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004047X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4048 return codegen_->GetAssembler();
4049}
4050
Mark Mendell7c8d0092015-01-26 11:21:33 -05004051void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004052 ScratchRegisterScope ensure_scratch(
4053 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4054 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4055 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4056 __ movl(temp_reg, Address(ESP, src + stack_offset));
4057 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004058}
4059
4060void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004061 ScratchRegisterScope ensure_scratch(
4062 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4063 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4064 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4065 __ movl(temp_reg, Address(ESP, src + stack_offset));
4066 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4067 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4068 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004069}
4070
4071void ParallelMoveResolverX86::EmitMove(size_t index) {
4072 MoveOperands* move = moves_.Get(index);
4073 Location source = move->GetSource();
4074 Location destination = move->GetDestination();
4075
4076 if (source.IsRegister()) {
4077 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004079 } else {
4080 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004081 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004082 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004083 } else if (source.IsFpuRegister()) {
4084 if (destination.IsFpuRegister()) {
4085 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4086 } else if (destination.IsStackSlot()) {
4087 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4088 } else {
4089 DCHECK(destination.IsDoubleStackSlot());
4090 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4091 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004092 } else if (source.IsStackSlot()) {
4093 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004095 } else if (destination.IsFpuRegister()) {
4096 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004097 } else {
4098 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004099 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4100 }
4101 } else if (source.IsDoubleStackSlot()) {
4102 if (destination.IsFpuRegister()) {
4103 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4104 } else {
4105 DCHECK(destination.IsDoubleStackSlot()) << destination;
4106 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004107 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004108 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004109 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004110 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004111 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004112 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004113 if (value == 0) {
4114 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4115 } else {
4116 __ movl(destination.AsRegister<Register>(), Immediate(value));
4117 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004118 } else {
4119 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004120 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004121 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004122 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004123 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004124 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004125 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004126 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004127 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4128 if (value == 0) {
4129 // Easy handling of 0.0.
4130 __ xorps(dest, dest);
4131 } else {
4132 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004133 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4134 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4135 __ movl(temp, Immediate(value));
4136 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004137 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004138 } else {
4139 DCHECK(destination.IsStackSlot()) << destination;
4140 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4141 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004142 } else if (constant->IsLongConstant()) {
4143 int64_t value = constant->AsLongConstant()->GetValue();
4144 int32_t low_value = Low32Bits(value);
4145 int32_t high_value = High32Bits(value);
4146 Immediate low(low_value);
4147 Immediate high(high_value);
4148 if (destination.IsDoubleStackSlot()) {
4149 __ movl(Address(ESP, destination.GetStackIndex()), low);
4150 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4151 } else {
4152 __ movl(destination.AsRegisterPairLow<Register>(), low);
4153 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4154 }
4155 } else {
4156 DCHECK(constant->IsDoubleConstant());
4157 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004158 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004159 int32_t low_value = Low32Bits(value);
4160 int32_t high_value = High32Bits(value);
4161 Immediate low(low_value);
4162 Immediate high(high_value);
4163 if (destination.IsFpuRegister()) {
4164 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4165 if (value == 0) {
4166 // Easy handling of 0.0.
4167 __ xorpd(dest, dest);
4168 } else {
4169 __ pushl(high);
4170 __ pushl(low);
4171 __ movsd(dest, Address(ESP, 0));
4172 __ addl(ESP, Immediate(8));
4173 }
4174 } else {
4175 DCHECK(destination.IsDoubleStackSlot()) << destination;
4176 __ movl(Address(ESP, destination.GetStackIndex()), low);
4177 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4178 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004179 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004180 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004181 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004182 }
4183}
4184
Mark Mendella5c19ce2015-04-01 12:51:05 -04004185void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004186 Register suggested_scratch = reg == EAX ? EBX : EAX;
4187 ScratchRegisterScope ensure_scratch(
4188 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4189
4190 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4191 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4192 __ movl(Address(ESP, mem + stack_offset), reg);
4193 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004194}
4195
Mark Mendell7c8d0092015-01-26 11:21:33 -05004196void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004197 ScratchRegisterScope ensure_scratch(
4198 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4199
4200 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4201 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4202 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4203 __ movss(Address(ESP, mem + stack_offset), reg);
4204 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004205}
4206
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004207void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004208 ScratchRegisterScope ensure_scratch1(
4209 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004210
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004211 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4212 ScratchRegisterScope ensure_scratch2(
4213 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004214
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004215 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4216 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4217 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4218 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4219 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4220 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004221}
4222
4223void ParallelMoveResolverX86::EmitSwap(size_t index) {
4224 MoveOperands* move = moves_.Get(index);
4225 Location source = move->GetSource();
4226 Location destination = move->GetDestination();
4227
4228 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004229 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004230 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004231 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004232 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004233 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004234 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4235 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004236 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4237 // Use XOR Swap algorithm to avoid a temporary.
4238 DCHECK_NE(source.reg(), destination.reg());
4239 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4240 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4241 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4242 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4243 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4244 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4245 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004246 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4247 // Take advantage of the 16 bytes in the XMM register.
4248 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4249 Address stack(ESP, destination.GetStackIndex());
4250 // Load the double into the high doubleword.
4251 __ movhpd(reg, stack);
4252
4253 // Store the low double into the destination.
4254 __ movsd(stack, reg);
4255
4256 // Move the high double to the low double.
4257 __ psrldq(reg, Immediate(8));
4258 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4259 // Take advantage of the 16 bytes in the XMM register.
4260 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4261 Address stack(ESP, source.GetStackIndex());
4262 // Load the double into the high doubleword.
4263 __ movhpd(reg, stack);
4264
4265 // Store the low double into the destination.
4266 __ movsd(stack, reg);
4267
4268 // Move the high double to the low double.
4269 __ psrldq(reg, Immediate(8));
4270 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4271 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4272 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004273 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004274 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004275 }
4276}
4277
4278void ParallelMoveResolverX86::SpillScratch(int reg) {
4279 __ pushl(static_cast<Register>(reg));
4280}
4281
4282void ParallelMoveResolverX86::RestoreScratch(int reg) {
4283 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004284}
4285
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004286void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004287 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4288 ? LocationSummary::kCallOnSlowPath
4289 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004290 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004291 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004292 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004293 locations->SetOut(Location::RequiresRegister());
4294}
4295
4296void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004297 LocationSummary* locations = cls->GetLocations();
4298 Register out = locations->Out().AsRegister<Register>();
4299 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004300 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004301 DCHECK(!cls->CanCallRuntime());
4302 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004303 __ movl(out, Address(current_method, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004304 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004305 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004306 __ movl(out, Address(
4307 current_method, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004308 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004309
4310 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4311 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4312 codegen_->AddSlowPath(slow_path);
4313 __ testl(out, out);
4314 __ j(kEqual, slow_path->GetEntryLabel());
4315 if (cls->MustGenerateClinitCheck()) {
4316 GenerateClassInitializationCheck(slow_path, out);
4317 } else {
4318 __ Bind(slow_path->GetExitLabel());
4319 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004320 }
4321}
4322
4323void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4324 LocationSummary* locations =
4325 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4326 locations->SetInAt(0, Location::RequiresRegister());
4327 if (check->HasUses()) {
4328 locations->SetOut(Location::SameAsFirstInput());
4329 }
4330}
4331
4332void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004333 // We assume the class to not be null.
4334 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4335 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004336 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004337 GenerateClassInitializationCheck(slow_path,
4338 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004339}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004340
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004341void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4342 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4344 Immediate(mirror::Class::kStatusInitialized));
4345 __ j(kLess, slow_path->GetEntryLabel());
4346 __ Bind(slow_path->GetExitLabel());
4347 // No need for memory fence, thanks to the X86 memory model.
4348}
4349
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004350void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4351 LocationSummary* locations =
4352 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
4353 locations->SetOut(Location::RequiresRegister());
4354}
4355
4356void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4357 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4358 codegen_->AddSlowPath(slow_path);
4359
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004361 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08004362 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
4363 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004364 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4365 __ testl(out, out);
4366 __ j(kEqual, slow_path->GetEntryLabel());
4367 __ Bind(slow_path->GetExitLabel());
4368}
4369
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004370void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4371 LocationSummary* locations =
4372 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4373 locations->SetOut(Location::RequiresRegister());
4374}
4375
4376void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4377 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004378 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004379 __ fs()->movl(address, Immediate(0));
4380}
4381
4382void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4383 LocationSummary* locations =
4384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4385 InvokeRuntimeCallingConvention calling_convention;
4386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4387}
4388
4389void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4390 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4391 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4392}
4393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004394void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004395 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4396 ? LocationSummary::kNoCall
4397 : LocationSummary::kCallOnSlowPath;
4398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4399 locations->SetInAt(0, Location::RequiresRegister());
4400 locations->SetInAt(1, Location::Any());
4401 locations->SetOut(Location::RequiresRegister());
4402}
4403
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004404void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004405 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004406 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004407 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004408 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4410 Label done, zero;
4411 SlowPathCodeX86* slow_path = nullptr;
4412
4413 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004414 // Avoid null check if we know obj is not null.
4415 if (instruction->MustDoNullCheck()) {
4416 __ testl(obj, obj);
4417 __ j(kEqual, &zero);
4418 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004419 __ movl(out, Address(obj, class_offset));
4420 // Compare the class of `obj` with `cls`.
4421 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004422 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004423 } else {
4424 DCHECK(cls.IsStackSlot()) << cls;
4425 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4426 }
4427
4428 if (instruction->IsClassFinal()) {
4429 // Classes must be equal for the instanceof to succeed.
4430 __ j(kNotEqual, &zero);
4431 __ movl(out, Immediate(1));
4432 __ jmp(&done);
4433 } else {
4434 // If the classes are not equal, we go into a slow path.
4435 DCHECK(locations->OnlyCallsOnSlowPath());
4436 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004437 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004438 codegen_->AddSlowPath(slow_path);
4439 __ j(kNotEqual, slow_path->GetEntryLabel());
4440 __ movl(out, Immediate(1));
4441 __ jmp(&done);
4442 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004443
4444 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4445 __ Bind(&zero);
4446 __ movl(out, Immediate(0));
4447 }
4448
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004449 if (slow_path != nullptr) {
4450 __ Bind(slow_path->GetExitLabel());
4451 }
4452 __ Bind(&done);
4453}
4454
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004455void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4457 instruction, LocationSummary::kCallOnSlowPath);
4458 locations->SetInAt(0, Location::RequiresRegister());
4459 locations->SetInAt(1, Location::Any());
4460 locations->AddTemp(Location::RequiresRegister());
4461}
4462
4463void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4464 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004465 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004466 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004467 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004468 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4469 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4470 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4471 codegen_->AddSlowPath(slow_path);
4472
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004473 // Avoid null check if we know obj is not null.
4474 if (instruction->MustDoNullCheck()) {
4475 __ testl(obj, obj);
4476 __ j(kEqual, slow_path->GetExitLabel());
4477 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004478
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004479 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004480 // Compare the class of `obj` with `cls`.
4481 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004482 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004483 } else {
4484 DCHECK(cls.IsStackSlot()) << cls;
4485 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4486 }
4487
4488 __ j(kNotEqual, slow_path->GetEntryLabel());
4489 __ Bind(slow_path->GetExitLabel());
4490}
4491
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004492void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4493 LocationSummary* locations =
4494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4495 InvokeRuntimeCallingConvention calling_convention;
4496 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4497}
4498
4499void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4500 __ fs()->call(Address::Absolute(instruction->IsEnter()
4501 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4502 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4503 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4504}
4505
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004506void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4507void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4508void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4509
4510void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4511 LocationSummary* locations =
4512 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4513 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4514 || instruction->GetResultType() == Primitive::kPrimLong);
4515 locations->SetInAt(0, Location::RequiresRegister());
4516 locations->SetInAt(1, Location::Any());
4517 locations->SetOut(Location::SameAsFirstInput());
4518}
4519
4520void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4521 HandleBitwiseOperation(instruction);
4522}
4523
4524void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4525 HandleBitwiseOperation(instruction);
4526}
4527
4528void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4529 HandleBitwiseOperation(instruction);
4530}
4531
4532void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4533 LocationSummary* locations = instruction->GetLocations();
4534 Location first = locations->InAt(0);
4535 Location second = locations->InAt(1);
4536 DCHECK(first.Equals(locations->Out()));
4537
4538 if (instruction->GetResultType() == Primitive::kPrimInt) {
4539 if (second.IsRegister()) {
4540 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004541 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004542 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004543 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004544 } else {
4545 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004547 }
4548 } else if (second.IsConstant()) {
4549 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004550 __ andl(first.AsRegister<Register>(),
4551 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004552 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004553 __ orl(first.AsRegister<Register>(),
4554 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004555 } else {
4556 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004557 __ xorl(first.AsRegister<Register>(),
4558 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559 }
4560 } else {
4561 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004562 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004563 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004565 } else {
4566 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004567 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004568 }
4569 }
4570 } else {
4571 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4572 if (second.IsRegisterPair()) {
4573 if (instruction->IsAnd()) {
4574 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4575 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4576 } else if (instruction->IsOr()) {
4577 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4578 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4579 } else {
4580 DCHECK(instruction->IsXor());
4581 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4582 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4583 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004584 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004585 if (instruction->IsAnd()) {
4586 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4587 __ andl(first.AsRegisterPairHigh<Register>(),
4588 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4589 } else if (instruction->IsOr()) {
4590 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4591 __ orl(first.AsRegisterPairHigh<Register>(),
4592 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4593 } else {
4594 DCHECK(instruction->IsXor());
4595 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4596 __ xorl(first.AsRegisterPairHigh<Register>(),
4597 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4598 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004599 } else {
4600 DCHECK(second.IsConstant()) << second;
4601 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004602 int32_t low_value = Low32Bits(value);
4603 int32_t high_value = High32Bits(value);
4604 Immediate low(low_value);
4605 Immediate high(high_value);
4606 Register first_low = first.AsRegisterPairLow<Register>();
4607 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004608 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004609 if (low_value == 0) {
4610 __ xorl(first_low, first_low);
4611 } else if (low_value != -1) {
4612 __ andl(first_low, low);
4613 }
4614 if (high_value == 0) {
4615 __ xorl(first_high, first_high);
4616 } else if (high_value != -1) {
4617 __ andl(first_high, high);
4618 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004619 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004620 if (low_value != 0) {
4621 __ orl(first_low, low);
4622 }
4623 if (high_value != 0) {
4624 __ orl(first_high, high);
4625 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004626 } else {
4627 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004628 if (low_value != 0) {
4629 __ xorl(first_low, low);
4630 }
4631 if (high_value != 0) {
4632 __ xorl(first_high, high);
4633 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004634 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004635 }
4636 }
4637}
4638
Calin Juravleb1498f62015-02-16 13:13:29 +00004639void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4640 // Nothing to do, this should be removed during prepare for register allocator.
4641 UNUSED(instruction);
4642 LOG(FATAL) << "Unreachable";
4643}
4644
4645void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4646 // Nothing to do, this should be removed during prepare for register allocator.
4647 UNUSED(instruction);
4648 LOG(FATAL) << "Unreachable";
4649}
4650
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004651} // namespace x86
4652} // namespace art