blob: 135a2bd60dc4fa0bd51e55a975083c92925230ce [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 Geoffray7b0e3532015-06-09 09:25:50 +0000530void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
531 DCHECK(RequiresCurrentMethod());
532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
533}
534
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
536 switch (load->GetType()) {
537 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545
546 case Primitive::kPrimBoolean:
547 case Primitive::kPrimByte:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimShort:
550 case Primitive::kPrimVoid:
551 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700552 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700556 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557}
558
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100559Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
560 switch (type) {
561 case Primitive::kPrimBoolean:
562 case Primitive::kPrimByte:
563 case Primitive::kPrimChar:
564 case Primitive::kPrimShort:
565 case Primitive::kPrimInt:
566 case Primitive::kPrimNot:
567 return Location::RegisterLocation(EAX);
568
569 case Primitive::kPrimLong:
570 return Location::RegisterPairLocation(EAX, EDX);
571
572 case Primitive::kPrimVoid:
573 return Location::NoLocation();
574
575 case Primitive::kPrimDouble:
576 case Primitive::kPrimFloat:
577 return Location::FpuRegisterLocation(XMM0);
578 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100579
580 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100581}
582
583Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
584 return Location::RegisterLocation(kMethodRegisterArgument);
585}
586
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100587Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100588 switch (type) {
589 case Primitive::kPrimBoolean:
590 case Primitive::kPrimByte:
591 case Primitive::kPrimChar:
592 case Primitive::kPrimShort:
593 case Primitive::kPrimInt:
594 case Primitive::kPrimNot: {
595 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000596 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100598 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000600 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100601 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100602 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000604 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100605 uint32_t index = gp_index_;
606 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000607 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100609 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
610 calling_convention.GetRegisterPairAt(index));
611 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000613 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
614 }
615 }
616
617 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100618 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000619 stack_index_++;
620 if (index < calling_convention.GetNumberOfFpuRegisters()) {
621 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
622 } else {
623 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
624 }
625 }
626
627 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100628 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000629 stack_index_ += 2;
630 if (index < calling_convention.GetNumberOfFpuRegisters()) {
631 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
632 } else {
633 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100634 }
635 }
636
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100637 case Primitive::kPrimVoid:
638 LOG(FATAL) << "Unexpected parameter type " << type;
639 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100640 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100641 return Location();
642}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644void CodeGeneratorX86::Move32(Location destination, Location source) {
645 if (source.Equals(destination)) {
646 return;
647 }
648 if (destination.IsRegister()) {
649 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else {
654 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else if (destination.IsFpuRegister()) {
658 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000659 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000661 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 } else {
663 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000667 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500672 } else if (source.IsConstant()) {
673 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000674 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500675 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 } else {
677 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100678 __ pushl(Address(ESP, source.GetStackIndex()));
679 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 }
681 }
682}
683
684void CodeGeneratorX86::Move64(Location destination, Location source) {
685 if (source.Equals(destination)) {
686 return;
687 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 if (destination.IsRegisterPair()) {
689 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000690 EmitParallelMoves(
691 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
692 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100693 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000694 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100695 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
696 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
698 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000700 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100702 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
703 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
705 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500707 if (source.IsFpuRegister()) {
708 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
709 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else {
712 LOG(FATAL) << "Unimplemented";
713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000715 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000717 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100720 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000723 } else if (source.IsConstant()) {
724 HConstant* constant = source.GetConstant();
725 int64_t value;
726 if (constant->IsLongConstant()) {
727 value = constant->AsLongConstant()->GetValue();
728 } else {
729 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000730 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000731 }
732 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
733 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000735 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000736 EmitParallelMoves(
737 Location::StackSlot(source.GetStackIndex()),
738 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100739 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000740 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100741 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
742 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 }
744 }
745}
746
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100747void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000748 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100749 if (instruction->IsCurrentMethod()) {
750 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
751 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100753 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000755 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
756 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000757 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000758 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 } else if (location.IsStackSlot()) {
760 __ movl(Address(ESP, location.GetStackIndex()), imm);
761 } else {
762 DCHECK(location.IsConstant());
763 DCHECK_EQ(location.GetConstant(), const_to_move);
764 }
765 } else if (const_to_move->IsLongConstant()) {
766 int64_t value = const_to_move->AsLongConstant()->GetValue();
767 if (location.IsRegisterPair()) {
768 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
769 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
770 } else if (location.IsDoubleStackSlot()) {
771 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000772 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
773 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000774 } else {
775 DCHECK(location.IsConstant());
776 DCHECK_EQ(location.GetConstant(), instruction);
777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000779 } else if (instruction->IsTemporary()) {
780 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000781 if (temp_location.IsStackSlot()) {
782 Move32(location, temp_location);
783 } else {
784 DCHECK(temp_location.IsDoubleStackSlot());
785 Move64(location, temp_location);
786 }
Roland Levillain476df552014-10-09 17:51:36 +0100787 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 switch (instruction->GetType()) {
790 case Primitive::kPrimBoolean:
791 case Primitive::kPrimByte:
792 case Primitive::kPrimChar:
793 case Primitive::kPrimShort:
794 case Primitive::kPrimInt:
795 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 case Primitive::kPrimFloat:
797 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 case Primitive::kPrimDouble:
802 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 break;
804
805 default:
806 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
807 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000808 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 switch (instruction->GetType()) {
811 case Primitive::kPrimBoolean:
812 case Primitive::kPrimByte:
813 case Primitive::kPrimChar:
814 case Primitive::kPrimShort:
815 case Primitive::kPrimInt:
816 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 break;
820
821 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 break;
825
826 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829 }
830}
831
832void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834}
835
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000837 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 DCHECK(!successor->IsExitBlock());
839
840 HBasicBlock* block = got->GetBlock();
841 HInstruction* previous = got->GetPrevious();
842
843 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000844 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
846 return;
847 }
848
849 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
850 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
851 }
852 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000854 }
855}
856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000859}
860
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000863}
864
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700865void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
866 Label* true_target,
867 Label* false_target,
868 Label* always_true_target) {
869 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 if (cond->IsIntConstant()) {
871 // Constant condition, statically compared against 1.
872 int32_t cond_value = cond->AsIntConstant()->GetValue();
873 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700874 if (always_true_target != nullptr) {
875 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100876 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 DCHECK_EQ(cond_value, 0);
880 }
881 } else {
882 bool materialized =
883 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
884 // Moves do not affect the eflags register, so if the condition is
885 // evaluated just before the if, we don't need to evaluate it
886 // again.
887 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700888 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100889 if (materialized) {
890 if (!eflags_set) {
891 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100893 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500894 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100895 } else {
896 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
897 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700898 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100899 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700900 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100901 }
902 } else {
903 Location lhs = cond->GetLocations()->InAt(0);
904 Location rhs = cond->GetLocations()->InAt(1);
905 // LHS is guaranteed to be in a register (see
906 // LocationsBuilderX86::VisitCondition).
907 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000908 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100909 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100910 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500911 if (constant == 0) {
912 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
913 } else {
914 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
915 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000917 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100918 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700919 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700920 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700922 if (false_target != nullptr) {
923 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924 }
925}
926
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700927void LocationsBuilderX86::VisitIf(HIf* if_instr) {
928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
930 HInstruction* cond = if_instr->InputAt(0);
931 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
932 locations->SetInAt(0, Location::Any());
933 }
934}
935
936void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
937 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
938 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
939 Label* always_true_target = true_target;
940 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
941 if_instr->IfTrueSuccessor())) {
942 always_true_target = nullptr;
943 }
944 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
945 if_instr->IfFalseSuccessor())) {
946 false_target = nullptr;
947 }
948 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
949}
950
951void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
952 LocationSummary* locations = new (GetGraph()->GetArena())
953 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
954 HInstruction* cond = deoptimize->InputAt(0);
955 DCHECK(cond->IsCondition());
956 if (cond->AsCondition()->NeedsMaterialization()) {
957 locations->SetInAt(0, Location::Any());
958 }
959}
960
961void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
962 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
963 DeoptimizationSlowPathX86(deoptimize);
964 codegen_->AddSlowPath(slow_path);
965 Label* slow_path_entry = slow_path->GetEntryLabel();
966 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
967}
968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000971}
972
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
974 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975}
976
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100978 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100982 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100989 switch (store->InputAt(1)->GetType()) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
998 break;
999
1000 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1003 break;
1004
1005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001008}
1009
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001010void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012}
1013
Roland Levillain0d37cd02015-05-27 16:39:19 +01001014void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001015 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001016 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001017 locations->SetInAt(0, Location::RequiresRegister());
1018 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001019 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001020 // We need a byte register.
1021 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001022 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023}
1024
Roland Levillain0d37cd02015-05-27 16:39:19 +01001025void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1026 if (cond->NeedsMaterialization()) {
1027 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001029 // Clear register: setcc only sets the low byte.
1030 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001031 Location lhs = locations->InAt(0);
1032 Location rhs = locations->InAt(1);
1033 if (rhs.IsRegister()) {
1034 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1035 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001036 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001037 if (constant == 0) {
1038 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1039 } else {
1040 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1041 }
Dave Allison20dfc792014-06-16 20:44:29 -07001042 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001043 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001044 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001045 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046 }
Dave Allison20dfc792014-06-16 20:44:29 -07001047}
1048
1049void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1062 VisitCondition(comp);
1063}
1064
1065void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1066 VisitCondition(comp);
1067}
1068
1069void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1070 VisitCondition(comp);
1071}
1072
1073void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1074 VisitCondition(comp);
1075}
1076
1077void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1078 VisitCondition(comp);
1079}
1080
1081void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1082 VisitCondition(comp);
1083}
1084
1085void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1086 VisitCondition(comp);
1087}
1088
1089void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1090 VisitCondition(comp);
1091}
1092
1093void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1094 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095}
1096
1097void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001100 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101}
1102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001103void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001105 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106}
1107
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001108void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1111 locations->SetOut(Location::ConstantLocation(constant));
1112}
1113
1114void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1115 // Will be generated at use site.
1116 UNUSED(constant);
1117}
1118
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001122 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123}
1124
1125void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128}
1129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001130void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1133 locations->SetOut(Location::ConstantLocation(constant));
1134}
1135
1136void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139}
1140
1141void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1144 locations->SetOut(Location::ConstantLocation(constant));
1145}
1146
1147void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1148 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001150}
1151
Calin Juravle27df7582015-04-17 19:12:31 +01001152void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1153 memory_barrier->SetLocations(nullptr);
1154}
1155
1156void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1157 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1158}
1159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162}
1163
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001164void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001166 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001167}
1168
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001170 LocationSummary* locations =
1171 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 switch (ret->InputAt(0)->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
1182 case Primitive::kPrimLong:
1183 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 break;
1186
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 case Primitive::kPrimFloat:
1188 case Primitive::kPrimDouble:
1189 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 break;
1192
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001198void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 if (kIsDebugBuild) {
1200 switch (ret->InputAt(0)->GetType()) {
1201 case Primitive::kPrimBoolean:
1202 case Primitive::kPrimByte:
1203 case Primitive::kPrimChar:
1204 case Primitive::kPrimShort:
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 break;
1209
1210 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1212 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 break;
1214
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 break;
1219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 }
1223 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001225}
1226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001228 // When we do not run baseline, explicit clinit checks triggered by static
1229 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1230 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001231
Mark Mendellfb8d2792015-03-31 22:16:59 -04001232 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001233 if (intrinsic.TryDispatch(invoke)) {
1234 return;
1235 }
1236
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001237 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001238
1239 if (codegen_->IsBaseline()) {
1240 // Baseline does not have enough registers if the current method also
1241 // needs a register. We therefore do not require a register for it, and let
1242 // the code generation of the invoke handle it.
1243 LocationSummary* locations = invoke->GetLocations();
1244 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1245 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1246 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1247 }
1248 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001249}
1250
Mark Mendell09ed1a32015-03-25 08:30:06 -04001251static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1252 if (invoke->GetLocations()->Intrinsified()) {
1253 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1254 intrinsic.Dispatch(invoke);
1255 return true;
1256 }
1257 return false;
1258}
1259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001261 // When we do not run baseline, explicit clinit checks triggered by static
1262 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1263 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001264
Mark Mendell09ed1a32015-03-25 08:30:06 -04001265 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1266 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001267 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001268
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001269 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001270 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001271 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001272 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001273}
1274
1275void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1276 HandleInvoke(invoke);
1277}
1278
1279void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001280 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001281 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001282}
1283
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001284void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001286 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1287 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288 LocationSummary* locations = invoke->GetLocations();
1289 Location receiver = locations->InAt(0);
1290 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001291 DCHECK(receiver.IsRegister());
1292 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001293 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001294 // temp = temp->GetMethodAt(method_offset);
1295 __ movl(temp, Address(temp, method_offset));
1296 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001297 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001298 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001299
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001300 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001301 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001302}
1303
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001304void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1305 HandleInvoke(invoke);
1306 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001307 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308}
1309
1310void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1311 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001313 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1314 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001315 LocationSummary* locations = invoke->GetLocations();
1316 Location receiver = locations->InAt(0);
1317 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1318
1319 // Set the hidden argument.
1320 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001321 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001322
1323 // temp = object->GetClass();
1324 if (receiver.IsStackSlot()) {
1325 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1326 __ movl(temp, Address(temp, class_offset));
1327 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001328 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001330 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001331 // temp = temp->GetImtEntryAt(method_offset);
1332 __ movl(temp, Address(temp, method_offset));
1333 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001334 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001335 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001336
1337 DCHECK(!codegen_->IsLeafMethod());
1338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1339}
1340
Roland Levillain88cb1752014-10-20 16:36:47 +01001341void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1342 LocationSummary* locations =
1343 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1344 switch (neg->GetResultType()) {
1345 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001346 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001347 locations->SetInAt(0, Location::RequiresRegister());
1348 locations->SetOut(Location::SameAsFirstInput());
1349 break;
1350
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001352 locations->SetInAt(0, Location::RequiresFpuRegister());
1353 locations->SetOut(Location::SameAsFirstInput());
1354 locations->AddTemp(Location::RequiresRegister());
1355 locations->AddTemp(Location::RequiresFpuRegister());
1356 break;
1357
Roland Levillain88cb1752014-10-20 16:36:47 +01001358 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001359 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001360 locations->SetOut(Location::SameAsFirstInput());
1361 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001362 break;
1363
1364 default:
1365 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1366 }
1367}
1368
1369void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1370 LocationSummary* locations = neg->GetLocations();
1371 Location out = locations->Out();
1372 Location in = locations->InAt(0);
1373 switch (neg->GetResultType()) {
1374 case Primitive::kPrimInt:
1375 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001376 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001378 break;
1379
1380 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001382 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001383 __ negl(out.AsRegisterPairLow<Register>());
1384 // Negation is similar to subtraction from zero. The least
1385 // significant byte triggers a borrow when it is different from
1386 // zero; to take it into account, add 1 to the most significant
1387 // byte if the carry flag (CF) is set to 1 after the first NEGL
1388 // operation.
1389 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1390 __ negl(out.AsRegisterPairHigh<Register>());
1391 break;
1392
Roland Levillain5368c212014-11-27 15:03:41 +00001393 case Primitive::kPrimFloat: {
1394 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001395 Register constant = locations->GetTemp(0).AsRegister<Register>();
1396 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001397 // Implement float negation with an exclusive or with value
1398 // 0x80000000 (mask for bit 31, representing the sign of a
1399 // single-precision floating-point number).
1400 __ movl(constant, Immediate(INT32_C(0x80000000)));
1401 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001402 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001404 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001405
Roland Levillain5368c212014-11-27 15:03:41 +00001406 case Primitive::kPrimDouble: {
1407 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001408 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001409 // Implement double negation with an exclusive or with value
1410 // 0x8000000000000000 (mask for bit 63, representing the sign of
1411 // a double-precision floating-point number).
1412 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001413 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001414 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001415 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001416
1417 default:
1418 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1419 }
1420}
1421
Roland Levillaindff1f282014-11-05 14:15:05 +00001422void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001423 Primitive::Type result_type = conversion->GetResultType();
1424 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001425 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001426
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001427 // The float-to-long and double-to-long type conversions rely on a
1428 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001429 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001430 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1431 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001432 ? LocationSummary::kCall
1433 : LocationSummary::kNoCall;
1434 LocationSummary* locations =
1435 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1436
David Brazdilb2bd1c52015-03-25 11:17:37 +00001437 // The Java language does not allow treating boolean as an integral type but
1438 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001439
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001441 case Primitive::kPrimByte:
1442 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001443 case Primitive::kPrimBoolean:
1444 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001445 case Primitive::kPrimShort:
1446 case Primitive::kPrimInt:
1447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001448 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001449 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1450 // Make the output overlap to please the register allocator. This greatly simplifies
1451 // the validation of the linear scan implementation
1452 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001453 break;
1454
1455 default:
1456 LOG(FATAL) << "Unexpected type conversion from " << input_type
1457 << " to " << result_type;
1458 }
1459 break;
1460
Roland Levillain01a8d712014-11-14 16:27:39 +00001461 case Primitive::kPrimShort:
1462 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001463 case Primitive::kPrimBoolean:
1464 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001465 case Primitive::kPrimByte:
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimChar:
1468 // Processing a Dex `int-to-short' instruction.
1469 locations->SetInAt(0, Location::Any());
1470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1471 break;
1472
1473 default:
1474 LOG(FATAL) << "Unexpected type conversion from " << input_type
1475 << " to " << result_type;
1476 }
1477 break;
1478
Roland Levillain946e1432014-11-11 17:35:19 +00001479 case Primitive::kPrimInt:
1480 switch (input_type) {
1481 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001482 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001483 locations->SetInAt(0, Location::Any());
1484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1485 break;
1486
1487 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001488 // Processing a Dex `float-to-int' instruction.
1489 locations->SetInAt(0, Location::RequiresFpuRegister());
1490 locations->SetOut(Location::RequiresRegister());
1491 locations->AddTemp(Location::RequiresFpuRegister());
1492 break;
1493
Roland Levillain946e1432014-11-11 17:35:19 +00001494 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001495 // Processing a Dex `double-to-int' instruction.
1496 locations->SetInAt(0, Location::RequiresFpuRegister());
1497 locations->SetOut(Location::RequiresRegister());
1498 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001499 break;
1500
1501 default:
1502 LOG(FATAL) << "Unexpected type conversion from " << input_type
1503 << " to " << result_type;
1504 }
1505 break;
1506
Roland Levillaindff1f282014-11-05 14:15:05 +00001507 case Primitive::kPrimLong:
1508 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001509 case Primitive::kPrimBoolean:
1510 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 case Primitive::kPrimByte:
1512 case Primitive::kPrimShort:
1513 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001514 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001515 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001516 locations->SetInAt(0, Location::RegisterLocation(EAX));
1517 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1518 break;
1519
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001522 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001523 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001524 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1525 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1526
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 // The runtime helper puts the result in EAX, EDX.
1528 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001529 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001530 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001531
1532 default:
1533 LOG(FATAL) << "Unexpected type conversion from " << input_type
1534 << " to " << result_type;
1535 }
1536 break;
1537
Roland Levillain981e4542014-11-14 11:47:14 +00001538 case Primitive::kPrimChar:
1539 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001540 case Primitive::kPrimBoolean:
1541 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001542 case Primitive::kPrimByte:
1543 case Primitive::kPrimShort:
1544 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001545 // Processing a Dex `int-to-char' instruction.
1546 locations->SetInAt(0, Location::Any());
1547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1548 break;
1549
1550 default:
1551 LOG(FATAL) << "Unexpected type conversion from " << input_type
1552 << " to " << result_type;
1553 }
1554 break;
1555
Roland Levillaindff1f282014-11-05 14:15:05 +00001556 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001558 case Primitive::kPrimBoolean:
1559 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001560 case Primitive::kPrimByte:
1561 case Primitive::kPrimShort:
1562 case Primitive::kPrimInt:
1563 case Primitive::kPrimChar:
1564 // Processing a Dex `int-to-float' instruction.
1565 locations->SetInAt(0, Location::RequiresRegister());
1566 locations->SetOut(Location::RequiresFpuRegister());
1567 break;
1568
1569 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001570 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001571 locations->SetInAt(0, Location::Any());
1572 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001573 break;
1574
Roland Levillaincff13742014-11-17 14:32:17 +00001575 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001576 // Processing a Dex `double-to-float' instruction.
1577 locations->SetInAt(0, Location::RequiresFpuRegister());
1578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001579 break;
1580
1581 default:
1582 LOG(FATAL) << "Unexpected type conversion from " << input_type
1583 << " to " << result_type;
1584 };
1585 break;
1586
Roland Levillaindff1f282014-11-05 14:15:05 +00001587 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001588 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001589 case Primitive::kPrimBoolean:
1590 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001591 case Primitive::kPrimByte:
1592 case Primitive::kPrimShort:
1593 case Primitive::kPrimInt:
1594 case Primitive::kPrimChar:
1595 // Processing a Dex `int-to-double' instruction.
1596 locations->SetInAt(0, Location::RequiresRegister());
1597 locations->SetOut(Location::RequiresFpuRegister());
1598 break;
1599
1600 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001601 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001602 locations->SetInAt(0, Location::Any());
1603 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001604 break;
1605
Roland Levillaincff13742014-11-17 14:32:17 +00001606 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001607 // Processing a Dex `float-to-double' instruction.
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
1609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001610 break;
1611
1612 default:
1613 LOG(FATAL) << "Unexpected type conversion from " << input_type
1614 << " to " << result_type;
1615 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001616 break;
1617
1618 default:
1619 LOG(FATAL) << "Unexpected type conversion from " << input_type
1620 << " to " << result_type;
1621 }
1622}
1623
1624void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1625 LocationSummary* locations = conversion->GetLocations();
1626 Location out = locations->Out();
1627 Location in = locations->InAt(0);
1628 Primitive::Type result_type = conversion->GetResultType();
1629 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001630 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001631 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001632 case Primitive::kPrimByte:
1633 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001634 case Primitive::kPrimBoolean:
1635 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001636 case Primitive::kPrimShort:
1637 case Primitive::kPrimInt:
1638 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001639 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 if (in.IsRegister()) {
1641 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001642 } else {
1643 DCHECK(in.GetConstant()->IsIntConstant());
1644 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1645 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1646 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001647 break;
1648
1649 default:
1650 LOG(FATAL) << "Unexpected type conversion from " << input_type
1651 << " to " << result_type;
1652 }
1653 break;
1654
Roland Levillain01a8d712014-11-14 16:27:39 +00001655 case Primitive::kPrimShort:
1656 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001657 case Primitive::kPrimBoolean:
1658 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 case Primitive::kPrimByte:
1660 case Primitive::kPrimInt:
1661 case Primitive::kPrimChar:
1662 // Processing a Dex `int-to-short' instruction.
1663 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001667 } else {
1668 DCHECK(in.GetConstant()->IsIntConstant());
1669 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001671 }
1672 break;
1673
1674 default:
1675 LOG(FATAL) << "Unexpected type conversion from " << input_type
1676 << " to " << result_type;
1677 }
1678 break;
1679
Roland Levillain946e1432014-11-11 17:35:19 +00001680 case Primitive::kPrimInt:
1681 switch (input_type) {
1682 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001683 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001684 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001688 } else {
1689 DCHECK(in.IsConstant());
1690 DCHECK(in.GetConstant()->IsLongConstant());
1691 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001692 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001693 }
1694 break;
1695
Roland Levillain3f8f9362014-12-02 17:45:01 +00001696 case Primitive::kPrimFloat: {
1697 // Processing a Dex `float-to-int' instruction.
1698 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1699 Register output = out.AsRegister<Register>();
1700 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1701 Label done, nan;
1702
1703 __ movl(output, Immediate(kPrimIntMax));
1704 // temp = int-to-float(output)
1705 __ cvtsi2ss(temp, output);
1706 // if input >= temp goto done
1707 __ comiss(input, temp);
1708 __ j(kAboveEqual, &done);
1709 // if input == NaN goto nan
1710 __ j(kUnordered, &nan);
1711 // output = float-to-int-truncate(input)
1712 __ cvttss2si(output, input);
1713 __ jmp(&done);
1714 __ Bind(&nan);
1715 // output = 0
1716 __ xorl(output, output);
1717 __ Bind(&done);
1718 break;
1719 }
1720
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001721 case Primitive::kPrimDouble: {
1722 // Processing a Dex `double-to-int' instruction.
1723 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1724 Register output = out.AsRegister<Register>();
1725 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1726 Label done, nan;
1727
1728 __ movl(output, Immediate(kPrimIntMax));
1729 // temp = int-to-double(output)
1730 __ cvtsi2sd(temp, output);
1731 // if input >= temp goto done
1732 __ comisd(input, temp);
1733 __ j(kAboveEqual, &done);
1734 // if input == NaN goto nan
1735 __ j(kUnordered, &nan);
1736 // output = double-to-int-truncate(input)
1737 __ cvttsd2si(output, input);
1738 __ jmp(&done);
1739 __ Bind(&nan);
1740 // output = 0
1741 __ xorl(output, output);
1742 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001743 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001744 }
Roland Levillain946e1432014-11-11 17:35:19 +00001745
1746 default:
1747 LOG(FATAL) << "Unexpected type conversion from " << input_type
1748 << " to " << result_type;
1749 }
1750 break;
1751
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 case Primitive::kPrimLong:
1753 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001754 case Primitive::kPrimBoolean:
1755 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 case Primitive::kPrimByte:
1757 case Primitive::kPrimShort:
1758 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001759 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001760 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1762 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001763 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001764 __ cdq();
1765 break;
1766
1767 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001768 // Processing a Dex `float-to-long' instruction.
1769 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001770 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1771 break;
1772
Roland Levillaindff1f282014-11-05 14:15:05 +00001773 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001774 // Processing a Dex `double-to-long' instruction.
1775 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1776 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 break;
1778
1779 default:
1780 LOG(FATAL) << "Unexpected type conversion from " << input_type
1781 << " to " << result_type;
1782 }
1783 break;
1784
Roland Levillain981e4542014-11-14 11:47:14 +00001785 case Primitive::kPrimChar:
1786 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001787 case Primitive::kPrimBoolean:
1788 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001789 case Primitive::kPrimByte:
1790 case Primitive::kPrimShort:
1791 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001792 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1793 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001796 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001797 } else {
1798 DCHECK(in.GetConstant()->IsIntConstant());
1799 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001801 }
1802 break;
1803
1804 default:
1805 LOG(FATAL) << "Unexpected type conversion from " << input_type
1806 << " to " << result_type;
1807 }
1808 break;
1809
Roland Levillaindff1f282014-11-05 14:15:05 +00001810 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001811 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001812 case Primitive::kPrimBoolean:
1813 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001814 case Primitive::kPrimByte:
1815 case Primitive::kPrimShort:
1816 case Primitive::kPrimInt:
1817 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001818 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001819 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001820 break;
1821
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 case Primitive::kPrimLong: {
1823 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001824 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001825
Roland Levillain232ade02015-04-20 15:14:36 +01001826 // Create stack space for the call to
1827 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1828 // TODO: enhance register allocator to ask for stack temporaries.
1829 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1830 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1831 __ subl(ESP, Immediate(adjustment));
1832 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001833
Roland Levillain232ade02015-04-20 15:14:36 +01001834 // Load the value to the FP stack, using temporaries if needed.
1835 PushOntoFPStack(in, 0, adjustment, false, true);
1836
1837 if (out.IsStackSlot()) {
1838 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1839 } else {
1840 __ fstps(Address(ESP, 0));
1841 Location stack_temp = Location::StackSlot(0);
1842 codegen_->Move32(out, stack_temp);
1843 }
1844
1845 // Remove the temporary stack space we allocated.
1846 if (adjustment != 0) {
1847 __ addl(ESP, Immediate(adjustment));
1848 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001849 break;
1850 }
1851
Roland Levillaincff13742014-11-17 14:32:17 +00001852 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001853 // Processing a Dex `double-to-float' instruction.
1854 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001855 break;
1856
1857 default:
1858 LOG(FATAL) << "Unexpected type conversion from " << input_type
1859 << " to " << result_type;
1860 };
1861 break;
1862
Roland Levillaindff1f282014-11-05 14:15:05 +00001863 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001864 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001865 case Primitive::kPrimBoolean:
1866 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001867 case Primitive::kPrimByte:
1868 case Primitive::kPrimShort:
1869 case Primitive::kPrimInt:
1870 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001871 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001872 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001873 break;
1874
Roland Levillain647b9ed2014-11-27 12:06:00 +00001875 case Primitive::kPrimLong: {
1876 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001877 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001878
Roland Levillain232ade02015-04-20 15:14:36 +01001879 // Create stack space for the call to
1880 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1881 // TODO: enhance register allocator to ask for stack temporaries.
1882 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1883 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1884 __ subl(ESP, Immediate(adjustment));
1885 }
1886
1887 // Load the value to the FP stack, using temporaries if needed.
1888 PushOntoFPStack(in, 0, adjustment, false, true);
1889
1890 if (out.IsDoubleStackSlot()) {
1891 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1892 } else {
1893 __ fstpl(Address(ESP, 0));
1894 Location stack_temp = Location::DoubleStackSlot(0);
1895 codegen_->Move64(out, stack_temp);
1896 }
1897
1898 // Remove the temporary stack space we allocated.
1899 if (adjustment != 0) {
1900 __ addl(ESP, Immediate(adjustment));
1901 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001902 break;
1903 }
1904
Roland Levillaincff13742014-11-17 14:32:17 +00001905 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001906 // Processing a Dex `float-to-double' instruction.
1907 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001914 break;
1915
1916 default:
1917 LOG(FATAL) << "Unexpected type conversion from " << input_type
1918 << " to " << result_type;
1919 }
1920}
1921
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001922void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001923 LocationSummary* locations =
1924 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001925 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001926 case Primitive::kPrimInt: {
1927 locations->SetInAt(0, Location::RequiresRegister());
1928 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1930 break;
1931 }
1932
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001934 locations->SetInAt(0, Location::RequiresRegister());
1935 locations->SetInAt(1, Location::Any());
1936 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937 break;
1938 }
1939
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 case Primitive::kPrimFloat:
1941 case Primitive::kPrimDouble: {
1942 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001943 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001946 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001949 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1950 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001951 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952}
1953
1954void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1955 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001956 Location first = locations->InAt(0);
1957 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001958 Location out = locations->Out();
1959
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001960 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001963 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1964 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001965 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1966 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001967 } else {
1968 __ leal(out.AsRegister<Register>(), Address(
1969 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1970 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001971 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001972 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1973 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1974 __ addl(out.AsRegister<Register>(), Immediate(value));
1975 } else {
1976 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1977 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001978 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001979 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001980 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001981 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001982 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001983 }
1984
1985 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001986 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001987 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1988 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001989 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001990 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1991 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001992 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001993 } else {
1994 DCHECK(second.IsConstant()) << second;
1995 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1996 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1997 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001998 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001999 break;
2000 }
2001
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002002 case Primitive::kPrimFloat: {
2003 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002006 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002007 }
2008
2009 case Primitive::kPrimDouble: {
2010 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002011 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002012 }
2013 break;
2014 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002015
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002016 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002017 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002018 }
2019}
2020
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002021void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002024 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002025 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002026 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002027 locations->SetInAt(0, Location::RequiresRegister());
2028 locations->SetInAt(1, Location::Any());
2029 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030 break;
2031 }
Calin Juravle11351682014-10-23 15:38:15 +01002032 case Primitive::kPrimFloat:
2033 case Primitive::kPrimDouble: {
2034 locations->SetInAt(0, Location::RequiresFpuRegister());
2035 locations->SetInAt(1, Location::RequiresFpuRegister());
2036 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037 break;
Calin Juravle11351682014-10-23 15:38:15 +01002038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002040 default:
Calin Juravle11351682014-10-23 15:38:15 +01002041 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043}
2044
2045void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2046 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 Location first = locations->InAt(0);
2048 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002049 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002050 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002051 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002054 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002055 __ subl(first.AsRegister<Register>(),
2056 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002057 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002059 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002060 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002061 }
2062
2063 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002064 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002065 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2066 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002067 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002068 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002069 __ sbbl(first.AsRegisterPairHigh<Register>(),
2070 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002071 } else {
2072 DCHECK(second.IsConstant()) << second;
2073 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2074 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2075 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077 break;
2078 }
2079
Calin Juravle11351682014-10-23 15:38:15 +01002080 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082 break;
Calin Juravle11351682014-10-23 15:38:15 +01002083 }
2084
2085 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002086 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002087 break;
2088 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002089
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002090 default:
Calin Juravle11351682014-10-23 15:38:15 +01002091 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002092 }
2093}
2094
Calin Juravle34bacdf2014-10-07 20:23:36 +01002095void LocationsBuilderX86::VisitMul(HMul* mul) {
2096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2098 switch (mul->GetResultType()) {
2099 case Primitive::kPrimInt:
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 locations->SetInAt(1, Location::Any());
2102 locations->SetOut(Location::SameAsFirstInput());
2103 break;
2104 case Primitive::kPrimLong: {
2105 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002106 locations->SetInAt(1, Location::Any());
2107 locations->SetOut(Location::SameAsFirstInput());
2108 // Needed for imul on 32bits with 64bits output.
2109 locations->AddTemp(Location::RegisterLocation(EAX));
2110 locations->AddTemp(Location::RegisterLocation(EDX));
2111 break;
2112 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002113 case Primitive::kPrimFloat:
2114 case Primitive::kPrimDouble: {
2115 locations->SetInAt(0, Location::RequiresFpuRegister());
2116 locations->SetInAt(1, Location::RequiresFpuRegister());
2117 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002120
2121 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002122 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002123 }
2124}
2125
2126void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2127 LocationSummary* locations = mul->GetLocations();
2128 Location first = locations->InAt(0);
2129 Location second = locations->InAt(1);
2130 DCHECK(first.Equals(locations->Out()));
2131
2132 switch (mul->GetResultType()) {
2133 case Primitive::kPrimInt: {
2134 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136 } else if (second.IsConstant()) {
2137 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139 } else {
2140 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002142 }
2143 break;
2144 }
2145
2146 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002147 Register in1_hi = first.AsRegisterPairHigh<Register>();
2148 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 Register eax = locations->GetTemp(0).AsRegister<Register>();
2150 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002151
2152 DCHECK_EQ(EAX, eax);
2153 DCHECK_EQ(EDX, edx);
2154
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002155 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002156 // output: in1
2157 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2158 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2159 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002160 if (second.IsConstant()) {
2161 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002162
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002163 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2164 int32_t low_value = Low32Bits(value);
2165 int32_t high_value = High32Bits(value);
2166 Immediate low(low_value);
2167 Immediate high(high_value);
2168
2169 __ movl(eax, high);
2170 // eax <- in1.lo * in2.hi
2171 __ imull(eax, in1_lo);
2172 // in1.hi <- in1.hi * in2.lo
2173 __ imull(in1_hi, low);
2174 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2175 __ addl(in1_hi, eax);
2176 // move in2_lo to eax to prepare for double precision
2177 __ movl(eax, low);
2178 // edx:eax <- in1.lo * in2.lo
2179 __ mull(in1_lo);
2180 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2181 __ addl(in1_hi, edx);
2182 // in1.lo <- (in1.lo * in2.lo)[31:0];
2183 __ movl(in1_lo, eax);
2184 } else if (second.IsRegisterPair()) {
2185 Register in2_hi = second.AsRegisterPairHigh<Register>();
2186 Register in2_lo = second.AsRegisterPairLow<Register>();
2187
2188 __ movl(eax, in2_hi);
2189 // eax <- in1.lo * in2.hi
2190 __ imull(eax, in1_lo);
2191 // in1.hi <- in1.hi * in2.lo
2192 __ imull(in1_hi, in2_lo);
2193 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2194 __ addl(in1_hi, eax);
2195 // move in1_lo to eax to prepare for double precision
2196 __ movl(eax, in1_lo);
2197 // edx:eax <- in1.lo * in2.lo
2198 __ mull(in2_lo);
2199 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2200 __ addl(in1_hi, edx);
2201 // in1.lo <- (in1.lo * in2.lo)[31:0];
2202 __ movl(in1_lo, eax);
2203 } else {
2204 DCHECK(second.IsDoubleStackSlot()) << second;
2205 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2206 Address in2_lo(ESP, second.GetStackIndex());
2207
2208 __ movl(eax, in2_hi);
2209 // eax <- in1.lo * in2.hi
2210 __ imull(eax, in1_lo);
2211 // in1.hi <- in1.hi * in2.lo
2212 __ imull(in1_hi, in2_lo);
2213 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2214 __ addl(in1_hi, eax);
2215 // move in1_lo to eax to prepare for double precision
2216 __ movl(eax, in1_lo);
2217 // edx:eax <- in1.lo * in2.lo
2218 __ mull(in2_lo);
2219 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2220 __ addl(in1_hi, edx);
2221 // in1.lo <- (in1.lo * in2.lo)[31:0];
2222 __ movl(in1_lo, eax);
2223 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002224
2225 break;
2226 }
2227
Calin Juravleb5bfa962014-10-21 18:02:24 +01002228 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002230 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002231 }
2232
2233 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 break;
2236 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002237
2238 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002239 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002240 }
2241}
2242
Roland Levillain232ade02015-04-20 15:14:36 +01002243void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2244 uint32_t temp_offset,
2245 uint32_t stack_adjustment,
2246 bool is_fp,
2247 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002248 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002249 DCHECK(!is_wide);
2250 if (is_fp) {
2251 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2252 } else {
2253 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2254 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002255 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002256 DCHECK(is_wide);
2257 if (is_fp) {
2258 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2259 } else {
2260 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2261 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002262 } else {
2263 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002264 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002265 Location stack_temp = Location::StackSlot(temp_offset);
2266 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002267 if (is_fp) {
2268 __ flds(Address(ESP, temp_offset));
2269 } else {
2270 __ filds(Address(ESP, temp_offset));
2271 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002272 } else {
2273 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2274 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002275 if (is_fp) {
2276 __ fldl(Address(ESP, temp_offset));
2277 } else {
2278 __ fildl(Address(ESP, temp_offset));
2279 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002280 }
2281 }
2282}
2283
2284void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2285 Primitive::Type type = rem->GetResultType();
2286 bool is_float = type == Primitive::kPrimFloat;
2287 size_t elem_size = Primitive::ComponentSize(type);
2288 LocationSummary* locations = rem->GetLocations();
2289 Location first = locations->InAt(0);
2290 Location second = locations->InAt(1);
2291 Location out = locations->Out();
2292
2293 // Create stack space for 2 elements.
2294 // TODO: enhance register allocator to ask for stack temporaries.
2295 __ subl(ESP, Immediate(2 * elem_size));
2296
2297 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002298 const bool is_wide = !is_float;
2299 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2300 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002301
2302 // Loop doing FPREM until we stabilize.
2303 Label retry;
2304 __ Bind(&retry);
2305 __ fprem();
2306
2307 // Move FP status to AX.
2308 __ fstsw();
2309
2310 // And see if the argument reduction is complete. This is signaled by the
2311 // C2 FPU flag bit set to 0.
2312 __ andl(EAX, Immediate(kC2ConditionMask));
2313 __ j(kNotEqual, &retry);
2314
2315 // We have settled on the final value. Retrieve it into an XMM register.
2316 // Store FP top of stack to real stack.
2317 if (is_float) {
2318 __ fsts(Address(ESP, 0));
2319 } else {
2320 __ fstl(Address(ESP, 0));
2321 }
2322
2323 // Pop the 2 items from the FP stack.
2324 __ fucompp();
2325
2326 // Load the value from the stack into an XMM register.
2327 DCHECK(out.IsFpuRegister()) << out;
2328 if (is_float) {
2329 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2330 } else {
2331 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2332 }
2333
2334 // And remove the temporary stack space we allocated.
2335 __ addl(ESP, Immediate(2 * elem_size));
2336}
2337
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002338
2339void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2340 DCHECK(instruction->IsDiv() || instruction->IsRem());
2341
2342 LocationSummary* locations = instruction->GetLocations();
2343 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002344 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002345
2346 Register out_register = locations->Out().AsRegister<Register>();
2347 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002348 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002349
2350 DCHECK(imm == 1 || imm == -1);
2351
2352 if (instruction->IsRem()) {
2353 __ xorl(out_register, out_register);
2354 } else {
2355 __ movl(out_register, input_register);
2356 if (imm == -1) {
2357 __ negl(out_register);
2358 }
2359 }
2360}
2361
2362
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002363void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002364 LocationSummary* locations = instruction->GetLocations();
2365
2366 Register out_register = locations->Out().AsRegister<Register>();
2367 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002368 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002369
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002370 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002371 Register num = locations->GetTemp(0).AsRegister<Register>();
2372
2373 __ leal(num, Address(input_register, std::abs(imm) - 1));
2374 __ testl(input_register, input_register);
2375 __ cmovl(kGreaterEqual, num, input_register);
2376 int shift = CTZ(imm);
2377 __ sarl(num, Immediate(shift));
2378
2379 if (imm < 0) {
2380 __ negl(num);
2381 }
2382
2383 __ movl(out_register, num);
2384}
2385
2386void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2387 DCHECK(instruction->IsDiv() || instruction->IsRem());
2388
2389 LocationSummary* locations = instruction->GetLocations();
2390 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2391
2392 Register eax = locations->InAt(0).AsRegister<Register>();
2393 Register out = locations->Out().AsRegister<Register>();
2394 Register num;
2395 Register edx;
2396
2397 if (instruction->IsDiv()) {
2398 edx = locations->GetTemp(0).AsRegister<Register>();
2399 num = locations->GetTemp(1).AsRegister<Register>();
2400 } else {
2401 edx = locations->Out().AsRegister<Register>();
2402 num = locations->GetTemp(0).AsRegister<Register>();
2403 }
2404
2405 DCHECK_EQ(EAX, eax);
2406 DCHECK_EQ(EDX, edx);
2407 if (instruction->IsDiv()) {
2408 DCHECK_EQ(EAX, out);
2409 } else {
2410 DCHECK_EQ(EDX, out);
2411 }
2412
2413 int64_t magic;
2414 int shift;
2415 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2416
2417 Label ndiv;
2418 Label end;
2419 // If numerator is 0, the result is 0, no computation needed.
2420 __ testl(eax, eax);
2421 __ j(kNotEqual, &ndiv);
2422
2423 __ xorl(out, out);
2424 __ jmp(&end);
2425
2426 __ Bind(&ndiv);
2427
2428 // Save the numerator.
2429 __ movl(num, eax);
2430
2431 // EAX = magic
2432 __ movl(eax, Immediate(magic));
2433
2434 // EDX:EAX = magic * numerator
2435 __ imull(num);
2436
2437 if (imm > 0 && magic < 0) {
2438 // EDX += num
2439 __ addl(edx, num);
2440 } else if (imm < 0 && magic > 0) {
2441 __ subl(edx, num);
2442 }
2443
2444 // Shift if needed.
2445 if (shift != 0) {
2446 __ sarl(edx, Immediate(shift));
2447 }
2448
2449 // EDX += 1 if EDX < 0
2450 __ movl(eax, edx);
2451 __ shrl(edx, Immediate(31));
2452 __ addl(edx, eax);
2453
2454 if (instruction->IsRem()) {
2455 __ movl(eax, num);
2456 __ imull(edx, Immediate(imm));
2457 __ subl(eax, edx);
2458 __ movl(edx, eax);
2459 } else {
2460 __ movl(eax, edx);
2461 }
2462 __ Bind(&end);
2463}
2464
Calin Juravlebacfec32014-11-14 15:54:36 +00002465void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2466 DCHECK(instruction->IsDiv() || instruction->IsRem());
2467
2468 LocationSummary* locations = instruction->GetLocations();
2469 Location out = locations->Out();
2470 Location first = locations->InAt(0);
2471 Location second = locations->InAt(1);
2472 bool is_div = instruction->IsDiv();
2473
2474 switch (instruction->GetResultType()) {
2475 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002476 DCHECK_EQ(EAX, first.AsRegister<Register>());
2477 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002478
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002479 if (instruction->InputAt(1)->IsIntConstant()) {
2480 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002481
2482 if (imm == 0) {
2483 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2484 } else if (imm == 1 || imm == -1) {
2485 DivRemOneOrMinusOne(instruction);
2486 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002487 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002488 } else {
2489 DCHECK(imm <= -2 || imm >= 2);
2490 GenerateDivRemWithAnyConstant(instruction);
2491 }
2492 } else {
2493 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002494 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002495 is_div);
2496 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002498 Register second_reg = second.AsRegister<Register>();
2499 // 0x80000000/-1 triggers an arithmetic exception!
2500 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2501 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002502
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002503 __ cmpl(second_reg, Immediate(-1));
2504 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002505
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002506 // edx:eax <- sign-extended of eax
2507 __ cdq();
2508 // eax = quotient, edx = remainder
2509 __ idivl(second_reg);
2510 __ Bind(slow_path->GetExitLabel());
2511 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002512 break;
2513 }
2514
2515 case Primitive::kPrimLong: {
2516 InvokeRuntimeCallingConvention calling_convention;
2517 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2518 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2519 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2520 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2521 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2522 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2523
2524 if (is_div) {
2525 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2526 } else {
2527 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2528 }
2529 uint32_t dex_pc = is_div
2530 ? instruction->AsDiv()->GetDexPc()
2531 : instruction->AsRem()->GetDexPc();
2532 codegen_->RecordPcInfo(instruction, dex_pc);
2533
2534 break;
2535 }
2536
2537 default:
2538 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2539 }
2540}
2541
Calin Juravle7c4954d2014-10-28 16:57:40 +00002542void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002543 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002544 ? LocationSummary::kCall
2545 : LocationSummary::kNoCall;
2546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2547
Calin Juravle7c4954d2014-10-28 16:57:40 +00002548 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002549 case Primitive::kPrimInt: {
2550 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002551 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002552 locations->SetOut(Location::SameAsFirstInput());
2553 // Intel uses edx:eax as the dividend.
2554 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2556 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2557 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002558 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002559 locations->AddTemp(Location::RequiresRegister());
2560 }
Calin Juravled0d48522014-11-04 16:40:20 +00002561 break;
2562 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002563 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002564 InvokeRuntimeCallingConvention calling_convention;
2565 locations->SetInAt(0, Location::RegisterPairLocation(
2566 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2567 locations->SetInAt(1, Location::RegisterPairLocation(
2568 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2569 // Runtime helper puts the result in EAX, EDX.
2570 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002571 break;
2572 }
2573 case Primitive::kPrimFloat:
2574 case Primitive::kPrimDouble: {
2575 locations->SetInAt(0, Location::RequiresFpuRegister());
2576 locations->SetInAt(1, Location::RequiresFpuRegister());
2577 locations->SetOut(Location::SameAsFirstInput());
2578 break;
2579 }
2580
2581 default:
2582 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2583 }
2584}
2585
2586void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2587 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002588 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589 Location first = locations->InAt(0);
2590 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002591
2592 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002593 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002595 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002596 break;
2597 }
2598
2599 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002600 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002602 break;
2603 }
2604
2605 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002606 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002608 break;
2609 }
2610
2611 default:
2612 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2613 }
2614}
2615
Calin Juravlebacfec32014-11-14 15:54:36 +00002616void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002617 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002618
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002619 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2620 ? LocationSummary::kCall
2621 : LocationSummary::kNoCall;
2622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002623
Calin Juravled2ec87d2014-12-08 14:24:46 +00002624 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002625 case Primitive::kPrimInt: {
2626 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002627 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002628 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002629 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2630 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2631 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002632 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002633 locations->AddTemp(Location::RequiresRegister());
2634 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002635 break;
2636 }
2637 case Primitive::kPrimLong: {
2638 InvokeRuntimeCallingConvention calling_convention;
2639 locations->SetInAt(0, Location::RegisterPairLocation(
2640 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2641 locations->SetInAt(1, Location::RegisterPairLocation(
2642 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2643 // Runtime helper puts the result in EAX, EDX.
2644 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2645 break;
2646 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002647 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002648 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002649 locations->SetInAt(0, Location::Any());
2650 locations->SetInAt(1, Location::Any());
2651 locations->SetOut(Location::RequiresFpuRegister());
2652 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002653 break;
2654 }
2655
2656 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002657 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002658 }
2659}
2660
2661void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2662 Primitive::Type type = rem->GetResultType();
2663 switch (type) {
2664 case Primitive::kPrimInt:
2665 case Primitive::kPrimLong: {
2666 GenerateDivRemIntegral(rem);
2667 break;
2668 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002669 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002670 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002671 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002672 break;
2673 }
2674 default:
2675 LOG(FATAL) << "Unexpected rem type " << type;
2676 }
2677}
2678
Calin Juravled0d48522014-11-04 16:40:20 +00002679void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2680 LocationSummary* locations =
2681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002682 switch (instruction->GetType()) {
2683 case Primitive::kPrimInt: {
2684 locations->SetInAt(0, Location::Any());
2685 break;
2686 }
2687 case Primitive::kPrimLong: {
2688 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2689 if (!instruction->IsConstant()) {
2690 locations->AddTemp(Location::RequiresRegister());
2691 }
2692 break;
2693 }
2694 default:
2695 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2696 }
Calin Juravled0d48522014-11-04 16:40:20 +00002697 if (instruction->HasUses()) {
2698 locations->SetOut(Location::SameAsFirstInput());
2699 }
2700}
2701
2702void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2703 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2704 codegen_->AddSlowPath(slow_path);
2705
2706 LocationSummary* locations = instruction->GetLocations();
2707 Location value = locations->InAt(0);
2708
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002709 switch (instruction->GetType()) {
2710 case Primitive::kPrimInt: {
2711 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002713 __ j(kEqual, slow_path->GetEntryLabel());
2714 } else if (value.IsStackSlot()) {
2715 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2716 __ j(kEqual, slow_path->GetEntryLabel());
2717 } else {
2718 DCHECK(value.IsConstant()) << value;
2719 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2720 __ jmp(slow_path->GetEntryLabel());
2721 }
2722 }
2723 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002724 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002725 case Primitive::kPrimLong: {
2726 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002728 __ movl(temp, value.AsRegisterPairLow<Register>());
2729 __ orl(temp, value.AsRegisterPairHigh<Register>());
2730 __ j(kEqual, slow_path->GetEntryLabel());
2731 } else {
2732 DCHECK(value.IsConstant()) << value;
2733 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2734 __ jmp(slow_path->GetEntryLabel());
2735 }
2736 }
2737 break;
2738 }
2739 default:
2740 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002741 }
Calin Juravled0d48522014-11-04 16:40:20 +00002742}
2743
Calin Juravle9aec02f2014-11-18 23:06:35 +00002744void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2745 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2746
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2749
2750 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002751 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002752 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002753 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002754 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002755 // The shift count needs to be in CL or a constant.
2756 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002757 locations->SetOut(Location::SameAsFirstInput());
2758 break;
2759 }
2760 default:
2761 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2762 }
2763}
2764
2765void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2766 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2767
2768 LocationSummary* locations = op->GetLocations();
2769 Location first = locations->InAt(0);
2770 Location second = locations->InAt(1);
2771 DCHECK(first.Equals(locations->Out()));
2772
2773 switch (op->GetResultType()) {
2774 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002775 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002776 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002779 DCHECK_EQ(ECX, second_reg);
2780 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002783 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002784 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002786 }
2787 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002788 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2789 if (shift == 0) {
2790 return;
2791 }
2792 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002793 if (op->IsShl()) {
2794 __ shll(first_reg, imm);
2795 } else if (op->IsShr()) {
2796 __ sarl(first_reg, imm);
2797 } else {
2798 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002799 }
2800 }
2801 break;
2802 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002803 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002804 if (second.IsRegister()) {
2805 Register second_reg = second.AsRegister<Register>();
2806 DCHECK_EQ(ECX, second_reg);
2807 if (op->IsShl()) {
2808 GenerateShlLong(first, second_reg);
2809 } else if (op->IsShr()) {
2810 GenerateShrLong(first, second_reg);
2811 } else {
2812 GenerateUShrLong(first, second_reg);
2813 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002814 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002815 // Shift by a constant.
2816 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2817 // Nothing to do if the shift is 0, as the input is already the output.
2818 if (shift != 0) {
2819 if (op->IsShl()) {
2820 GenerateShlLong(first, shift);
2821 } else if (op->IsShr()) {
2822 GenerateShrLong(first, shift);
2823 } else {
2824 GenerateUShrLong(first, shift);
2825 }
2826 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002827 }
2828 break;
2829 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002830 default:
2831 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2832 }
2833}
2834
Mark P Mendell73945692015-04-29 14:56:17 +00002835void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2836 Register low = loc.AsRegisterPairLow<Register>();
2837 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002838 if (shift == 1) {
2839 // This is just an addition.
2840 __ addl(low, low);
2841 __ adcl(high, high);
2842 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002843 // Shift by 32 is easy. High gets low, and low gets 0.
2844 codegen_->EmitParallelMoves(
2845 loc.ToLow(),
2846 loc.ToHigh(),
2847 Primitive::kPrimInt,
2848 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2849 loc.ToLow(),
2850 Primitive::kPrimInt);
2851 } else if (shift > 32) {
2852 // Low part becomes 0. High part is low part << (shift-32).
2853 __ movl(high, low);
2854 __ shll(high, Immediate(shift - 32));
2855 __ xorl(low, low);
2856 } else {
2857 // Between 1 and 31.
2858 __ shld(high, low, Immediate(shift));
2859 __ shll(low, Immediate(shift));
2860 }
2861}
2862
Calin Juravle9aec02f2014-11-18 23:06:35 +00002863void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2864 Label done;
2865 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2866 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2867 __ testl(shifter, Immediate(32));
2868 __ j(kEqual, &done);
2869 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2870 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2871 __ Bind(&done);
2872}
2873
Mark P Mendell73945692015-04-29 14:56:17 +00002874void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2875 Register low = loc.AsRegisterPairLow<Register>();
2876 Register high = loc.AsRegisterPairHigh<Register>();
2877 if (shift == 32) {
2878 // Need to copy the sign.
2879 DCHECK_NE(low, high);
2880 __ movl(low, high);
2881 __ sarl(high, Immediate(31));
2882 } else if (shift > 32) {
2883 DCHECK_NE(low, high);
2884 // High part becomes sign. Low part is shifted by shift - 32.
2885 __ movl(low, high);
2886 __ sarl(high, Immediate(31));
2887 __ sarl(low, Immediate(shift - 32));
2888 } else {
2889 // Between 1 and 31.
2890 __ shrd(low, high, Immediate(shift));
2891 __ sarl(high, Immediate(shift));
2892 }
2893}
2894
Calin Juravle9aec02f2014-11-18 23:06:35 +00002895void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2896 Label done;
2897 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2898 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2899 __ testl(shifter, Immediate(32));
2900 __ j(kEqual, &done);
2901 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2902 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2903 __ Bind(&done);
2904}
2905
Mark P Mendell73945692015-04-29 14:56:17 +00002906void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2907 Register low = loc.AsRegisterPairLow<Register>();
2908 Register high = loc.AsRegisterPairHigh<Register>();
2909 if (shift == 32) {
2910 // Shift by 32 is easy. Low gets high, and high gets 0.
2911 codegen_->EmitParallelMoves(
2912 loc.ToHigh(),
2913 loc.ToLow(),
2914 Primitive::kPrimInt,
2915 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2916 loc.ToHigh(),
2917 Primitive::kPrimInt);
2918 } else if (shift > 32) {
2919 // Low part is high >> (shift - 32). High part becomes 0.
2920 __ movl(low, high);
2921 __ shrl(low, Immediate(shift - 32));
2922 __ xorl(high, high);
2923 } else {
2924 // Between 1 and 31.
2925 __ shrd(low, high, Immediate(shift));
2926 __ shrl(high, Immediate(shift));
2927 }
2928}
2929
Calin Juravle9aec02f2014-11-18 23:06:35 +00002930void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2931 Label done;
2932 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2933 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2934 __ testl(shifter, Immediate(32));
2935 __ j(kEqual, &done);
2936 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2937 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2938 __ Bind(&done);
2939}
2940
2941void LocationsBuilderX86::VisitShl(HShl* shl) {
2942 HandleShift(shl);
2943}
2944
2945void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2946 HandleShift(shl);
2947}
2948
2949void LocationsBuilderX86::VisitShr(HShr* shr) {
2950 HandleShift(shr);
2951}
2952
2953void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2954 HandleShift(shr);
2955}
2956
2957void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2958 HandleShift(ushr);
2959}
2960
2961void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2962 HandleShift(ushr);
2963}
2964
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002965void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 LocationSummary* locations =
2967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002968 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002969 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002970 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002971 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002972}
2973
2974void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2975 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002976 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002977 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002978
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002979 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002980
Nicolas Geoffray39468442014-09-02 15:17:15 +01002981 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002982 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002983}
2984
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002985void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2986 LocationSummary* locations =
2987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2988 locations->SetOut(Location::RegisterLocation(EAX));
2989 InvokeRuntimeCallingConvention calling_convention;
2990 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002991 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002992 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002993}
2994
2995void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2996 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002997 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002998 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2999
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003000 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003001
3002 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3003 DCHECK(!codegen_->IsLeafMethod());
3004}
3005
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003009 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3010 if (location.IsStackSlot()) {
3011 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3012 } else if (location.IsDoubleStackSlot()) {
3013 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003014 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003015 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003016}
3017
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003018void InstructionCodeGeneratorX86::VisitParameterValue(
3019 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3020}
3021
3022void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3023 LocationSummary* locations =
3024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3025 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3026}
3027
3028void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003029}
3030
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003031void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003032 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003033 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003034 locations->SetInAt(0, Location::RequiresRegister());
3035 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003036}
3037
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003038void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3039 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003040 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003041 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003042 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003043 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003044 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003046 break;
3047
3048 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003049 __ notl(out.AsRegisterPairLow<Register>());
3050 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003051 break;
3052
3053 default:
3054 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3055 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003056}
3057
David Brazdil66d126e2015-04-03 16:02:44 +01003058void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3061 locations->SetInAt(0, Location::RequiresRegister());
3062 locations->SetOut(Location::SameAsFirstInput());
3063}
3064
3065void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003066 LocationSummary* locations = bool_not->GetLocations();
3067 Location in = locations->InAt(0);
3068 Location out = locations->Out();
3069 DCHECK(in.Equals(out));
3070 __ xorl(out.AsRegister<Register>(), Immediate(1));
3071}
3072
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003073void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003074 LocationSummary* locations =
3075 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003076 switch (compare->InputAt(0)->GetType()) {
3077 case Primitive::kPrimLong: {
3078 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003079 locations->SetInAt(1, Location::Any());
3080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3081 break;
3082 }
3083 case Primitive::kPrimFloat:
3084 case Primitive::kPrimDouble: {
3085 locations->SetInAt(0, Location::RequiresFpuRegister());
3086 locations->SetInAt(1, Location::RequiresFpuRegister());
3087 locations->SetOut(Location::RequiresRegister());
3088 break;
3089 }
3090 default:
3091 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3092 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003093}
3094
3095void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003096 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003098 Location left = locations->InAt(0);
3099 Location right = locations->InAt(1);
3100
3101 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003102 switch (compare->InputAt(0)->GetType()) {
3103 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003104 Register left_low = left.AsRegisterPairLow<Register>();
3105 Register left_high = left.AsRegisterPairHigh<Register>();
3106 int32_t val_low = 0;
3107 int32_t val_high = 0;
3108 bool right_is_const = false;
3109
3110 if (right.IsConstant()) {
3111 DCHECK(right.GetConstant()->IsLongConstant());
3112 right_is_const = true;
3113 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3114 val_low = Low32Bits(val);
3115 val_high = High32Bits(val);
3116 }
3117
Calin Juravleddb7df22014-11-25 20:56:51 +00003118 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003119 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003120 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003121 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003122 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003123 DCHECK(right_is_const) << right;
3124 if (val_high == 0) {
3125 __ testl(left_high, left_high);
3126 } else {
3127 __ cmpl(left_high, Immediate(val_high));
3128 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003129 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003130 __ j(kLess, &less); // Signed compare.
3131 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003132 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003133 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003134 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003135 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003136 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003137 DCHECK(right_is_const) << right;
3138 if (val_low == 0) {
3139 __ testl(left_low, left_low);
3140 } else {
3141 __ cmpl(left_low, Immediate(val_low));
3142 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003143 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003144 break;
3145 }
3146 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003147 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003148 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3149 break;
3150 }
3151 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003152 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003153 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003154 break;
3155 }
3156 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003157 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003158 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003159 __ movl(out, Immediate(0));
3160 __ j(kEqual, &done);
3161 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3162
3163 __ Bind(&greater);
3164 __ movl(out, Immediate(1));
3165 __ jmp(&done);
3166
3167 __ Bind(&less);
3168 __ movl(out, Immediate(-1));
3169
3170 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003171}
3172
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003173void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003174 LocationSummary* locations =
3175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003176 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3177 locations->SetInAt(i, Location::Any());
3178 }
3179 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003180}
3181
3182void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003183 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003184 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003185}
3186
Calin Juravle52c48962014-12-16 17:02:57 +00003187void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3188 /*
3189 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3190 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3191 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3192 */
3193 switch (kind) {
3194 case MemBarrierKind::kAnyAny: {
3195 __ mfence();
3196 break;
3197 }
3198 case MemBarrierKind::kAnyStore:
3199 case MemBarrierKind::kLoadAny:
3200 case MemBarrierKind::kStoreStore: {
3201 // nop
3202 break;
3203 }
3204 default:
3205 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003206 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207}
3208
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003209
Mark Mendell09ed1a32015-03-25 08:30:06 -04003210void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003211 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003212 // TODO: Implement all kinds of calls:
3213 // 1) boot -> boot
3214 // 2) app -> boot
3215 // 3) app -> app
3216 //
3217 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003218
3219 if (invoke->IsStringInit()) {
3220 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003221 Register reg = temp.AsRegister<Register>();
3222 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003223 // (temp + offset_of_quick_compiled_code)()
3224 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003225 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3226 } else if (invoke->IsRecursive()) {
3227 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003228 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003229 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3230
3231 Register method_reg;
3232 Register reg = temp.AsRegister<Register>();
3233 if (current_method.IsRegister()) {
3234 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003235 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003236 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003237 DCHECK(!current_method.IsValid());
3238 method_reg = reg;
3239 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003240 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003241 // temp = temp->dex_cache_resolved_methods_;
3242 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3243 // temp = temp[index_in_cache]
3244 __ movl(reg, Address(reg,
3245 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3246 // (temp + offset_of_quick_compiled_code)()
3247 __ call(Address(reg,
3248 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003249 }
3250
3251 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003252}
3253
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003254void CodeGeneratorX86::MarkGCCard(Register temp,
3255 Register card,
3256 Register object,
3257 Register value,
3258 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003259 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003260 if (value_can_be_null) {
3261 __ testl(value, value);
3262 __ j(kEqual, &is_null);
3263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3265 __ movl(temp, object);
3266 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003267 __ movb(Address(temp, card, TIMES_1, 0),
3268 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003269 if (value_can_be_null) {
3270 __ Bind(&is_null);
3271 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272}
3273
Calin Juravle52c48962014-12-16 17:02:57 +00003274void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3275 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003278 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003279
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003280 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3281 locations->SetOut(Location::RequiresFpuRegister());
3282 } else {
3283 // The output overlaps in case of long: we don't want the low move to overwrite
3284 // the object's location.
3285 locations->SetOut(Location::RequiresRegister(),
3286 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3287 : Location::kNoOutputOverlap);
3288 }
Calin Juravle52c48962014-12-16 17:02:57 +00003289
3290 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3291 // Long values can be loaded atomically into an XMM using movsd.
3292 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3293 // and then copy the XMM into the output 32bits at a time).
3294 locations->AddTemp(Location::RequiresFpuRegister());
3295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003296}
3297
Calin Juravle52c48962014-12-16 17:02:57 +00003298void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3299 const FieldInfo& field_info) {
3300 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301
Calin Juravle52c48962014-12-16 17:02:57 +00003302 LocationSummary* locations = instruction->GetLocations();
3303 Register base = locations->InAt(0).AsRegister<Register>();
3304 Location out = locations->Out();
3305 bool is_volatile = field_info.IsVolatile();
3306 Primitive::Type field_type = field_info.GetFieldType();
3307 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3308
3309 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003310 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003311 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003312 break;
3313 }
3314
3315 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003316 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003317 break;
3318 }
3319
3320 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003322 break;
3323 }
3324
3325 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003326 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 break;
3328 }
3329
3330 case Primitive::kPrimInt:
3331 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003332 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003333 break;
3334 }
3335
3336 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003337 if (is_volatile) {
3338 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3339 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003341 __ movd(out.AsRegisterPairLow<Register>(), temp);
3342 __ psrlq(temp, Immediate(32));
3343 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3344 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003345 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003346 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003347 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003348 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3349 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003350 break;
3351 }
3352
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003353 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003354 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003355 break;
3356 }
3357
3358 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003359 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003360 break;
3361 }
3362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003363 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003364 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003365 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003366 }
Calin Juravle52c48962014-12-16 17:02:57 +00003367
Calin Juravle77520bc2015-01-12 18:45:46 +00003368 // Longs are handled in the switch.
3369 if (field_type != Primitive::kPrimLong) {
3370 codegen_->MaybeRecordImplicitNullCheck(instruction);
3371 }
3372
Calin Juravle52c48962014-12-16 17:02:57 +00003373 if (is_volatile) {
3374 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3375 }
3376}
3377
3378void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3379 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3380
3381 LocationSummary* locations =
3382 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3383 locations->SetInAt(0, Location::RequiresRegister());
3384 bool is_volatile = field_info.IsVolatile();
3385 Primitive::Type field_type = field_info.GetFieldType();
3386 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3387 || (field_type == Primitive::kPrimByte);
3388
3389 // The register allocator does not support multiple
3390 // inputs that die at entry with one in a specific register.
3391 if (is_byte_type) {
3392 // Ensure the value is in a byte register.
3393 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003394 } else if (Primitive::IsFloatingPointType(field_type)) {
3395 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003396 } else {
3397 locations->SetInAt(1, Location::RequiresRegister());
3398 }
3399 // Temporary registers for the write barrier.
3400 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3401 locations->AddTemp(Location::RequiresRegister());
3402 // Ensure the card is in a byte register.
3403 locations->AddTemp(Location::RegisterLocation(ECX));
3404 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3405 // 64bits value can be atomically written to an address with movsd and an XMM register.
3406 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3407 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3408 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3409 // isolated cases when we need this it isn't worth adding the extra complexity.
3410 locations->AddTemp(Location::RequiresFpuRegister());
3411 locations->AddTemp(Location::RequiresFpuRegister());
3412 }
3413}
3414
3415void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003416 const FieldInfo& field_info,
3417 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003418 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3419
3420 LocationSummary* locations = instruction->GetLocations();
3421 Register base = locations->InAt(0).AsRegister<Register>();
3422 Location value = locations->InAt(1);
3423 bool is_volatile = field_info.IsVolatile();
3424 Primitive::Type field_type = field_info.GetFieldType();
3425 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3426
3427 if (is_volatile) {
3428 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3429 }
3430
3431 switch (field_type) {
3432 case Primitive::kPrimBoolean:
3433 case Primitive::kPrimByte: {
3434 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3435 break;
3436 }
3437
3438 case Primitive::kPrimShort:
3439 case Primitive::kPrimChar: {
3440 __ movw(Address(base, offset), value.AsRegister<Register>());
3441 break;
3442 }
3443
3444 case Primitive::kPrimInt:
3445 case Primitive::kPrimNot: {
3446 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003447 break;
3448 }
3449
3450 case Primitive::kPrimLong: {
3451 if (is_volatile) {
3452 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3453 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3454 __ movd(temp1, value.AsRegisterPairLow<Register>());
3455 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3456 __ punpckldq(temp1, temp2);
3457 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003458 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003459 } else {
3460 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003461 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003462 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3463 }
3464 break;
3465 }
3466
3467 case Primitive::kPrimFloat: {
3468 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3469 break;
3470 }
3471
3472 case Primitive::kPrimDouble: {
3473 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3474 break;
3475 }
3476
3477 case Primitive::kPrimVoid:
3478 LOG(FATAL) << "Unreachable type " << field_type;
3479 UNREACHABLE();
3480 }
3481
Calin Juravle77520bc2015-01-12 18:45:46 +00003482 // Longs are handled in the switch.
3483 if (field_type != Primitive::kPrimLong) {
3484 codegen_->MaybeRecordImplicitNullCheck(instruction);
3485 }
3486
3487 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3488 Register temp = locations->GetTemp(0).AsRegister<Register>();
3489 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003490 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003491 }
3492
Calin Juravle52c48962014-12-16 17:02:57 +00003493 if (is_volatile) {
3494 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3495 }
3496}
3497
3498void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3499 HandleFieldGet(instruction, instruction->GetFieldInfo());
3500}
3501
3502void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3503 HandleFieldGet(instruction, instruction->GetFieldInfo());
3504}
3505
3506void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3507 HandleFieldSet(instruction, instruction->GetFieldInfo());
3508}
3509
3510void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* 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::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3515 HandleFieldSet(instruction, instruction->GetFieldInfo());
3516}
3517
3518void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003519 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003520}
3521
3522void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3523 HandleFieldGet(instruction, instruction->GetFieldInfo());
3524}
3525
3526void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3527 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528}
3529
3530void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003531 LocationSummary* locations =
3532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003533 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3534 ? Location::RequiresRegister()
3535 : Location::Any();
3536 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003537 if (instruction->HasUses()) {
3538 locations->SetOut(Location::SameAsFirstInput());
3539 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540}
3541
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003542void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003543 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3544 return;
3545 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003546 LocationSummary* locations = instruction->GetLocations();
3547 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003548
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003549 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3550 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3551}
3552
3553void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003554 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003555 codegen_->AddSlowPath(slow_path);
3556
3557 LocationSummary* locations = instruction->GetLocations();
3558 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003559
3560 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003561 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003562 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003563 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003564 } else {
3565 DCHECK(obj.IsConstant()) << obj;
3566 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3567 __ jmp(slow_path->GetEntryLabel());
3568 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003569 }
3570 __ j(kEqual, slow_path->GetEntryLabel());
3571}
3572
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003573void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3574 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3575 GenerateImplicitNullCheck(instruction);
3576 } else {
3577 GenerateExplicitNullCheck(instruction);
3578 }
3579}
3580
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003581void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003582 LocationSummary* locations =
3583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003584 locations->SetInAt(0, Location::RequiresRegister());
3585 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003586 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3588 } else {
3589 // The output overlaps in case of long: we don't want the low move to overwrite
3590 // the array's location.
3591 locations->SetOut(Location::RequiresRegister(),
3592 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3593 : Location::kNoOutputOverlap);
3594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003595}
3596
3597void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3598 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 Location index = locations->InAt(1);
3601
Calin Juravle77520bc2015-01-12 18:45:46 +00003602 Primitive::Type type = instruction->GetType();
3603 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 case Primitive::kPrimBoolean: {
3605 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003607 if (index.IsConstant()) {
3608 __ movzxb(out, Address(obj,
3609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3610 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003612 }
3613 break;
3614 }
3615
3616 case Primitive::kPrimByte: {
3617 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003618 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003619 if (index.IsConstant()) {
3620 __ movsxb(out, Address(obj,
3621 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3622 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624 }
3625 break;
3626 }
3627
3628 case Primitive::kPrimShort: {
3629 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003631 if (index.IsConstant()) {
3632 __ movsxw(out, Address(obj,
3633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3634 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003635 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003636 }
3637 break;
3638 }
3639
3640 case Primitive::kPrimChar: {
3641 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003642 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643 if (index.IsConstant()) {
3644 __ movzxw(out, Address(obj,
3645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3646 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648 }
3649 break;
3650 }
3651
3652 case Primitive::kPrimInt:
3653 case Primitive::kPrimNot: {
3654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003656 if (index.IsConstant()) {
3657 __ movl(out, Address(obj,
3658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3659 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003661 }
3662 break;
3663 }
3664
3665 case Primitive::kPrimLong: {
3666 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003667 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003668 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003669 if (index.IsConstant()) {
3670 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003671 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003672 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003673 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003674 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003675 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003678 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003679 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003680 }
3681 break;
3682 }
3683
Mark Mendell7c8d0092015-01-26 11:21:33 -05003684 case Primitive::kPrimFloat: {
3685 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3686 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3687 if (index.IsConstant()) {
3688 __ movss(out, Address(obj,
3689 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3690 } else {
3691 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3692 }
3693 break;
3694 }
3695
3696 case Primitive::kPrimDouble: {
3697 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3698 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3699 if (index.IsConstant()) {
3700 __ movsd(out, Address(obj,
3701 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3702 } else {
3703 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3704 }
3705 break;
3706 }
3707
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003708 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003709 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003710 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003711 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003712
3713 if (type != Primitive::kPrimLong) {
3714 codegen_->MaybeRecordImplicitNullCheck(instruction);
3715 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003716}
3717
3718void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003719 // This location builder might end up asking to up to four registers, which is
3720 // not currently possible for baseline. The situation in which we need four
3721 // registers cannot be met by baseline though, because it has not run any
3722 // optimization.
3723
Nicolas Geoffray39468442014-09-02 15:17:15 +01003724 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003725 bool needs_write_barrier =
3726 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3727
Mark Mendell5f874182015-03-04 15:42:45 -05003728 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003729
Nicolas Geoffray39468442014-09-02 15:17:15 +01003730 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3731 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003732 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003733
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003734 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003736 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3737 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3738 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003740 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3741 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003742 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003743 // In case of a byte operation, the register allocator does not support multiple
3744 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003745 locations->SetInAt(0, Location::RequiresRegister());
3746 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003747 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003748 // Ensure the value is in a byte register.
3749 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003750 } else if (Primitive::IsFloatingPointType(value_type)) {
3751 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003753 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003754 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003755 // Temporary registers for the write barrier.
3756 if (needs_write_barrier) {
3757 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003758 // Ensure the card is in a byte register.
3759 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762}
3763
3764void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3765 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003766 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003768 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003769 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 bool needs_runtime_call = locations->WillCall();
3771 bool needs_write_barrier =
3772 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003773
3774 switch (value_type) {
3775 case Primitive::kPrimBoolean:
3776 case Primitive::kPrimByte: {
3777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 if (index.IsConstant()) {
3779 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003780 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003781 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003782 } else {
3783 __ movb(Address(obj, offset),
3784 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3785 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003787 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003788 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003789 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003790 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003792 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3793 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 break;
3797 }
3798
3799 case Primitive::kPrimShort:
3800 case Primitive::kPrimChar: {
3801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 if (index.IsConstant()) {
3803 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003804 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003806 } else {
3807 __ movw(Address(obj, offset),
3808 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3809 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003810 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003811 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3813 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003814 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003816 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3817 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003818 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 break;
3821 }
3822
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003823 case Primitive::kPrimInt:
3824 case Primitive::kPrimNot: {
3825 if (!needs_runtime_call) {
3826 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3827 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003828 size_t offset =
3829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003830 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003831 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003832 } else {
3833 DCHECK(value.IsConstant()) << value;
3834 __ movl(Address(obj, offset),
3835 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3836 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003837 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003838 DCHECK(index.IsRegister()) << index;
3839 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003840 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3841 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003842 } else {
3843 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3846 }
3847 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003849
3850 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 Register temp = locations->GetTemp(0).AsRegister<Register>();
3852 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003853 codegen_->MarkGCCard(
3854 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003855 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003857 DCHECK_EQ(value_type, Primitive::kPrimNot);
3858 DCHECK(!codegen_->IsLeafMethod());
3859 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3860 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003861 }
3862 break;
3863 }
3864
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 case Primitive::kPrimLong: {
3866 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003867 if (index.IsConstant()) {
3868 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003869 if (value.IsRegisterPair()) {
3870 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003871 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003872 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003873 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003874 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003875 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3876 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003877 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003878 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003881 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003883 value.AsRegisterPairLow<Register>());
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 Geoffray56b9ee62014-10-09 11:47:51 +01003886 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003887 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003888 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003889 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003890 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003891 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003892 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003894 Immediate(High32Bits(val)));
3895 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003896 }
3897 break;
3898 }
3899
Mark Mendell7c8d0092015-01-26 11:21:33 -05003900 case Primitive::kPrimFloat: {
3901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3902 DCHECK(value.IsFpuRegister());
3903 if (index.IsConstant()) {
3904 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3905 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3906 } else {
3907 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3908 value.AsFpuRegister<XmmRegister>());
3909 }
3910 break;
3911 }
3912
3913 case Primitive::kPrimDouble: {
3914 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3915 DCHECK(value.IsFpuRegister());
3916 if (index.IsConstant()) {
3917 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3918 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3919 } else {
3920 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3921 value.AsFpuRegister<XmmRegister>());
3922 }
3923 break;
3924 }
3925
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003926 case Primitive::kPrimVoid:
3927 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003928 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003929 }
3930}
3931
3932void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003934 locations->SetInAt(0, Location::RequiresRegister());
3935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936}
3937
3938void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3939 LocationSummary* locations = instruction->GetLocations();
3940 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 Register obj = locations->InAt(0).AsRegister<Register>();
3942 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945}
3946
3947void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003948 LocationSummary* locations =
3949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003950 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003951 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003952 if (instruction->HasUses()) {
3953 locations->SetOut(Location::SameAsFirstInput());
3954 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955}
3956
3957void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3958 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003959 Location index_loc = locations->InAt(0);
3960 Location length_loc = locations->InAt(1);
3961 SlowPathCodeX86* slow_path =
3962 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963
Mark Mendell99dbd682015-04-22 16:18:52 -04003964 if (length_loc.IsConstant()) {
3965 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3966 if (index_loc.IsConstant()) {
3967 // BCE will remove the bounds check if we are guarenteed to pass.
3968 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3969 if (index < 0 || index >= length) {
3970 codegen_->AddSlowPath(slow_path);
3971 __ jmp(slow_path->GetEntryLabel());
3972 } else {
3973 // Some optimization after BCE may have generated this, and we should not
3974 // generate a bounds check if it is a valid range.
3975 }
3976 return;
3977 }
3978
3979 // We have to reverse the jump condition because the length is the constant.
3980 Register index_reg = index_loc.AsRegister<Register>();
3981 __ cmpl(index_reg, Immediate(length));
3982 codegen_->AddSlowPath(slow_path);
3983 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003984 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003985 Register length = length_loc.AsRegister<Register>();
3986 if (index_loc.IsConstant()) {
3987 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3988 __ cmpl(length, Immediate(value));
3989 } else {
3990 __ cmpl(length, index_loc.AsRegister<Register>());
3991 }
3992 codegen_->AddSlowPath(slow_path);
3993 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003994 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995}
3996
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003997void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3998 temp->SetLocations(nullptr);
3999}
4000
4001void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4002 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004003 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004004}
4005
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004006void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004007 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004008 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004009}
4010
4011void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004012 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4013}
4014
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004015void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4017}
4018
4019void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004020 HBasicBlock* block = instruction->GetBlock();
4021 if (block->GetLoopInformation() != nullptr) {
4022 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4023 // The back edge will generate the suspend check.
4024 return;
4025 }
4026 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4027 // The goto will generate the suspend check.
4028 return;
4029 }
4030 GenerateSuspendCheck(instruction, nullptr);
4031}
4032
4033void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4034 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004035 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004036 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4037 if (slow_path == nullptr) {
4038 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4039 instruction->SetSlowPath(slow_path);
4040 codegen_->AddSlowPath(slow_path);
4041 if (successor != nullptr) {
4042 DCHECK(successor->IsLoopHeader());
4043 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4044 }
4045 } else {
4046 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4047 }
4048
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004049 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004050 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004051 if (successor == nullptr) {
4052 __ j(kNotEqual, slow_path->GetEntryLabel());
4053 __ Bind(slow_path->GetReturnLabel());
4054 } else {
4055 __ j(kEqual, codegen_->GetLabelOf(successor));
4056 __ jmp(slow_path->GetEntryLabel());
4057 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004058}
4059
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004060X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4061 return codegen_->GetAssembler();
4062}
4063
Mark Mendell7c8d0092015-01-26 11:21:33 -05004064void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004065 ScratchRegisterScope ensure_scratch(
4066 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4067 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4068 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4069 __ movl(temp_reg, Address(ESP, src + stack_offset));
4070 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004071}
4072
4073void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004074 ScratchRegisterScope ensure_scratch(
4075 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4076 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4077 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4078 __ movl(temp_reg, Address(ESP, src + stack_offset));
4079 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4080 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4081 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004082}
4083
4084void ParallelMoveResolverX86::EmitMove(size_t index) {
4085 MoveOperands* move = moves_.Get(index);
4086 Location source = move->GetSource();
4087 Location destination = move->GetDestination();
4088
4089 if (source.IsRegister()) {
4090 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004092 } else {
4093 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004094 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004095 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004096 } else if (source.IsFpuRegister()) {
4097 if (destination.IsFpuRegister()) {
4098 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4099 } else if (destination.IsStackSlot()) {
4100 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4101 } else {
4102 DCHECK(destination.IsDoubleStackSlot());
4103 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4104 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004105 } else if (source.IsStackSlot()) {
4106 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004108 } else if (destination.IsFpuRegister()) {
4109 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004110 } else {
4111 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004112 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4113 }
4114 } else if (source.IsDoubleStackSlot()) {
4115 if (destination.IsFpuRegister()) {
4116 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4117 } else {
4118 DCHECK(destination.IsDoubleStackSlot()) << destination;
4119 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004120 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004121 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004122 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004123 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004124 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004125 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004126 if (value == 0) {
4127 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4128 } else {
4129 __ movl(destination.AsRegister<Register>(), Immediate(value));
4130 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004131 } else {
4132 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004133 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004134 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004135 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004136 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004137 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004138 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004139 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004140 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4141 if (value == 0) {
4142 // Easy handling of 0.0.
4143 __ xorps(dest, dest);
4144 } else {
4145 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004146 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4147 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4148 __ movl(temp, Immediate(value));
4149 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004150 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004151 } else {
4152 DCHECK(destination.IsStackSlot()) << destination;
4153 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4154 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004155 } else if (constant->IsLongConstant()) {
4156 int64_t value = constant->AsLongConstant()->GetValue();
4157 int32_t low_value = Low32Bits(value);
4158 int32_t high_value = High32Bits(value);
4159 Immediate low(low_value);
4160 Immediate high(high_value);
4161 if (destination.IsDoubleStackSlot()) {
4162 __ movl(Address(ESP, destination.GetStackIndex()), low);
4163 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4164 } else {
4165 __ movl(destination.AsRegisterPairLow<Register>(), low);
4166 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4167 }
4168 } else {
4169 DCHECK(constant->IsDoubleConstant());
4170 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004171 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004172 int32_t low_value = Low32Bits(value);
4173 int32_t high_value = High32Bits(value);
4174 Immediate low(low_value);
4175 Immediate high(high_value);
4176 if (destination.IsFpuRegister()) {
4177 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4178 if (value == 0) {
4179 // Easy handling of 0.0.
4180 __ xorpd(dest, dest);
4181 } else {
4182 __ pushl(high);
4183 __ pushl(low);
4184 __ movsd(dest, Address(ESP, 0));
4185 __ addl(ESP, Immediate(8));
4186 }
4187 } else {
4188 DCHECK(destination.IsDoubleStackSlot()) << destination;
4189 __ movl(Address(ESP, destination.GetStackIndex()), low);
4190 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4191 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004192 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004193 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004194 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004195 }
4196}
4197
Mark Mendella5c19ce2015-04-01 12:51:05 -04004198void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004199 Register suggested_scratch = reg == EAX ? EBX : EAX;
4200 ScratchRegisterScope ensure_scratch(
4201 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4202
4203 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4204 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4205 __ movl(Address(ESP, mem + stack_offset), reg);
4206 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004207}
4208
Mark Mendell7c8d0092015-01-26 11:21:33 -05004209void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004210 ScratchRegisterScope ensure_scratch(
4211 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4212
4213 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4214 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4215 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4216 __ movss(Address(ESP, mem + stack_offset), reg);
4217 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004218}
4219
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004220void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004221 ScratchRegisterScope ensure_scratch1(
4222 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004223
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004224 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4225 ScratchRegisterScope ensure_scratch2(
4226 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004227
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004228 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4229 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4230 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4231 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4232 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4233 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004234}
4235
4236void ParallelMoveResolverX86::EmitSwap(size_t index) {
4237 MoveOperands* move = moves_.Get(index);
4238 Location source = move->GetSource();
4239 Location destination = move->GetDestination();
4240
4241 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004242 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004243 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004244 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004245 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004246 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004247 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4248 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004249 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4250 // Use XOR Swap algorithm to avoid a temporary.
4251 DCHECK_NE(source.reg(), destination.reg());
4252 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4253 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4254 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4255 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4256 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4257 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4258 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004259 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4260 // Take advantage of the 16 bytes in the XMM register.
4261 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4262 Address stack(ESP, destination.GetStackIndex());
4263 // Load the double into the high doubleword.
4264 __ movhpd(reg, stack);
4265
4266 // Store the low double into the destination.
4267 __ movsd(stack, reg);
4268
4269 // Move the high double to the low double.
4270 __ psrldq(reg, Immediate(8));
4271 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4272 // Take advantage of the 16 bytes in the XMM register.
4273 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4274 Address stack(ESP, source.GetStackIndex());
4275 // Load the double into the high doubleword.
4276 __ movhpd(reg, stack);
4277
4278 // Store the low double into the destination.
4279 __ movsd(stack, reg);
4280
4281 // Move the high double to the low double.
4282 __ psrldq(reg, Immediate(8));
4283 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4284 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4285 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004286 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004287 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004288 }
4289}
4290
4291void ParallelMoveResolverX86::SpillScratch(int reg) {
4292 __ pushl(static_cast<Register>(reg));
4293}
4294
4295void ParallelMoveResolverX86::RestoreScratch(int reg) {
4296 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004297}
4298
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004299void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004300 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4301 ? LocationSummary::kCallOnSlowPath
4302 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004303 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004304 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004305 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004306 locations->SetOut(Location::RequiresRegister());
4307}
4308
4309void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004310 LocationSummary* locations = cls->GetLocations();
4311 Register out = locations->Out().AsRegister<Register>();
4312 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004313 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004314 DCHECK(!cls->CanCallRuntime());
4315 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004316 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004317 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004318 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004319 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004320 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004321 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004322
4323 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4324 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4325 codegen_->AddSlowPath(slow_path);
4326 __ testl(out, out);
4327 __ j(kEqual, slow_path->GetEntryLabel());
4328 if (cls->MustGenerateClinitCheck()) {
4329 GenerateClassInitializationCheck(slow_path, out);
4330 } else {
4331 __ Bind(slow_path->GetExitLabel());
4332 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 }
4334}
4335
4336void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4337 LocationSummary* locations =
4338 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4339 locations->SetInAt(0, Location::RequiresRegister());
4340 if (check->HasUses()) {
4341 locations->SetOut(Location::SameAsFirstInput());
4342 }
4343}
4344
4345void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004346 // We assume the class to not be null.
4347 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4348 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004349 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004350 GenerateClassInitializationCheck(slow_path,
4351 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004352}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004353
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004354void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4355 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004356 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4357 Immediate(mirror::Class::kStatusInitialized));
4358 __ j(kLess, slow_path->GetEntryLabel());
4359 __ Bind(slow_path->GetExitLabel());
4360 // No need for memory fence, thanks to the X86 memory model.
4361}
4362
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004363void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4364 LocationSummary* locations =
4365 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004366 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004367 locations->SetOut(Location::RequiresRegister());
4368}
4369
4370void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4371 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4372 codegen_->AddSlowPath(slow_path);
4373
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004374 LocationSummary* locations = load->GetLocations();
4375 Register out = locations->Out().AsRegister<Register>();
4376 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004377 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004378 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004379 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4380 __ testl(out, out);
4381 __ j(kEqual, slow_path->GetEntryLabel());
4382 __ Bind(slow_path->GetExitLabel());
4383}
4384
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004385void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4386 LocationSummary* locations =
4387 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4388 locations->SetOut(Location::RequiresRegister());
4389}
4390
4391void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4392 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004394 __ fs()->movl(address, Immediate(0));
4395}
4396
4397void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4398 LocationSummary* locations =
4399 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4400 InvokeRuntimeCallingConvention calling_convention;
4401 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4402}
4403
4404void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4405 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4406 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4407}
4408
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004409void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004410 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4411 ? LocationSummary::kNoCall
4412 : LocationSummary::kCallOnSlowPath;
4413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4414 locations->SetInAt(0, Location::RequiresRegister());
4415 locations->SetInAt(1, Location::Any());
4416 locations->SetOut(Location::RequiresRegister());
4417}
4418
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004419void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004420 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004422 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004423 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004424 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4425 Label done, zero;
4426 SlowPathCodeX86* slow_path = nullptr;
4427
4428 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004429 // Avoid null check if we know obj is not null.
4430 if (instruction->MustDoNullCheck()) {
4431 __ testl(obj, obj);
4432 __ j(kEqual, &zero);
4433 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004434 __ movl(out, Address(obj, class_offset));
4435 // Compare the class of `obj` with `cls`.
4436 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004437 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004438 } else {
4439 DCHECK(cls.IsStackSlot()) << cls;
4440 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4441 }
4442
4443 if (instruction->IsClassFinal()) {
4444 // Classes must be equal for the instanceof to succeed.
4445 __ j(kNotEqual, &zero);
4446 __ movl(out, Immediate(1));
4447 __ jmp(&done);
4448 } else {
4449 // If the classes are not equal, we go into a slow path.
4450 DCHECK(locations->OnlyCallsOnSlowPath());
4451 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004452 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004453 codegen_->AddSlowPath(slow_path);
4454 __ j(kNotEqual, slow_path->GetEntryLabel());
4455 __ movl(out, Immediate(1));
4456 __ jmp(&done);
4457 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004458
4459 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4460 __ Bind(&zero);
4461 __ movl(out, Immediate(0));
4462 }
4463
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004464 if (slow_path != nullptr) {
4465 __ Bind(slow_path->GetExitLabel());
4466 }
4467 __ Bind(&done);
4468}
4469
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004470void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4472 instruction, LocationSummary::kCallOnSlowPath);
4473 locations->SetInAt(0, Location::RequiresRegister());
4474 locations->SetInAt(1, Location::Any());
4475 locations->AddTemp(Location::RequiresRegister());
4476}
4477
4478void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4479 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004480 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004481 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004482 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004483 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4484 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4485 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4486 codegen_->AddSlowPath(slow_path);
4487
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004488 // Avoid null check if we know obj is not null.
4489 if (instruction->MustDoNullCheck()) {
4490 __ testl(obj, obj);
4491 __ j(kEqual, slow_path->GetExitLabel());
4492 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004493
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004494 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004495 // Compare the class of `obj` with `cls`.
4496 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004498 } else {
4499 DCHECK(cls.IsStackSlot()) << cls;
4500 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4501 }
4502
4503 __ j(kNotEqual, slow_path->GetEntryLabel());
4504 __ Bind(slow_path->GetExitLabel());
4505}
4506
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004507void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4508 LocationSummary* locations =
4509 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4510 InvokeRuntimeCallingConvention calling_convention;
4511 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4512}
4513
4514void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4515 __ fs()->call(Address::Absolute(instruction->IsEnter()
4516 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4517 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4518 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4519}
4520
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004521void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4522void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4523void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4524
4525void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4526 LocationSummary* locations =
4527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4528 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4529 || instruction->GetResultType() == Primitive::kPrimLong);
4530 locations->SetInAt(0, Location::RequiresRegister());
4531 locations->SetInAt(1, Location::Any());
4532 locations->SetOut(Location::SameAsFirstInput());
4533}
4534
4535void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4536 HandleBitwiseOperation(instruction);
4537}
4538
4539void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4540 HandleBitwiseOperation(instruction);
4541}
4542
4543void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4544 HandleBitwiseOperation(instruction);
4545}
4546
4547void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4548 LocationSummary* locations = instruction->GetLocations();
4549 Location first = locations->InAt(0);
4550 Location second = locations->InAt(1);
4551 DCHECK(first.Equals(locations->Out()));
4552
4553 if (instruction->GetResultType() == Primitive::kPrimInt) {
4554 if (second.IsRegister()) {
4555 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004556 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004557 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004558 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559 } else {
4560 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004562 }
4563 } else if (second.IsConstant()) {
4564 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004565 __ andl(first.AsRegister<Register>(),
4566 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004567 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004568 __ orl(first.AsRegister<Register>(),
4569 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004570 } else {
4571 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004572 __ xorl(first.AsRegister<Register>(),
4573 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004574 }
4575 } else {
4576 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004577 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004578 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004579 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580 } else {
4581 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004583 }
4584 }
4585 } else {
4586 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4587 if (second.IsRegisterPair()) {
4588 if (instruction->IsAnd()) {
4589 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4590 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4591 } else if (instruction->IsOr()) {
4592 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4593 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4594 } else {
4595 DCHECK(instruction->IsXor());
4596 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4597 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4598 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004599 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004600 if (instruction->IsAnd()) {
4601 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4602 __ andl(first.AsRegisterPairHigh<Register>(),
4603 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4604 } else if (instruction->IsOr()) {
4605 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4606 __ orl(first.AsRegisterPairHigh<Register>(),
4607 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4608 } else {
4609 DCHECK(instruction->IsXor());
4610 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4611 __ xorl(first.AsRegisterPairHigh<Register>(),
4612 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4613 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004614 } else {
4615 DCHECK(second.IsConstant()) << second;
4616 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004617 int32_t low_value = Low32Bits(value);
4618 int32_t high_value = High32Bits(value);
4619 Immediate low(low_value);
4620 Immediate high(high_value);
4621 Register first_low = first.AsRegisterPairLow<Register>();
4622 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004623 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004624 if (low_value == 0) {
4625 __ xorl(first_low, first_low);
4626 } else if (low_value != -1) {
4627 __ andl(first_low, low);
4628 }
4629 if (high_value == 0) {
4630 __ xorl(first_high, first_high);
4631 } else if (high_value != -1) {
4632 __ andl(first_high, high);
4633 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004634 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004635 if (low_value != 0) {
4636 __ orl(first_low, low);
4637 }
4638 if (high_value != 0) {
4639 __ orl(first_high, high);
4640 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004641 } else {
4642 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004643 if (low_value != 0) {
4644 __ xorl(first_low, low);
4645 }
4646 if (high_value != 0) {
4647 __ xorl(first_high, high);
4648 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004649 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004650 }
4651 }
4652}
4653
Calin Juravleb1498f62015-02-16 13:13:29 +00004654void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4655 // Nothing to do, this should be removed during prepare for register allocator.
4656 UNUSED(instruction);
4657 LOG(FATAL) << "Unreachable";
4658}
4659
4660void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4661 // Nothing to do, this should be removed during prepare for register allocator.
4662 UNUSED(instruction);
4663 LOG(FATAL) << "Unreachable";
4664}
4665
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004666} // namespace x86
4667} // namespace art