blob: e39a1c2bd5980bd26d9941086a953a31c2c3de1d [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
59 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
62};
63
Calin Juravled0d48522014-11-04 16:40:20 +000064class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
65 public:
66 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
67
Alexandre Rames2ed20af2015-03-06 13:55:35 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000069 __ Bind(GetEntryLabel());
70 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070071 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000072 }
73
74 private:
75 HDivZeroCheck* const instruction_;
76 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
77};
78
Calin Juravlebacfec32014-11-14 15:54:36 +000079class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000081 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000085 if (is_div_) {
86 __ negl(reg_);
87 } else {
88 __ movl(reg_, Immediate(0));
89 }
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ jmp(GetExitLabel());
91 }
92
93 private:
94 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000095 bool is_div_;
96 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000097};
98
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010099class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100100 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100101 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
102 Location index_location,
103 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000104 : instruction_(instruction),
105 index_location_(index_location),
106 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100110 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000111 // We're moving two locations to locations that could overlap, so we need a parallel
112 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000114 x86_codegen->EmitParallelMoves(
115 index_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100117 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000118 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100119 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
120 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700122 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 }
124
125 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100126 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 const Location index_location_;
128 const Location length_location_;
129
130 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000135 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000138 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700143 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 private:
162 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 Label return_label_;
165
166 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
167};
168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169class LoadStringSlowPathX86 : public SlowPathCodeX86 {
170 public:
171 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000174 LocationSummary* locations = instruction_->GetLocations();
175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
176
177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
178 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000185 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000187
188 __ jmp(GetExitLabel());
189 }
190
191 private:
192 HLoadString* const instruction_;
193
194 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 LoadClassSlowPathX86(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
206
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 InvokeRuntimeCallingConvention calling_convention;
214 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ fs()->call(Address::Absolute(do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ jmp(GetExitLabel());
229 }
230
231 private:
232 // The class this slow path will load.
233 HLoadClass* const cls_;
234
235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
246};
247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
249 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 TypeCheckSlowPathX86(HInstruction* instruction,
251 Location class_to_check,
252 Location object_class,
253 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 class_to_check_(class_to_check),
256 object_class_(object_class),
257 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000261 DCHECK(instruction_->IsCheckCast()
262 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
268 // We're moving two locations to locations that could overlap, so we need a parallel
269 // move resolver.
270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000271 x86_codegen->EmitParallelMoves(
272 class_to_check_,
273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100274 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000275 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100276 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
277 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000280 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
281 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 } else {
283 DCHECK(instruction_->IsCheckCast());
284 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
289 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 __ jmp(GetExitLabel());
294 }
295
296 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 HInstruction* const instruction_;
298 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
303};
304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700305class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
306 public:
307 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
308 : instruction_(instruction) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, instruction_->GetLocations());
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
314 // No need to restore live registers.
315 DCHECK(instruction_->IsDeoptimize());
316 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
317 uint32_t dex_pc = deoptimize->GetDexPc();
318 codegen->RecordPcInfo(instruction_, dex_pc, this);
319 }
320
321 private:
322 HInstruction* const instruction_;
323 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100327#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100344 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100348 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Mark Mendell7c8d0092015-01-26 11:21:33 -0500361size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
363 return GetFloatingPointSpillSlotSize();
364}
365
366size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
367 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
368 return GetFloatingPointSpillSlotSize();
369}
370
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
372 const X86InstructionSetFeatures& isa_features,
373 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500374 : CodeGenerator(graph,
375 kNumberOfCpuRegisters,
376 kNumberOfXmmRegisters,
377 kNumberOfRegisterPairs,
378 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
379 arraysize(kCoreCalleeSaves))
380 | (1 << kFakeReturnRegister),
381 0,
382 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100383 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 move_resolver_(graph->GetArena(), this),
387 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000388 // Use a fake return address register to mimic Quick.
389 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100390}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 switch (type) {
394 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 X86ManagedRegister pair =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
399 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
401 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100402 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimByte:
407 case Primitive::kPrimBoolean:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
416 X86ManagedRegister current =
417 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
418 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 }
421 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 return Location::FpuRegisterLocation(
428 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Mark Mendell5f874182015-03-04 15:42:45 -0500438void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
Mark Mendell5f874182015-03-04 15:42:45 -0500445 if (is_baseline) {
446 blocked_core_registers_[EBP] = true;
447 blocked_core_registers_[ESI] = true;
448 blocked_core_registers_[EDI] = true;
449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100450
451 UpdateBlockedPairRegisters();
452}
453
454void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
455 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
456 X86ManagedRegister current =
457 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
458 if (blocked_core_registers_[current.AsRegisterPairLow()]
459 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
460 blocked_register_pairs_[i] = true;
461 }
462 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463}
464
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
466 : HGraphVisitor(graph),
467 assembler_(codegen->GetAssembler()),
468 codegen_(codegen) {}
469
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100471 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472}
473
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100475 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000476 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000477 bool skip_overflow_check =
478 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000480
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 }
485
Mark Mendell5f874182015-03-04 15:42:45 -0500486 if (HasEmptyFrame()) {
487 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000488 }
Mark Mendell5f874182015-03-04 15:42:45 -0500489
490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
491 Register reg = kCoreCalleeSaves[i];
492 if (allocated_registers_.ContainsCoreRegister(reg)) {
493 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().AdjustCFAOffset(kX86WordSize);
495 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500496 }
497 }
498
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 int adjust = GetFrameSize() - FrameEntrySpillSize();
500 __ subl(ESP, Immediate(adjust));
501 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100502 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000503}
504
505void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100506 __ cfi().RememberState();
507 if (!HasEmptyFrame()) {
508 int adjust = GetFrameSize() - FrameEntrySpillSize();
509 __ addl(ESP, Immediate(adjust));
510 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500511
David Srbeckyc34dc932015-04-12 09:27:43 +0100512 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
513 Register reg = kCoreCalleeSaves[i];
514 if (allocated_registers_.ContainsCoreRegister(reg)) {
515 __ popl(reg);
516 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
517 __ cfi().Restore(DWARFReg(reg));
518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100521 __ ret();
522 __ cfi().RestoreState();
523 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528}
529
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
531 switch (load->GetType()) {
532 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
536 case Primitive::kPrimInt:
537 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimBoolean:
542 case Primitive::kPrimByte:
543 case Primitive::kPrimChar:
544 case Primitive::kPrimShort:
545 case Primitive::kPrimVoid:
546 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700547 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548 }
549
550 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552}
553
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100554Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
555 switch (type) {
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimByte:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot:
562 return Location::RegisterLocation(EAX);
563
564 case Primitive::kPrimLong:
565 return Location::RegisterPairLocation(EAX, EDX);
566
567 case Primitive::kPrimVoid:
568 return Location::NoLocation();
569
570 case Primitive::kPrimDouble:
571 case Primitive::kPrimFloat:
572 return Location::FpuRegisterLocation(XMM0);
573 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100574
575 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100576}
577
578Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
579 return Location::RegisterLocation(kMethodRegisterArgument);
580}
581
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100582Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 switch (type) {
584 case Primitive::kPrimBoolean:
585 case Primitive::kPrimByte:
586 case Primitive::kPrimChar:
587 case Primitive::kPrimShort:
588 case Primitive::kPrimInt:
589 case Primitive::kPrimNot: {
590 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000591 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100592 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100593 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100594 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000595 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100596 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100597 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100598
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000599 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100600 uint32_t index = gp_index_;
601 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000602 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100604 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
605 calling_convention.GetRegisterPairAt(index));
606 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100607 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000608 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
609 }
610 }
611
612 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100613 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000614 stack_index_++;
615 if (index < calling_convention.GetNumberOfFpuRegisters()) {
616 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
617 } else {
618 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
619 }
620 }
621
622 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100623 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000624 stack_index_ += 2;
625 if (index < calling_convention.GetNumberOfFpuRegisters()) {
626 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
627 } else {
628 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100629 }
630 }
631
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100632 case Primitive::kPrimVoid:
633 LOG(FATAL) << "Unexpected parameter type " << type;
634 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100635 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100636 return Location();
637}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100638
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639void CodeGeneratorX86::Move32(Location destination, Location source) {
640 if (source.Equals(destination)) {
641 return;
642 }
643 if (destination.IsRegister()) {
644 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000645 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000647 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 } else {
649 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 } else if (destination.IsFpuRegister()) {
653 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000654 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000656 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else {
658 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000659 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000666 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500667 } else if (source.IsConstant()) {
668 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000669 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500670 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
672 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100673 __ pushl(Address(ESP, source.GetStackIndex()));
674 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 }
676 }
677}
678
679void CodeGeneratorX86::Move64(Location destination, Location source) {
680 if (source.Equals(destination)) {
681 return;
682 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100683 if (destination.IsRegisterPair()) {
684 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000685 EmitParallelMoves(
686 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
687 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100688 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000689 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100690 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
691 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 } else if (source.IsFpuRegister()) {
693 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000695 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100696 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100697 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
698 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
700 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500702 if (source.IsFpuRegister()) {
703 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
704 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else {
707 LOG(FATAL) << "Unimplemented";
708 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100709 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000710 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100711 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000712 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100713 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100715 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000718 } else if (source.IsConstant()) {
719 HConstant* constant = source.GetConstant();
720 int64_t value;
721 if (constant->IsLongConstant()) {
722 value = constant->AsLongConstant()->GetValue();
723 } else {
724 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000725 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000726 }
727 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
728 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000730 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000731 EmitParallelMoves(
732 Location::StackSlot(source.GetStackIndex()),
733 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100734 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000735 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100736 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
737 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 }
739 }
740}
741
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100742void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100744 if (instruction->IsCurrentMethod()) {
745 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
746 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000747 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100748 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000749 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000750 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
751 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000753 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 } else if (location.IsStackSlot()) {
755 __ movl(Address(ESP, location.GetStackIndex()), imm);
756 } else {
757 DCHECK(location.IsConstant());
758 DCHECK_EQ(location.GetConstant(), const_to_move);
759 }
760 } else if (const_to_move->IsLongConstant()) {
761 int64_t value = const_to_move->AsLongConstant()->GetValue();
762 if (location.IsRegisterPair()) {
763 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
764 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
765 } else if (location.IsDoubleStackSlot()) {
766 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000767 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
768 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000769 } else {
770 DCHECK(location.IsConstant());
771 DCHECK_EQ(location.GetConstant(), instruction);
772 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000774 } else if (instruction->IsTemporary()) {
775 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000776 if (temp_location.IsStackSlot()) {
777 Move32(location, temp_location);
778 } else {
779 DCHECK(temp_location.IsDoubleStackSlot());
780 Move64(location, temp_location);
781 }
Roland Levillain476df552014-10-09 17:51:36 +0100782 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 switch (instruction->GetType()) {
785 case Primitive::kPrimBoolean:
786 case Primitive::kPrimByte:
787 case Primitive::kPrimChar:
788 case Primitive::kPrimShort:
789 case Primitive::kPrimInt:
790 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimFloat:
792 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794
795 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 case Primitive::kPrimDouble:
797 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 default:
801 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
802 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100804 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100805 switch (instruction->GetType()) {
806 case Primitive::kPrimBoolean:
807 case Primitive::kPrimByte:
808 case Primitive::kPrimChar:
809 case Primitive::kPrimShort:
810 case Primitive::kPrimInt:
811 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000813 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 break;
815
816 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 break;
820
821 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000824 }
825}
826
827void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000832 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100833 DCHECK(!successor->IsExitBlock());
834
835 HBasicBlock* block = got->GetBlock();
836 HInstruction* previous = got->GetPrevious();
837
838 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000839 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
841 return;
842 }
843
844 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
845 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
846 }
847 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000848 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000849 }
850}
851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854}
855
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700857 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000858}
859
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
861 Label* true_target,
862 Label* false_target,
863 Label* always_true_target) {
864 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 if (cond->IsIntConstant()) {
866 // Constant condition, statically compared against 1.
867 int32_t cond_value = cond->AsIntConstant()->GetValue();
868 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700869 if (always_true_target != nullptr) {
870 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100871 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100872 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100873 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100874 DCHECK_EQ(cond_value, 0);
875 }
876 } else {
877 bool materialized =
878 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
879 // Moves do not affect the eflags register, so if the condition is
880 // evaluated just before the if, we don't need to evaluate it
881 // again.
882 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700883 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100884 if (materialized) {
885 if (!eflags_set) {
886 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500889 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100890 } else {
891 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
892 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700893 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100894 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700895 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100896 }
897 } else {
898 Location lhs = cond->GetLocations()->InAt(0);
899 Location rhs = cond->GetLocations()->InAt(1);
900 // LHS is guaranteed to be in a register (see
901 // LocationsBuilderX86::VisitCondition).
902 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000903 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100904 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100905 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500906 if (constant == 0) {
907 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
908 } else {
909 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
910 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100911 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000912 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100913 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700914 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700915 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700917 if (false_target != nullptr) {
918 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000919 }
920}
921
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700922void LocationsBuilderX86::VisitIf(HIf* if_instr) {
923 LocationSummary* locations =
924 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
925 HInstruction* cond = if_instr->InputAt(0);
926 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
927 locations->SetInAt(0, Location::Any());
928 }
929}
930
931void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
932 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
933 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
934 Label* always_true_target = true_target;
935 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
936 if_instr->IfTrueSuccessor())) {
937 always_true_target = nullptr;
938 }
939 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
940 if_instr->IfFalseSuccessor())) {
941 false_target = nullptr;
942 }
943 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
944}
945
946void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
947 LocationSummary* locations = new (GetGraph()->GetArena())
948 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
949 HInstruction* cond = deoptimize->InputAt(0);
950 DCHECK(cond->IsCondition());
951 if (cond->AsCondition()->NeedsMaterialization()) {
952 locations->SetInAt(0, Location::Any());
953 }
954}
955
956void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
957 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
958 DeoptimizationSlowPathX86(deoptimize);
959 codegen_->AddSlowPath(slow_path);
960 Label* slow_path_entry = slow_path->GetEntryLabel();
961 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
962}
963
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000965 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000966}
967
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000968void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
969 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000970}
971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100973 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000974}
975
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000976void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100977 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700978 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100982 LocationSummary* locations =
983 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 switch (store->InputAt(1)->GetType()) {
985 case Primitive::kPrimBoolean:
986 case Primitive::kPrimByte:
987 case Primitive::kPrimChar:
988 case Primitive::kPrimShort:
989 case Primitive::kPrimInt:
990 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
993 break;
994
995 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
998 break;
999
1000 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001003}
1004
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001007}
1008
Roland Levillain0d37cd02015-05-27 16:39:19 +01001009void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001011 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001012 locations->SetInAt(0, Location::RequiresRegister());
1013 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001014 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001015 // We need a byte register.
1016 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001017 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018}
1019
Roland Levillain0d37cd02015-05-27 16:39:19 +01001020void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1021 if (cond->NeedsMaterialization()) {
1022 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001024 // Clear register: setcc only sets the low byte.
1025 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001026 Location lhs = locations->InAt(0);
1027 Location rhs = locations->InAt(1);
1028 if (rhs.IsRegister()) {
1029 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1030 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001031 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001032 if (constant == 0) {
1033 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1034 } else {
1035 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1036 }
Dave Allison20dfc792014-06-16 20:44:29 -07001037 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001038 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001039 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001040 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 }
Dave Allison20dfc792014-06-16 20:44:29 -07001042}
1043
1044void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1065 VisitCondition(comp);
1066}
1067
1068void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1069 VisitCondition(comp);
1070}
1071
1072void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1073 VisitCondition(comp);
1074}
1075
1076void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1077 VisitCondition(comp);
1078}
1079
1080void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1081 VisitCondition(comp);
1082}
1083
1084void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1085 VisitCondition(comp);
1086}
1087
1088void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1089 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001090}
1091
1092void LocationsBuilderX86::VisitIntConstant(HIntConstant* 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 Geoffray3ff386a2014-03-04 14:46:47 +00001096}
1097
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001098void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001100 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101}
1102
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001103void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1104 LocationSummary* locations =
1105 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1106 locations->SetOut(Location::ConstantLocation(constant));
1107}
1108
1109void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1110 // Will be generated at use site.
1111 UNUSED(constant);
1112}
1113
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001117 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118}
1119
1120void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1121 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123}
1124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001125void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1126 LocationSummary* locations =
1127 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1128 locations->SetOut(Location::ConstantLocation(constant));
1129}
1130
1131void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1132 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001133 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001134}
1135
1136void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1137 LocationSummary* locations =
1138 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1139 locations->SetOut(Location::ConstantLocation(constant));
1140}
1141
1142void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1143 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001144 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001145}
1146
Calin Juravle27df7582015-04-17 19:12:31 +01001147void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1148 memory_barrier->SetLocations(nullptr);
1149}
1150
1151void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1152 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1153}
1154
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001157}
1158
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001159void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001160 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001162}
1163
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001164void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001165 LocationSummary* locations =
1166 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 switch (ret->InputAt(0)->GetType()) {
1168 case Primitive::kPrimBoolean:
1169 case Primitive::kPrimByte:
1170 case Primitive::kPrimChar:
1171 case Primitive::kPrimShort:
1172 case Primitive::kPrimInt:
1173 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 break;
1176
1177 case Primitive::kPrimLong:
1178 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 case Primitive::kPrimFloat:
1183 case Primitive::kPrimDouble:
1184 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 break;
1187
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191}
1192
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 if (kIsDebugBuild) {
1195 switch (ret->InputAt(0)->GetType()) {
1196 case Primitive::kPrimBoolean:
1197 case Primitive::kPrimByte:
1198 case Primitive::kPrimChar:
1199 case Primitive::kPrimShort:
1200 case Primitive::kPrimInt:
1201 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 break;
1204
1205 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001206 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1207 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 break;
1209
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 case Primitive::kPrimFloat:
1211 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 break;
1214
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001217 }
1218 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001219 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void LocationsBuilderX86::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 Mendellfb8d2792015-03-31 22:16:59 -04001227 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001228 if (intrinsic.TryDispatch(invoke)) {
1229 return;
1230 }
1231
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001232 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001233
1234 if (codegen_->IsBaseline()) {
1235 // Baseline does not have enough registers if the current method also
1236 // needs a register. We therefore do not require a register for it, and let
1237 // the code generation of the invoke handle it.
1238 LocationSummary* locations = invoke->GetLocations();
1239 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1240 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1241 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1242 }
1243 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001244}
1245
Mark Mendell09ed1a32015-03-25 08:30:06 -04001246static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1247 if (invoke->GetLocations()->Intrinsified()) {
1248 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1249 intrinsic.Dispatch(invoke);
1250 return true;
1251 }
1252 return false;
1253}
1254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001255void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001256 // When we do not run baseline, explicit clinit checks triggered by static
1257 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1258 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001259
Mark Mendell09ed1a32015-03-25 08:30:06 -04001260 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1261 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001262 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001263
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001264 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001265 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001266 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001267 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268}
1269
1270void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1271 HandleInvoke(invoke);
1272}
1273
1274void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001275 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001276 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001277}
1278
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001279void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001280 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001281 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1282 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001283 LocationSummary* locations = invoke->GetLocations();
1284 Location receiver = locations->InAt(0);
1285 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001286 DCHECK(receiver.IsRegister());
1287 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001288 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001289 // temp = temp->GetMethodAt(method_offset);
1290 __ movl(temp, Address(temp, method_offset));
1291 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001292 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001293 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001294
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001295 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001296 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297}
1298
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001299void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1300 HandleInvoke(invoke);
1301 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001302 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001303}
1304
1305void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1306 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1309 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310 LocationSummary* locations = invoke->GetLocations();
1311 Location receiver = locations->InAt(0);
1312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1313
1314 // Set the hidden argument.
1315 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001317
1318 // temp = object->GetClass();
1319 if (receiver.IsStackSlot()) {
1320 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1321 __ movl(temp, Address(temp, class_offset));
1322 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001323 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001324 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001325 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326 // temp = temp->GetImtEntryAt(method_offset);
1327 __ movl(temp, Address(temp, method_offset));
1328 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001329 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001330 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001331
1332 DCHECK(!codegen_->IsLeafMethod());
1333 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1334}
1335
Roland Levillain88cb1752014-10-20 16:36:47 +01001336void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1337 LocationSummary* locations =
1338 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1339 switch (neg->GetResultType()) {
1340 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001341 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001342 locations->SetInAt(0, Location::RequiresRegister());
1343 locations->SetOut(Location::SameAsFirstInput());
1344 break;
1345
Roland Levillain88cb1752014-10-20 16:36:47 +01001346 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001347 locations->SetInAt(0, Location::RequiresFpuRegister());
1348 locations->SetOut(Location::SameAsFirstInput());
1349 locations->AddTemp(Location::RequiresRegister());
1350 locations->AddTemp(Location::RequiresFpuRegister());
1351 break;
1352
Roland Levillain88cb1752014-10-20 16:36:47 +01001353 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001354 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001355 locations->SetOut(Location::SameAsFirstInput());
1356 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001357 break;
1358
1359 default:
1360 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1361 }
1362}
1363
1364void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1365 LocationSummary* locations = neg->GetLocations();
1366 Location out = locations->Out();
1367 Location in = locations->InAt(0);
1368 switch (neg->GetResultType()) {
1369 case Primitive::kPrimInt:
1370 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001371 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001372 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001373 break;
1374
1375 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001376 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001377 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001378 __ negl(out.AsRegisterPairLow<Register>());
1379 // Negation is similar to subtraction from zero. The least
1380 // significant byte triggers a borrow when it is different from
1381 // zero; to take it into account, add 1 to the most significant
1382 // byte if the carry flag (CF) is set to 1 after the first NEGL
1383 // operation.
1384 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1385 __ negl(out.AsRegisterPairHigh<Register>());
1386 break;
1387
Roland Levillain5368c212014-11-27 15:03:41 +00001388 case Primitive::kPrimFloat: {
1389 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 Register constant = locations->GetTemp(0).AsRegister<Register>();
1391 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001392 // Implement float negation with an exclusive or with value
1393 // 0x80000000 (mask for bit 31, representing the sign of a
1394 // single-precision floating-point number).
1395 __ movl(constant, Immediate(INT32_C(0x80000000)));
1396 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001397 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001398 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001399 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001400
Roland Levillain5368c212014-11-27 15:03:41 +00001401 case Primitive::kPrimDouble: {
1402 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001403 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001404 // Implement double negation with an exclusive or with value
1405 // 0x8000000000000000 (mask for bit 63, representing the sign of
1406 // a double-precision floating-point number).
1407 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001408 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001409 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001410 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001411
1412 default:
1413 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1414 }
1415}
1416
Roland Levillaindff1f282014-11-05 14:15:05 +00001417void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001418 Primitive::Type result_type = conversion->GetResultType();
1419 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001420 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001421
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001422 // The float-to-long and double-to-long type conversions rely on a
1423 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001424 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001425 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1426 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001427 ? LocationSummary::kCall
1428 : LocationSummary::kNoCall;
1429 LocationSummary* locations =
1430 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1431
David Brazdilb2bd1c52015-03-25 11:17:37 +00001432 // The Java language does not allow treating boolean as an integral type but
1433 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001434
Roland Levillaindff1f282014-11-05 14:15:05 +00001435 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001436 case Primitive::kPrimByte:
1437 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001438 case Primitive::kPrimBoolean:
1439 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001440 case Primitive::kPrimShort:
1441 case Primitive::kPrimInt:
1442 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001443 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001444 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1445 // Make the output overlap to please the register allocator. This greatly simplifies
1446 // the validation of the linear scan implementation
1447 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001448 break;
1449
1450 default:
1451 LOG(FATAL) << "Unexpected type conversion from " << input_type
1452 << " to " << result_type;
1453 }
1454 break;
1455
Roland Levillain01a8d712014-11-14 16:27:39 +00001456 case Primitive::kPrimShort:
1457 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001458 case Primitive::kPrimBoolean:
1459 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001460 case Primitive::kPrimByte:
1461 case Primitive::kPrimInt:
1462 case Primitive::kPrimChar:
1463 // Processing a Dex `int-to-short' instruction.
1464 locations->SetInAt(0, Location::Any());
1465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1466 break;
1467
1468 default:
1469 LOG(FATAL) << "Unexpected type conversion from " << input_type
1470 << " to " << result_type;
1471 }
1472 break;
1473
Roland Levillain946e1432014-11-11 17:35:19 +00001474 case Primitive::kPrimInt:
1475 switch (input_type) {
1476 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001477 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001478 locations->SetInAt(0, Location::Any());
1479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1480 break;
1481
1482 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001483 // Processing a Dex `float-to-int' instruction.
1484 locations->SetInAt(0, Location::RequiresFpuRegister());
1485 locations->SetOut(Location::RequiresRegister());
1486 locations->AddTemp(Location::RequiresFpuRegister());
1487 break;
1488
Roland Levillain946e1432014-11-11 17:35:19 +00001489 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001490 // Processing a Dex `double-to-int' instruction.
1491 locations->SetInAt(0, Location::RequiresFpuRegister());
1492 locations->SetOut(Location::RequiresRegister());
1493 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001494 break;
1495
1496 default:
1497 LOG(FATAL) << "Unexpected type conversion from " << input_type
1498 << " to " << result_type;
1499 }
1500 break;
1501
Roland Levillaindff1f282014-11-05 14:15:05 +00001502 case Primitive::kPrimLong:
1503 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001504 case Primitive::kPrimBoolean:
1505 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001506 case Primitive::kPrimByte:
1507 case Primitive::kPrimShort:
1508 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001509 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001510 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 locations->SetInAt(0, Location::RegisterLocation(EAX));
1512 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1513 break;
1514
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001515 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001516 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001517 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001518 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001519 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1520 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1521
Vladimir Marko949c91f2015-01-27 10:48:44 +00001522 // The runtime helper puts the result in EAX, EDX.
1523 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001524 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001525 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001526
1527 default:
1528 LOG(FATAL) << "Unexpected type conversion from " << input_type
1529 << " to " << result_type;
1530 }
1531 break;
1532
Roland Levillain981e4542014-11-14 11:47:14 +00001533 case Primitive::kPrimChar:
1534 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001535 case Primitive::kPrimBoolean:
1536 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001537 case Primitive::kPrimByte:
1538 case Primitive::kPrimShort:
1539 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001540 // Processing a Dex `int-to-char' instruction.
1541 locations->SetInAt(0, Location::Any());
1542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1543 break;
1544
1545 default:
1546 LOG(FATAL) << "Unexpected type conversion from " << input_type
1547 << " to " << result_type;
1548 }
1549 break;
1550
Roland Levillaindff1f282014-11-05 14:15:05 +00001551 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001552 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001553 case Primitive::kPrimBoolean:
1554 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001555 case Primitive::kPrimByte:
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimChar:
1559 // Processing a Dex `int-to-float' instruction.
1560 locations->SetInAt(0, Location::RequiresRegister());
1561 locations->SetOut(Location::RequiresFpuRegister());
1562 break;
1563
1564 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001565 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001566 locations->SetInAt(0, Location::Any());
1567 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001568 break;
1569
Roland Levillaincff13742014-11-17 14:32:17 +00001570 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001571 // Processing a Dex `double-to-float' instruction.
1572 locations->SetInAt(0, Location::RequiresFpuRegister());
1573 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001574 break;
1575
1576 default:
1577 LOG(FATAL) << "Unexpected type conversion from " << input_type
1578 << " to " << result_type;
1579 };
1580 break;
1581
Roland Levillaindff1f282014-11-05 14:15:05 +00001582 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001583 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001584 case Primitive::kPrimBoolean:
1585 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001586 case Primitive::kPrimByte:
1587 case Primitive::kPrimShort:
1588 case Primitive::kPrimInt:
1589 case Primitive::kPrimChar:
1590 // Processing a Dex `int-to-double' instruction.
1591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetOut(Location::RequiresFpuRegister());
1593 break;
1594
1595 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001596 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001597 locations->SetInAt(0, Location::Any());
1598 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 break;
1600
Roland Levillaincff13742014-11-17 14:32:17 +00001601 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001602 // Processing a Dex `float-to-double' instruction.
1603 locations->SetInAt(0, Location::RequiresFpuRegister());
1604 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001605 break;
1606
1607 default:
1608 LOG(FATAL) << "Unexpected type conversion from " << input_type
1609 << " to " << result_type;
1610 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001611 break;
1612
1613 default:
1614 LOG(FATAL) << "Unexpected type conversion from " << input_type
1615 << " to " << result_type;
1616 }
1617}
1618
1619void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1620 LocationSummary* locations = conversion->GetLocations();
1621 Location out = locations->Out();
1622 Location in = locations->InAt(0);
1623 Primitive::Type result_type = conversion->GetResultType();
1624 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001625 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001626 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001627 case Primitive::kPrimByte:
1628 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001629 case Primitive::kPrimBoolean:
1630 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001631 case Primitive::kPrimShort:
1632 case Primitive::kPrimInt:
1633 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001634 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001635 if (in.IsRegister()) {
1636 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001637 } else {
1638 DCHECK(in.GetConstant()->IsIntConstant());
1639 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1640 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1641 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001642 break;
1643
1644 default:
1645 LOG(FATAL) << "Unexpected type conversion from " << input_type
1646 << " to " << result_type;
1647 }
1648 break;
1649
Roland Levillain01a8d712014-11-14 16:27:39 +00001650 case Primitive::kPrimShort:
1651 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001652 case Primitive::kPrimBoolean:
1653 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001654 case Primitive::kPrimByte:
1655 case Primitive::kPrimInt:
1656 case Primitive::kPrimChar:
1657 // Processing a Dex `int-to-short' instruction.
1658 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001660 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001662 } else {
1663 DCHECK(in.GetConstant()->IsIntConstant());
1664 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001666 }
1667 break;
1668
1669 default:
1670 LOG(FATAL) << "Unexpected type conversion from " << input_type
1671 << " to " << result_type;
1672 }
1673 break;
1674
Roland Levillain946e1432014-11-11 17:35:19 +00001675 case Primitive::kPrimInt:
1676 switch (input_type) {
1677 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001678 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001679 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001681 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001683 } else {
1684 DCHECK(in.IsConstant());
1685 DCHECK(in.GetConstant()->IsLongConstant());
1686 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001688 }
1689 break;
1690
Roland Levillain3f8f9362014-12-02 17:45:01 +00001691 case Primitive::kPrimFloat: {
1692 // Processing a Dex `float-to-int' instruction.
1693 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1694 Register output = out.AsRegister<Register>();
1695 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1696 Label done, nan;
1697
1698 __ movl(output, Immediate(kPrimIntMax));
1699 // temp = int-to-float(output)
1700 __ cvtsi2ss(temp, output);
1701 // if input >= temp goto done
1702 __ comiss(input, temp);
1703 __ j(kAboveEqual, &done);
1704 // if input == NaN goto nan
1705 __ j(kUnordered, &nan);
1706 // output = float-to-int-truncate(input)
1707 __ cvttss2si(output, input);
1708 __ jmp(&done);
1709 __ Bind(&nan);
1710 // output = 0
1711 __ xorl(output, output);
1712 __ Bind(&done);
1713 break;
1714 }
1715
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001716 case Primitive::kPrimDouble: {
1717 // Processing a Dex `double-to-int' instruction.
1718 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1719 Register output = out.AsRegister<Register>();
1720 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1721 Label done, nan;
1722
1723 __ movl(output, Immediate(kPrimIntMax));
1724 // temp = int-to-double(output)
1725 __ cvtsi2sd(temp, output);
1726 // if input >= temp goto done
1727 __ comisd(input, temp);
1728 __ j(kAboveEqual, &done);
1729 // if input == NaN goto nan
1730 __ j(kUnordered, &nan);
1731 // output = double-to-int-truncate(input)
1732 __ cvttsd2si(output, input);
1733 __ jmp(&done);
1734 __ Bind(&nan);
1735 // output = 0
1736 __ xorl(output, output);
1737 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001738 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001739 }
Roland Levillain946e1432014-11-11 17:35:19 +00001740
1741 default:
1742 LOG(FATAL) << "Unexpected type conversion from " << input_type
1743 << " to " << result_type;
1744 }
1745 break;
1746
Roland Levillaindff1f282014-11-05 14:15:05 +00001747 case Primitive::kPrimLong:
1748 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001749 case Primitive::kPrimBoolean:
1750 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001751 case Primitive::kPrimByte:
1752 case Primitive::kPrimShort:
1753 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001754 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001755 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1757 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001759 __ cdq();
1760 break;
1761
1762 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001763 // Processing a Dex `float-to-long' instruction.
1764 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001765 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1766 break;
1767
Roland Levillaindff1f282014-11-05 14:15:05 +00001768 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001769 // Processing a Dex `double-to-long' instruction.
1770 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1771 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001772 break;
1773
1774 default:
1775 LOG(FATAL) << "Unexpected type conversion from " << input_type
1776 << " to " << result_type;
1777 }
1778 break;
1779
Roland Levillain981e4542014-11-14 11:47:14 +00001780 case Primitive::kPrimChar:
1781 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001782 case Primitive::kPrimBoolean:
1783 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001784 case Primitive::kPrimByte:
1785 case Primitive::kPrimShort:
1786 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001787 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1788 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001789 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001790 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001792 } else {
1793 DCHECK(in.GetConstant()->IsIntConstant());
1794 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001795 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001796 }
1797 break;
1798
1799 default:
1800 LOG(FATAL) << "Unexpected type conversion from " << input_type
1801 << " to " << result_type;
1802 }
1803 break;
1804
Roland Levillaindff1f282014-11-05 14:15:05 +00001805 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001807 case Primitive::kPrimBoolean:
1808 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001809 case Primitive::kPrimByte:
1810 case Primitive::kPrimShort:
1811 case Primitive::kPrimInt:
1812 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001813 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001814 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001815 break;
1816
Roland Levillain6d0e4832014-11-27 18:31:21 +00001817 case Primitive::kPrimLong: {
1818 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001819 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001820
Roland Levillain232ade02015-04-20 15:14:36 +01001821 // Create stack space for the call to
1822 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1823 // TODO: enhance register allocator to ask for stack temporaries.
1824 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1825 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1826 __ subl(ESP, Immediate(adjustment));
1827 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001828
Roland Levillain232ade02015-04-20 15:14:36 +01001829 // Load the value to the FP stack, using temporaries if needed.
1830 PushOntoFPStack(in, 0, adjustment, false, true);
1831
1832 if (out.IsStackSlot()) {
1833 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1834 } else {
1835 __ fstps(Address(ESP, 0));
1836 Location stack_temp = Location::StackSlot(0);
1837 codegen_->Move32(out, stack_temp);
1838 }
1839
1840 // Remove the temporary stack space we allocated.
1841 if (adjustment != 0) {
1842 __ addl(ESP, Immediate(adjustment));
1843 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001844 break;
1845 }
1846
Roland Levillaincff13742014-11-17 14:32:17 +00001847 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001848 // Processing a Dex `double-to-float' instruction.
1849 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001850 break;
1851
1852 default:
1853 LOG(FATAL) << "Unexpected type conversion from " << input_type
1854 << " to " << result_type;
1855 };
1856 break;
1857
Roland Levillaindff1f282014-11-05 14:15:05 +00001858 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001859 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001860 case Primitive::kPrimBoolean:
1861 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001862 case Primitive::kPrimByte:
1863 case Primitive::kPrimShort:
1864 case Primitive::kPrimInt:
1865 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001866 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001867 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001868 break;
1869
Roland Levillain647b9ed2014-11-27 12:06:00 +00001870 case Primitive::kPrimLong: {
1871 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001872 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873
Roland Levillain232ade02015-04-20 15:14:36 +01001874 // Create stack space for the call to
1875 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1876 // TODO: enhance register allocator to ask for stack temporaries.
1877 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1878 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1879 __ subl(ESP, Immediate(adjustment));
1880 }
1881
1882 // Load the value to the FP stack, using temporaries if needed.
1883 PushOntoFPStack(in, 0, adjustment, false, true);
1884
1885 if (out.IsDoubleStackSlot()) {
1886 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1887 } else {
1888 __ fstpl(Address(ESP, 0));
1889 Location stack_temp = Location::DoubleStackSlot(0);
1890 codegen_->Move64(out, stack_temp);
1891 }
1892
1893 // Remove the temporary stack space we allocated.
1894 if (adjustment != 0) {
1895 __ addl(ESP, Immediate(adjustment));
1896 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001897 break;
1898 }
1899
Roland Levillaincff13742014-11-17 14:32:17 +00001900 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001901 // Processing a Dex `float-to-double' instruction.
1902 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001903 break;
1904
1905 default:
1906 LOG(FATAL) << "Unexpected type conversion from " << input_type
1907 << " to " << result_type;
1908 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001909 break;
1910
1911 default:
1912 LOG(FATAL) << "Unexpected type conversion from " << input_type
1913 << " to " << result_type;
1914 }
1915}
1916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001917void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 LocationSummary* locations =
1919 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001921 case Primitive::kPrimInt: {
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1925 break;
1926 }
1927
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001929 locations->SetInAt(0, Location::RequiresRegister());
1930 locations->SetInAt(1, Location::Any());
1931 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 break;
1933 }
1934
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 case Primitive::kPrimFloat:
1936 case Primitive::kPrimDouble: {
1937 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001938 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001941 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001943 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1945 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001947}
1948
1949void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1950 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001951 Location first = locations->InAt(0);
1952 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001953 Location out = locations->Out();
1954
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001958 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1959 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001960 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1961 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001962 } else {
1963 __ leal(out.AsRegister<Register>(), Address(
1964 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1965 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001967 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1968 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1969 __ addl(out.AsRegister<Register>(), Immediate(value));
1970 } else {
1971 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1972 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001973 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001974 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001975 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001976 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001977 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001978 }
1979
1980 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001981 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1983 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001984 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001985 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1986 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001987 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001988 } else {
1989 DCHECK(second.IsConstant()) << second;
1990 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1991 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1992 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001993 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001994 break;
1995 }
1996
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001997 case Primitive::kPrimFloat: {
1998 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002001 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 }
2003
2004 case Primitive::kPrimDouble: {
2005 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002006 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002007 }
2008 break;
2009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002011 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002012 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002013 }
2014}
2015
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002016void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 LocationSummary* locations =
2018 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002021 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::Any());
2024 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025 break;
2026 }
Calin Juravle11351682014-10-23 15:38:15 +01002027 case Primitive::kPrimFloat:
2028 case Primitive::kPrimDouble: {
2029 locations->SetInAt(0, Location::RequiresFpuRegister());
2030 locations->SetInAt(1, Location::RequiresFpuRegister());
2031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002032 break;
Calin Juravle11351682014-10-23 15:38:15 +01002033 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002035 default:
Calin Juravle11351682014-10-23 15:38:15 +01002036 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038}
2039
2040void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2041 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002042 Location first = locations->InAt(0);
2043 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002044 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002045 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002050 __ subl(first.AsRegister<Register>(),
2051 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002052 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002054 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002055 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002056 }
2057
2058 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002059 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002060 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2061 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002062 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002063 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002064 __ sbbl(first.AsRegisterPairHigh<Register>(),
2065 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002066 } else {
2067 DCHECK(second.IsConstant()) << second;
2068 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2069 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2070 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002071 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072 break;
2073 }
2074
Calin Juravle11351682014-10-23 15:38:15 +01002075 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 break;
Calin Juravle11351682014-10-23 15:38:15 +01002078 }
2079
2080 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002082 break;
2083 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002085 default:
Calin Juravle11351682014-10-23 15:38:15 +01002086 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002087 }
2088}
2089
Calin Juravle34bacdf2014-10-07 20:23:36 +01002090void LocationsBuilderX86::VisitMul(HMul* mul) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2093 switch (mul->GetResultType()) {
2094 case Primitive::kPrimInt:
2095 locations->SetInAt(0, Location::RequiresRegister());
2096 locations->SetInAt(1, Location::Any());
2097 locations->SetOut(Location::SameAsFirstInput());
2098 break;
2099 case Primitive::kPrimLong: {
2100 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002101 locations->SetInAt(1, Location::Any());
2102 locations->SetOut(Location::SameAsFirstInput());
2103 // Needed for imul on 32bits with 64bits output.
2104 locations->AddTemp(Location::RegisterLocation(EAX));
2105 locations->AddTemp(Location::RegisterLocation(EDX));
2106 break;
2107 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002108 case Primitive::kPrimFloat:
2109 case Primitive::kPrimDouble: {
2110 locations->SetInAt(0, Location::RequiresFpuRegister());
2111 locations->SetInAt(1, Location::RequiresFpuRegister());
2112 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002113 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002114 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002115
2116 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002117 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118 }
2119}
2120
2121void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2122 LocationSummary* locations = mul->GetLocations();
2123 Location first = locations->InAt(0);
2124 Location second = locations->InAt(1);
2125 DCHECK(first.Equals(locations->Out()));
2126
2127 switch (mul->GetResultType()) {
2128 case Primitive::kPrimInt: {
2129 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002131 } else if (second.IsConstant()) {
2132 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 } else {
2135 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002137 }
2138 break;
2139 }
2140
2141 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002142 Register in1_hi = first.AsRegisterPairHigh<Register>();
2143 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 Register eax = locations->GetTemp(0).AsRegister<Register>();
2145 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002146
2147 DCHECK_EQ(EAX, eax);
2148 DCHECK_EQ(EDX, edx);
2149
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002150 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002151 // output: in1
2152 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2153 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2154 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002155 if (second.IsConstant()) {
2156 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002157
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002158 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2159 int32_t low_value = Low32Bits(value);
2160 int32_t high_value = High32Bits(value);
2161 Immediate low(low_value);
2162 Immediate high(high_value);
2163
2164 __ movl(eax, high);
2165 // eax <- in1.lo * in2.hi
2166 __ imull(eax, in1_lo);
2167 // in1.hi <- in1.hi * in2.lo
2168 __ imull(in1_hi, low);
2169 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2170 __ addl(in1_hi, eax);
2171 // move in2_lo to eax to prepare for double precision
2172 __ movl(eax, low);
2173 // edx:eax <- in1.lo * in2.lo
2174 __ mull(in1_lo);
2175 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2176 __ addl(in1_hi, edx);
2177 // in1.lo <- (in1.lo * in2.lo)[31:0];
2178 __ movl(in1_lo, eax);
2179 } else if (second.IsRegisterPair()) {
2180 Register in2_hi = second.AsRegisterPairHigh<Register>();
2181 Register in2_lo = second.AsRegisterPairLow<Register>();
2182
2183 __ movl(eax, in2_hi);
2184 // eax <- in1.lo * in2.hi
2185 __ imull(eax, in1_lo);
2186 // in1.hi <- in1.hi * in2.lo
2187 __ imull(in1_hi, in2_lo);
2188 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2189 __ addl(in1_hi, eax);
2190 // move in1_lo to eax to prepare for double precision
2191 __ movl(eax, in1_lo);
2192 // edx:eax <- in1.lo * in2.lo
2193 __ mull(in2_lo);
2194 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2195 __ addl(in1_hi, edx);
2196 // in1.lo <- (in1.lo * in2.lo)[31:0];
2197 __ movl(in1_lo, eax);
2198 } else {
2199 DCHECK(second.IsDoubleStackSlot()) << second;
2200 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2201 Address in2_lo(ESP, second.GetStackIndex());
2202
2203 __ movl(eax, in2_hi);
2204 // eax <- in1.lo * in2.hi
2205 __ imull(eax, in1_lo);
2206 // in1.hi <- in1.hi * in2.lo
2207 __ imull(in1_hi, in2_lo);
2208 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2209 __ addl(in1_hi, eax);
2210 // move in1_lo to eax to prepare for double precision
2211 __ movl(eax, in1_lo);
2212 // edx:eax <- in1.lo * in2.lo
2213 __ mull(in2_lo);
2214 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2215 __ addl(in1_hi, edx);
2216 // in1.lo <- (in1.lo * in2.lo)[31:0];
2217 __ movl(in1_lo, eax);
2218 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002219
2220 break;
2221 }
2222
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002224 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002225 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002226 }
2227
2228 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002230 break;
2231 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002232
2233 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002234 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002235 }
2236}
2237
Roland Levillain232ade02015-04-20 15:14:36 +01002238void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2239 uint32_t temp_offset,
2240 uint32_t stack_adjustment,
2241 bool is_fp,
2242 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002243 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002244 DCHECK(!is_wide);
2245 if (is_fp) {
2246 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2247 } else {
2248 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2249 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002250 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002251 DCHECK(is_wide);
2252 if (is_fp) {
2253 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2254 } else {
2255 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2256 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002257 } else {
2258 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002259 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002260 Location stack_temp = Location::StackSlot(temp_offset);
2261 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002262 if (is_fp) {
2263 __ flds(Address(ESP, temp_offset));
2264 } else {
2265 __ filds(Address(ESP, temp_offset));
2266 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002267 } else {
2268 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2269 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002270 if (is_fp) {
2271 __ fldl(Address(ESP, temp_offset));
2272 } else {
2273 __ fildl(Address(ESP, temp_offset));
2274 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002275 }
2276 }
2277}
2278
2279void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2280 Primitive::Type type = rem->GetResultType();
2281 bool is_float = type == Primitive::kPrimFloat;
2282 size_t elem_size = Primitive::ComponentSize(type);
2283 LocationSummary* locations = rem->GetLocations();
2284 Location first = locations->InAt(0);
2285 Location second = locations->InAt(1);
2286 Location out = locations->Out();
2287
2288 // Create stack space for 2 elements.
2289 // TODO: enhance register allocator to ask for stack temporaries.
2290 __ subl(ESP, Immediate(2 * elem_size));
2291
2292 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002293 const bool is_wide = !is_float;
2294 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2295 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002296
2297 // Loop doing FPREM until we stabilize.
2298 Label retry;
2299 __ Bind(&retry);
2300 __ fprem();
2301
2302 // Move FP status to AX.
2303 __ fstsw();
2304
2305 // And see if the argument reduction is complete. This is signaled by the
2306 // C2 FPU flag bit set to 0.
2307 __ andl(EAX, Immediate(kC2ConditionMask));
2308 __ j(kNotEqual, &retry);
2309
2310 // We have settled on the final value. Retrieve it into an XMM register.
2311 // Store FP top of stack to real stack.
2312 if (is_float) {
2313 __ fsts(Address(ESP, 0));
2314 } else {
2315 __ fstl(Address(ESP, 0));
2316 }
2317
2318 // Pop the 2 items from the FP stack.
2319 __ fucompp();
2320
2321 // Load the value from the stack into an XMM register.
2322 DCHECK(out.IsFpuRegister()) << out;
2323 if (is_float) {
2324 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2325 } else {
2326 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2327 }
2328
2329 // And remove the temporary stack space we allocated.
2330 __ addl(ESP, Immediate(2 * elem_size));
2331}
2332
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002333
2334void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2335 DCHECK(instruction->IsDiv() || instruction->IsRem());
2336
2337 LocationSummary* locations = instruction->GetLocations();
2338 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002339 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002340
2341 Register out_register = locations->Out().AsRegister<Register>();
2342 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002343 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002344
2345 DCHECK(imm == 1 || imm == -1);
2346
2347 if (instruction->IsRem()) {
2348 __ xorl(out_register, out_register);
2349 } else {
2350 __ movl(out_register, input_register);
2351 if (imm == -1) {
2352 __ negl(out_register);
2353 }
2354 }
2355}
2356
2357
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002358void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002359 LocationSummary* locations = instruction->GetLocations();
2360
2361 Register out_register = locations->Out().AsRegister<Register>();
2362 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002363 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002364
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002365 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002366 Register num = locations->GetTemp(0).AsRegister<Register>();
2367
2368 __ leal(num, Address(input_register, std::abs(imm) - 1));
2369 __ testl(input_register, input_register);
2370 __ cmovl(kGreaterEqual, num, input_register);
2371 int shift = CTZ(imm);
2372 __ sarl(num, Immediate(shift));
2373
2374 if (imm < 0) {
2375 __ negl(num);
2376 }
2377
2378 __ movl(out_register, num);
2379}
2380
2381void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2382 DCHECK(instruction->IsDiv() || instruction->IsRem());
2383
2384 LocationSummary* locations = instruction->GetLocations();
2385 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2386
2387 Register eax = locations->InAt(0).AsRegister<Register>();
2388 Register out = locations->Out().AsRegister<Register>();
2389 Register num;
2390 Register edx;
2391
2392 if (instruction->IsDiv()) {
2393 edx = locations->GetTemp(0).AsRegister<Register>();
2394 num = locations->GetTemp(1).AsRegister<Register>();
2395 } else {
2396 edx = locations->Out().AsRegister<Register>();
2397 num = locations->GetTemp(0).AsRegister<Register>();
2398 }
2399
2400 DCHECK_EQ(EAX, eax);
2401 DCHECK_EQ(EDX, edx);
2402 if (instruction->IsDiv()) {
2403 DCHECK_EQ(EAX, out);
2404 } else {
2405 DCHECK_EQ(EDX, out);
2406 }
2407
2408 int64_t magic;
2409 int shift;
2410 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2411
2412 Label ndiv;
2413 Label end;
2414 // If numerator is 0, the result is 0, no computation needed.
2415 __ testl(eax, eax);
2416 __ j(kNotEqual, &ndiv);
2417
2418 __ xorl(out, out);
2419 __ jmp(&end);
2420
2421 __ Bind(&ndiv);
2422
2423 // Save the numerator.
2424 __ movl(num, eax);
2425
2426 // EAX = magic
2427 __ movl(eax, Immediate(magic));
2428
2429 // EDX:EAX = magic * numerator
2430 __ imull(num);
2431
2432 if (imm > 0 && magic < 0) {
2433 // EDX += num
2434 __ addl(edx, num);
2435 } else if (imm < 0 && magic > 0) {
2436 __ subl(edx, num);
2437 }
2438
2439 // Shift if needed.
2440 if (shift != 0) {
2441 __ sarl(edx, Immediate(shift));
2442 }
2443
2444 // EDX += 1 if EDX < 0
2445 __ movl(eax, edx);
2446 __ shrl(edx, Immediate(31));
2447 __ addl(edx, eax);
2448
2449 if (instruction->IsRem()) {
2450 __ movl(eax, num);
2451 __ imull(edx, Immediate(imm));
2452 __ subl(eax, edx);
2453 __ movl(edx, eax);
2454 } else {
2455 __ movl(eax, edx);
2456 }
2457 __ Bind(&end);
2458}
2459
Calin Juravlebacfec32014-11-14 15:54:36 +00002460void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2461 DCHECK(instruction->IsDiv() || instruction->IsRem());
2462
2463 LocationSummary* locations = instruction->GetLocations();
2464 Location out = locations->Out();
2465 Location first = locations->InAt(0);
2466 Location second = locations->InAt(1);
2467 bool is_div = instruction->IsDiv();
2468
2469 switch (instruction->GetResultType()) {
2470 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 DCHECK_EQ(EAX, first.AsRegister<Register>());
2472 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002473
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002474 if (instruction->InputAt(1)->IsIntConstant()) {
2475 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002476
2477 if (imm == 0) {
2478 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2479 } else if (imm == 1 || imm == -1) {
2480 DivRemOneOrMinusOne(instruction);
2481 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002482 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002483 } else {
2484 DCHECK(imm <= -2 || imm >= 2);
2485 GenerateDivRemWithAnyConstant(instruction);
2486 }
2487 } else {
2488 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002489 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002490 is_div);
2491 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002492
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002493 Register second_reg = second.AsRegister<Register>();
2494 // 0x80000000/-1 triggers an arithmetic exception!
2495 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2496 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 __ cmpl(second_reg, Immediate(-1));
2499 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501 // edx:eax <- sign-extended of eax
2502 __ cdq();
2503 // eax = quotient, edx = remainder
2504 __ idivl(second_reg);
2505 __ Bind(slow_path->GetExitLabel());
2506 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002507 break;
2508 }
2509
2510 case Primitive::kPrimLong: {
2511 InvokeRuntimeCallingConvention calling_convention;
2512 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2513 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2514 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2515 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2516 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2517 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2518
2519 if (is_div) {
2520 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2521 } else {
2522 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2523 }
2524 uint32_t dex_pc = is_div
2525 ? instruction->AsDiv()->GetDexPc()
2526 : instruction->AsRem()->GetDexPc();
2527 codegen_->RecordPcInfo(instruction, dex_pc);
2528
2529 break;
2530 }
2531
2532 default:
2533 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2534 }
2535}
2536
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002538 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002539 ? LocationSummary::kCall
2540 : LocationSummary::kNoCall;
2541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2542
Calin Juravle7c4954d2014-10-28 16:57:40 +00002543 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002544 case Primitive::kPrimInt: {
2545 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002546 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002547 locations->SetOut(Location::SameAsFirstInput());
2548 // Intel uses edx:eax as the dividend.
2549 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002550 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2551 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2552 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002553 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002554 locations->AddTemp(Location::RequiresRegister());
2555 }
Calin Juravled0d48522014-11-04 16:40:20 +00002556 break;
2557 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002558 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002559 InvokeRuntimeCallingConvention calling_convention;
2560 locations->SetInAt(0, Location::RegisterPairLocation(
2561 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2562 locations->SetInAt(1, Location::RegisterPairLocation(
2563 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2564 // Runtime helper puts the result in EAX, EDX.
2565 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002566 break;
2567 }
2568 case Primitive::kPrimFloat:
2569 case Primitive::kPrimDouble: {
2570 locations->SetInAt(0, Location::RequiresFpuRegister());
2571 locations->SetInAt(1, Location::RequiresFpuRegister());
2572 locations->SetOut(Location::SameAsFirstInput());
2573 break;
2574 }
2575
2576 default:
2577 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2578 }
2579}
2580
2581void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2582 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002583 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 Location first = locations->InAt(0);
2585 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002586
2587 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002588 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002590 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002591 break;
2592 }
2593
2594 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002595 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002597 break;
2598 }
2599
2600 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002601 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002603 break;
2604 }
2605
2606 default:
2607 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2608 }
2609}
2610
Calin Juravlebacfec32014-11-14 15:54:36 +00002611void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002612 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002613
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002614 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2615 ? LocationSummary::kCall
2616 : LocationSummary::kNoCall;
2617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002618
Calin Juravled2ec87d2014-12-08 14:24:46 +00002619 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002620 case Primitive::kPrimInt: {
2621 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002622 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002624 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2625 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2626 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002627 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002628 locations->AddTemp(Location::RequiresRegister());
2629 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002630 break;
2631 }
2632 case Primitive::kPrimLong: {
2633 InvokeRuntimeCallingConvention calling_convention;
2634 locations->SetInAt(0, Location::RegisterPairLocation(
2635 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2636 locations->SetInAt(1, Location::RegisterPairLocation(
2637 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2638 // Runtime helper puts the result in EAX, EDX.
2639 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2640 break;
2641 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002642 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002643 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002644 locations->SetInAt(0, Location::Any());
2645 locations->SetInAt(1, Location::Any());
2646 locations->SetOut(Location::RequiresFpuRegister());
2647 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002648 break;
2649 }
2650
2651 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002652 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002653 }
2654}
2655
2656void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2657 Primitive::Type type = rem->GetResultType();
2658 switch (type) {
2659 case Primitive::kPrimInt:
2660 case Primitive::kPrimLong: {
2661 GenerateDivRemIntegral(rem);
2662 break;
2663 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002664 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002665 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002666 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002667 break;
2668 }
2669 default:
2670 LOG(FATAL) << "Unexpected rem type " << type;
2671 }
2672}
2673
Calin Juravled0d48522014-11-04 16:40:20 +00002674void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002677 switch (instruction->GetType()) {
2678 case Primitive::kPrimInt: {
2679 locations->SetInAt(0, Location::Any());
2680 break;
2681 }
2682 case Primitive::kPrimLong: {
2683 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2684 if (!instruction->IsConstant()) {
2685 locations->AddTemp(Location::RequiresRegister());
2686 }
2687 break;
2688 }
2689 default:
2690 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2691 }
Calin Juravled0d48522014-11-04 16:40:20 +00002692 if (instruction->HasUses()) {
2693 locations->SetOut(Location::SameAsFirstInput());
2694 }
2695}
2696
2697void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2698 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2699 codegen_->AddSlowPath(slow_path);
2700
2701 LocationSummary* locations = instruction->GetLocations();
2702 Location value = locations->InAt(0);
2703
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002704 switch (instruction->GetType()) {
2705 case Primitive::kPrimInt: {
2706 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002708 __ j(kEqual, slow_path->GetEntryLabel());
2709 } else if (value.IsStackSlot()) {
2710 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2711 __ j(kEqual, slow_path->GetEntryLabel());
2712 } else {
2713 DCHECK(value.IsConstant()) << value;
2714 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2715 __ jmp(slow_path->GetEntryLabel());
2716 }
2717 }
2718 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002719 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002720 case Primitive::kPrimLong: {
2721 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002722 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002723 __ movl(temp, value.AsRegisterPairLow<Register>());
2724 __ orl(temp, value.AsRegisterPairHigh<Register>());
2725 __ j(kEqual, slow_path->GetEntryLabel());
2726 } else {
2727 DCHECK(value.IsConstant()) << value;
2728 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2729 __ jmp(slow_path->GetEntryLabel());
2730 }
2731 }
2732 break;
2733 }
2734 default:
2735 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002736 }
Calin Juravled0d48522014-11-04 16:40:20 +00002737}
2738
Calin Juravle9aec02f2014-11-18 23:06:35 +00002739void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2740 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2741
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2744
2745 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002746 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002747 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002748 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002749 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002750 // The shift count needs to be in CL or a constant.
2751 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002752 locations->SetOut(Location::SameAsFirstInput());
2753 break;
2754 }
2755 default:
2756 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2757 }
2758}
2759
2760void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2761 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2762
2763 LocationSummary* locations = op->GetLocations();
2764 Location first = locations->InAt(0);
2765 Location second = locations->InAt(1);
2766 DCHECK(first.Equals(locations->Out()));
2767
2768 switch (op->GetResultType()) {
2769 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002770 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002771 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 DCHECK_EQ(ECX, second_reg);
2775 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002778 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002780 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002781 }
2782 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002783 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2784 if (shift == 0) {
2785 return;
2786 }
2787 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002788 if (op->IsShl()) {
2789 __ shll(first_reg, imm);
2790 } else if (op->IsShr()) {
2791 __ sarl(first_reg, imm);
2792 } else {
2793 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002794 }
2795 }
2796 break;
2797 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002798 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002799 if (second.IsRegister()) {
2800 Register second_reg = second.AsRegister<Register>();
2801 DCHECK_EQ(ECX, second_reg);
2802 if (op->IsShl()) {
2803 GenerateShlLong(first, second_reg);
2804 } else if (op->IsShr()) {
2805 GenerateShrLong(first, second_reg);
2806 } else {
2807 GenerateUShrLong(first, second_reg);
2808 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002809 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002810 // Shift by a constant.
2811 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2812 // Nothing to do if the shift is 0, as the input is already the output.
2813 if (shift != 0) {
2814 if (op->IsShl()) {
2815 GenerateShlLong(first, shift);
2816 } else if (op->IsShr()) {
2817 GenerateShrLong(first, shift);
2818 } else {
2819 GenerateUShrLong(first, shift);
2820 }
2821 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002822 }
2823 break;
2824 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002825 default:
2826 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2827 }
2828}
2829
Mark P Mendell73945692015-04-29 14:56:17 +00002830void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2831 Register low = loc.AsRegisterPairLow<Register>();
2832 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002833 if (shift == 1) {
2834 // This is just an addition.
2835 __ addl(low, low);
2836 __ adcl(high, high);
2837 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002838 // Shift by 32 is easy. High gets low, and low gets 0.
2839 codegen_->EmitParallelMoves(
2840 loc.ToLow(),
2841 loc.ToHigh(),
2842 Primitive::kPrimInt,
2843 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2844 loc.ToLow(),
2845 Primitive::kPrimInt);
2846 } else if (shift > 32) {
2847 // Low part becomes 0. High part is low part << (shift-32).
2848 __ movl(high, low);
2849 __ shll(high, Immediate(shift - 32));
2850 __ xorl(low, low);
2851 } else {
2852 // Between 1 and 31.
2853 __ shld(high, low, Immediate(shift));
2854 __ shll(low, Immediate(shift));
2855 }
2856}
2857
Calin Juravle9aec02f2014-11-18 23:06:35 +00002858void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2859 Label done;
2860 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2861 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2862 __ testl(shifter, Immediate(32));
2863 __ j(kEqual, &done);
2864 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2865 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2866 __ Bind(&done);
2867}
2868
Mark P Mendell73945692015-04-29 14:56:17 +00002869void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2870 Register low = loc.AsRegisterPairLow<Register>();
2871 Register high = loc.AsRegisterPairHigh<Register>();
2872 if (shift == 32) {
2873 // Need to copy the sign.
2874 DCHECK_NE(low, high);
2875 __ movl(low, high);
2876 __ sarl(high, Immediate(31));
2877 } else if (shift > 32) {
2878 DCHECK_NE(low, high);
2879 // High part becomes sign. Low part is shifted by shift - 32.
2880 __ movl(low, high);
2881 __ sarl(high, Immediate(31));
2882 __ sarl(low, Immediate(shift - 32));
2883 } else {
2884 // Between 1 and 31.
2885 __ shrd(low, high, Immediate(shift));
2886 __ sarl(high, Immediate(shift));
2887 }
2888}
2889
Calin Juravle9aec02f2014-11-18 23:06:35 +00002890void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2891 Label done;
2892 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2893 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2894 __ testl(shifter, Immediate(32));
2895 __ j(kEqual, &done);
2896 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2897 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2898 __ Bind(&done);
2899}
2900
Mark P Mendell73945692015-04-29 14:56:17 +00002901void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2902 Register low = loc.AsRegisterPairLow<Register>();
2903 Register high = loc.AsRegisterPairHigh<Register>();
2904 if (shift == 32) {
2905 // Shift by 32 is easy. Low gets high, and high gets 0.
2906 codegen_->EmitParallelMoves(
2907 loc.ToHigh(),
2908 loc.ToLow(),
2909 Primitive::kPrimInt,
2910 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2911 loc.ToHigh(),
2912 Primitive::kPrimInt);
2913 } else if (shift > 32) {
2914 // Low part is high >> (shift - 32). High part becomes 0.
2915 __ movl(low, high);
2916 __ shrl(low, Immediate(shift - 32));
2917 __ xorl(high, high);
2918 } else {
2919 // Between 1 and 31.
2920 __ shrd(low, high, Immediate(shift));
2921 __ shrl(high, Immediate(shift));
2922 }
2923}
2924
Calin Juravle9aec02f2014-11-18 23:06:35 +00002925void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2926 Label done;
2927 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2928 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2929 __ testl(shifter, Immediate(32));
2930 __ j(kEqual, &done);
2931 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2932 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2933 __ Bind(&done);
2934}
2935
2936void LocationsBuilderX86::VisitShl(HShl* shl) {
2937 HandleShift(shl);
2938}
2939
2940void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2941 HandleShift(shl);
2942}
2943
2944void LocationsBuilderX86::VisitShr(HShr* shr) {
2945 HandleShift(shr);
2946}
2947
2948void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2949 HandleShift(shr);
2950}
2951
2952void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2953 HandleShift(ushr);
2954}
2955
2956void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2957 HandleShift(ushr);
2958}
2959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002960void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002963 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002964 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002965 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002966 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002967}
2968
2969void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2970 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002971 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002972 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002973
Nicolas Geoffray39468442014-09-02 15:17:15 +01002974 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002975 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002976}
2977
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002978void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2981 locations->SetOut(Location::RegisterLocation(EAX));
2982 InvokeRuntimeCallingConvention calling_convention;
2983 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002985 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002986}
2987
2988void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2989 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002990 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2991
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002992 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002993
2994 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2995 DCHECK(!codegen_->IsLeafMethod());
2996}
2997
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002998void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002999 LocationSummary* locations =
3000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003001 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3002 if (location.IsStackSlot()) {
3003 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3004 } else if (location.IsDoubleStackSlot()) {
3005 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003007 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003008}
3009
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003010void InstructionCodeGeneratorX86::VisitParameterValue(
3011 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3012}
3013
3014void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3017 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3018}
3019
3020void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003021}
3022
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003023void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003024 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003025 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003026 locations->SetInAt(0, Location::RequiresRegister());
3027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003028}
3029
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003030void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3031 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003032 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003033 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003034 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003035 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003036 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003038 break;
3039
3040 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003041 __ notl(out.AsRegisterPairLow<Register>());
3042 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003043 break;
3044
3045 default:
3046 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3047 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003048}
3049
David Brazdil66d126e2015-04-03 16:02:44 +01003050void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3053 locations->SetInAt(0, Location::RequiresRegister());
3054 locations->SetOut(Location::SameAsFirstInput());
3055}
3056
3057void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003058 LocationSummary* locations = bool_not->GetLocations();
3059 Location in = locations->InAt(0);
3060 Location out = locations->Out();
3061 DCHECK(in.Equals(out));
3062 __ xorl(out.AsRegister<Register>(), Immediate(1));
3063}
3064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003065void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003066 LocationSummary* locations =
3067 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003068 switch (compare->InputAt(0)->GetType()) {
3069 case Primitive::kPrimLong: {
3070 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003071 locations->SetInAt(1, Location::Any());
3072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3073 break;
3074 }
3075 case Primitive::kPrimFloat:
3076 case Primitive::kPrimDouble: {
3077 locations->SetInAt(0, Location::RequiresFpuRegister());
3078 locations->SetInAt(1, Location::RequiresFpuRegister());
3079 locations->SetOut(Location::RequiresRegister());
3080 break;
3081 }
3082 default:
3083 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3084 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003085}
3086
3087void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003088 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003090 Location left = locations->InAt(0);
3091 Location right = locations->InAt(1);
3092
3093 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003094 switch (compare->InputAt(0)->GetType()) {
3095 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003096 Register left_low = left.AsRegisterPairLow<Register>();
3097 Register left_high = left.AsRegisterPairHigh<Register>();
3098 int32_t val_low = 0;
3099 int32_t val_high = 0;
3100 bool right_is_const = false;
3101
3102 if (right.IsConstant()) {
3103 DCHECK(right.GetConstant()->IsLongConstant());
3104 right_is_const = true;
3105 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3106 val_low = Low32Bits(val);
3107 val_high = High32Bits(val);
3108 }
3109
Calin Juravleddb7df22014-11-25 20:56:51 +00003110 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003111 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003112 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003113 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003114 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003115 DCHECK(right_is_const) << right;
3116 if (val_high == 0) {
3117 __ testl(left_high, left_high);
3118 } else {
3119 __ cmpl(left_high, Immediate(val_high));
3120 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003121 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003122 __ j(kLess, &less); // Signed compare.
3123 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003124 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003125 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003126 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003127 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003128 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003129 DCHECK(right_is_const) << right;
3130 if (val_low == 0) {
3131 __ testl(left_low, left_low);
3132 } else {
3133 __ cmpl(left_low, Immediate(val_low));
3134 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003135 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003136 break;
3137 }
3138 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003139 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003140 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3141 break;
3142 }
3143 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003145 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003146 break;
3147 }
3148 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003149 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003150 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003151 __ movl(out, Immediate(0));
3152 __ j(kEqual, &done);
3153 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3154
3155 __ Bind(&greater);
3156 __ movl(out, Immediate(1));
3157 __ jmp(&done);
3158
3159 __ Bind(&less);
3160 __ movl(out, Immediate(-1));
3161
3162 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003163}
3164
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003165void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003166 LocationSummary* locations =
3167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003168 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3169 locations->SetInAt(i, Location::Any());
3170 }
3171 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003172}
3173
3174void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003175 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003176 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003177}
3178
Calin Juravle52c48962014-12-16 17:02:57 +00003179void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3180 /*
3181 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3182 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3183 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3184 */
3185 switch (kind) {
3186 case MemBarrierKind::kAnyAny: {
3187 __ mfence();
3188 break;
3189 }
3190 case MemBarrierKind::kAnyStore:
3191 case MemBarrierKind::kLoadAny:
3192 case MemBarrierKind::kStoreStore: {
3193 // nop
3194 break;
3195 }
3196 default:
3197 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003198 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003199}
3200
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003201
Mark Mendell09ed1a32015-03-25 08:30:06 -04003202void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003203 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003204 // TODO: Implement all kinds of calls:
3205 // 1) boot -> boot
3206 // 2) app -> boot
3207 // 3) app -> app
3208 //
3209 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003210
3211 if (invoke->IsStringInit()) {
3212 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003213 Register reg = temp.AsRegister<Register>();
3214 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003215 // (temp + offset_of_quick_compiled_code)()
3216 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003217 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3218 } else if (invoke->IsRecursive()) {
3219 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003220 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003221 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3222
3223 Register method_reg;
3224 Register reg = temp.AsRegister<Register>();
3225 if (current_method.IsRegister()) {
3226 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003227 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003228 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003229 DCHECK(!current_method.IsValid());
3230 method_reg = reg;
3231 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003232 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003233 // temp = temp->dex_cache_resolved_methods_;
3234 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3235 // temp = temp[index_in_cache]
3236 __ movl(reg, Address(reg,
3237 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3238 // (temp + offset_of_quick_compiled_code)()
3239 __ call(Address(reg,
3240 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003241 }
3242
3243 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003244}
3245
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003246void CodeGeneratorX86::MarkGCCard(Register temp,
3247 Register card,
3248 Register object,
3249 Register value,
3250 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003251 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003252 if (value_can_be_null) {
3253 __ testl(value, value);
3254 __ j(kEqual, &is_null);
3255 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003256 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3257 __ movl(temp, object);
3258 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003259 __ movb(Address(temp, card, TIMES_1, 0),
3260 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003261 if (value_can_be_null) {
3262 __ Bind(&is_null);
3263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264}
3265
Calin Juravle52c48962014-12-16 17:02:57 +00003266void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3267 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003268 LocationSummary* locations =
3269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003270 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003271
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003272 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3273 locations->SetOut(Location::RequiresFpuRegister());
3274 } else {
3275 // The output overlaps in case of long: we don't want the low move to overwrite
3276 // the object's location.
3277 locations->SetOut(Location::RequiresRegister(),
3278 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3279 : Location::kNoOutputOverlap);
3280 }
Calin Juravle52c48962014-12-16 17:02:57 +00003281
3282 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3283 // Long values can be loaded atomically into an XMM using movsd.
3284 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3285 // and then copy the XMM into the output 32bits at a time).
3286 locations->AddTemp(Location::RequiresFpuRegister());
3287 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003288}
3289
Calin Juravle52c48962014-12-16 17:02:57 +00003290void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3291 const FieldInfo& field_info) {
3292 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003293
Calin Juravle52c48962014-12-16 17:02:57 +00003294 LocationSummary* locations = instruction->GetLocations();
3295 Register base = locations->InAt(0).AsRegister<Register>();
3296 Location out = locations->Out();
3297 bool is_volatile = field_info.IsVolatile();
3298 Primitive::Type field_type = field_info.GetFieldType();
3299 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3300
3301 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003302 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003303 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003304 break;
3305 }
3306
3307 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003308 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003309 break;
3310 }
3311
3312 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003313 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003314 break;
3315 }
3316
3317 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003318 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003319 break;
3320 }
3321
3322 case Primitive::kPrimInt:
3323 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003324 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003325 break;
3326 }
3327
3328 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003329 if (is_volatile) {
3330 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3331 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003332 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003333 __ movd(out.AsRegisterPairLow<Register>(), temp);
3334 __ psrlq(temp, Immediate(32));
3335 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3336 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003337 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003338 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003339 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003340 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3341 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003342 break;
3343 }
3344
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003345 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003346 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003347 break;
3348 }
3349
3350 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003351 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003352 break;
3353 }
3354
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003355 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003356 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003357 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358 }
Calin Juravle52c48962014-12-16 17:02:57 +00003359
Calin Juravle77520bc2015-01-12 18:45:46 +00003360 // Longs are handled in the switch.
3361 if (field_type != Primitive::kPrimLong) {
3362 codegen_->MaybeRecordImplicitNullCheck(instruction);
3363 }
3364
Calin Juravle52c48962014-12-16 17:02:57 +00003365 if (is_volatile) {
3366 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3367 }
3368}
3369
3370void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3371 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3372
3373 LocationSummary* locations =
3374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3375 locations->SetInAt(0, Location::RequiresRegister());
3376 bool is_volatile = field_info.IsVolatile();
3377 Primitive::Type field_type = field_info.GetFieldType();
3378 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3379 || (field_type == Primitive::kPrimByte);
3380
3381 // The register allocator does not support multiple
3382 // inputs that die at entry with one in a specific register.
3383 if (is_byte_type) {
3384 // Ensure the value is in a byte register.
3385 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003386 } else if (Primitive::IsFloatingPointType(field_type)) {
3387 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003388 } else {
3389 locations->SetInAt(1, Location::RequiresRegister());
3390 }
3391 // Temporary registers for the write barrier.
3392 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3393 locations->AddTemp(Location::RequiresRegister());
3394 // Ensure the card is in a byte register.
3395 locations->AddTemp(Location::RegisterLocation(ECX));
3396 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3397 // 64bits value can be atomically written to an address with movsd and an XMM register.
3398 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3399 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3400 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3401 // isolated cases when we need this it isn't worth adding the extra complexity.
3402 locations->AddTemp(Location::RequiresFpuRegister());
3403 locations->AddTemp(Location::RequiresFpuRegister());
3404 }
3405}
3406
3407void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003408 const FieldInfo& field_info,
3409 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003410 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3411
3412 LocationSummary* locations = instruction->GetLocations();
3413 Register base = locations->InAt(0).AsRegister<Register>();
3414 Location value = locations->InAt(1);
3415 bool is_volatile = field_info.IsVolatile();
3416 Primitive::Type field_type = field_info.GetFieldType();
3417 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3418
3419 if (is_volatile) {
3420 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3421 }
3422
3423 switch (field_type) {
3424 case Primitive::kPrimBoolean:
3425 case Primitive::kPrimByte: {
3426 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3427 break;
3428 }
3429
3430 case Primitive::kPrimShort:
3431 case Primitive::kPrimChar: {
3432 __ movw(Address(base, offset), value.AsRegister<Register>());
3433 break;
3434 }
3435
3436 case Primitive::kPrimInt:
3437 case Primitive::kPrimNot: {
3438 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003439 break;
3440 }
3441
3442 case Primitive::kPrimLong: {
3443 if (is_volatile) {
3444 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3445 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3446 __ movd(temp1, value.AsRegisterPairLow<Register>());
3447 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3448 __ punpckldq(temp1, temp2);
3449 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003450 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003451 } else {
3452 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003453 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003454 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3455 }
3456 break;
3457 }
3458
3459 case Primitive::kPrimFloat: {
3460 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3461 break;
3462 }
3463
3464 case Primitive::kPrimDouble: {
3465 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3466 break;
3467 }
3468
3469 case Primitive::kPrimVoid:
3470 LOG(FATAL) << "Unreachable type " << field_type;
3471 UNREACHABLE();
3472 }
3473
Calin Juravle77520bc2015-01-12 18:45:46 +00003474 // Longs are handled in the switch.
3475 if (field_type != Primitive::kPrimLong) {
3476 codegen_->MaybeRecordImplicitNullCheck(instruction);
3477 }
3478
3479 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3480 Register temp = locations->GetTemp(0).AsRegister<Register>();
3481 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003482 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003483 }
3484
Calin Juravle52c48962014-12-16 17:02:57 +00003485 if (is_volatile) {
3486 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3487 }
3488}
3489
3490void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3491 HandleFieldGet(instruction, instruction->GetFieldInfo());
3492}
3493
3494void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3495 HandleFieldGet(instruction, instruction->GetFieldInfo());
3496}
3497
3498void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3499 HandleFieldSet(instruction, instruction->GetFieldInfo());
3500}
3501
3502void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003503 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003504}
3505
3506void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3507 HandleFieldSet(instruction, instruction->GetFieldInfo());
3508}
3509
3510void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003511 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003512}
3513
3514void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3515 HandleFieldGet(instruction, instruction->GetFieldInfo());
3516}
3517
3518void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3519 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003520}
3521
3522void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003523 LocationSummary* locations =
3524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003525 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3526 ? Location::RequiresRegister()
3527 : Location::Any();
3528 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003529 if (instruction->HasUses()) {
3530 locations->SetOut(Location::SameAsFirstInput());
3531 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003532}
3533
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003534void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003535 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3536 return;
3537 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003538 LocationSummary* locations = instruction->GetLocations();
3539 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003540
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003541 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3542 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3543}
3544
3545void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003546 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003547 codegen_->AddSlowPath(slow_path);
3548
3549 LocationSummary* locations = instruction->GetLocations();
3550 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003551
3552 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003553 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003554 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003555 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003556 } else {
3557 DCHECK(obj.IsConstant()) << obj;
3558 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3559 __ jmp(slow_path->GetEntryLabel());
3560 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003561 }
3562 __ j(kEqual, slow_path->GetEntryLabel());
3563}
3564
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003565void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3566 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3567 GenerateImplicitNullCheck(instruction);
3568 } else {
3569 GenerateExplicitNullCheck(instruction);
3570 }
3571}
3572
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003574 LocationSummary* locations =
3575 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003576 locations->SetInAt(0, Location::RequiresRegister());
3577 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003578 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3579 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3580 } else {
3581 // The output overlaps in case of long: we don't want the low move to overwrite
3582 // the array's location.
3583 locations->SetOut(Location::RequiresRegister(),
3584 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3585 : Location::kNoOutputOverlap);
3586 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003587}
3588
3589void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3590 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 Location index = locations->InAt(1);
3593
Calin Juravle77520bc2015-01-12 18:45:46 +00003594 Primitive::Type type = instruction->GetType();
3595 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 case Primitive::kPrimBoolean: {
3597 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599 if (index.IsConstant()) {
3600 __ movzxb(out, Address(obj,
3601 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3602 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 }
3605 break;
3606 }
3607
3608 case Primitive::kPrimByte: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 if (index.IsConstant()) {
3612 __ movsxb(out, Address(obj,
3613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 }
3617 break;
3618 }
3619
3620 case Primitive::kPrimShort: {
3621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623 if (index.IsConstant()) {
3624 __ movsxw(out, Address(obj,
3625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 }
3629 break;
3630 }
3631
3632 case Primitive::kPrimChar: {
3633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 if (index.IsConstant()) {
3636 __ movzxw(out, Address(obj,
3637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3638 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640 }
3641 break;
3642 }
3643
3644 case Primitive::kPrimInt:
3645 case Primitive::kPrimNot: {
3646 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648 if (index.IsConstant()) {
3649 __ movl(out, Address(obj,
3650 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3651 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 }
3654 break;
3655 }
3656
3657 case Primitive::kPrimLong: {
3658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003659 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003660 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 if (index.IsConstant()) {
3662 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003663 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003664 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003665 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003667 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003669 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003670 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003672 }
3673 break;
3674 }
3675
Mark Mendell7c8d0092015-01-26 11:21:33 -05003676 case Primitive::kPrimFloat: {
3677 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3678 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3679 if (index.IsConstant()) {
3680 __ movss(out, Address(obj,
3681 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3682 } else {
3683 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3684 }
3685 break;
3686 }
3687
3688 case Primitive::kPrimDouble: {
3689 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3690 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3691 if (index.IsConstant()) {
3692 __ movsd(out, Address(obj,
3693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3694 } else {
3695 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3696 }
3697 break;
3698 }
3699
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003701 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003702 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003703 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003704
3705 if (type != Primitive::kPrimLong) {
3706 codegen_->MaybeRecordImplicitNullCheck(instruction);
3707 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003708}
3709
3710void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003711 // This location builder might end up asking to up to four registers, which is
3712 // not currently possible for baseline. The situation in which we need four
3713 // registers cannot be met by baseline though, because it has not run any
3714 // optimization.
3715
Nicolas Geoffray39468442014-09-02 15:17:15 +01003716 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003717 bool needs_write_barrier =
3718 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3719
Mark Mendell5f874182015-03-04 15:42:45 -05003720 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003721
Nicolas Geoffray39468442014-09-02 15:17:15 +01003722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3723 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003724 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003725
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003726 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003728 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3729 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3730 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003732 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3733 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003734 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003735 // In case of a byte operation, the register allocator does not support multiple
3736 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003737 locations->SetInAt(0, Location::RequiresRegister());
3738 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003739 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003740 // Ensure the value is in a byte register.
3741 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003742 } else if (Primitive::IsFloatingPointType(value_type)) {
3743 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003745 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003746 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003747 // Temporary registers for the write barrier.
3748 if (needs_write_barrier) {
3749 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003750 // Ensure the card is in a byte register.
3751 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003752 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003754}
3755
3756void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3757 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003760 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003761 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003762 bool needs_runtime_call = locations->WillCall();
3763 bool needs_write_barrier =
3764 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765
3766 switch (value_type) {
3767 case Primitive::kPrimBoolean:
3768 case Primitive::kPrimByte: {
3769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 if (index.IsConstant()) {
3771 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003772 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003773 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003774 } else {
3775 __ movb(Address(obj, offset),
3776 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3777 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003779 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003781 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003784 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3785 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003787 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 break;
3789 }
3790
3791 case Primitive::kPrimShort:
3792 case Primitive::kPrimChar: {
3793 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 if (index.IsConstant()) {
3795 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003796 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003797 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003798 } else {
3799 __ movw(Address(obj, offset),
3800 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3801 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003803 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003804 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3805 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003806 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003807 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003808 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3809 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003810 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 break;
3813 }
3814
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003815 case Primitive::kPrimInt:
3816 case Primitive::kPrimNot: {
3817 if (!needs_runtime_call) {
3818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3819 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003820 size_t offset =
3821 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003822 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003824 } else {
3825 DCHECK(value.IsConstant()) << value;
3826 __ movl(Address(obj, offset),
3827 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3828 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003829 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003830 DCHECK(index.IsRegister()) << index;
3831 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003832 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3833 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003834 } else {
3835 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003836 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003837 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3838 }
3839 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003840 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003841
3842 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003843 Register temp = locations->GetTemp(0).AsRegister<Register>();
3844 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003845 codegen_->MarkGCCard(
3846 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003847 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003848 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003849 DCHECK_EQ(value_type, Primitive::kPrimNot);
3850 DCHECK(!codegen_->IsLeafMethod());
3851 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3852 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853 }
3854 break;
3855 }
3856
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003857 case Primitive::kPrimLong: {
3858 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003859 if (index.IsConstant()) {
3860 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003861 if (value.IsRegisterPair()) {
3862 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003864 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003865 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003866 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003867 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3868 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003870 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3871 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003872 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003873 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003875 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003878 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003879 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003880 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003881 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003883 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003884 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003886 Immediate(High32Bits(val)));
3887 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 }
3889 break;
3890 }
3891
Mark Mendell7c8d0092015-01-26 11:21:33 -05003892 case Primitive::kPrimFloat: {
3893 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3894 DCHECK(value.IsFpuRegister());
3895 if (index.IsConstant()) {
3896 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3897 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3898 } else {
3899 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3900 value.AsFpuRegister<XmmRegister>());
3901 }
3902 break;
3903 }
3904
3905 case Primitive::kPrimDouble: {
3906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3907 DCHECK(value.IsFpuRegister());
3908 if (index.IsConstant()) {
3909 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3910 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3911 } else {
3912 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3913 value.AsFpuRegister<XmmRegister>());
3914 }
3915 break;
3916 }
3917
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 case Primitive::kPrimVoid:
3919 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003920 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921 }
3922}
3923
3924void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003926 locations->SetInAt(0, Location::RequiresRegister());
3927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928}
3929
3930void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3931 LocationSummary* locations = instruction->GetLocations();
3932 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 Register obj = locations->InAt(0).AsRegister<Register>();
3934 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003936 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003937}
3938
3939void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003940 LocationSummary* locations =
3941 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003942 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003943 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003944 if (instruction->HasUses()) {
3945 locations->SetOut(Location::SameAsFirstInput());
3946 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947}
3948
3949void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3950 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003951 Location index_loc = locations->InAt(0);
3952 Location length_loc = locations->InAt(1);
3953 SlowPathCodeX86* slow_path =
3954 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955
Mark Mendell99dbd682015-04-22 16:18:52 -04003956 if (length_loc.IsConstant()) {
3957 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3958 if (index_loc.IsConstant()) {
3959 // BCE will remove the bounds check if we are guarenteed to pass.
3960 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3961 if (index < 0 || index >= length) {
3962 codegen_->AddSlowPath(slow_path);
3963 __ jmp(slow_path->GetEntryLabel());
3964 } else {
3965 // Some optimization after BCE may have generated this, and we should not
3966 // generate a bounds check if it is a valid range.
3967 }
3968 return;
3969 }
3970
3971 // We have to reverse the jump condition because the length is the constant.
3972 Register index_reg = index_loc.AsRegister<Register>();
3973 __ cmpl(index_reg, Immediate(length));
3974 codegen_->AddSlowPath(slow_path);
3975 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003976 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003977 Register length = length_loc.AsRegister<Register>();
3978 if (index_loc.IsConstant()) {
3979 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3980 __ cmpl(length, Immediate(value));
3981 } else {
3982 __ cmpl(length, index_loc.AsRegister<Register>());
3983 }
3984 codegen_->AddSlowPath(slow_path);
3985 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003986 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987}
3988
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3990 temp->SetLocations(nullptr);
3991}
3992
3993void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3994 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003995 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003996}
3997
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003998void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003999 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004000 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004001}
4002
4003void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004004 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4005}
4006
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004007void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4009}
4010
4011void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004012 HBasicBlock* block = instruction->GetBlock();
4013 if (block->GetLoopInformation() != nullptr) {
4014 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4015 // The back edge will generate the suspend check.
4016 return;
4017 }
4018 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4019 // The goto will generate the suspend check.
4020 return;
4021 }
4022 GenerateSuspendCheck(instruction, nullptr);
4023}
4024
4025void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4026 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004027 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004028 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4029 if (slow_path == nullptr) {
4030 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4031 instruction->SetSlowPath(slow_path);
4032 codegen_->AddSlowPath(slow_path);
4033 if (successor != nullptr) {
4034 DCHECK(successor->IsLoopHeader());
4035 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4036 }
4037 } else {
4038 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4039 }
4040
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004041 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004042 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004043 if (successor == nullptr) {
4044 __ j(kNotEqual, slow_path->GetEntryLabel());
4045 __ Bind(slow_path->GetReturnLabel());
4046 } else {
4047 __ j(kEqual, codegen_->GetLabelOf(successor));
4048 __ jmp(slow_path->GetEntryLabel());
4049 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004050}
4051
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004052X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4053 return codegen_->GetAssembler();
4054}
4055
Mark Mendell7c8d0092015-01-26 11:21:33 -05004056void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004057 ScratchRegisterScope ensure_scratch(
4058 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4059 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4060 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4061 __ movl(temp_reg, Address(ESP, src + stack_offset));
4062 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004063}
4064
4065void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004066 ScratchRegisterScope ensure_scratch(
4067 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4068 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4069 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4070 __ movl(temp_reg, Address(ESP, src + stack_offset));
4071 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4072 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4073 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004074}
4075
4076void ParallelMoveResolverX86::EmitMove(size_t index) {
4077 MoveOperands* move = moves_.Get(index);
4078 Location source = move->GetSource();
4079 Location destination = move->GetDestination();
4080
4081 if (source.IsRegister()) {
4082 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004083 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004084 } else {
4085 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004087 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004088 } else if (source.IsFpuRegister()) {
4089 if (destination.IsFpuRegister()) {
4090 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4091 } else if (destination.IsStackSlot()) {
4092 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4093 } else {
4094 DCHECK(destination.IsDoubleStackSlot());
4095 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4096 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004097 } else if (source.IsStackSlot()) {
4098 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004100 } else if (destination.IsFpuRegister()) {
4101 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004102 } else {
4103 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004104 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4105 }
4106 } else if (source.IsDoubleStackSlot()) {
4107 if (destination.IsFpuRegister()) {
4108 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4109 } else {
4110 DCHECK(destination.IsDoubleStackSlot()) << destination;
4111 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004112 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004113 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004114 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004115 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004116 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004117 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004118 if (value == 0) {
4119 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4120 } else {
4121 __ movl(destination.AsRegister<Register>(), Immediate(value));
4122 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004123 } else {
4124 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004125 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004126 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004127 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004128 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004129 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004130 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004131 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004132 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4133 if (value == 0) {
4134 // Easy handling of 0.0.
4135 __ xorps(dest, dest);
4136 } else {
4137 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004138 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4139 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4140 __ movl(temp, Immediate(value));
4141 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004142 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004143 } else {
4144 DCHECK(destination.IsStackSlot()) << destination;
4145 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4146 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004147 } else if (constant->IsLongConstant()) {
4148 int64_t value = constant->AsLongConstant()->GetValue();
4149 int32_t low_value = Low32Bits(value);
4150 int32_t high_value = High32Bits(value);
4151 Immediate low(low_value);
4152 Immediate high(high_value);
4153 if (destination.IsDoubleStackSlot()) {
4154 __ movl(Address(ESP, destination.GetStackIndex()), low);
4155 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4156 } else {
4157 __ movl(destination.AsRegisterPairLow<Register>(), low);
4158 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4159 }
4160 } else {
4161 DCHECK(constant->IsDoubleConstant());
4162 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004163 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004164 int32_t low_value = Low32Bits(value);
4165 int32_t high_value = High32Bits(value);
4166 Immediate low(low_value);
4167 Immediate high(high_value);
4168 if (destination.IsFpuRegister()) {
4169 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4170 if (value == 0) {
4171 // Easy handling of 0.0.
4172 __ xorpd(dest, dest);
4173 } else {
4174 __ pushl(high);
4175 __ pushl(low);
4176 __ movsd(dest, Address(ESP, 0));
4177 __ addl(ESP, Immediate(8));
4178 }
4179 } else {
4180 DCHECK(destination.IsDoubleStackSlot()) << destination;
4181 __ movl(Address(ESP, destination.GetStackIndex()), low);
4182 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4183 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004184 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004185 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004186 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004187 }
4188}
4189
Mark Mendella5c19ce2015-04-01 12:51:05 -04004190void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004191 Register suggested_scratch = reg == EAX ? EBX : EAX;
4192 ScratchRegisterScope ensure_scratch(
4193 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4194
4195 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4196 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4197 __ movl(Address(ESP, mem + stack_offset), reg);
4198 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004199}
4200
Mark Mendell7c8d0092015-01-26 11:21:33 -05004201void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004202 ScratchRegisterScope ensure_scratch(
4203 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4204
4205 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4206 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4207 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4208 __ movss(Address(ESP, mem + stack_offset), reg);
4209 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004210}
4211
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004212void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004213 ScratchRegisterScope ensure_scratch1(
4214 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004215
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004216 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4217 ScratchRegisterScope ensure_scratch2(
4218 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004219
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004220 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4221 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4222 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4223 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4224 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4225 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004226}
4227
4228void ParallelMoveResolverX86::EmitSwap(size_t index) {
4229 MoveOperands* move = moves_.Get(index);
4230 Location source = move->GetSource();
4231 Location destination = move->GetDestination();
4232
4233 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004234 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004235 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004236 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004237 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004238 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004239 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4240 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004241 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4242 // Use XOR Swap algorithm to avoid a temporary.
4243 DCHECK_NE(source.reg(), destination.reg());
4244 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4245 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4246 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4247 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4248 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4249 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4250 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004251 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4252 // Take advantage of the 16 bytes in the XMM register.
4253 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4254 Address stack(ESP, destination.GetStackIndex());
4255 // Load the double into the high doubleword.
4256 __ movhpd(reg, stack);
4257
4258 // Store the low double into the destination.
4259 __ movsd(stack, reg);
4260
4261 // Move the high double to the low double.
4262 __ psrldq(reg, Immediate(8));
4263 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4264 // Take advantage of the 16 bytes in the XMM register.
4265 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4266 Address stack(ESP, source.GetStackIndex());
4267 // Load the double into the high doubleword.
4268 __ movhpd(reg, stack);
4269
4270 // Store the low double into the destination.
4271 __ movsd(stack, reg);
4272
4273 // Move the high double to the low double.
4274 __ psrldq(reg, Immediate(8));
4275 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4276 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4277 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004278 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004279 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004280 }
4281}
4282
4283void ParallelMoveResolverX86::SpillScratch(int reg) {
4284 __ pushl(static_cast<Register>(reg));
4285}
4286
4287void ParallelMoveResolverX86::RestoreScratch(int reg) {
4288 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004289}
4290
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004291void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004292 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4293 ? LocationSummary::kCallOnSlowPath
4294 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004295 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004296 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004297 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004298 locations->SetOut(Location::RequiresRegister());
4299}
4300
4301void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004302 LocationSummary* locations = cls->GetLocations();
4303 Register out = locations->Out().AsRegister<Register>();
4304 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004305 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004306 DCHECK(!cls->CanCallRuntime());
4307 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004308 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004309 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004310 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004311 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004312 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004313 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004314
4315 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4316 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4317 codegen_->AddSlowPath(slow_path);
4318 __ testl(out, out);
4319 __ j(kEqual, slow_path->GetEntryLabel());
4320 if (cls->MustGenerateClinitCheck()) {
4321 GenerateClassInitializationCheck(slow_path, out);
4322 } else {
4323 __ Bind(slow_path->GetExitLabel());
4324 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004325 }
4326}
4327
4328void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4329 LocationSummary* locations =
4330 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4331 locations->SetInAt(0, Location::RequiresRegister());
4332 if (check->HasUses()) {
4333 locations->SetOut(Location::SameAsFirstInput());
4334 }
4335}
4336
4337void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004338 // We assume the class to not be null.
4339 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4340 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004341 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004342 GenerateClassInitializationCheck(slow_path,
4343 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004344}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004345
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004346void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4347 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004348 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4349 Immediate(mirror::Class::kStatusInitialized));
4350 __ j(kLess, slow_path->GetEntryLabel());
4351 __ Bind(slow_path->GetExitLabel());
4352 // No need for memory fence, thanks to the X86 memory model.
4353}
4354
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004355void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4356 LocationSummary* locations =
4357 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004358 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004359 locations->SetOut(Location::RequiresRegister());
4360}
4361
4362void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4363 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4364 codegen_->AddSlowPath(slow_path);
4365
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004366 LocationSummary* locations = load->GetLocations();
4367 Register out = locations->Out().AsRegister<Register>();
4368 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004369 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004370 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004371 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4372 __ testl(out, out);
4373 __ j(kEqual, slow_path->GetEntryLabel());
4374 __ Bind(slow_path->GetExitLabel());
4375}
4376
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004377void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4378 LocationSummary* locations =
4379 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4380 locations->SetOut(Location::RequiresRegister());
4381}
4382
4383void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4384 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004386 __ fs()->movl(address, Immediate(0));
4387}
4388
4389void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4390 LocationSummary* locations =
4391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4392 InvokeRuntimeCallingConvention calling_convention;
4393 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4394}
4395
4396void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4397 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4398 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4399}
4400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004401void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004402 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4403 ? LocationSummary::kNoCall
4404 : LocationSummary::kCallOnSlowPath;
4405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4406 locations->SetInAt(0, Location::RequiresRegister());
4407 locations->SetInAt(1, Location::Any());
4408 locations->SetOut(Location::RequiresRegister());
4409}
4410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004411void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004412 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004414 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004415 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004416 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4417 Label done, zero;
4418 SlowPathCodeX86* slow_path = nullptr;
4419
4420 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004421 // Avoid null check if we know obj is not null.
4422 if (instruction->MustDoNullCheck()) {
4423 __ testl(obj, obj);
4424 __ j(kEqual, &zero);
4425 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004426 __ movl(out, Address(obj, class_offset));
4427 // Compare the class of `obj` with `cls`.
4428 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004429 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004430 } else {
4431 DCHECK(cls.IsStackSlot()) << cls;
4432 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4433 }
4434
4435 if (instruction->IsClassFinal()) {
4436 // Classes must be equal for the instanceof to succeed.
4437 __ j(kNotEqual, &zero);
4438 __ movl(out, Immediate(1));
4439 __ jmp(&done);
4440 } else {
4441 // If the classes are not equal, we go into a slow path.
4442 DCHECK(locations->OnlyCallsOnSlowPath());
4443 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004444 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004445 codegen_->AddSlowPath(slow_path);
4446 __ j(kNotEqual, slow_path->GetEntryLabel());
4447 __ movl(out, Immediate(1));
4448 __ jmp(&done);
4449 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004450
4451 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4452 __ Bind(&zero);
4453 __ movl(out, Immediate(0));
4454 }
4455
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004456 if (slow_path != nullptr) {
4457 __ Bind(slow_path->GetExitLabel());
4458 }
4459 __ Bind(&done);
4460}
4461
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004462void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4464 instruction, LocationSummary::kCallOnSlowPath);
4465 locations->SetInAt(0, Location::RequiresRegister());
4466 locations->SetInAt(1, Location::Any());
4467 locations->AddTemp(Location::RequiresRegister());
4468}
4469
4470void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4471 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004472 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004473 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004474 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004475 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4476 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4477 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4478 codegen_->AddSlowPath(slow_path);
4479
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004480 // Avoid null check if we know obj is not null.
4481 if (instruction->MustDoNullCheck()) {
4482 __ testl(obj, obj);
4483 __ j(kEqual, slow_path->GetExitLabel());
4484 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004485
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004486 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004487 // Compare the class of `obj` with `cls`.
4488 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004489 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004490 } else {
4491 DCHECK(cls.IsStackSlot()) << cls;
4492 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4493 }
4494
4495 __ j(kNotEqual, slow_path->GetEntryLabel());
4496 __ Bind(slow_path->GetExitLabel());
4497}
4498
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004499void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4500 LocationSummary* locations =
4501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4502 InvokeRuntimeCallingConvention calling_convention;
4503 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4504}
4505
4506void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4507 __ fs()->call(Address::Absolute(instruction->IsEnter()
4508 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4509 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4510 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4511}
4512
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004513void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4514void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4515void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4516
4517void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4518 LocationSummary* locations =
4519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4520 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4521 || instruction->GetResultType() == Primitive::kPrimLong);
4522 locations->SetInAt(0, Location::RequiresRegister());
4523 locations->SetInAt(1, Location::Any());
4524 locations->SetOut(Location::SameAsFirstInput());
4525}
4526
4527void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4528 HandleBitwiseOperation(instruction);
4529}
4530
4531void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4532 HandleBitwiseOperation(instruction);
4533}
4534
4535void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4536 HandleBitwiseOperation(instruction);
4537}
4538
4539void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4540 LocationSummary* locations = instruction->GetLocations();
4541 Location first = locations->InAt(0);
4542 Location second = locations->InAt(1);
4543 DCHECK(first.Equals(locations->Out()));
4544
4545 if (instruction->GetResultType() == Primitive::kPrimInt) {
4546 if (second.IsRegister()) {
4547 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004548 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004549 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004550 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004551 } else {
4552 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004553 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004554 }
4555 } else if (second.IsConstant()) {
4556 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004557 __ andl(first.AsRegister<Register>(),
4558 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004560 __ orl(first.AsRegister<Register>(),
4561 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004562 } else {
4563 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004564 __ xorl(first.AsRegister<Register>(),
4565 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004566 }
4567 } else {
4568 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004569 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004570 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004571 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004572 } else {
4573 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004574 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004575 }
4576 }
4577 } else {
4578 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4579 if (second.IsRegisterPair()) {
4580 if (instruction->IsAnd()) {
4581 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4582 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4583 } else if (instruction->IsOr()) {
4584 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4585 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4586 } else {
4587 DCHECK(instruction->IsXor());
4588 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4589 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4590 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004591 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004592 if (instruction->IsAnd()) {
4593 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4594 __ andl(first.AsRegisterPairHigh<Register>(),
4595 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4596 } else if (instruction->IsOr()) {
4597 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4598 __ orl(first.AsRegisterPairHigh<Register>(),
4599 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4600 } else {
4601 DCHECK(instruction->IsXor());
4602 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4603 __ xorl(first.AsRegisterPairHigh<Register>(),
4604 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4605 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004606 } else {
4607 DCHECK(second.IsConstant()) << second;
4608 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004609 int32_t low_value = Low32Bits(value);
4610 int32_t high_value = High32Bits(value);
4611 Immediate low(low_value);
4612 Immediate high(high_value);
4613 Register first_low = first.AsRegisterPairLow<Register>();
4614 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004615 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004616 if (low_value == 0) {
4617 __ xorl(first_low, first_low);
4618 } else if (low_value != -1) {
4619 __ andl(first_low, low);
4620 }
4621 if (high_value == 0) {
4622 __ xorl(first_high, first_high);
4623 } else if (high_value != -1) {
4624 __ andl(first_high, high);
4625 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004626 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004627 if (low_value != 0) {
4628 __ orl(first_low, low);
4629 }
4630 if (high_value != 0) {
4631 __ orl(first_high, high);
4632 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004633 } else {
4634 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004635 if (low_value != 0) {
4636 __ xorl(first_low, low);
4637 }
4638 if (high_value != 0) {
4639 __ xorl(first_high, high);
4640 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004641 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642 }
4643 }
4644}
4645
Calin Juravleb1498f62015-02-16 13:13:29 +00004646void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4647 // Nothing to do, this should be removed during prepare for register allocator.
4648 UNUSED(instruction);
4649 LOG(FATAL) << "Unreachable";
4650}
4651
4652void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4653 // Nothing to do, this should be removed during prepare for register allocator.
4654 UNUSED(instruction);
4655 LOG(FATAL) << "Unreachable";
4656}
4657
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004658} // namespace x86
4659} // namespace art