blob: baeb7755c0d82097ab5b0248eb602e6a0f6d601e [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
59 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
62};
63
Calin Juravled0d48522014-11-04 16:40:20 +000064class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
65 public:
66 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
67
Alexandre Rames2ed20af2015-03-06 13:55:35 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000069 __ Bind(GetEntryLabel());
70 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070071 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000072 }
73
74 private:
75 HDivZeroCheck* const instruction_;
76 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
77};
78
Calin Juravlebacfec32014-11-14 15:54:36 +000079class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000081 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000085 if (is_div_) {
86 __ negl(reg_);
87 } else {
88 __ movl(reg_, Immediate(0));
89 }
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ jmp(GetExitLabel());
91 }
92
93 private:
94 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000095 bool is_div_;
96 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000097};
98
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010099class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100100 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100101 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
102 Location index_location,
103 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000104 : instruction_(instruction),
105 index_location_(index_location),
106 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100110 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000111 // We're moving two locations to locations that could overlap, so we need a parallel
112 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000114 x86_codegen->EmitParallelMoves(
115 index_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100117 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000118 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100119 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
120 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700122 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 }
124
125 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100126 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 const Location index_location_;
128 const Location length_location_;
129
130 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000135 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000138 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700143 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 private:
162 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 Label return_label_;
165
166 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
167};
168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169class LoadStringSlowPathX86 : public SlowPathCodeX86 {
170 public:
171 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000174 LocationSummary* locations = instruction_->GetLocations();
175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
176
177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
178 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000185 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000187
188 __ jmp(GetExitLabel());
189 }
190
191 private:
192 HLoadString* const instruction_;
193
194 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 LoadClassSlowPathX86(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
206
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 InvokeRuntimeCallingConvention calling_convention;
214 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ fs()->call(Address::Absolute(do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ jmp(GetExitLabel());
229 }
230
231 private:
232 // The class this slow path will load.
233 HLoadClass* const cls_;
234
235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
246};
247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
249 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 TypeCheckSlowPathX86(HInstruction* instruction,
251 Location class_to_check,
252 Location object_class,
253 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 class_to_check_(class_to_check),
256 object_class_(object_class),
257 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000261 DCHECK(instruction_->IsCheckCast()
262 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
268 // We're moving two locations to locations that could overlap, so we need a parallel
269 // move resolver.
270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000271 x86_codegen->EmitParallelMoves(
272 class_to_check_,
273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100274 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000275 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100276 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
277 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000280 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
281 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 } else {
283 DCHECK(instruction_->IsCheckCast());
284 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
289 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 __ jmp(GetExitLabel());
294 }
295
296 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 HInstruction* const instruction_;
298 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
303};
304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700305class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
306 public:
307 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
308 : instruction_(instruction) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, instruction_->GetLocations());
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
314 // No need to restore live registers.
315 DCHECK(instruction_->IsDeoptimize());
316 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
317 uint32_t dex_pc = deoptimize->GetDexPc();
318 codegen->RecordPcInfo(instruction_, dex_pc, this);
319 }
320
321 private:
322 HInstruction* const instruction_;
323 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100327#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100344 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100348 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Mark Mendell7c8d0092015-01-26 11:21:33 -0500361size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
363 return GetFloatingPointSpillSlotSize();
364}
365
366size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
367 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
368 return GetFloatingPointSpillSlotSize();
369}
370
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
372 const X86InstructionSetFeatures& isa_features,
373 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500374 : CodeGenerator(graph,
375 kNumberOfCpuRegisters,
376 kNumberOfXmmRegisters,
377 kNumberOfRegisterPairs,
378 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
379 arraysize(kCoreCalleeSaves))
380 | (1 << kFakeReturnRegister),
381 0,
382 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100383 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 move_resolver_(graph->GetArena(), this),
387 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000388 // Use a fake return address register to mimic Quick.
389 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100390}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 switch (type) {
394 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 X86ManagedRegister pair =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
399 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
401 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100402 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimByte:
407 case Primitive::kPrimBoolean:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
416 X86ManagedRegister current =
417 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
418 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 }
421 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 return Location::FpuRegisterLocation(
428 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Mark Mendell5f874182015-03-04 15:42:45 -0500438void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
Mark Mendell5f874182015-03-04 15:42:45 -0500445 if (is_baseline) {
446 blocked_core_registers_[EBP] = true;
447 blocked_core_registers_[ESI] = true;
448 blocked_core_registers_[EDI] = true;
449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100450
451 UpdateBlockedPairRegisters();
452}
453
454void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
455 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
456 X86ManagedRegister current =
457 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
458 if (blocked_core_registers_[current.AsRegisterPairLow()]
459 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
460 blocked_register_pairs_[i] = true;
461 }
462 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463}
464
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
466 : HGraphVisitor(graph),
467 assembler_(codegen->GetAssembler()),
468 codegen_(codegen) {}
469
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100471 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472}
473
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100475 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000476 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000477 bool skip_overflow_check =
478 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000480
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 }
485
Mark Mendell5f874182015-03-04 15:42:45 -0500486 if (HasEmptyFrame()) {
487 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000488 }
Mark Mendell5f874182015-03-04 15:42:45 -0500489
490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
491 Register reg = kCoreCalleeSaves[i];
492 if (allocated_registers_.ContainsCoreRegister(reg)) {
493 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().AdjustCFAOffset(kX86WordSize);
495 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500496 }
497 }
498
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 int adjust = GetFrameSize() - FrameEntrySpillSize();
500 __ subl(ESP, Immediate(adjust));
501 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100502 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000503}
504
505void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100506 __ cfi().RememberState();
507 if (!HasEmptyFrame()) {
508 int adjust = GetFrameSize() - FrameEntrySpillSize();
509 __ addl(ESP, Immediate(adjust));
510 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500511
David Srbeckyc34dc932015-04-12 09:27:43 +0100512 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
513 Register reg = kCoreCalleeSaves[i];
514 if (allocated_registers_.ContainsCoreRegister(reg)) {
515 __ popl(reg);
516 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
517 __ cfi().Restore(DWARFReg(reg));
518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100521 __ ret();
522 __ cfi().RestoreState();
523 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528}
529
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
531 switch (load->GetType()) {
532 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
536 case Primitive::kPrimInt:
537 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimBoolean:
542 case Primitive::kPrimByte:
543 case Primitive::kPrimChar:
544 case Primitive::kPrimShort:
545 case Primitive::kPrimVoid:
546 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700547 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548 }
549
550 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700551 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552}
553
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100554Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
555 switch (type) {
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimByte:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot:
562 return Location::RegisterLocation(EAX);
563
564 case Primitive::kPrimLong:
565 return Location::RegisterPairLocation(EAX, EDX);
566
567 case Primitive::kPrimVoid:
568 return Location::NoLocation();
569
570 case Primitive::kPrimDouble:
571 case Primitive::kPrimFloat:
572 return Location::FpuRegisterLocation(XMM0);
573 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100574
575 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100576}
577
578Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
579 return Location::RegisterLocation(kMethodRegisterArgument);
580}
581
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100582Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100583 switch (type) {
584 case Primitive::kPrimBoolean:
585 case Primitive::kPrimByte:
586 case Primitive::kPrimChar:
587 case Primitive::kPrimShort:
588 case Primitive::kPrimInt:
589 case Primitive::kPrimNot: {
590 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000591 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100592 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100593 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100594 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000595 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100596 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100597 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100598
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000599 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100600 uint32_t index = gp_index_;
601 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000602 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100604 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
605 calling_convention.GetRegisterPairAt(index));
606 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100607 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000608 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
609 }
610 }
611
612 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100613 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000614 stack_index_++;
615 if (index < calling_convention.GetNumberOfFpuRegisters()) {
616 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
617 } else {
618 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
619 }
620 }
621
622 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100623 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000624 stack_index_ += 2;
625 if (index < calling_convention.GetNumberOfFpuRegisters()) {
626 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
627 } else {
628 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100629 }
630 }
631
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100632 case Primitive::kPrimVoid:
633 LOG(FATAL) << "Unexpected parameter type " << type;
634 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100635 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100636 return Location();
637}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100638
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639void CodeGeneratorX86::Move32(Location destination, Location source) {
640 if (source.Equals(destination)) {
641 return;
642 }
643 if (destination.IsRegister()) {
644 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000645 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000647 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 } else {
649 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 } else if (destination.IsFpuRegister()) {
653 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000654 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000656 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else {
658 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000659 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000662 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000666 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500667 } else if (source.IsConstant()) {
668 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000669 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500670 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
672 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100673 __ pushl(Address(ESP, source.GetStackIndex()));
674 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 }
676 }
677}
678
679void CodeGeneratorX86::Move64(Location destination, Location source) {
680 if (source.Equals(destination)) {
681 return;
682 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100683 if (destination.IsRegisterPair()) {
684 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000685 EmitParallelMoves(
686 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
687 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100688 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000689 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100690 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
691 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 } else if (source.IsFpuRegister()) {
693 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000695 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100696 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100697 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
698 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
700 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500702 if (source.IsFpuRegister()) {
703 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
704 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else {
707 LOG(FATAL) << "Unimplemented";
708 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100709 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000710 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100711 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000712 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100713 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100715 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000718 } else if (source.IsConstant()) {
719 HConstant* constant = source.GetConstant();
720 int64_t value;
721 if (constant->IsLongConstant()) {
722 value = constant->AsLongConstant()->GetValue();
723 } else {
724 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000725 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000726 }
727 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
728 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000730 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000731 EmitParallelMoves(
732 Location::StackSlot(source.GetStackIndex()),
733 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100734 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000735 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100736 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
737 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 }
739 }
740}
741
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100742void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100744 if (instruction->IsCurrentMethod()) {
745 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
746 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000747 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100748 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000749 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000750 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
751 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000753 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 } else if (location.IsStackSlot()) {
755 __ movl(Address(ESP, location.GetStackIndex()), imm);
756 } else {
757 DCHECK(location.IsConstant());
758 DCHECK_EQ(location.GetConstant(), const_to_move);
759 }
760 } else if (const_to_move->IsLongConstant()) {
761 int64_t value = const_to_move->AsLongConstant()->GetValue();
762 if (location.IsRegisterPair()) {
763 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
764 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
765 } else if (location.IsDoubleStackSlot()) {
766 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000767 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
768 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000769 } else {
770 DCHECK(location.IsConstant());
771 DCHECK_EQ(location.GetConstant(), instruction);
772 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000774 } else if (instruction->IsTemporary()) {
775 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000776 if (temp_location.IsStackSlot()) {
777 Move32(location, temp_location);
778 } else {
779 DCHECK(temp_location.IsDoubleStackSlot());
780 Move64(location, temp_location);
781 }
Roland Levillain476df552014-10-09 17:51:36 +0100782 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 switch (instruction->GetType()) {
785 case Primitive::kPrimBoolean:
786 case Primitive::kPrimByte:
787 case Primitive::kPrimChar:
788 case Primitive::kPrimShort:
789 case Primitive::kPrimInt:
790 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimFloat:
792 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100793 break;
794
795 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 case Primitive::kPrimDouble:
797 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 default:
801 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
802 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100804 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100805 switch (instruction->GetType()) {
806 case Primitive::kPrimBoolean:
807 case Primitive::kPrimByte:
808 case Primitive::kPrimChar:
809 case Primitive::kPrimShort:
810 case Primitive::kPrimInt:
811 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000813 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 break;
815
816 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 break;
820
821 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000824 }
825}
826
827void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000832 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100833 DCHECK(!successor->IsExitBlock());
834
835 HBasicBlock* block = got->GetBlock();
836 HInstruction* previous = got->GetPrevious();
837
838 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000839 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
841 return;
842 }
843
844 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
845 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
846 }
847 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000848 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000849 }
850}
851
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854}
855
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700857 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000858}
859
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
861 Label* true_target,
862 Label* false_target,
863 Label* always_true_target) {
864 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 if (cond->IsIntConstant()) {
866 // Constant condition, statically compared against 1.
867 int32_t cond_value = cond->AsIntConstant()->GetValue();
868 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700869 if (always_true_target != nullptr) {
870 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100871 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100872 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100873 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100874 DCHECK_EQ(cond_value, 0);
875 }
876 } else {
877 bool materialized =
878 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
879 // Moves do not affect the eflags register, so if the condition is
880 // evaluated just before the if, we don't need to evaluate it
881 // again.
882 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700883 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100884 if (materialized) {
885 if (!eflags_set) {
886 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500889 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100890 } else {
891 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
892 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700893 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100894 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700895 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100896 }
897 } else {
898 Location lhs = cond->GetLocations()->InAt(0);
899 Location rhs = cond->GetLocations()->InAt(1);
900 // LHS is guaranteed to be in a register (see
901 // LocationsBuilderX86::VisitCondition).
902 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000903 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100904 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100905 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500906 if (constant == 0) {
907 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
908 } else {
909 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
910 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100911 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000912 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100913 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700914 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700915 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700917 if (false_target != nullptr) {
918 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000919 }
920}
921
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700922void LocationsBuilderX86::VisitIf(HIf* if_instr) {
923 LocationSummary* locations =
924 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
925 HInstruction* cond = if_instr->InputAt(0);
926 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
927 locations->SetInAt(0, Location::Any());
928 }
929}
930
931void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
932 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
933 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
934 Label* always_true_target = true_target;
935 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
936 if_instr->IfTrueSuccessor())) {
937 always_true_target = nullptr;
938 }
939 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
940 if_instr->IfFalseSuccessor())) {
941 false_target = nullptr;
942 }
943 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
944}
945
946void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
947 LocationSummary* locations = new (GetGraph()->GetArena())
948 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
949 HInstruction* cond = deoptimize->InputAt(0);
950 DCHECK(cond->IsCondition());
951 if (cond->AsCondition()->NeedsMaterialization()) {
952 locations->SetInAt(0, Location::Any());
953 }
954}
955
956void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
957 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
958 DeoptimizationSlowPathX86(deoptimize);
959 codegen_->AddSlowPath(slow_path);
960 Label* slow_path_entry = slow_path->GetEntryLabel();
961 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
962}
963
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000965 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000966}
967
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000968void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
969 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000970}
971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100973 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000974}
975
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000976void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100977 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700978 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100982 LocationSummary* locations =
983 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 switch (store->InputAt(1)->GetType()) {
985 case Primitive::kPrimBoolean:
986 case Primitive::kPrimByte:
987 case Primitive::kPrimChar:
988 case Primitive::kPrimShort:
989 case Primitive::kPrimInt:
990 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
993 break;
994
995 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
998 break;
999
1000 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001003}
1004
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001007}
1008
Roland Levillain0d37cd02015-05-27 16:39:19 +01001009void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001011 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001012 locations->SetInAt(0, Location::RequiresRegister());
1013 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001014 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001015 // We need a byte register.
1016 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001017 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018}
1019
Roland Levillain0d37cd02015-05-27 16:39:19 +01001020void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1021 if (cond->NeedsMaterialization()) {
1022 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001024 // Clear register: setcc only sets the low byte.
1025 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001026 Location lhs = locations->InAt(0);
1027 Location rhs = locations->InAt(1);
1028 if (rhs.IsRegister()) {
1029 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1030 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001031 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001032 if (constant == 0) {
1033 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1034 } else {
1035 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1036 }
Dave Allison20dfc792014-06-16 20:44:29 -07001037 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001038 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001039 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001040 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 }
Dave Allison20dfc792014-06-16 20:44:29 -07001042}
1043
1044void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1045 VisitCondition(comp);
1046}
1047
1048void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1049 VisitCondition(comp);
1050}
1051
1052void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1053 VisitCondition(comp);
1054}
1055
1056void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1057 VisitCondition(comp);
1058}
1059
1060void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1061 VisitCondition(comp);
1062}
1063
1064void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1065 VisitCondition(comp);
1066}
1067
1068void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1069 VisitCondition(comp);
1070}
1071
1072void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1073 VisitCondition(comp);
1074}
1075
1076void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1077 VisitCondition(comp);
1078}
1079
1080void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1081 VisitCondition(comp);
1082}
1083
1084void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1085 VisitCondition(comp);
1086}
1087
1088void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1089 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001090}
1091
1092void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001093 LocationSummary* locations =
1094 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001095 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001096}
1097
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001098void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001100 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101}
1102
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001103void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1104 LocationSummary* locations =
1105 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1106 locations->SetOut(Location::ConstantLocation(constant));
1107}
1108
1109void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1110 // Will be generated at use site.
1111 UNUSED(constant);
1112}
1113
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001117 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118}
1119
1120void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1121 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123}
1124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001125void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1126 LocationSummary* locations =
1127 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1128 locations->SetOut(Location::ConstantLocation(constant));
1129}
1130
1131void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1132 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001133 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001134}
1135
1136void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1137 LocationSummary* locations =
1138 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1139 locations->SetOut(Location::ConstantLocation(constant));
1140}
1141
1142void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1143 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001144 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001145}
1146
Calin Juravle27df7582015-04-17 19:12:31 +01001147void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1148 memory_barrier->SetLocations(nullptr);
1149}
1150
1151void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1152 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1153}
1154
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001156 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001157}
1158
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001159void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001160 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001162}
1163
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001164void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001165 LocationSummary* locations =
1166 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 switch (ret->InputAt(0)->GetType()) {
1168 case Primitive::kPrimBoolean:
1169 case Primitive::kPrimByte:
1170 case Primitive::kPrimChar:
1171 case Primitive::kPrimShort:
1172 case Primitive::kPrimInt:
1173 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 break;
1176
1177 case Primitive::kPrimLong:
1178 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 case Primitive::kPrimFloat:
1183 case Primitive::kPrimDouble:
1184 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 break;
1187
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191}
1192
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 if (kIsDebugBuild) {
1195 switch (ret->InputAt(0)->GetType()) {
1196 case Primitive::kPrimBoolean:
1197 case Primitive::kPrimByte:
1198 case Primitive::kPrimChar:
1199 case Primitive::kPrimShort:
1200 case Primitive::kPrimInt:
1201 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 break;
1204
1205 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001206 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1207 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 break;
1209
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 case Primitive::kPrimFloat:
1211 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001213 break;
1214
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001216 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001217 }
1218 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001219 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001223 // When we do not run baseline, explicit clinit checks triggered by static
1224 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1225 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001226
Mark Mendellfb8d2792015-03-31 22:16:59 -04001227 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001228 if (intrinsic.TryDispatch(invoke)) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001229 LocationSummary* locations = invoke->GetLocations();
1230 if (locations->CanCall()) {
1231 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::RequiresRegister());
1232 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001233 return;
1234 }
1235
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001236 HandleInvoke(invoke);
1237}
1238
Mark Mendell09ed1a32015-03-25 08:30:06 -04001239static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1240 if (invoke->GetLocations()->Intrinsified()) {
1241 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1242 intrinsic.Dispatch(invoke);
1243 return true;
1244 }
1245 return false;
1246}
1247
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001248void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001249 // When we do not run baseline, explicit clinit checks triggered by static
1250 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1251 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001252
Mark Mendell09ed1a32015-03-25 08:30:06 -04001253 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1254 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001255 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001256
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001257 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001258 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001259 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001260 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001261}
1262
1263void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1264 HandleInvoke(invoke);
1265}
1266
1267void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001268 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001269 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270}
1271
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001272void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001274 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1275 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001276 LocationSummary* locations = invoke->GetLocations();
1277 Location receiver = locations->InAt(0);
1278 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001279 DCHECK(receiver.IsRegister());
1280 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001281 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001282 // temp = temp->GetMethodAt(method_offset);
1283 __ movl(temp, Address(temp, method_offset));
1284 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001285 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001286 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001287
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001288 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001289 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001290}
1291
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001292void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1293 HandleInvoke(invoke);
1294 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001295 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001296}
1297
1298void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1299 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001301 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1302 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001303 LocationSummary* locations = invoke->GetLocations();
1304 Location receiver = locations->InAt(0);
1305 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1306
1307 // Set the hidden argument.
1308 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310
1311 // temp = object->GetClass();
1312 if (receiver.IsStackSlot()) {
1313 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1314 __ movl(temp, Address(temp, class_offset));
1315 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001317 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001318 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319 // temp = temp->GetImtEntryAt(method_offset);
1320 __ movl(temp, Address(temp, method_offset));
1321 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001322 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001323 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001324
1325 DCHECK(!codegen_->IsLeafMethod());
1326 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1327}
1328
Roland Levillain88cb1752014-10-20 16:36:47 +01001329void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1330 LocationSummary* locations =
1331 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1332 switch (neg->GetResultType()) {
1333 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001334 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001335 locations->SetInAt(0, Location::RequiresRegister());
1336 locations->SetOut(Location::SameAsFirstInput());
1337 break;
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001340 locations->SetInAt(0, Location::RequiresFpuRegister());
1341 locations->SetOut(Location::SameAsFirstInput());
1342 locations->AddTemp(Location::RequiresRegister());
1343 locations->AddTemp(Location::RequiresFpuRegister());
1344 break;
1345
Roland Levillain88cb1752014-10-20 16:36:47 +01001346 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001347 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001348 locations->SetOut(Location::SameAsFirstInput());
1349 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1354 }
1355}
1356
1357void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1358 LocationSummary* locations = neg->GetLocations();
1359 Location out = locations->Out();
1360 Location in = locations->InAt(0);
1361 switch (neg->GetResultType()) {
1362 case Primitive::kPrimInt:
1363 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001366 break;
1367
1368 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001369 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001370 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001371 __ negl(out.AsRegisterPairLow<Register>());
1372 // Negation is similar to subtraction from zero. The least
1373 // significant byte triggers a borrow when it is different from
1374 // zero; to take it into account, add 1 to the most significant
1375 // byte if the carry flag (CF) is set to 1 after the first NEGL
1376 // operation.
1377 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1378 __ negl(out.AsRegisterPairHigh<Register>());
1379 break;
1380
Roland Levillain5368c212014-11-27 15:03:41 +00001381 case Primitive::kPrimFloat: {
1382 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 Register constant = locations->GetTemp(0).AsRegister<Register>();
1384 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001385 // Implement float negation with an exclusive or with value
1386 // 0x80000000 (mask for bit 31, representing the sign of a
1387 // single-precision floating-point number).
1388 __ movl(constant, Immediate(INT32_C(0x80000000)));
1389 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001390 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001391 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001392 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001393
Roland Levillain5368c212014-11-27 15:03:41 +00001394 case Primitive::kPrimDouble: {
1395 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001396 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001397 // Implement double negation with an exclusive or with value
1398 // 0x8000000000000000 (mask for bit 63, representing the sign of
1399 // a double-precision floating-point number).
1400 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001401 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001402 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001403 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001404
1405 default:
1406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1407 }
1408}
1409
Roland Levillaindff1f282014-11-05 14:15:05 +00001410void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 Primitive::Type result_type = conversion->GetResultType();
1412 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001413 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001414
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001415 // The float-to-long and double-to-long type conversions rely on a
1416 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001417 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001418 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1419 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001420 ? LocationSummary::kCall
1421 : LocationSummary::kNoCall;
1422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1424
David Brazdilb2bd1c52015-03-25 11:17:37 +00001425 // The Java language does not allow treating boolean as an integral type but
1426 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001427
Roland Levillaindff1f282014-11-05 14:15:05 +00001428 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001429 case Primitive::kPrimByte:
1430 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001431 case Primitive::kPrimBoolean:
1432 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001433 case Primitive::kPrimShort:
1434 case Primitive::kPrimInt:
1435 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001436 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001437 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1438 // Make the output overlap to please the register allocator. This greatly simplifies
1439 // the validation of the linear scan implementation
1440 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001441 break;
1442
1443 default:
1444 LOG(FATAL) << "Unexpected type conversion from " << input_type
1445 << " to " << result_type;
1446 }
1447 break;
1448
Roland Levillain01a8d712014-11-14 16:27:39 +00001449 case Primitive::kPrimShort:
1450 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001451 case Primitive::kPrimBoolean:
1452 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001453 case Primitive::kPrimByte:
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimChar:
1456 // Processing a Dex `int-to-short' instruction.
1457 locations->SetInAt(0, Location::Any());
1458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1459 break;
1460
1461 default:
1462 LOG(FATAL) << "Unexpected type conversion from " << input_type
1463 << " to " << result_type;
1464 }
1465 break;
1466
Roland Levillain946e1432014-11-11 17:35:19 +00001467 case Primitive::kPrimInt:
1468 switch (input_type) {
1469 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001470 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001471 locations->SetInAt(0, Location::Any());
1472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1473 break;
1474
1475 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001476 // Processing a Dex `float-to-int' instruction.
1477 locations->SetInAt(0, Location::RequiresFpuRegister());
1478 locations->SetOut(Location::RequiresRegister());
1479 locations->AddTemp(Location::RequiresFpuRegister());
1480 break;
1481
Roland Levillain946e1432014-11-11 17:35:19 +00001482 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001483 // Processing a Dex `double-to-int' instruction.
1484 locations->SetInAt(0, Location::RequiresFpuRegister());
1485 locations->SetOut(Location::RequiresRegister());
1486 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001487 break;
1488
1489 default:
1490 LOG(FATAL) << "Unexpected type conversion from " << input_type
1491 << " to " << result_type;
1492 }
1493 break;
1494
Roland Levillaindff1f282014-11-05 14:15:05 +00001495 case Primitive::kPrimLong:
1496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001497 case Primitive::kPrimBoolean:
1498 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001499 case Primitive::kPrimByte:
1500 case Primitive::kPrimShort:
1501 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001502 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001503 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 locations->SetInAt(0, Location::RegisterLocation(EAX));
1505 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1506 break;
1507
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001508 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001509 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001510 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001511 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001512 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1513 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1514
Vladimir Marko949c91f2015-01-27 10:48:44 +00001515 // The runtime helper puts the result in EAX, EDX.
1516 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001517 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001519
1520 default:
1521 LOG(FATAL) << "Unexpected type conversion from " << input_type
1522 << " to " << result_type;
1523 }
1524 break;
1525
Roland Levillain981e4542014-11-14 11:47:14 +00001526 case Primitive::kPrimChar:
1527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001528 case Primitive::kPrimBoolean:
1529 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001530 case Primitive::kPrimByte:
1531 case Primitive::kPrimShort:
1532 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001533 // Processing a Dex `int-to-char' instruction.
1534 locations->SetInAt(0, Location::Any());
1535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001545 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001546 case Primitive::kPrimBoolean:
1547 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001548 case Primitive::kPrimByte:
1549 case Primitive::kPrimShort:
1550 case Primitive::kPrimInt:
1551 case Primitive::kPrimChar:
1552 // Processing a Dex `int-to-float' instruction.
1553 locations->SetInAt(0, Location::RequiresRegister());
1554 locations->SetOut(Location::RequiresFpuRegister());
1555 break;
1556
1557 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001558 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001559 locations->SetInAt(0, Location::Any());
1560 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001561 break;
1562
Roland Levillaincff13742014-11-17 14:32:17 +00001563 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001564 // Processing a Dex `double-to-float' instruction.
1565 locations->SetInAt(0, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001567 break;
1568
1569 default:
1570 LOG(FATAL) << "Unexpected type conversion from " << input_type
1571 << " to " << result_type;
1572 };
1573 break;
1574
Roland Levillaindff1f282014-11-05 14:15:05 +00001575 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001576 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001577 case Primitive::kPrimBoolean:
1578 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001579 case Primitive::kPrimByte:
1580 case Primitive::kPrimShort:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 // Processing a Dex `int-to-double' instruction.
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetOut(Location::RequiresFpuRegister());
1586 break;
1587
1588 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001589 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001590 locations->SetInAt(0, Location::Any());
1591 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001592 break;
1593
Roland Levillaincff13742014-11-17 14:32:17 +00001594 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001595 // Processing a Dex `float-to-double' instruction.
1596 locations->SetInAt(0, Location::RequiresFpuRegister());
1597 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001598 break;
1599
1600 default:
1601 LOG(FATAL) << "Unexpected type conversion from " << input_type
1602 << " to " << result_type;
1603 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001604 break;
1605
1606 default:
1607 LOG(FATAL) << "Unexpected type conversion from " << input_type
1608 << " to " << result_type;
1609 }
1610}
1611
1612void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1613 LocationSummary* locations = conversion->GetLocations();
1614 Location out = locations->Out();
1615 Location in = locations->InAt(0);
1616 Primitive::Type result_type = conversion->GetResultType();
1617 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001618 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001619 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001620 case Primitive::kPrimByte:
1621 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001622 case Primitive::kPrimBoolean:
1623 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
1626 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001627 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001628 if (in.IsRegister()) {
1629 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001630 } else {
1631 DCHECK(in.GetConstant()->IsIntConstant());
1632 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1634 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001635 break;
1636
1637 default:
1638 LOG(FATAL) << "Unexpected type conversion from " << input_type
1639 << " to " << result_type;
1640 }
1641 break;
1642
Roland Levillain01a8d712014-11-14 16:27:39 +00001643 case Primitive::kPrimShort:
1644 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001645 case Primitive::kPrimBoolean:
1646 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001647 case Primitive::kPrimByte:
1648 case Primitive::kPrimInt:
1649 case Primitive::kPrimChar:
1650 // Processing a Dex `int-to-short' instruction.
1651 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001655 } else {
1656 DCHECK(in.GetConstant()->IsIntConstant());
1657 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001658 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 }
1660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected type conversion from " << input_type
1664 << " to " << result_type;
1665 }
1666 break;
1667
Roland Levillain946e1432014-11-11 17:35:19 +00001668 case Primitive::kPrimInt:
1669 switch (input_type) {
1670 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001671 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001672 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001674 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001676 } else {
1677 DCHECK(in.IsConstant());
1678 DCHECK(in.GetConstant()->IsLongConstant());
1679 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001681 }
1682 break;
1683
Roland Levillain3f8f9362014-12-02 17:45:01 +00001684 case Primitive::kPrimFloat: {
1685 // Processing a Dex `float-to-int' instruction.
1686 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1687 Register output = out.AsRegister<Register>();
1688 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1689 Label done, nan;
1690
1691 __ movl(output, Immediate(kPrimIntMax));
1692 // temp = int-to-float(output)
1693 __ cvtsi2ss(temp, output);
1694 // if input >= temp goto done
1695 __ comiss(input, temp);
1696 __ j(kAboveEqual, &done);
1697 // if input == NaN goto nan
1698 __ j(kUnordered, &nan);
1699 // output = float-to-int-truncate(input)
1700 __ cvttss2si(output, input);
1701 __ jmp(&done);
1702 __ Bind(&nan);
1703 // output = 0
1704 __ xorl(output, output);
1705 __ Bind(&done);
1706 break;
1707 }
1708
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001709 case Primitive::kPrimDouble: {
1710 // Processing a Dex `double-to-int' instruction.
1711 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1712 Register output = out.AsRegister<Register>();
1713 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1714 Label done, nan;
1715
1716 __ movl(output, Immediate(kPrimIntMax));
1717 // temp = int-to-double(output)
1718 __ cvtsi2sd(temp, output);
1719 // if input >= temp goto done
1720 __ comisd(input, temp);
1721 __ j(kAboveEqual, &done);
1722 // if input == NaN goto nan
1723 __ j(kUnordered, &nan);
1724 // output = double-to-int-truncate(input)
1725 __ cvttsd2si(output, input);
1726 __ jmp(&done);
1727 __ Bind(&nan);
1728 // output = 0
1729 __ xorl(output, output);
1730 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001731 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001732 }
Roland Levillain946e1432014-11-11 17:35:19 +00001733
1734 default:
1735 LOG(FATAL) << "Unexpected type conversion from " << input_type
1736 << " to " << result_type;
1737 }
1738 break;
1739
Roland Levillaindff1f282014-11-05 14:15:05 +00001740 case Primitive::kPrimLong:
1741 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001742 case Primitive::kPrimBoolean:
1743 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 case Primitive::kPrimByte:
1745 case Primitive::kPrimShort:
1746 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001747 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001748 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001749 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1750 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001751 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001752 __ cdq();
1753 break;
1754
1755 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001756 // Processing a Dex `float-to-long' instruction.
1757 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001758 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1759 break;
1760
Roland Levillaindff1f282014-11-05 14:15:05 +00001761 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001762 // Processing a Dex `double-to-long' instruction.
1763 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1764 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 break;
1766
1767 default:
1768 LOG(FATAL) << "Unexpected type conversion from " << input_type
1769 << " to " << result_type;
1770 }
1771 break;
1772
Roland Levillain981e4542014-11-14 11:47:14 +00001773 case Primitive::kPrimChar:
1774 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001775 case Primitive::kPrimBoolean:
1776 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001777 case Primitive::kPrimByte:
1778 case Primitive::kPrimShort:
1779 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001780 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1781 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001783 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001784 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001785 } else {
1786 DCHECK(in.GetConstant()->IsIntConstant());
1787 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001789 }
1790 break;
1791
1792 default:
1793 LOG(FATAL) << "Unexpected type conversion from " << input_type
1794 << " to " << result_type;
1795 }
1796 break;
1797
Roland Levillaindff1f282014-11-05 14:15:05 +00001798 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001799 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001800 case Primitive::kPrimBoolean:
1801 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimByte:
1803 case Primitive::kPrimShort:
1804 case Primitive::kPrimInt:
1805 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001806 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001807 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001808 break;
1809
Roland Levillain6d0e4832014-11-27 18:31:21 +00001810 case Primitive::kPrimLong: {
1811 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001812 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001813
Roland Levillain232ade02015-04-20 15:14:36 +01001814 // Create stack space for the call to
1815 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1816 // TODO: enhance register allocator to ask for stack temporaries.
1817 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1818 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1819 __ subl(ESP, Immediate(adjustment));
1820 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001821
Roland Levillain232ade02015-04-20 15:14:36 +01001822 // Load the value to the FP stack, using temporaries if needed.
1823 PushOntoFPStack(in, 0, adjustment, false, true);
1824
1825 if (out.IsStackSlot()) {
1826 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1827 } else {
1828 __ fstps(Address(ESP, 0));
1829 Location stack_temp = Location::StackSlot(0);
1830 codegen_->Move32(out, stack_temp);
1831 }
1832
1833 // Remove the temporary stack space we allocated.
1834 if (adjustment != 0) {
1835 __ addl(ESP, Immediate(adjustment));
1836 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001837 break;
1838 }
1839
Roland Levillaincff13742014-11-17 14:32:17 +00001840 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001841 // Processing a Dex `double-to-float' instruction.
1842 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001843 break;
1844
1845 default:
1846 LOG(FATAL) << "Unexpected type conversion from " << input_type
1847 << " to " << result_type;
1848 };
1849 break;
1850
Roland Levillaindff1f282014-11-05 14:15:05 +00001851 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001852 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001853 case Primitive::kPrimBoolean:
1854 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimByte:
1856 case Primitive::kPrimShort:
1857 case Primitive::kPrimInt:
1858 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001859 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001861 break;
1862
Roland Levillain647b9ed2014-11-27 12:06:00 +00001863 case Primitive::kPrimLong: {
1864 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001865 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001866
Roland Levillain232ade02015-04-20 15:14:36 +01001867 // Create stack space for the call to
1868 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1869 // TODO: enhance register allocator to ask for stack temporaries.
1870 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1871 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1872 __ subl(ESP, Immediate(adjustment));
1873 }
1874
1875 // Load the value to the FP stack, using temporaries if needed.
1876 PushOntoFPStack(in, 0, adjustment, false, true);
1877
1878 if (out.IsDoubleStackSlot()) {
1879 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1880 } else {
1881 __ fstpl(Address(ESP, 0));
1882 Location stack_temp = Location::DoubleStackSlot(0);
1883 codegen_->Move64(out, stack_temp);
1884 }
1885
1886 // Remove the temporary stack space we allocated.
1887 if (adjustment != 0) {
1888 __ addl(ESP, Immediate(adjustment));
1889 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001890 break;
1891 }
1892
Roland Levillaincff13742014-11-17 14:32:17 +00001893 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001894 // Processing a Dex `float-to-double' instruction.
1895 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001896 break;
1897
1898 default:
1899 LOG(FATAL) << "Unexpected type conversion from " << input_type
1900 << " to " << result_type;
1901 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 }
1908}
1909
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001910void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001911 LocationSummary* locations =
1912 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001913 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001914 case Primitive::kPrimInt: {
1915 locations->SetInAt(0, Location::RequiresRegister());
1916 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1918 break;
1919 }
1920
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetInAt(1, Location::Any());
1924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926 }
1927
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001928 case Primitive::kPrimFloat:
1929 case Primitive::kPrimDouble: {
1930 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001931 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001936 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1938 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001939 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001940}
1941
1942void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1943 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 Location first = locations->InAt(0);
1945 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001946 Location out = locations->Out();
1947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001948 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001951 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1952 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001953 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1954 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001955 } else {
1956 __ leal(out.AsRegister<Register>(), Address(
1957 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1958 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001960 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1962 __ addl(out.AsRegister<Register>(), Immediate(value));
1963 } else {
1964 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1965 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001966 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001967 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001969 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001970 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971 }
1972
1973 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001974 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1976 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001977 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001978 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1979 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001980 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001981 } else {
1982 DCHECK(second.IsConstant()) << second;
1983 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1984 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1985 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001987 break;
1988 }
1989
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 case Primitive::kPrimFloat: {
1991 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001993 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001994 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001995 }
1996
1997 case Primitive::kPrimDouble: {
1998 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 }
2001 break;
2002 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002004 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002006 }
2007}
2008
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002009void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002012 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002013 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002014 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002015 locations->SetInAt(0, Location::RequiresRegister());
2016 locations->SetInAt(1, Location::Any());
2017 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002018 break;
2019 }
Calin Juravle11351682014-10-23 15:38:15 +01002020 case Primitive::kPrimFloat:
2021 case Primitive::kPrimDouble: {
2022 locations->SetInAt(0, Location::RequiresFpuRegister());
2023 locations->SetInAt(1, Location::RequiresFpuRegister());
2024 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002025 break;
Calin Juravle11351682014-10-23 15:38:15 +01002026 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002027
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002028 default:
Calin Juravle11351682014-10-23 15:38:15 +01002029 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002030 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002031}
2032
2033void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2034 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 Location first = locations->InAt(0);
2036 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002037 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002039 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002042 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002043 __ subl(first.AsRegister<Register>(),
2044 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002045 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002047 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002048 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 }
2050
2051 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002052 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002053 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2054 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002055 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002056 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002057 __ sbbl(first.AsRegisterPairHigh<Register>(),
2058 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002059 } else {
2060 DCHECK(second.IsConstant()) << second;
2061 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2062 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2063 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002064 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 break;
2066 }
2067
Calin Juravle11351682014-10-23 15:38:15 +01002068 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070 break;
Calin Juravle11351682014-10-23 15:38:15 +01002071 }
2072
2073 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002075 break;
2076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002077
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002078 default:
Calin Juravle11351682014-10-23 15:38:15 +01002079 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002080 }
2081}
2082
Calin Juravle34bacdf2014-10-07 20:23:36 +01002083void LocationsBuilderX86::VisitMul(HMul* mul) {
2084 LocationSummary* locations =
2085 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2086 switch (mul->GetResultType()) {
2087 case Primitive::kPrimInt:
2088 locations->SetInAt(0, Location::RequiresRegister());
2089 locations->SetInAt(1, Location::Any());
2090 locations->SetOut(Location::SameAsFirstInput());
2091 break;
2092 case Primitive::kPrimLong: {
2093 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002094 locations->SetInAt(1, Location::Any());
2095 locations->SetOut(Location::SameAsFirstInput());
2096 // Needed for imul on 32bits with 64bits output.
2097 locations->AddTemp(Location::RegisterLocation(EAX));
2098 locations->AddTemp(Location::RegisterLocation(EDX));
2099 break;
2100 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble: {
2103 locations->SetInAt(0, Location::RequiresFpuRegister());
2104 locations->SetInAt(1, Location::RequiresFpuRegister());
2105 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002106 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002107 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002108
2109 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002110 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002111 }
2112}
2113
2114void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2115 LocationSummary* locations = mul->GetLocations();
2116 Location first = locations->InAt(0);
2117 Location second = locations->InAt(1);
2118 DCHECK(first.Equals(locations->Out()));
2119
2120 switch (mul->GetResultType()) {
2121 case Primitive::kPrimInt: {
2122 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002124 } else if (second.IsConstant()) {
2125 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002126 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002127 } else {
2128 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002130 }
2131 break;
2132 }
2133
2134 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002135 Register in1_hi = first.AsRegisterPairHigh<Register>();
2136 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 Register eax = locations->GetTemp(0).AsRegister<Register>();
2138 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139
2140 DCHECK_EQ(EAX, eax);
2141 DCHECK_EQ(EDX, edx);
2142
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002143 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002144 // output: in1
2145 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2146 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2147 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002148 if (second.IsConstant()) {
2149 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002150
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002151 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2152 int32_t low_value = Low32Bits(value);
2153 int32_t high_value = High32Bits(value);
2154 Immediate low(low_value);
2155 Immediate high(high_value);
2156
2157 __ movl(eax, high);
2158 // eax <- in1.lo * in2.hi
2159 __ imull(eax, in1_lo);
2160 // in1.hi <- in1.hi * in2.lo
2161 __ imull(in1_hi, low);
2162 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2163 __ addl(in1_hi, eax);
2164 // move in2_lo to eax to prepare for double precision
2165 __ movl(eax, low);
2166 // edx:eax <- in1.lo * in2.lo
2167 __ mull(in1_lo);
2168 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2169 __ addl(in1_hi, edx);
2170 // in1.lo <- (in1.lo * in2.lo)[31:0];
2171 __ movl(in1_lo, eax);
2172 } else if (second.IsRegisterPair()) {
2173 Register in2_hi = second.AsRegisterPairHigh<Register>();
2174 Register in2_lo = second.AsRegisterPairLow<Register>();
2175
2176 __ movl(eax, in2_hi);
2177 // eax <- in1.lo * in2.hi
2178 __ imull(eax, in1_lo);
2179 // in1.hi <- in1.hi * in2.lo
2180 __ imull(in1_hi, in2_lo);
2181 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2182 __ addl(in1_hi, eax);
2183 // move in1_lo to eax to prepare for double precision
2184 __ movl(eax, in1_lo);
2185 // edx:eax <- in1.lo * in2.lo
2186 __ mull(in2_lo);
2187 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2188 __ addl(in1_hi, edx);
2189 // in1.lo <- (in1.lo * in2.lo)[31:0];
2190 __ movl(in1_lo, eax);
2191 } else {
2192 DCHECK(second.IsDoubleStackSlot()) << second;
2193 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2194 Address in2_lo(ESP, second.GetStackIndex());
2195
2196 __ movl(eax, in2_hi);
2197 // eax <- in1.lo * in2.hi
2198 __ imull(eax, in1_lo);
2199 // in1.hi <- in1.hi * in2.lo
2200 __ imull(in1_hi, in2_lo);
2201 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2202 __ addl(in1_hi, eax);
2203 // move in1_lo to eax to prepare for double precision
2204 __ movl(eax, in1_lo);
2205 // edx:eax <- in1.lo * in2.lo
2206 __ mull(in2_lo);
2207 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2208 __ addl(in1_hi, edx);
2209 // in1.lo <- (in1.lo * in2.lo)[31:0];
2210 __ movl(in1_lo, eax);
2211 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002212
2213 break;
2214 }
2215
Calin Juravleb5bfa962014-10-21 18:02:24 +01002216 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002218 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002219 }
2220
2221 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002223 break;
2224 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002225
2226 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002227 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002228 }
2229}
2230
Roland Levillain232ade02015-04-20 15:14:36 +01002231void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2232 uint32_t temp_offset,
2233 uint32_t stack_adjustment,
2234 bool is_fp,
2235 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002236 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002237 DCHECK(!is_wide);
2238 if (is_fp) {
2239 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2240 } else {
2241 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2242 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002243 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002244 DCHECK(is_wide);
2245 if (is_fp) {
2246 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2247 } else {
2248 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2249 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002250 } else {
2251 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002252 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002253 Location stack_temp = Location::StackSlot(temp_offset);
2254 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002255 if (is_fp) {
2256 __ flds(Address(ESP, temp_offset));
2257 } else {
2258 __ filds(Address(ESP, temp_offset));
2259 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002260 } else {
2261 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2262 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002263 if (is_fp) {
2264 __ fldl(Address(ESP, temp_offset));
2265 } else {
2266 __ fildl(Address(ESP, temp_offset));
2267 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002268 }
2269 }
2270}
2271
2272void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2273 Primitive::Type type = rem->GetResultType();
2274 bool is_float = type == Primitive::kPrimFloat;
2275 size_t elem_size = Primitive::ComponentSize(type);
2276 LocationSummary* locations = rem->GetLocations();
2277 Location first = locations->InAt(0);
2278 Location second = locations->InAt(1);
2279 Location out = locations->Out();
2280
2281 // Create stack space for 2 elements.
2282 // TODO: enhance register allocator to ask for stack temporaries.
2283 __ subl(ESP, Immediate(2 * elem_size));
2284
2285 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002286 const bool is_wide = !is_float;
2287 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2288 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002289
2290 // Loop doing FPREM until we stabilize.
2291 Label retry;
2292 __ Bind(&retry);
2293 __ fprem();
2294
2295 // Move FP status to AX.
2296 __ fstsw();
2297
2298 // And see if the argument reduction is complete. This is signaled by the
2299 // C2 FPU flag bit set to 0.
2300 __ andl(EAX, Immediate(kC2ConditionMask));
2301 __ j(kNotEqual, &retry);
2302
2303 // We have settled on the final value. Retrieve it into an XMM register.
2304 // Store FP top of stack to real stack.
2305 if (is_float) {
2306 __ fsts(Address(ESP, 0));
2307 } else {
2308 __ fstl(Address(ESP, 0));
2309 }
2310
2311 // Pop the 2 items from the FP stack.
2312 __ fucompp();
2313
2314 // Load the value from the stack into an XMM register.
2315 DCHECK(out.IsFpuRegister()) << out;
2316 if (is_float) {
2317 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2318 } else {
2319 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2320 }
2321
2322 // And remove the temporary stack space we allocated.
2323 __ addl(ESP, Immediate(2 * elem_size));
2324}
2325
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002326
2327void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2328 DCHECK(instruction->IsDiv() || instruction->IsRem());
2329
2330 LocationSummary* locations = instruction->GetLocations();
2331 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002332 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002333
2334 Register out_register = locations->Out().AsRegister<Register>();
2335 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002336 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002337
2338 DCHECK(imm == 1 || imm == -1);
2339
2340 if (instruction->IsRem()) {
2341 __ xorl(out_register, out_register);
2342 } else {
2343 __ movl(out_register, input_register);
2344 if (imm == -1) {
2345 __ negl(out_register);
2346 }
2347 }
2348}
2349
2350
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002351void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002352 LocationSummary* locations = instruction->GetLocations();
2353
2354 Register out_register = locations->Out().AsRegister<Register>();
2355 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002356 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002357
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002358 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002359 Register num = locations->GetTemp(0).AsRegister<Register>();
2360
2361 __ leal(num, Address(input_register, std::abs(imm) - 1));
2362 __ testl(input_register, input_register);
2363 __ cmovl(kGreaterEqual, num, input_register);
2364 int shift = CTZ(imm);
2365 __ sarl(num, Immediate(shift));
2366
2367 if (imm < 0) {
2368 __ negl(num);
2369 }
2370
2371 __ movl(out_register, num);
2372}
2373
2374void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2375 DCHECK(instruction->IsDiv() || instruction->IsRem());
2376
2377 LocationSummary* locations = instruction->GetLocations();
2378 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2379
2380 Register eax = locations->InAt(0).AsRegister<Register>();
2381 Register out = locations->Out().AsRegister<Register>();
2382 Register num;
2383 Register edx;
2384
2385 if (instruction->IsDiv()) {
2386 edx = locations->GetTemp(0).AsRegister<Register>();
2387 num = locations->GetTemp(1).AsRegister<Register>();
2388 } else {
2389 edx = locations->Out().AsRegister<Register>();
2390 num = locations->GetTemp(0).AsRegister<Register>();
2391 }
2392
2393 DCHECK_EQ(EAX, eax);
2394 DCHECK_EQ(EDX, edx);
2395 if (instruction->IsDiv()) {
2396 DCHECK_EQ(EAX, out);
2397 } else {
2398 DCHECK_EQ(EDX, out);
2399 }
2400
2401 int64_t magic;
2402 int shift;
2403 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2404
2405 Label ndiv;
2406 Label end;
2407 // If numerator is 0, the result is 0, no computation needed.
2408 __ testl(eax, eax);
2409 __ j(kNotEqual, &ndiv);
2410
2411 __ xorl(out, out);
2412 __ jmp(&end);
2413
2414 __ Bind(&ndiv);
2415
2416 // Save the numerator.
2417 __ movl(num, eax);
2418
2419 // EAX = magic
2420 __ movl(eax, Immediate(magic));
2421
2422 // EDX:EAX = magic * numerator
2423 __ imull(num);
2424
2425 if (imm > 0 && magic < 0) {
2426 // EDX += num
2427 __ addl(edx, num);
2428 } else if (imm < 0 && magic > 0) {
2429 __ subl(edx, num);
2430 }
2431
2432 // Shift if needed.
2433 if (shift != 0) {
2434 __ sarl(edx, Immediate(shift));
2435 }
2436
2437 // EDX += 1 if EDX < 0
2438 __ movl(eax, edx);
2439 __ shrl(edx, Immediate(31));
2440 __ addl(edx, eax);
2441
2442 if (instruction->IsRem()) {
2443 __ movl(eax, num);
2444 __ imull(edx, Immediate(imm));
2445 __ subl(eax, edx);
2446 __ movl(edx, eax);
2447 } else {
2448 __ movl(eax, edx);
2449 }
2450 __ Bind(&end);
2451}
2452
Calin Juravlebacfec32014-11-14 15:54:36 +00002453void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2454 DCHECK(instruction->IsDiv() || instruction->IsRem());
2455
2456 LocationSummary* locations = instruction->GetLocations();
2457 Location out = locations->Out();
2458 Location first = locations->InAt(0);
2459 Location second = locations->InAt(1);
2460 bool is_div = instruction->IsDiv();
2461
2462 switch (instruction->GetResultType()) {
2463 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 DCHECK_EQ(EAX, first.AsRegister<Register>());
2465 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002466
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002467 if (instruction->InputAt(1)->IsIntConstant()) {
2468 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002469
2470 if (imm == 0) {
2471 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2472 } else if (imm == 1 || imm == -1) {
2473 DivRemOneOrMinusOne(instruction);
2474 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002475 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002476 } else {
2477 DCHECK(imm <= -2 || imm >= 2);
2478 GenerateDivRemWithAnyConstant(instruction);
2479 }
2480 } else {
2481 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002482 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002483 is_div);
2484 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002485
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002486 Register second_reg = second.AsRegister<Register>();
2487 // 0x80000000/-1 triggers an arithmetic exception!
2488 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2489 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002490
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002491 __ cmpl(second_reg, Immediate(-1));
2492 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002493
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002494 // edx:eax <- sign-extended of eax
2495 __ cdq();
2496 // eax = quotient, edx = remainder
2497 __ idivl(second_reg);
2498 __ Bind(slow_path->GetExitLabel());
2499 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002500 break;
2501 }
2502
2503 case Primitive::kPrimLong: {
2504 InvokeRuntimeCallingConvention calling_convention;
2505 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2506 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2507 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2508 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2509 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2510 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2511
2512 if (is_div) {
2513 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2514 } else {
2515 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2516 }
2517 uint32_t dex_pc = is_div
2518 ? instruction->AsDiv()->GetDexPc()
2519 : instruction->AsRem()->GetDexPc();
2520 codegen_->RecordPcInfo(instruction, dex_pc);
2521
2522 break;
2523 }
2524
2525 default:
2526 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2527 }
2528}
2529
Calin Juravle7c4954d2014-10-28 16:57:40 +00002530void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002531 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002532 ? LocationSummary::kCall
2533 : LocationSummary::kNoCall;
2534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2535
Calin Juravle7c4954d2014-10-28 16:57:40 +00002536 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002537 case Primitive::kPrimInt: {
2538 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002539 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002540 locations->SetOut(Location::SameAsFirstInput());
2541 // Intel uses edx:eax as the dividend.
2542 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002543 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2544 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2545 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002546 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002547 locations->AddTemp(Location::RequiresRegister());
2548 }
Calin Juravled0d48522014-11-04 16:40:20 +00002549 break;
2550 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002551 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002552 InvokeRuntimeCallingConvention calling_convention;
2553 locations->SetInAt(0, Location::RegisterPairLocation(
2554 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2555 locations->SetInAt(1, Location::RegisterPairLocation(
2556 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2557 // Runtime helper puts the result in EAX, EDX.
2558 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002559 break;
2560 }
2561 case Primitive::kPrimFloat:
2562 case Primitive::kPrimDouble: {
2563 locations->SetInAt(0, Location::RequiresFpuRegister());
2564 locations->SetInAt(1, Location::RequiresFpuRegister());
2565 locations->SetOut(Location::SameAsFirstInput());
2566 break;
2567 }
2568
2569 default:
2570 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2571 }
2572}
2573
2574void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2575 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002576 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002577 Location first = locations->InAt(0);
2578 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002579
2580 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002581 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002582 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002583 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002584 break;
2585 }
2586
2587 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002588 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 break;
2591 }
2592
2593 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002594 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002596 break;
2597 }
2598
2599 default:
2600 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2601 }
2602}
2603
Calin Juravlebacfec32014-11-14 15:54:36 +00002604void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002605 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002606
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002607 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2608 ? LocationSummary::kCall
2609 : LocationSummary::kNoCall;
2610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002611
Calin Juravled2ec87d2014-12-08 14:24:46 +00002612 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002613 case Primitive::kPrimInt: {
2614 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002615 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002616 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002617 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2618 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2619 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002620 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002621 locations->AddTemp(Location::RequiresRegister());
2622 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 break;
2624 }
2625 case Primitive::kPrimLong: {
2626 InvokeRuntimeCallingConvention calling_convention;
2627 locations->SetInAt(0, Location::RegisterPairLocation(
2628 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2629 locations->SetInAt(1, Location::RegisterPairLocation(
2630 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2631 // Runtime helper puts the result in EAX, EDX.
2632 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2633 break;
2634 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002635 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002636 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002637 locations->SetInAt(0, Location::Any());
2638 locations->SetInAt(1, Location::Any());
2639 locations->SetOut(Location::RequiresFpuRegister());
2640 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002641 break;
2642 }
2643
2644 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002645 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002646 }
2647}
2648
2649void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2650 Primitive::Type type = rem->GetResultType();
2651 switch (type) {
2652 case Primitive::kPrimInt:
2653 case Primitive::kPrimLong: {
2654 GenerateDivRemIntegral(rem);
2655 break;
2656 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002657 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002658 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002659 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002660 break;
2661 }
2662 default:
2663 LOG(FATAL) << "Unexpected rem type " << type;
2664 }
2665}
2666
Calin Juravled0d48522014-11-04 16:40:20 +00002667void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2668 LocationSummary* locations =
2669 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002670 switch (instruction->GetType()) {
2671 case Primitive::kPrimInt: {
2672 locations->SetInAt(0, Location::Any());
2673 break;
2674 }
2675 case Primitive::kPrimLong: {
2676 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2677 if (!instruction->IsConstant()) {
2678 locations->AddTemp(Location::RequiresRegister());
2679 }
2680 break;
2681 }
2682 default:
2683 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2684 }
Calin Juravled0d48522014-11-04 16:40:20 +00002685 if (instruction->HasUses()) {
2686 locations->SetOut(Location::SameAsFirstInput());
2687 }
2688}
2689
2690void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2691 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2692 codegen_->AddSlowPath(slow_path);
2693
2694 LocationSummary* locations = instruction->GetLocations();
2695 Location value = locations->InAt(0);
2696
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002697 switch (instruction->GetType()) {
2698 case Primitive::kPrimInt: {
2699 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002701 __ j(kEqual, slow_path->GetEntryLabel());
2702 } else if (value.IsStackSlot()) {
2703 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2704 __ j(kEqual, slow_path->GetEntryLabel());
2705 } else {
2706 DCHECK(value.IsConstant()) << value;
2707 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2708 __ jmp(slow_path->GetEntryLabel());
2709 }
2710 }
2711 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002712 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002713 case Primitive::kPrimLong: {
2714 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002716 __ movl(temp, value.AsRegisterPairLow<Register>());
2717 __ orl(temp, value.AsRegisterPairHigh<Register>());
2718 __ j(kEqual, slow_path->GetEntryLabel());
2719 } else {
2720 DCHECK(value.IsConstant()) << value;
2721 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2722 __ jmp(slow_path->GetEntryLabel());
2723 }
2724 }
2725 break;
2726 }
2727 default:
2728 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002729 }
Calin Juravled0d48522014-11-04 16:40:20 +00002730}
2731
Calin Juravle9aec02f2014-11-18 23:06:35 +00002732void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2733 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2734
2735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2737
2738 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002739 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002740 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002741 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002742 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002743 // The shift count needs to be in CL or a constant.
2744 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002745 locations->SetOut(Location::SameAsFirstInput());
2746 break;
2747 }
2748 default:
2749 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2750 }
2751}
2752
2753void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2754 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2755
2756 LocationSummary* locations = op->GetLocations();
2757 Location first = locations->InAt(0);
2758 Location second = locations->InAt(1);
2759 DCHECK(first.Equals(locations->Out()));
2760
2761 switch (op->GetResultType()) {
2762 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002763 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002764 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002765 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002766 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002767 DCHECK_EQ(ECX, second_reg);
2768 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002769 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002770 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002771 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002772 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002773 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774 }
2775 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002776 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2777 if (shift == 0) {
2778 return;
2779 }
2780 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 if (op->IsShl()) {
2782 __ shll(first_reg, imm);
2783 } else if (op->IsShr()) {
2784 __ sarl(first_reg, imm);
2785 } else {
2786 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002787 }
2788 }
2789 break;
2790 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002791 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002792 if (second.IsRegister()) {
2793 Register second_reg = second.AsRegister<Register>();
2794 DCHECK_EQ(ECX, second_reg);
2795 if (op->IsShl()) {
2796 GenerateShlLong(first, second_reg);
2797 } else if (op->IsShr()) {
2798 GenerateShrLong(first, second_reg);
2799 } else {
2800 GenerateUShrLong(first, second_reg);
2801 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002802 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002803 // Shift by a constant.
2804 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2805 // Nothing to do if the shift is 0, as the input is already the output.
2806 if (shift != 0) {
2807 if (op->IsShl()) {
2808 GenerateShlLong(first, shift);
2809 } else if (op->IsShr()) {
2810 GenerateShrLong(first, shift);
2811 } else {
2812 GenerateUShrLong(first, shift);
2813 }
2814 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002815 }
2816 break;
2817 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002818 default:
2819 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2820 }
2821}
2822
Mark P Mendell73945692015-04-29 14:56:17 +00002823void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2824 Register low = loc.AsRegisterPairLow<Register>();
2825 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002826 if (shift == 1) {
2827 // This is just an addition.
2828 __ addl(low, low);
2829 __ adcl(high, high);
2830 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002831 // Shift by 32 is easy. High gets low, and low gets 0.
2832 codegen_->EmitParallelMoves(
2833 loc.ToLow(),
2834 loc.ToHigh(),
2835 Primitive::kPrimInt,
2836 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2837 loc.ToLow(),
2838 Primitive::kPrimInt);
2839 } else if (shift > 32) {
2840 // Low part becomes 0. High part is low part << (shift-32).
2841 __ movl(high, low);
2842 __ shll(high, Immediate(shift - 32));
2843 __ xorl(low, low);
2844 } else {
2845 // Between 1 and 31.
2846 __ shld(high, low, Immediate(shift));
2847 __ shll(low, Immediate(shift));
2848 }
2849}
2850
Calin Juravle9aec02f2014-11-18 23:06:35 +00002851void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2852 Label done;
2853 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2854 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2855 __ testl(shifter, Immediate(32));
2856 __ j(kEqual, &done);
2857 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2858 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2859 __ Bind(&done);
2860}
2861
Mark P Mendell73945692015-04-29 14:56:17 +00002862void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2863 Register low = loc.AsRegisterPairLow<Register>();
2864 Register high = loc.AsRegisterPairHigh<Register>();
2865 if (shift == 32) {
2866 // Need to copy the sign.
2867 DCHECK_NE(low, high);
2868 __ movl(low, high);
2869 __ sarl(high, Immediate(31));
2870 } else if (shift > 32) {
2871 DCHECK_NE(low, high);
2872 // High part becomes sign. Low part is shifted by shift - 32.
2873 __ movl(low, high);
2874 __ sarl(high, Immediate(31));
2875 __ sarl(low, Immediate(shift - 32));
2876 } else {
2877 // Between 1 and 31.
2878 __ shrd(low, high, Immediate(shift));
2879 __ sarl(high, Immediate(shift));
2880 }
2881}
2882
Calin Juravle9aec02f2014-11-18 23:06:35 +00002883void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2884 Label done;
2885 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2886 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2887 __ testl(shifter, Immediate(32));
2888 __ j(kEqual, &done);
2889 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2890 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2891 __ Bind(&done);
2892}
2893
Mark P Mendell73945692015-04-29 14:56:17 +00002894void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2895 Register low = loc.AsRegisterPairLow<Register>();
2896 Register high = loc.AsRegisterPairHigh<Register>();
2897 if (shift == 32) {
2898 // Shift by 32 is easy. Low gets high, and high gets 0.
2899 codegen_->EmitParallelMoves(
2900 loc.ToHigh(),
2901 loc.ToLow(),
2902 Primitive::kPrimInt,
2903 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2904 loc.ToHigh(),
2905 Primitive::kPrimInt);
2906 } else if (shift > 32) {
2907 // Low part is high >> (shift - 32). High part becomes 0.
2908 __ movl(low, high);
2909 __ shrl(low, Immediate(shift - 32));
2910 __ xorl(high, high);
2911 } else {
2912 // Between 1 and 31.
2913 __ shrd(low, high, Immediate(shift));
2914 __ shrl(high, Immediate(shift));
2915 }
2916}
2917
Calin Juravle9aec02f2014-11-18 23:06:35 +00002918void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2919 Label done;
2920 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2921 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2922 __ testl(shifter, Immediate(32));
2923 __ j(kEqual, &done);
2924 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2925 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2926 __ Bind(&done);
2927}
2928
2929void LocationsBuilderX86::VisitShl(HShl* shl) {
2930 HandleShift(shl);
2931}
2932
2933void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2934 HandleShift(shl);
2935}
2936
2937void LocationsBuilderX86::VisitShr(HShr* shr) {
2938 HandleShift(shr);
2939}
2940
2941void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2942 HandleShift(shr);
2943}
2944
2945void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2946 HandleShift(ushr);
2947}
2948
2949void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2950 HandleShift(ushr);
2951}
2952
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002953void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002954 LocationSummary* locations =
2955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002956 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002957 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002958 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraye21aa422015-06-08 15:35:07 +01002959 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002960}
2961
2962void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2963 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002964 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002965 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002966
Nicolas Geoffray39468442014-09-02 15:17:15 +01002967 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002968 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002969}
2970
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002971void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2972 LocationSummary* locations =
2973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2974 locations->SetOut(Location::RegisterLocation(EAX));
2975 InvokeRuntimeCallingConvention calling_convention;
2976 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002977 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraye21aa422015-06-08 15:35:07 +01002978 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002979}
2980
2981void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2982 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002983 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2984
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002985 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002986
2987 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2988 DCHECK(!codegen_->IsLeafMethod());
2989}
2990
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002991void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002992 LocationSummary* locations =
2993 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002994 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2995 if (location.IsStackSlot()) {
2996 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2997 } else if (location.IsDoubleStackSlot()) {
2998 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002999 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003000 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003001}
3002
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003003void InstructionCodeGeneratorX86::VisitParameterValue(
3004 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3005}
3006
3007void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3008 LocationSummary* locations =
3009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3010 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3011}
3012
3013void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003014}
3015
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003016void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003018 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003021}
3022
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003023void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3024 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003025 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003026 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003027 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003028 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003029 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003031 break;
3032
3033 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003034 __ notl(out.AsRegisterPairLow<Register>());
3035 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003036 break;
3037
3038 default:
3039 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3040 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003041}
3042
David Brazdil66d126e2015-04-03 16:02:44 +01003043void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3044 LocationSummary* locations =
3045 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3046 locations->SetInAt(0, Location::RequiresRegister());
3047 locations->SetOut(Location::SameAsFirstInput());
3048}
3049
3050void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003051 LocationSummary* locations = bool_not->GetLocations();
3052 Location in = locations->InAt(0);
3053 Location out = locations->Out();
3054 DCHECK(in.Equals(out));
3055 __ xorl(out.AsRegister<Register>(), Immediate(1));
3056}
3057
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003058void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003059 LocationSummary* locations =
3060 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003061 switch (compare->InputAt(0)->GetType()) {
3062 case Primitive::kPrimLong: {
3063 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003064 locations->SetInAt(1, Location::Any());
3065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3066 break;
3067 }
3068 case Primitive::kPrimFloat:
3069 case Primitive::kPrimDouble: {
3070 locations->SetInAt(0, Location::RequiresFpuRegister());
3071 locations->SetInAt(1, Location::RequiresFpuRegister());
3072 locations->SetOut(Location::RequiresRegister());
3073 break;
3074 }
3075 default:
3076 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3077 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003078}
3079
3080void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003081 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003083 Location left = locations->InAt(0);
3084 Location right = locations->InAt(1);
3085
3086 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003087 switch (compare->InputAt(0)->GetType()) {
3088 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003089 Register left_low = left.AsRegisterPairLow<Register>();
3090 Register left_high = left.AsRegisterPairHigh<Register>();
3091 int32_t val_low = 0;
3092 int32_t val_high = 0;
3093 bool right_is_const = false;
3094
3095 if (right.IsConstant()) {
3096 DCHECK(right.GetConstant()->IsLongConstant());
3097 right_is_const = true;
3098 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3099 val_low = Low32Bits(val);
3100 val_high = High32Bits(val);
3101 }
3102
Calin Juravleddb7df22014-11-25 20:56:51 +00003103 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003104 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003105 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003106 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003107 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003108 DCHECK(right_is_const) << right;
3109 if (val_high == 0) {
3110 __ testl(left_high, left_high);
3111 } else {
3112 __ cmpl(left_high, Immediate(val_high));
3113 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003114 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003115 __ j(kLess, &less); // Signed compare.
3116 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003117 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003118 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003119 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003120 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003121 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003122 DCHECK(right_is_const) << right;
3123 if (val_low == 0) {
3124 __ testl(left_low, left_low);
3125 } else {
3126 __ cmpl(left_low, Immediate(val_low));
3127 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003128 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003129 break;
3130 }
3131 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003133 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3134 break;
3135 }
3136 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003138 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003139 break;
3140 }
3141 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003142 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003143 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003144 __ movl(out, Immediate(0));
3145 __ j(kEqual, &done);
3146 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3147
3148 __ Bind(&greater);
3149 __ movl(out, Immediate(1));
3150 __ jmp(&done);
3151
3152 __ Bind(&less);
3153 __ movl(out, Immediate(-1));
3154
3155 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003156}
3157
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003158void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003159 LocationSummary* locations =
3160 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003161 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3162 locations->SetInAt(i, Location::Any());
3163 }
3164 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003165}
3166
3167void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003168 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003169 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003170}
3171
Calin Juravle52c48962014-12-16 17:02:57 +00003172void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3173 /*
3174 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3175 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3176 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3177 */
3178 switch (kind) {
3179 case MemBarrierKind::kAnyAny: {
3180 __ mfence();
3181 break;
3182 }
3183 case MemBarrierKind::kAnyStore:
3184 case MemBarrierKind::kLoadAny:
3185 case MemBarrierKind::kStoreStore: {
3186 // nop
3187 break;
3188 }
3189 default:
3190 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003191 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003192}
3193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003194
Mark Mendell09ed1a32015-03-25 08:30:06 -04003195void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003196 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003197 // TODO: Implement all kinds of calls:
3198 // 1) boot -> boot
3199 // 2) app -> boot
3200 // 3) app -> app
3201 //
3202 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003203
3204 if (invoke->IsStringInit()) {
3205 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003206 Register reg = temp.AsRegister<Register>();
3207 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003208 // (temp + offset_of_quick_compiled_code)()
3209 __ call(Address(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003210 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3211 } else if (invoke->IsRecursive()) {
3212 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003213 } else {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003214 Register current_method =
3215 invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex()).AsRegister<Register>();
3216 Register reg = temp.AsRegister<Register>();
3217 // temp = temp->dex_cache_resolved_methods_;
3218 __ movl(reg, Address(
3219 current_method, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3220 // temp = temp[index_in_cache]
3221 __ movl(reg, Address(reg,
3222 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3223 // (temp + offset_of_quick_compiled_code)()
3224 __ call(Address(reg,
3225 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003226 }
3227
3228 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003229}
3230
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003231void CodeGeneratorX86::MarkGCCard(Register temp,
3232 Register card,
3233 Register object,
3234 Register value,
3235 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003237 if (value_can_be_null) {
3238 __ testl(value, value);
3239 __ j(kEqual, &is_null);
3240 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003241 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3242 __ movl(temp, object);
3243 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003244 __ movb(Address(temp, card, TIMES_1, 0),
3245 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003246 if (value_can_be_null) {
3247 __ Bind(&is_null);
3248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003249}
3250
Calin Juravle52c48962014-12-16 17:02:57 +00003251void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3252 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003253 LocationSummary* locations =
3254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003255 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003256
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003257 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3258 locations->SetOut(Location::RequiresFpuRegister());
3259 } else {
3260 // The output overlaps in case of long: we don't want the low move to overwrite
3261 // the object's location.
3262 locations->SetOut(Location::RequiresRegister(),
3263 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3264 : Location::kNoOutputOverlap);
3265 }
Calin Juravle52c48962014-12-16 17:02:57 +00003266
3267 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3268 // Long values can be loaded atomically into an XMM using movsd.
3269 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3270 // and then copy the XMM into the output 32bits at a time).
3271 locations->AddTemp(Location::RequiresFpuRegister());
3272 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273}
3274
Calin Juravle52c48962014-12-16 17:02:57 +00003275void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3276 const FieldInfo& field_info) {
3277 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278
Calin Juravle52c48962014-12-16 17:02:57 +00003279 LocationSummary* locations = instruction->GetLocations();
3280 Register base = locations->InAt(0).AsRegister<Register>();
3281 Location out = locations->Out();
3282 bool is_volatile = field_info.IsVolatile();
3283 Primitive::Type field_type = field_info.GetFieldType();
3284 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3285
3286 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003287 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003288 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003289 break;
3290 }
3291
3292 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003293 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003294 break;
3295 }
3296
3297 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003298 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299 break;
3300 }
3301
3302 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003303 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003304 break;
3305 }
3306
3307 case Primitive::kPrimInt:
3308 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003309 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003310 break;
3311 }
3312
3313 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003314 if (is_volatile) {
3315 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3316 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003317 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003318 __ movd(out.AsRegisterPairLow<Register>(), temp);
3319 __ psrlq(temp, Immediate(32));
3320 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3321 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003322 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003323 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003325 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3326 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327 break;
3328 }
3329
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003330 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003331 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003332 break;
3333 }
3334
3335 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003336 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003337 break;
3338 }
3339
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003341 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003342 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 }
Calin Juravle52c48962014-12-16 17:02:57 +00003344
Calin Juravle77520bc2015-01-12 18:45:46 +00003345 // Longs are handled in the switch.
3346 if (field_type != Primitive::kPrimLong) {
3347 codegen_->MaybeRecordImplicitNullCheck(instruction);
3348 }
3349
Calin Juravle52c48962014-12-16 17:02:57 +00003350 if (is_volatile) {
3351 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3352 }
3353}
3354
3355void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3356 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3357
3358 LocationSummary* locations =
3359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3360 locations->SetInAt(0, Location::RequiresRegister());
3361 bool is_volatile = field_info.IsVolatile();
3362 Primitive::Type field_type = field_info.GetFieldType();
3363 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3364 || (field_type == Primitive::kPrimByte);
3365
3366 // The register allocator does not support multiple
3367 // inputs that die at entry with one in a specific register.
3368 if (is_byte_type) {
3369 // Ensure the value is in a byte register.
3370 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003371 } else if (Primitive::IsFloatingPointType(field_type)) {
3372 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003373 } else {
3374 locations->SetInAt(1, Location::RequiresRegister());
3375 }
3376 // Temporary registers for the write barrier.
3377 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3378 locations->AddTemp(Location::RequiresRegister());
3379 // Ensure the card is in a byte register.
3380 locations->AddTemp(Location::RegisterLocation(ECX));
3381 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3382 // 64bits value can be atomically written to an address with movsd and an XMM register.
3383 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3384 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3385 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3386 // isolated cases when we need this it isn't worth adding the extra complexity.
3387 locations->AddTemp(Location::RequiresFpuRegister());
3388 locations->AddTemp(Location::RequiresFpuRegister());
3389 }
3390}
3391
3392void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003393 const FieldInfo& field_info,
3394 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003395 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3396
3397 LocationSummary* locations = instruction->GetLocations();
3398 Register base = locations->InAt(0).AsRegister<Register>();
3399 Location value = locations->InAt(1);
3400 bool is_volatile = field_info.IsVolatile();
3401 Primitive::Type field_type = field_info.GetFieldType();
3402 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3403
3404 if (is_volatile) {
3405 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3406 }
3407
3408 switch (field_type) {
3409 case Primitive::kPrimBoolean:
3410 case Primitive::kPrimByte: {
3411 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3412 break;
3413 }
3414
3415 case Primitive::kPrimShort:
3416 case Primitive::kPrimChar: {
3417 __ movw(Address(base, offset), value.AsRegister<Register>());
3418 break;
3419 }
3420
3421 case Primitive::kPrimInt:
3422 case Primitive::kPrimNot: {
3423 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003424 break;
3425 }
3426
3427 case Primitive::kPrimLong: {
3428 if (is_volatile) {
3429 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3430 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3431 __ movd(temp1, value.AsRegisterPairLow<Register>());
3432 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3433 __ punpckldq(temp1, temp2);
3434 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003436 } else {
3437 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003439 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3440 }
3441 break;
3442 }
3443
3444 case Primitive::kPrimFloat: {
3445 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3446 break;
3447 }
3448
3449 case Primitive::kPrimDouble: {
3450 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3451 break;
3452 }
3453
3454 case Primitive::kPrimVoid:
3455 LOG(FATAL) << "Unreachable type " << field_type;
3456 UNREACHABLE();
3457 }
3458
Calin Juravle77520bc2015-01-12 18:45:46 +00003459 // Longs are handled in the switch.
3460 if (field_type != Primitive::kPrimLong) {
3461 codegen_->MaybeRecordImplicitNullCheck(instruction);
3462 }
3463
3464 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3465 Register temp = locations->GetTemp(0).AsRegister<Register>();
3466 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003467 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 }
3469
Calin Juravle52c48962014-12-16 17:02:57 +00003470 if (is_volatile) {
3471 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3472 }
3473}
3474
3475void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3476 HandleFieldGet(instruction, instruction->GetFieldInfo());
3477}
3478
3479void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3480 HandleFieldGet(instruction, instruction->GetFieldInfo());
3481}
3482
3483void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3484 HandleFieldSet(instruction, instruction->GetFieldInfo());
3485}
3486
3487void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003488 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003489}
3490
3491void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3492 HandleFieldSet(instruction, instruction->GetFieldInfo());
3493}
3494
3495void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003496 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003497}
3498
3499void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3500 HandleFieldGet(instruction, instruction->GetFieldInfo());
3501}
3502
3503void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3504 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003505}
3506
3507void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003508 LocationSummary* locations =
3509 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003510 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3511 ? Location::RequiresRegister()
3512 : Location::Any();
3513 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003514 if (instruction->HasUses()) {
3515 locations->SetOut(Location::SameAsFirstInput());
3516 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003517}
3518
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003519void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003520 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3521 return;
3522 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003523 LocationSummary* locations = instruction->GetLocations();
3524 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003525
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003526 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3527 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3528}
3529
3530void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003531 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003532 codegen_->AddSlowPath(slow_path);
3533
3534 LocationSummary* locations = instruction->GetLocations();
3535 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003536
3537 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003538 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003539 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003541 } else {
3542 DCHECK(obj.IsConstant()) << obj;
3543 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3544 __ jmp(slow_path->GetEntryLabel());
3545 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003546 }
3547 __ j(kEqual, slow_path->GetEntryLabel());
3548}
3549
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003550void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3551 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3552 GenerateImplicitNullCheck(instruction);
3553 } else {
3554 GenerateExplicitNullCheck(instruction);
3555 }
3556}
3557
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003558void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003561 locations->SetInAt(0, Location::RequiresRegister());
3562 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003563 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3564 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3565 } else {
3566 // The output overlaps in case of long: we don't want the low move to overwrite
3567 // the array's location.
3568 locations->SetOut(Location::RequiresRegister(),
3569 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3570 : Location::kNoOutputOverlap);
3571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572}
3573
3574void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3575 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003576 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003577 Location index = locations->InAt(1);
3578
Calin Juravle77520bc2015-01-12 18:45:46 +00003579 Primitive::Type type = instruction->GetType();
3580 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003581 case Primitive::kPrimBoolean: {
3582 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584 if (index.IsConstant()) {
3585 __ movzxb(out, Address(obj,
3586 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3587 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003588 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 }
3590 break;
3591 }
3592
3593 case Primitive::kPrimByte: {
3594 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 if (index.IsConstant()) {
3597 __ movsxb(out, Address(obj,
3598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3599 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 }
3602 break;
3603 }
3604
3605 case Primitive::kPrimShort: {
3606 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 if (index.IsConstant()) {
3609 __ movsxw(out, Address(obj,
3610 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3611 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 }
3614 break;
3615 }
3616
3617 case Primitive::kPrimChar: {
3618 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 if (index.IsConstant()) {
3621 __ movzxw(out, Address(obj,
3622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3623 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003625 }
3626 break;
3627 }
3628
3629 case Primitive::kPrimInt:
3630 case Primitive::kPrimNot: {
3631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003633 if (index.IsConstant()) {
3634 __ movl(out, Address(obj,
3635 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3636 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638 }
3639 break;
3640 }
3641
3642 case Primitive::kPrimLong: {
3643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003644 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003645 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003646 if (index.IsConstant()) {
3647 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003648 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003649 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003650 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003651 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003652 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003655 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003657 }
3658 break;
3659 }
3660
Mark Mendell7c8d0092015-01-26 11:21:33 -05003661 case Primitive::kPrimFloat: {
3662 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3663 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3664 if (index.IsConstant()) {
3665 __ movss(out, Address(obj,
3666 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3667 } else {
3668 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3669 }
3670 break;
3671 }
3672
3673 case Primitive::kPrimDouble: {
3674 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3675 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3676 if (index.IsConstant()) {
3677 __ movsd(out, Address(obj,
3678 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3679 } else {
3680 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3681 }
3682 break;
3683 }
3684
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003685 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003686 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003687 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003689
3690 if (type != Primitive::kPrimLong) {
3691 codegen_->MaybeRecordImplicitNullCheck(instruction);
3692 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003693}
3694
3695void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003696 // This location builder might end up asking to up to four registers, which is
3697 // not currently possible for baseline. The situation in which we need four
3698 // registers cannot be met by baseline though, because it has not run any
3699 // optimization.
3700
Nicolas Geoffray39468442014-09-02 15:17:15 +01003701 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003702 bool needs_write_barrier =
3703 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3704
Mark Mendell5f874182015-03-04 15:42:45 -05003705 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003706
Nicolas Geoffray39468442014-09-02 15:17:15 +01003707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3708 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003709 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003710
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003711 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003713 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3714 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3715 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003716 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003717 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3718 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003719 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003720 // In case of a byte operation, the register allocator does not support multiple
3721 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003722 locations->SetInAt(0, Location::RequiresRegister());
3723 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003724 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003725 // Ensure the value is in a byte register.
3726 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003727 } else if (Primitive::IsFloatingPointType(value_type)) {
3728 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003729 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003730 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003732 // Temporary registers for the write barrier.
3733 if (needs_write_barrier) {
3734 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003735 // Ensure the card is in a byte register.
3736 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003737 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739}
3740
3741void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3742 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003743 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003745 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003746 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003747 bool needs_runtime_call = locations->WillCall();
3748 bool needs_write_barrier =
3749 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003750
3751 switch (value_type) {
3752 case Primitive::kPrimBoolean:
3753 case Primitive::kPrimByte: {
3754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003755 if (index.IsConstant()) {
3756 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003758 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003759 } else {
3760 __ movb(Address(obj, offset),
3761 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3762 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003764 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003765 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003766 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003767 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003768 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003769 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003772 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003773 break;
3774 }
3775
3776 case Primitive::kPrimShort:
3777 case Primitive::kPrimChar: {
3778 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003779 if (index.IsConstant()) {
3780 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003781 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003783 } else {
3784 __ movw(Address(obj, offset),
3785 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3786 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003788 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3790 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003791 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003793 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3794 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003795 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003797 break;
3798 }
3799
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003800 case Primitive::kPrimInt:
3801 case Primitive::kPrimNot: {
3802 if (!needs_runtime_call) {
3803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3804 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003805 size_t offset =
3806 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003807 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003809 } else {
3810 DCHECK(value.IsConstant()) << value;
3811 __ movl(Address(obj, offset),
3812 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3813 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003814 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003815 DCHECK(index.IsRegister()) << index;
3816 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3818 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003819 } else {
3820 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003821 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003822 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3823 }
3824 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003826
3827 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 Register temp = locations->GetTemp(0).AsRegister<Register>();
3829 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003830 codegen_->MarkGCCard(
3831 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003832 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003833 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003834 DCHECK_EQ(value_type, Primitive::kPrimNot);
3835 DCHECK(!codegen_->IsLeafMethod());
3836 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3837 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003838 }
3839 break;
3840 }
3841
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 case Primitive::kPrimLong: {
3843 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003844 if (index.IsConstant()) {
3845 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003846 if (value.IsRegisterPair()) {
3847 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003849 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003850 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003851 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003852 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3853 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003854 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003855 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3856 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003857 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003858 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003859 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003860 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003861 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003862 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003863 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003864 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003865 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003866 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003868 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003871 Immediate(High32Bits(val)));
3872 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003873 }
3874 break;
3875 }
3876
Mark Mendell7c8d0092015-01-26 11:21:33 -05003877 case Primitive::kPrimFloat: {
3878 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3879 DCHECK(value.IsFpuRegister());
3880 if (index.IsConstant()) {
3881 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3882 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3883 } else {
3884 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3885 value.AsFpuRegister<XmmRegister>());
3886 }
3887 break;
3888 }
3889
3890 case Primitive::kPrimDouble: {
3891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3892 DCHECK(value.IsFpuRegister());
3893 if (index.IsConstant()) {
3894 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3895 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3896 } else {
3897 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3898 value.AsFpuRegister<XmmRegister>());
3899 }
3900 break;
3901 }
3902
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 case Primitive::kPrimVoid:
3904 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003905 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003906 }
3907}
3908
3909void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003911 locations->SetInAt(0, Location::RequiresRegister());
3912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913}
3914
3915void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3916 LocationSummary* locations = instruction->GetLocations();
3917 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003918 Register obj = locations->InAt(0).AsRegister<Register>();
3919 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003920 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003921 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922}
3923
3924void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003925 LocationSummary* locations =
3926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003927 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003928 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003929 if (instruction->HasUses()) {
3930 locations->SetOut(Location::SameAsFirstInput());
3931 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932}
3933
3934void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3935 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003936 Location index_loc = locations->InAt(0);
3937 Location length_loc = locations->InAt(1);
3938 SlowPathCodeX86* slow_path =
3939 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003940
Mark Mendell99dbd682015-04-22 16:18:52 -04003941 if (length_loc.IsConstant()) {
3942 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3943 if (index_loc.IsConstant()) {
3944 // BCE will remove the bounds check if we are guarenteed to pass.
3945 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3946 if (index < 0 || index >= length) {
3947 codegen_->AddSlowPath(slow_path);
3948 __ jmp(slow_path->GetEntryLabel());
3949 } else {
3950 // Some optimization after BCE may have generated this, and we should not
3951 // generate a bounds check if it is a valid range.
3952 }
3953 return;
3954 }
3955
3956 // We have to reverse the jump condition because the length is the constant.
3957 Register index_reg = index_loc.AsRegister<Register>();
3958 __ cmpl(index_reg, Immediate(length));
3959 codegen_->AddSlowPath(slow_path);
3960 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003961 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003962 Register length = length_loc.AsRegister<Register>();
3963 if (index_loc.IsConstant()) {
3964 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3965 __ cmpl(length, Immediate(value));
3966 } else {
3967 __ cmpl(length, index_loc.AsRegister<Register>());
3968 }
3969 codegen_->AddSlowPath(slow_path);
3970 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003971 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003972}
3973
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3975 temp->SetLocations(nullptr);
3976}
3977
3978void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3979 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003980 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003981}
3982
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003983void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003984 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003985 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003986}
3987
3988void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003989 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3990}
3991
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003992void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3993 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3994}
3995
3996void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003997 HBasicBlock* block = instruction->GetBlock();
3998 if (block->GetLoopInformation() != nullptr) {
3999 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4000 // The back edge will generate the suspend check.
4001 return;
4002 }
4003 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4004 // The goto will generate the suspend check.
4005 return;
4006 }
4007 GenerateSuspendCheck(instruction, nullptr);
4008}
4009
4010void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4011 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004012 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004013 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4014 if (slow_path == nullptr) {
4015 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4016 instruction->SetSlowPath(slow_path);
4017 codegen_->AddSlowPath(slow_path);
4018 if (successor != nullptr) {
4019 DCHECK(successor->IsLoopHeader());
4020 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4021 }
4022 } else {
4023 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4024 }
4025
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004026 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004027 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004028 if (successor == nullptr) {
4029 __ j(kNotEqual, slow_path->GetEntryLabel());
4030 __ Bind(slow_path->GetReturnLabel());
4031 } else {
4032 __ j(kEqual, codegen_->GetLabelOf(successor));
4033 __ jmp(slow_path->GetEntryLabel());
4034 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004035}
4036
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004037X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4038 return codegen_->GetAssembler();
4039}
4040
Mark Mendell7c8d0092015-01-26 11:21:33 -05004041void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004042 ScratchRegisterScope ensure_scratch(
4043 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4044 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4045 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4046 __ movl(temp_reg, Address(ESP, src + stack_offset));
4047 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004048}
4049
4050void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004051 ScratchRegisterScope ensure_scratch(
4052 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4053 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4054 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4055 __ movl(temp_reg, Address(ESP, src + stack_offset));
4056 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4057 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4058 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004059}
4060
4061void ParallelMoveResolverX86::EmitMove(size_t index) {
4062 MoveOperands* move = moves_.Get(index);
4063 Location source = move->GetSource();
4064 Location destination = move->GetDestination();
4065
4066 if (source.IsRegister()) {
4067 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004069 } else {
4070 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004072 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004073 } else if (source.IsFpuRegister()) {
4074 if (destination.IsFpuRegister()) {
4075 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4076 } else if (destination.IsStackSlot()) {
4077 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4078 } else {
4079 DCHECK(destination.IsDoubleStackSlot());
4080 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4081 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004082 } else if (source.IsStackSlot()) {
4083 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004085 } else if (destination.IsFpuRegister()) {
4086 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004087 } else {
4088 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004089 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4090 }
4091 } else if (source.IsDoubleStackSlot()) {
4092 if (destination.IsFpuRegister()) {
4093 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4094 } else {
4095 DCHECK(destination.IsDoubleStackSlot()) << destination;
4096 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004097 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004098 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004099 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004100 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004101 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004102 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004103 if (value == 0) {
4104 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4105 } else {
4106 __ movl(destination.AsRegister<Register>(), Immediate(value));
4107 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004108 } else {
4109 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004110 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004111 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004112 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004113 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004114 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004115 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004116 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004117 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4118 if (value == 0) {
4119 // Easy handling of 0.0.
4120 __ xorps(dest, dest);
4121 } else {
4122 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004123 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4124 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4125 __ movl(temp, Immediate(value));
4126 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004127 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004128 } else {
4129 DCHECK(destination.IsStackSlot()) << destination;
4130 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4131 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004132 } else if (constant->IsLongConstant()) {
4133 int64_t value = constant->AsLongConstant()->GetValue();
4134 int32_t low_value = Low32Bits(value);
4135 int32_t high_value = High32Bits(value);
4136 Immediate low(low_value);
4137 Immediate high(high_value);
4138 if (destination.IsDoubleStackSlot()) {
4139 __ movl(Address(ESP, destination.GetStackIndex()), low);
4140 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4141 } else {
4142 __ movl(destination.AsRegisterPairLow<Register>(), low);
4143 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4144 }
4145 } else {
4146 DCHECK(constant->IsDoubleConstant());
4147 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004148 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004149 int32_t low_value = Low32Bits(value);
4150 int32_t high_value = High32Bits(value);
4151 Immediate low(low_value);
4152 Immediate high(high_value);
4153 if (destination.IsFpuRegister()) {
4154 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4155 if (value == 0) {
4156 // Easy handling of 0.0.
4157 __ xorpd(dest, dest);
4158 } else {
4159 __ pushl(high);
4160 __ pushl(low);
4161 __ movsd(dest, Address(ESP, 0));
4162 __ addl(ESP, Immediate(8));
4163 }
4164 } else {
4165 DCHECK(destination.IsDoubleStackSlot()) << destination;
4166 __ movl(Address(ESP, destination.GetStackIndex()), low);
4167 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4168 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004169 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004170 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004171 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004172 }
4173}
4174
Mark Mendella5c19ce2015-04-01 12:51:05 -04004175void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004176 Register suggested_scratch = reg == EAX ? EBX : EAX;
4177 ScratchRegisterScope ensure_scratch(
4178 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4179
4180 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4181 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4182 __ movl(Address(ESP, mem + stack_offset), reg);
4183 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004184}
4185
Mark Mendell7c8d0092015-01-26 11:21:33 -05004186void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004187 ScratchRegisterScope ensure_scratch(
4188 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4189
4190 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4191 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4192 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4193 __ movss(Address(ESP, mem + stack_offset), reg);
4194 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004195}
4196
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004197void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004198 ScratchRegisterScope ensure_scratch1(
4199 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004200
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004201 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4202 ScratchRegisterScope ensure_scratch2(
4203 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004204
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004205 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4206 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4207 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4208 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4209 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4210 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004211}
4212
4213void ParallelMoveResolverX86::EmitSwap(size_t index) {
4214 MoveOperands* move = moves_.Get(index);
4215 Location source = move->GetSource();
4216 Location destination = move->GetDestination();
4217
4218 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004219 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004220 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004221 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004222 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004223 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4225 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004226 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4227 // Use XOR Swap algorithm to avoid a temporary.
4228 DCHECK_NE(source.reg(), destination.reg());
4229 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4230 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4231 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4232 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4233 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4234 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4235 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004236 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4237 // Take advantage of the 16 bytes in the XMM register.
4238 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4239 Address stack(ESP, destination.GetStackIndex());
4240 // Load the double into the high doubleword.
4241 __ movhpd(reg, stack);
4242
4243 // Store the low double into the destination.
4244 __ movsd(stack, reg);
4245
4246 // Move the high double to the low double.
4247 __ psrldq(reg, Immediate(8));
4248 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4249 // Take advantage of the 16 bytes in the XMM register.
4250 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4251 Address stack(ESP, source.GetStackIndex());
4252 // Load the double into the high doubleword.
4253 __ movhpd(reg, stack);
4254
4255 // Store the low double into the destination.
4256 __ movsd(stack, reg);
4257
4258 // Move the high double to the low double.
4259 __ psrldq(reg, Immediate(8));
4260 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4261 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4262 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004263 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004264 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004265 }
4266}
4267
4268void ParallelMoveResolverX86::SpillScratch(int reg) {
4269 __ pushl(static_cast<Register>(reg));
4270}
4271
4272void ParallelMoveResolverX86::RestoreScratch(int reg) {
4273 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004274}
4275
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004276void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004277 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4278 ? LocationSummary::kCallOnSlowPath
4279 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004280 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004281 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004282 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004283 locations->SetOut(Location::RequiresRegister());
4284}
4285
4286void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004287 LocationSummary* locations = cls->GetLocations();
4288 Register out = locations->Out().AsRegister<Register>();
4289 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004290 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004291 DCHECK(!cls->CanCallRuntime());
4292 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004293 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004294 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004295 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004296 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004297 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004298 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004299
4300 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4301 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4302 codegen_->AddSlowPath(slow_path);
4303 __ testl(out, out);
4304 __ j(kEqual, slow_path->GetEntryLabel());
4305 if (cls->MustGenerateClinitCheck()) {
4306 GenerateClassInitializationCheck(slow_path, out);
4307 } else {
4308 __ Bind(slow_path->GetExitLabel());
4309 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004310 }
4311}
4312
4313void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4314 LocationSummary* locations =
4315 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4316 locations->SetInAt(0, Location::RequiresRegister());
4317 if (check->HasUses()) {
4318 locations->SetOut(Location::SameAsFirstInput());
4319 }
4320}
4321
4322void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004323 // We assume the class to not be null.
4324 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4325 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004326 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004327 GenerateClassInitializationCheck(slow_path,
4328 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004329}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004330
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004331void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4332 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004333 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4334 Immediate(mirror::Class::kStatusInitialized));
4335 __ j(kLess, slow_path->GetEntryLabel());
4336 __ Bind(slow_path->GetExitLabel());
4337 // No need for memory fence, thanks to the X86 memory model.
4338}
4339
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004340void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4341 LocationSummary* locations =
4342 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004343 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004344 locations->SetOut(Location::RequiresRegister());
4345}
4346
4347void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4348 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4349 codegen_->AddSlowPath(slow_path);
4350
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004351 LocationSummary* locations = load->GetLocations();
4352 Register out = locations->Out().AsRegister<Register>();
4353 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004354 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004355 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004356 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4357 __ testl(out, out);
4358 __ j(kEqual, slow_path->GetEntryLabel());
4359 __ Bind(slow_path->GetExitLabel());
4360}
4361
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004362void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4363 LocationSummary* locations =
4364 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4365 locations->SetOut(Location::RequiresRegister());
4366}
4367
4368void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4369 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004371 __ fs()->movl(address, Immediate(0));
4372}
4373
4374void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4375 LocationSummary* locations =
4376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4377 InvokeRuntimeCallingConvention calling_convention;
4378 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4379}
4380
4381void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4382 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4383 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4384}
4385
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004386void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004387 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4388 ? LocationSummary::kNoCall
4389 : LocationSummary::kCallOnSlowPath;
4390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4391 locations->SetInAt(0, Location::RequiresRegister());
4392 locations->SetInAt(1, Location::Any());
4393 locations->SetOut(Location::RequiresRegister());
4394}
4395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004396void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004397 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004399 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004400 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004401 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4402 Label done, zero;
4403 SlowPathCodeX86* slow_path = nullptr;
4404
4405 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004406 // Avoid null check if we know obj is not null.
4407 if (instruction->MustDoNullCheck()) {
4408 __ testl(obj, obj);
4409 __ j(kEqual, &zero);
4410 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004411 __ movl(out, Address(obj, class_offset));
4412 // Compare the class of `obj` with `cls`.
4413 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004414 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004415 } else {
4416 DCHECK(cls.IsStackSlot()) << cls;
4417 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4418 }
4419
4420 if (instruction->IsClassFinal()) {
4421 // Classes must be equal for the instanceof to succeed.
4422 __ j(kNotEqual, &zero);
4423 __ movl(out, Immediate(1));
4424 __ jmp(&done);
4425 } else {
4426 // If the classes are not equal, we go into a slow path.
4427 DCHECK(locations->OnlyCallsOnSlowPath());
4428 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004429 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004430 codegen_->AddSlowPath(slow_path);
4431 __ j(kNotEqual, slow_path->GetEntryLabel());
4432 __ movl(out, Immediate(1));
4433 __ jmp(&done);
4434 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004435
4436 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4437 __ Bind(&zero);
4438 __ movl(out, Immediate(0));
4439 }
4440
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004441 if (slow_path != nullptr) {
4442 __ Bind(slow_path->GetExitLabel());
4443 }
4444 __ Bind(&done);
4445}
4446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004447void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4448 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4449 instruction, LocationSummary::kCallOnSlowPath);
4450 locations->SetInAt(0, Location::RequiresRegister());
4451 locations->SetInAt(1, Location::Any());
4452 locations->AddTemp(Location::RequiresRegister());
4453}
4454
4455void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4456 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004457 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004458 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004459 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004460 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4461 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4462 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4463 codegen_->AddSlowPath(slow_path);
4464
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004465 // Avoid null check if we know obj is not null.
4466 if (instruction->MustDoNullCheck()) {
4467 __ testl(obj, obj);
4468 __ j(kEqual, slow_path->GetExitLabel());
4469 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004470
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004471 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004472 // Compare the class of `obj` with `cls`.
4473 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004474 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004475 } else {
4476 DCHECK(cls.IsStackSlot()) << cls;
4477 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4478 }
4479
4480 __ j(kNotEqual, slow_path->GetEntryLabel());
4481 __ Bind(slow_path->GetExitLabel());
4482}
4483
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004484void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4485 LocationSummary* locations =
4486 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4487 InvokeRuntimeCallingConvention calling_convention;
4488 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4489}
4490
4491void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4492 __ fs()->call(Address::Absolute(instruction->IsEnter()
4493 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4494 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4495 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4496}
4497
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004498void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4499void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4500void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4501
4502void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4503 LocationSummary* locations =
4504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4505 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4506 || instruction->GetResultType() == Primitive::kPrimLong);
4507 locations->SetInAt(0, Location::RequiresRegister());
4508 locations->SetInAt(1, Location::Any());
4509 locations->SetOut(Location::SameAsFirstInput());
4510}
4511
4512void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4513 HandleBitwiseOperation(instruction);
4514}
4515
4516void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4517 HandleBitwiseOperation(instruction);
4518}
4519
4520void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4521 HandleBitwiseOperation(instruction);
4522}
4523
4524void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4525 LocationSummary* locations = instruction->GetLocations();
4526 Location first = locations->InAt(0);
4527 Location second = locations->InAt(1);
4528 DCHECK(first.Equals(locations->Out()));
4529
4530 if (instruction->GetResultType() == Primitive::kPrimInt) {
4531 if (second.IsRegister()) {
4532 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004533 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004534 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004535 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004536 } else {
4537 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004538 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004539 }
4540 } else if (second.IsConstant()) {
4541 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004542 __ andl(first.AsRegister<Register>(),
4543 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004544 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004545 __ orl(first.AsRegister<Register>(),
4546 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004547 } else {
4548 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004549 __ xorl(first.AsRegister<Register>(),
4550 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004551 }
4552 } else {
4553 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004554 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004555 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004556 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004557 } else {
4558 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004559 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004560 }
4561 }
4562 } else {
4563 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4564 if (second.IsRegisterPair()) {
4565 if (instruction->IsAnd()) {
4566 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4567 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4568 } else if (instruction->IsOr()) {
4569 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4570 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4571 } else {
4572 DCHECK(instruction->IsXor());
4573 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4574 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4575 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004576 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004577 if (instruction->IsAnd()) {
4578 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4579 __ andl(first.AsRegisterPairHigh<Register>(),
4580 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4581 } else if (instruction->IsOr()) {
4582 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4583 __ orl(first.AsRegisterPairHigh<Register>(),
4584 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4585 } else {
4586 DCHECK(instruction->IsXor());
4587 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4588 __ xorl(first.AsRegisterPairHigh<Register>(),
4589 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4590 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004591 } else {
4592 DCHECK(second.IsConstant()) << second;
4593 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004594 int32_t low_value = Low32Bits(value);
4595 int32_t high_value = High32Bits(value);
4596 Immediate low(low_value);
4597 Immediate high(high_value);
4598 Register first_low = first.AsRegisterPairLow<Register>();
4599 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004600 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004601 if (low_value == 0) {
4602 __ xorl(first_low, first_low);
4603 } else if (low_value != -1) {
4604 __ andl(first_low, low);
4605 }
4606 if (high_value == 0) {
4607 __ xorl(first_high, first_high);
4608 } else if (high_value != -1) {
4609 __ andl(first_high, high);
4610 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004611 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004612 if (low_value != 0) {
4613 __ orl(first_low, low);
4614 }
4615 if (high_value != 0) {
4616 __ orl(first_high, high);
4617 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004618 } else {
4619 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004620 if (low_value != 0) {
4621 __ xorl(first_low, low);
4622 }
4623 if (high_value != 0) {
4624 __ xorl(first_high, high);
4625 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004626 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004627 }
4628 }
4629}
4630
Calin Juravleb1498f62015-02-16 13:13:29 +00004631void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4632 // Nothing to do, this should be removed during prepare for register allocator.
4633 UNUSED(instruction);
4634 LOG(FATAL) << "Unreachable";
4635}
4636
4637void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4638 // Nothing to do, this should be removed during prepare for register allocator.
4639 UNUSED(instruction);
4640 LOG(FATAL) << "Unreachable";
4641}
4642
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004643} // namespace x86
4644} // namespace art