blob: f6110a558ecf4cd46b96a5e14ca74a1df71cba30 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
59 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
62};
63
Calin Juravled0d48522014-11-04 16:40:20 +000064class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
65 public:
66 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
67
Alexandre Rames2ed20af2015-03-06 13:55:35 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000069 __ Bind(GetEntryLabel());
70 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070071 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000072 }
73
74 private:
75 HDivZeroCheck* const instruction_;
76 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
77};
78
Calin Juravlebacfec32014-11-14 15:54:36 +000079class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000081 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000085 if (is_div_) {
86 __ negl(reg_);
87 } else {
88 __ movl(reg_, Immediate(0));
89 }
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ jmp(GetExitLabel());
91 }
92
93 private:
94 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000095 bool is_div_;
96 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000097};
98
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010099class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100100 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100101 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
102 Location index_location,
103 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000104 : instruction_(instruction),
105 index_location_(index_location),
106 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100110 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000111 // We're moving two locations to locations that could overlap, so we need a parallel
112 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000114 x86_codegen->EmitParallelMoves(
115 index_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100117 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000118 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100119 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
120 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700122 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 }
124
125 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100126 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 const Location index_location_;
128 const Location length_location_;
129
130 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000135 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000138 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700143 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 private:
162 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 Label return_label_;
165
166 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
167};
168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169class LoadStringSlowPathX86 : public SlowPathCodeX86 {
170 public:
171 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000174 LocationSummary* locations = instruction_->GetLocations();
175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
176
177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
178 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000185 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000187
188 __ jmp(GetExitLabel());
189 }
190
191 private:
192 HLoadString* const instruction_;
193
194 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 LoadClassSlowPathX86(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
206
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 InvokeRuntimeCallingConvention calling_convention;
214 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ fs()->call(Address::Absolute(do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ jmp(GetExitLabel());
229 }
230
231 private:
232 // The class this slow path will load.
233 HLoadClass* const cls_;
234
235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
246};
247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
249 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 TypeCheckSlowPathX86(HInstruction* instruction,
251 Location class_to_check,
252 Location object_class,
253 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 class_to_check_(class_to_check),
256 object_class_(object_class),
257 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000261 DCHECK(instruction_->IsCheckCast()
262 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
268 // We're moving two locations to locations that could overlap, so we need a parallel
269 // move resolver.
270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000271 x86_codegen->EmitParallelMoves(
272 class_to_check_,
273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100274 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000275 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100276 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
277 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000280 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
281 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 } else {
283 DCHECK(instruction_->IsCheckCast());
284 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
289 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 __ jmp(GetExitLabel());
294 }
295
296 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 HInstruction* const instruction_;
298 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
303};
304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700305class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
306 public:
307 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
308 : instruction_(instruction) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, instruction_->GetLocations());
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
314 // No need to restore live registers.
315 DCHECK(instruction_->IsDeoptimize());
316 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
317 uint32_t dex_pc = deoptimize->GetDexPc();
318 codegen->RecordPcInfo(instruction_, dex_pc, this);
319 }
320
321 private:
322 HInstruction* const instruction_;
323 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100327#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100344 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100348 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Mark Mendell7c8d0092015-01-26 11:21:33 -0500361size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
363 return GetFloatingPointSpillSlotSize();
364}
365
366size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
367 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
368 return GetFloatingPointSpillSlotSize();
369}
370
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
372 const X86InstructionSetFeatures& isa_features,
373 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500374 : CodeGenerator(graph,
375 kNumberOfCpuRegisters,
376 kNumberOfXmmRegisters,
377 kNumberOfRegisterPairs,
378 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
379 arraysize(kCoreCalleeSaves))
380 | (1 << kFakeReturnRegister),
381 0,
382 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100383 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 move_resolver_(graph->GetArena(), this),
387 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000388 // Use a fake return address register to mimic Quick.
389 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100390}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 switch (type) {
394 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 X86ManagedRegister pair =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
399 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
401 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100402 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimByte:
407 case Primitive::kPrimBoolean:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
416 X86ManagedRegister current =
417 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
418 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 }
421 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 return Location::FpuRegisterLocation(
428 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Mark Mendell5f874182015-03-04 15:42:45 -0500438void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
Mark Mendell5f874182015-03-04 15:42:45 -0500445 if (is_baseline) {
446 blocked_core_registers_[EBP] = true;
447 blocked_core_registers_[ESI] = true;
448 blocked_core_registers_[EDI] = true;
449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100450
451 UpdateBlockedPairRegisters();
452}
453
454void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
455 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
456 X86ManagedRegister current =
457 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
458 if (blocked_core_registers_[current.AsRegisterPairLow()]
459 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
460 blocked_register_pairs_[i] = true;
461 }
462 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463}
464
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
466 : HGraphVisitor(graph),
467 assembler_(codegen->GetAssembler()),
468 codegen_(codegen) {}
469
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100471 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472}
473
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100475 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000476 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000477 bool skip_overflow_check =
478 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000480
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 }
485
Mark Mendell5f874182015-03-04 15:42:45 -0500486 if (HasEmptyFrame()) {
487 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000488 }
Mark Mendell5f874182015-03-04 15:42:45 -0500489
490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
491 Register reg = kCoreCalleeSaves[i];
492 if (allocated_registers_.ContainsCoreRegister(reg)) {
493 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().AdjustCFAOffset(kX86WordSize);
495 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500496 }
497 }
498
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 int adjust = GetFrameSize() - FrameEntrySpillSize();
500 __ subl(ESP, Immediate(adjust));
501 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100502 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000503}
504
505void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100506 __ cfi().RememberState();
507 if (!HasEmptyFrame()) {
508 int adjust = GetFrameSize() - FrameEntrySpillSize();
509 __ addl(ESP, Immediate(adjust));
510 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500511
David Srbeckyc34dc932015-04-12 09:27:43 +0100512 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
513 Register reg = kCoreCalleeSaves[i];
514 if (allocated_registers_.ContainsCoreRegister(reg)) {
515 __ popl(reg);
516 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
517 __ cfi().Restore(DWARFReg(reg));
518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100521 __ ret();
522 __ cfi().RestoreState();
523 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528}
529
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +0000530void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
531 DCHECK(RequiresCurrentMethod());
532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
533}
534
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
536 switch (load->GetType()) {
537 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545
546 case Primitive::kPrimBoolean:
547 case Primitive::kPrimByte:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimShort:
550 case Primitive::kPrimVoid:
551 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700552 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700556 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557}
558
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100559Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
560 switch (type) {
561 case Primitive::kPrimBoolean:
562 case Primitive::kPrimByte:
563 case Primitive::kPrimChar:
564 case Primitive::kPrimShort:
565 case Primitive::kPrimInt:
566 case Primitive::kPrimNot:
567 return Location::RegisterLocation(EAX);
568
569 case Primitive::kPrimLong:
570 return Location::RegisterPairLocation(EAX, EDX);
571
572 case Primitive::kPrimVoid:
573 return Location::NoLocation();
574
575 case Primitive::kPrimDouble:
576 case Primitive::kPrimFloat:
577 return Location::FpuRegisterLocation(XMM0);
578 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100579
580 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100581}
582
583Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
584 return Location::RegisterLocation(kMethodRegisterArgument);
585}
586
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100587Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100588 switch (type) {
589 case Primitive::kPrimBoolean:
590 case Primitive::kPrimByte:
591 case Primitive::kPrimChar:
592 case Primitive::kPrimShort:
593 case Primitive::kPrimInt:
594 case Primitive::kPrimNot: {
595 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000596 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100597 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100598 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100599 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000600 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100601 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100602 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100603
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000604 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100605 uint32_t index = gp_index_;
606 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000607 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100608 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100609 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
610 calling_convention.GetRegisterPairAt(index));
611 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000613 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
614 }
615 }
616
617 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100618 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000619 stack_index_++;
620 if (index < calling_convention.GetNumberOfFpuRegisters()) {
621 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
622 } else {
623 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
624 }
625 }
626
627 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100628 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000629 stack_index_ += 2;
630 if (index < calling_convention.GetNumberOfFpuRegisters()) {
631 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
632 } else {
633 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100634 }
635 }
636
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100637 case Primitive::kPrimVoid:
638 LOG(FATAL) << "Unexpected parameter type " << type;
639 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100640 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100641 return Location();
642}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644void CodeGeneratorX86::Move32(Location destination, Location source) {
645 if (source.Equals(destination)) {
646 return;
647 }
648 if (destination.IsRegister()) {
649 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else {
654 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000655 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else if (destination.IsFpuRegister()) {
658 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000659 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000661 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 } else {
663 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000664 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000667 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100668 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000669 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500672 } else if (source.IsConstant()) {
673 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000674 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500675 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 } else {
677 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100678 __ pushl(Address(ESP, source.GetStackIndex()));
679 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 }
681 }
682}
683
684void CodeGeneratorX86::Move64(Location destination, Location source) {
685 if (source.Equals(destination)) {
686 return;
687 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 if (destination.IsRegisterPair()) {
689 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000690 EmitParallelMoves(
691 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
692 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100693 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000694 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100695 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
696 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
698 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000700 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100702 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
703 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
705 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500707 if (source.IsFpuRegister()) {
708 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
709 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else {
712 LOG(FATAL) << "Unimplemented";
713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000715 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000717 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100718 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100720 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000723 } else if (source.IsConstant()) {
724 HConstant* constant = source.GetConstant();
725 int64_t value;
726 if (constant->IsLongConstant()) {
727 value = constant->AsLongConstant()->GetValue();
728 } else {
729 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000730 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000731 }
732 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
733 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000735 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000736 EmitParallelMoves(
737 Location::StackSlot(source.GetStackIndex()),
738 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100739 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000740 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100741 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
742 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 }
744 }
745}
746
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100747void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000748 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100749 if (instruction->IsCurrentMethod()) {
750 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
751 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000752 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100753 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000755 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
756 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000757 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000758 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 } else if (location.IsStackSlot()) {
760 __ movl(Address(ESP, location.GetStackIndex()), imm);
761 } else {
762 DCHECK(location.IsConstant());
763 DCHECK_EQ(location.GetConstant(), const_to_move);
764 }
765 } else if (const_to_move->IsLongConstant()) {
766 int64_t value = const_to_move->AsLongConstant()->GetValue();
767 if (location.IsRegisterPair()) {
768 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
769 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
770 } else if (location.IsDoubleStackSlot()) {
771 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000772 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
773 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000774 } else {
775 DCHECK(location.IsConstant());
776 DCHECK_EQ(location.GetConstant(), instruction);
777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000779 } else if (instruction->IsTemporary()) {
780 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000781 if (temp_location.IsStackSlot()) {
782 Move32(location, temp_location);
783 } else {
784 DCHECK(temp_location.IsDoubleStackSlot());
785 Move64(location, temp_location);
786 }
Roland Levillain476df552014-10-09 17:51:36 +0100787 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 switch (instruction->GetType()) {
790 case Primitive::kPrimBoolean:
791 case Primitive::kPrimByte:
792 case Primitive::kPrimChar:
793 case Primitive::kPrimShort:
794 case Primitive::kPrimInt:
795 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 case Primitive::kPrimFloat:
797 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 break;
799
800 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 case Primitive::kPrimDouble:
802 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 break;
804
805 default:
806 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
807 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000808 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 switch (instruction->GetType()) {
811 case Primitive::kPrimBoolean:
812 case Primitive::kPrimByte:
813 case Primitive::kPrimChar:
814 case Primitive::kPrimShort:
815 case Primitive::kPrimInt:
816 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100817 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 break;
820
821 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000823 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 break;
825
826 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100827 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829 }
830}
831
832void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834}
835
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000837 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 DCHECK(!successor->IsExitBlock());
839
840 HBasicBlock* block = got->GetBlock();
841 HInstruction* previous = got->GetPrevious();
842
843 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000844 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
846 return;
847 }
848
849 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
850 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
851 }
852 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000854 }
855}
856
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000859}
860
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000863}
864
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700865void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
866 Label* true_target,
867 Label* false_target,
868 Label* always_true_target) {
869 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 if (cond->IsIntConstant()) {
871 // Constant condition, statically compared against 1.
872 int32_t cond_value = cond->AsIntConstant()->GetValue();
873 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700874 if (always_true_target != nullptr) {
875 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100876 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100877 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100879 DCHECK_EQ(cond_value, 0);
880 }
881 } else {
882 bool materialized =
883 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
884 // Moves do not affect the eflags register, so if the condition is
885 // evaluated just before the if, we don't need to evaluate it
886 // again.
887 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700888 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100889 if (materialized) {
890 if (!eflags_set) {
891 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100893 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500894 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100895 } else {
896 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
897 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700898 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100899 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700900 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100901 }
902 } else {
903 Location lhs = cond->GetLocations()->InAt(0);
904 Location rhs = cond->GetLocations()->InAt(1);
905 // LHS is guaranteed to be in a register (see
906 // LocationsBuilderX86::VisitCondition).
907 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000908 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100909 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100910 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500911 if (constant == 0) {
912 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
913 } else {
914 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
915 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000917 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100918 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700919 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700920 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700922 if (false_target != nullptr) {
923 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924 }
925}
926
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700927void LocationsBuilderX86::VisitIf(HIf* if_instr) {
928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
930 HInstruction* cond = if_instr->InputAt(0);
931 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
932 locations->SetInAt(0, Location::Any());
933 }
934}
935
936void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
937 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
938 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
939 Label* always_true_target = true_target;
940 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
941 if_instr->IfTrueSuccessor())) {
942 always_true_target = nullptr;
943 }
944 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
945 if_instr->IfFalseSuccessor())) {
946 false_target = nullptr;
947 }
948 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
949}
950
951void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
952 LocationSummary* locations = new (GetGraph()->GetArena())
953 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
954 HInstruction* cond = deoptimize->InputAt(0);
955 DCHECK(cond->IsCondition());
956 if (cond->AsCondition()->NeedsMaterialization()) {
957 locations->SetInAt(0, Location::Any());
958 }
959}
960
961void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
962 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
963 DeoptimizationSlowPathX86(deoptimize);
964 codegen_->AddSlowPath(slow_path);
965 Label* slow_path_entry = slow_path->GetEntryLabel();
966 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
967}
968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000971}
972
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
974 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975}
976
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100978 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100982 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100989 switch (store->InputAt(1)->GetType()) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
998 break;
999
1000 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1003 break;
1004
1005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001008}
1009
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001010void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012}
1013
Roland Levillain0d37cd02015-05-27 16:39:19 +01001014void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001015 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001016 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001017 locations->SetInAt(0, Location::RequiresRegister());
1018 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001019 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001020 // We need a byte register.
1021 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001022 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023}
1024
Roland Levillain0d37cd02015-05-27 16:39:19 +01001025void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1026 if (cond->NeedsMaterialization()) {
1027 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001029 // Clear register: setcc only sets the low byte.
1030 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001031 Location lhs = locations->InAt(0);
1032 Location rhs = locations->InAt(1);
1033 if (rhs.IsRegister()) {
1034 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1035 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001036 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001037 if (constant == 0) {
1038 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1039 } else {
1040 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1041 }
Dave Allison20dfc792014-06-16 20:44:29 -07001042 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001043 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001044 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001045 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046 }
Dave Allison20dfc792014-06-16 20:44:29 -07001047}
1048
1049void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1050 VisitCondition(comp);
1051}
1052
1053void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1054 VisitCondition(comp);
1055}
1056
1057void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1058 VisitCondition(comp);
1059}
1060
1061void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1062 VisitCondition(comp);
1063}
1064
1065void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1066 VisitCondition(comp);
1067}
1068
1069void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1070 VisitCondition(comp);
1071}
1072
1073void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1074 VisitCondition(comp);
1075}
1076
1077void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1078 VisitCondition(comp);
1079}
1080
1081void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1082 VisitCondition(comp);
1083}
1084
1085void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1086 VisitCondition(comp);
1087}
1088
1089void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1090 VisitCondition(comp);
1091}
1092
1093void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1094 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095}
1096
1097void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001100 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101}
1102
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001103void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001105 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106}
1107
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001108void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1109 LocationSummary* locations =
1110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1111 locations->SetOut(Location::ConstantLocation(constant));
1112}
1113
1114void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1115 // Will be generated at use site.
1116 UNUSED(constant);
1117}
1118
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001122 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123}
1124
1125void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001127 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128}
1129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001130void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1133 locations->SetOut(Location::ConstantLocation(constant));
1134}
1135
1136void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1137 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139}
1140
1141void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1144 locations->SetOut(Location::ConstantLocation(constant));
1145}
1146
1147void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1148 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001149 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001150}
1151
Calin Juravle27df7582015-04-17 19:12:31 +01001152void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1153 memory_barrier->SetLocations(nullptr);
1154}
1155
1156void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1157 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1158}
1159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001160void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162}
1163
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001164void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001166 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001167}
1168
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001170 LocationSummary* locations =
1171 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 switch (ret->InputAt(0)->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001179 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181
1182 case Primitive::kPrimLong:
1183 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 break;
1186
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001187 case Primitive::kPrimFloat:
1188 case Primitive::kPrimDouble:
1189 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001190 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 break;
1192
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001198void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 if (kIsDebugBuild) {
1200 switch (ret->InputAt(0)->GetType()) {
1201 case Primitive::kPrimBoolean:
1202 case Primitive::kPrimByte:
1203 case Primitive::kPrimChar:
1204 case Primitive::kPrimShort:
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001207 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 break;
1209
1210 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1212 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 break;
1214
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 break;
1219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 }
1223 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001225}
1226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001228 // When we do not run baseline, explicit clinit checks triggered by static
1229 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1230 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001231
Mark Mendellfb8d2792015-03-31 22:16:59 -04001232 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001233 if (intrinsic.TryDispatch(invoke)) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001234 LocationSummary* locations = invoke->GetLocations();
1235 if (locations->CanCall()) {
1236 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::RequiresRegister());
1237 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001238 return;
1239 }
1240
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001241 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001242
1243 if (codegen_->IsBaseline()) {
1244 // Baseline does not have enough registers if the current method also
1245 // needs a register. We therefore do not require a register for it, and let
1246 // the code generation of the invoke handle it.
1247 LocationSummary* locations = invoke->GetLocations();
1248 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1249 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1250 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1251 }
1252 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001253}
1254
Mark Mendell09ed1a32015-03-25 08:30:06 -04001255static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1256 if (invoke->GetLocations()->Intrinsified()) {
1257 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1258 intrinsic.Dispatch(invoke);
1259 return true;
1260 }
1261 return false;
1262}
1263
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001264void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001265 // When we do not run baseline, explicit clinit checks triggered by static
1266 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1267 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001268
Mark Mendell09ed1a32015-03-25 08:30:06 -04001269 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1270 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001271 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001272
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001273 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001274 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001275 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001276 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277}
1278
1279void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1280 HandleInvoke(invoke);
1281}
1282
1283void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001284 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001285 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001286}
1287
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001288void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001290 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1291 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 LocationSummary* locations = invoke->GetLocations();
1293 Location receiver = locations->InAt(0);
1294 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001295 DCHECK(receiver.IsRegister());
1296 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001297 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001298 // temp = temp->GetMethodAt(method_offset);
1299 __ movl(temp, Address(temp, method_offset));
1300 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001301 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001302 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001303
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001304 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001305 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001306}
1307
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001308void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 HandleInvoke(invoke);
1310 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001311 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001312}
1313
1314void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1315 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001317 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1318 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319 LocationSummary* locations = invoke->GetLocations();
1320 Location receiver = locations->InAt(0);
1321 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1322
1323 // Set the hidden argument.
1324 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326
1327 // temp = object->GetClass();
1328 if (receiver.IsStackSlot()) {
1329 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1330 __ movl(temp, Address(temp, class_offset));
1331 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001332 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001334 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001335 // temp = temp->GetImtEntryAt(method_offset);
1336 __ movl(temp, Address(temp, method_offset));
1337 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001338 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001339 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001340
1341 DCHECK(!codegen_->IsLeafMethod());
1342 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1343}
1344
Roland Levillain88cb1752014-10-20 16:36:47 +01001345void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1346 LocationSummary* locations =
1347 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1348 switch (neg->GetResultType()) {
1349 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001350 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001351 locations->SetInAt(0, Location::RequiresRegister());
1352 locations->SetOut(Location::SameAsFirstInput());
1353 break;
1354
Roland Levillain88cb1752014-10-20 16:36:47 +01001355 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001356 locations->SetInAt(0, Location::RequiresFpuRegister());
1357 locations->SetOut(Location::SameAsFirstInput());
1358 locations->AddTemp(Location::RequiresRegister());
1359 locations->AddTemp(Location::RequiresFpuRegister());
1360 break;
1361
Roland Levillain88cb1752014-10-20 16:36:47 +01001362 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001363 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001364 locations->SetOut(Location::SameAsFirstInput());
1365 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1370 }
1371}
1372
1373void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1374 LocationSummary* locations = neg->GetLocations();
1375 Location out = locations->Out();
1376 Location in = locations->InAt(0);
1377 switch (neg->GetResultType()) {
1378 case Primitive::kPrimInt:
1379 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001382 break;
1383
1384 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001385 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001386 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001387 __ negl(out.AsRegisterPairLow<Register>());
1388 // Negation is similar to subtraction from zero. The least
1389 // significant byte triggers a borrow when it is different from
1390 // zero; to take it into account, add 1 to the most significant
1391 // byte if the carry flag (CF) is set to 1 after the first NEGL
1392 // operation.
1393 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1394 __ negl(out.AsRegisterPairHigh<Register>());
1395 break;
1396
Roland Levillain5368c212014-11-27 15:03:41 +00001397 case Primitive::kPrimFloat: {
1398 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 Register constant = locations->GetTemp(0).AsRegister<Register>();
1400 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001401 // Implement float negation with an exclusive or with value
1402 // 0x80000000 (mask for bit 31, representing the sign of a
1403 // single-precision floating-point number).
1404 __ movl(constant, Immediate(INT32_C(0x80000000)));
1405 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001407 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001408 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001409
Roland Levillain5368c212014-11-27 15:03:41 +00001410 case Primitive::kPrimDouble: {
1411 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001412 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001413 // Implement double negation with an exclusive or with value
1414 // 0x8000000000000000 (mask for bit 63, representing the sign of
1415 // a double-precision floating-point number).
1416 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001417 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001418 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001419 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001420
1421 default:
1422 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1423 }
1424}
1425
Roland Levillaindff1f282014-11-05 14:15:05 +00001426void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001427 Primitive::Type result_type = conversion->GetResultType();
1428 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001429 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001430
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001431 // The float-to-long and double-to-long type conversions rely on a
1432 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001433 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001434 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1435 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001436 ? LocationSummary::kCall
1437 : LocationSummary::kNoCall;
1438 LocationSummary* locations =
1439 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1440
David Brazdilb2bd1c52015-03-25 11:17:37 +00001441 // The Java language does not allow treating boolean as an integral type but
1442 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001443
Roland Levillaindff1f282014-11-05 14:15:05 +00001444 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001445 case Primitive::kPrimByte:
1446 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001447 case Primitive::kPrimBoolean:
1448 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001449 case Primitive::kPrimShort:
1450 case Primitive::kPrimInt:
1451 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001452 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001453 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1454 // Make the output overlap to please the register allocator. This greatly simplifies
1455 // the validation of the linear scan implementation
1456 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001457 break;
1458
1459 default:
1460 LOG(FATAL) << "Unexpected type conversion from " << input_type
1461 << " to " << result_type;
1462 }
1463 break;
1464
Roland Levillain01a8d712014-11-14 16:27:39 +00001465 case Primitive::kPrimShort:
1466 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001467 case Primitive::kPrimBoolean:
1468 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001469 case Primitive::kPrimByte:
1470 case Primitive::kPrimInt:
1471 case Primitive::kPrimChar:
1472 // Processing a Dex `int-to-short' instruction.
1473 locations->SetInAt(0, Location::Any());
1474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1475 break;
1476
1477 default:
1478 LOG(FATAL) << "Unexpected type conversion from " << input_type
1479 << " to " << result_type;
1480 }
1481 break;
1482
Roland Levillain946e1432014-11-11 17:35:19 +00001483 case Primitive::kPrimInt:
1484 switch (input_type) {
1485 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001486 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001487 locations->SetInAt(0, Location::Any());
1488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1489 break;
1490
1491 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001492 // Processing a Dex `float-to-int' instruction.
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetOut(Location::RequiresRegister());
1495 locations->AddTemp(Location::RequiresFpuRegister());
1496 break;
1497
Roland Levillain946e1432014-11-11 17:35:19 +00001498 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001499 // Processing a Dex `double-to-int' instruction.
1500 locations->SetInAt(0, Location::RequiresFpuRegister());
1501 locations->SetOut(Location::RequiresRegister());
1502 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001503 break;
1504
1505 default:
1506 LOG(FATAL) << "Unexpected type conversion from " << input_type
1507 << " to " << result_type;
1508 }
1509 break;
1510
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 case Primitive::kPrimLong:
1512 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001513 case Primitive::kPrimBoolean:
1514 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001515 case Primitive::kPrimByte:
1516 case Primitive::kPrimShort:
1517 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001518 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001519 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001520 locations->SetInAt(0, Location::RegisterLocation(EAX));
1521 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1522 break;
1523
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001524 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001526 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001528 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1529 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1530
Vladimir Marko949c91f2015-01-27 10:48:44 +00001531 // The runtime helper puts the result in EAX, EDX.
1532 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001533 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001534 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540 break;
1541
Roland Levillain981e4542014-11-14 11:47:14 +00001542 case Primitive::kPrimChar:
1543 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001544 case Primitive::kPrimBoolean:
1545 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001546 case Primitive::kPrimByte:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001549 // Processing a Dex `int-to-char' instruction.
1550 locations->SetInAt(0, Location::Any());
1551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1552 break;
1553
1554 default:
1555 LOG(FATAL) << "Unexpected type conversion from " << input_type
1556 << " to " << result_type;
1557 }
1558 break;
1559
Roland Levillaindff1f282014-11-05 14:15:05 +00001560 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001561 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001562 case Primitive::kPrimBoolean:
1563 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001564 case Primitive::kPrimByte:
1565 case Primitive::kPrimShort:
1566 case Primitive::kPrimInt:
1567 case Primitive::kPrimChar:
1568 // Processing a Dex `int-to-float' instruction.
1569 locations->SetInAt(0, Location::RequiresRegister());
1570 locations->SetOut(Location::RequiresFpuRegister());
1571 break;
1572
1573 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001574 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001575 locations->SetInAt(0, Location::Any());
1576 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001577 break;
1578
Roland Levillaincff13742014-11-17 14:32:17 +00001579 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001580 // Processing a Dex `double-to-float' instruction.
1581 locations->SetInAt(0, Location::RequiresFpuRegister());
1582 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001583 break;
1584
1585 default:
1586 LOG(FATAL) << "Unexpected type conversion from " << input_type
1587 << " to " << result_type;
1588 };
1589 break;
1590
Roland Levillaindff1f282014-11-05 14:15:05 +00001591 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001592 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001593 case Primitive::kPrimBoolean:
1594 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001595 case Primitive::kPrimByte:
1596 case Primitive::kPrimShort:
1597 case Primitive::kPrimInt:
1598 case Primitive::kPrimChar:
1599 // Processing a Dex `int-to-double' instruction.
1600 locations->SetInAt(0, Location::RequiresRegister());
1601 locations->SetOut(Location::RequiresFpuRegister());
1602 break;
1603
1604 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001605 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001606 locations->SetInAt(0, Location::Any());
1607 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001608 break;
1609
Roland Levillaincff13742014-11-17 14:32:17 +00001610 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001611 // Processing a Dex `float-to-double' instruction.
1612 locations->SetInAt(0, Location::RequiresFpuRegister());
1613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001620 break;
1621
1622 default:
1623 LOG(FATAL) << "Unexpected type conversion from " << input_type
1624 << " to " << result_type;
1625 }
1626}
1627
1628void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1629 LocationSummary* locations = conversion->GetLocations();
1630 Location out = locations->Out();
1631 Location in = locations->InAt(0);
1632 Primitive::Type result_type = conversion->GetResultType();
1633 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001634 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001635 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001636 case Primitive::kPrimByte:
1637 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001638 case Primitive::kPrimBoolean:
1639 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001640 case Primitive::kPrimShort:
1641 case Primitive::kPrimInt:
1642 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001643 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001644 if (in.IsRegister()) {
1645 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001646 } else {
1647 DCHECK(in.GetConstant()->IsIntConstant());
1648 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1649 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1650 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001651 break;
1652
1653 default:
1654 LOG(FATAL) << "Unexpected type conversion from " << input_type
1655 << " to " << result_type;
1656 }
1657 break;
1658
Roland Levillain01a8d712014-11-14 16:27:39 +00001659 case Primitive::kPrimShort:
1660 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001661 case Primitive::kPrimBoolean:
1662 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 case Primitive::kPrimByte:
1664 case Primitive::kPrimInt:
1665 case Primitive::kPrimChar:
1666 // Processing a Dex `int-to-short' instruction.
1667 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001669 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001671 } else {
1672 DCHECK(in.GetConstant()->IsIntConstant());
1673 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001674 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001675 }
1676 break;
1677
1678 default:
1679 LOG(FATAL) << "Unexpected type conversion from " << input_type
1680 << " to " << result_type;
1681 }
1682 break;
1683
Roland Levillain946e1432014-11-11 17:35:19 +00001684 case Primitive::kPrimInt:
1685 switch (input_type) {
1686 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001687 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001688 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001689 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001690 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001692 } else {
1693 DCHECK(in.IsConstant());
1694 DCHECK(in.GetConstant()->IsLongConstant());
1695 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001697 }
1698 break;
1699
Roland Levillain3f8f9362014-12-02 17:45:01 +00001700 case Primitive::kPrimFloat: {
1701 // Processing a Dex `float-to-int' instruction.
1702 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1703 Register output = out.AsRegister<Register>();
1704 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1705 Label done, nan;
1706
1707 __ movl(output, Immediate(kPrimIntMax));
1708 // temp = int-to-float(output)
1709 __ cvtsi2ss(temp, output);
1710 // if input >= temp goto done
1711 __ comiss(input, temp);
1712 __ j(kAboveEqual, &done);
1713 // if input == NaN goto nan
1714 __ j(kUnordered, &nan);
1715 // output = float-to-int-truncate(input)
1716 __ cvttss2si(output, input);
1717 __ jmp(&done);
1718 __ Bind(&nan);
1719 // output = 0
1720 __ xorl(output, output);
1721 __ Bind(&done);
1722 break;
1723 }
1724
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001725 case Primitive::kPrimDouble: {
1726 // Processing a Dex `double-to-int' instruction.
1727 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1728 Register output = out.AsRegister<Register>();
1729 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1730 Label done, nan;
1731
1732 __ movl(output, Immediate(kPrimIntMax));
1733 // temp = int-to-double(output)
1734 __ cvtsi2sd(temp, output);
1735 // if input >= temp goto done
1736 __ comisd(input, temp);
1737 __ j(kAboveEqual, &done);
1738 // if input == NaN goto nan
1739 __ j(kUnordered, &nan);
1740 // output = double-to-int-truncate(input)
1741 __ cvttsd2si(output, input);
1742 __ jmp(&done);
1743 __ Bind(&nan);
1744 // output = 0
1745 __ xorl(output, output);
1746 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001747 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001748 }
Roland Levillain946e1432014-11-11 17:35:19 +00001749
1750 default:
1751 LOG(FATAL) << "Unexpected type conversion from " << input_type
1752 << " to " << result_type;
1753 }
1754 break;
1755
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 case Primitive::kPrimLong:
1757 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001758 case Primitive::kPrimBoolean:
1759 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 case Primitive::kPrimByte:
1761 case Primitive::kPrimShort:
1762 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001763 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001764 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1766 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001768 __ cdq();
1769 break;
1770
1771 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001772 // Processing a Dex `float-to-long' instruction.
1773 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001774 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001778 // Processing a Dex `double-to-long' instruction.
1779 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1780 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001781 break;
1782
1783 default:
1784 LOG(FATAL) << "Unexpected type conversion from " << input_type
1785 << " to " << result_type;
1786 }
1787 break;
1788
Roland Levillain981e4542014-11-14 11:47:14 +00001789 case Primitive::kPrimChar:
1790 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001791 case Primitive::kPrimBoolean:
1792 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001793 case Primitive::kPrimByte:
1794 case Primitive::kPrimShort:
1795 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001796 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1797 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001799 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001800 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001801 } else {
1802 DCHECK(in.GetConstant()->IsIntConstant());
1803 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001804 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001805 }
1806 break;
1807
1808 default:
1809 LOG(FATAL) << "Unexpected type conversion from " << input_type
1810 << " to " << result_type;
1811 }
1812 break;
1813
Roland Levillaindff1f282014-11-05 14:15:05 +00001814 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001815 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001816 case Primitive::kPrimBoolean:
1817 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001818 case Primitive::kPrimByte:
1819 case Primitive::kPrimShort:
1820 case Primitive::kPrimInt:
1821 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001823 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001824 break;
1825
Roland Levillain6d0e4832014-11-27 18:31:21 +00001826 case Primitive::kPrimLong: {
1827 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001828 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001829
Roland Levillain232ade02015-04-20 15:14:36 +01001830 // Create stack space for the call to
1831 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1832 // TODO: enhance register allocator to ask for stack temporaries.
1833 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1834 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1835 __ subl(ESP, Immediate(adjustment));
1836 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001837
Roland Levillain232ade02015-04-20 15:14:36 +01001838 // Load the value to the FP stack, using temporaries if needed.
1839 PushOntoFPStack(in, 0, adjustment, false, true);
1840
1841 if (out.IsStackSlot()) {
1842 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1843 } else {
1844 __ fstps(Address(ESP, 0));
1845 Location stack_temp = Location::StackSlot(0);
1846 codegen_->Move32(out, stack_temp);
1847 }
1848
1849 // Remove the temporary stack space we allocated.
1850 if (adjustment != 0) {
1851 __ addl(ESP, Immediate(adjustment));
1852 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001853 break;
1854 }
1855
Roland Levillaincff13742014-11-17 14:32:17 +00001856 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001857 // Processing a Dex `double-to-float' instruction.
1858 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001859 break;
1860
1861 default:
1862 LOG(FATAL) << "Unexpected type conversion from " << input_type
1863 << " to " << result_type;
1864 };
1865 break;
1866
Roland Levillaindff1f282014-11-05 14:15:05 +00001867 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001868 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001869 case Primitive::kPrimBoolean:
1870 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimInt:
1874 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001875 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001877 break;
1878
Roland Levillain647b9ed2014-11-27 12:06:00 +00001879 case Primitive::kPrimLong: {
1880 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001881 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001882
Roland Levillain232ade02015-04-20 15:14:36 +01001883 // Create stack space for the call to
1884 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1885 // TODO: enhance register allocator to ask for stack temporaries.
1886 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1887 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1888 __ subl(ESP, Immediate(adjustment));
1889 }
1890
1891 // Load the value to the FP stack, using temporaries if needed.
1892 PushOntoFPStack(in, 0, adjustment, false, true);
1893
1894 if (out.IsDoubleStackSlot()) {
1895 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1896 } else {
1897 __ fstpl(Address(ESP, 0));
1898 Location stack_temp = Location::DoubleStackSlot(0);
1899 codegen_->Move64(out, stack_temp);
1900 }
1901
1902 // Remove the temporary stack space we allocated.
1903 if (adjustment != 0) {
1904 __ addl(ESP, Immediate(adjustment));
1905 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001906 break;
1907 }
1908
Roland Levillaincff13742014-11-17 14:32:17 +00001909 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001910 // Processing a Dex `float-to-double' instruction.
1911 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001918 break;
1919
1920 default:
1921 LOG(FATAL) << "Unexpected type conversion from " << input_type
1922 << " to " << result_type;
1923 }
1924}
1925
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001926void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001927 LocationSummary* locations =
1928 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001929 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001930 case Primitive::kPrimInt: {
1931 locations->SetInAt(0, Location::RequiresRegister());
1932 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1934 break;
1935 }
1936
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001938 locations->SetInAt(0, Location::RequiresRegister());
1939 locations->SetInAt(1, Location::Any());
1940 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
1942 }
1943
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 case Primitive::kPrimFloat:
1945 case Primitive::kPrimDouble: {
1946 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001947 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1954 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001956}
1957
1958void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1959 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 Location first = locations->InAt(0);
1961 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001962 Location out = locations->Out();
1963
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001964 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001967 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1968 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001969 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1970 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001971 } else {
1972 __ leal(out.AsRegister<Register>(), Address(
1973 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1974 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001975 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001976 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1977 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1978 __ addl(out.AsRegister<Register>(), Immediate(value));
1979 } else {
1980 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1981 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001982 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001983 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001985 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001986 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001987 }
1988
1989 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001990 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001991 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1992 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001993 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001994 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1995 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001996 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001997 } else {
1998 DCHECK(second.IsConstant()) << second;
1999 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2000 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2001 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002002 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002003 break;
2004 }
2005
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002006 case Primitive::kPrimFloat: {
2007 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002008 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002011 }
2012
2013 case Primitive::kPrimDouble: {
2014 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002015 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002016 }
2017 break;
2018 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002019
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002020 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002021 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002022 }
2023}
2024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002025void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002028 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002029 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002031 locations->SetInAt(0, Location::RequiresRegister());
2032 locations->SetInAt(1, Location::Any());
2033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034 break;
2035 }
Calin Juravle11351682014-10-23 15:38:15 +01002036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 locations->SetInAt(0, Location::RequiresFpuRegister());
2039 locations->SetInAt(1, Location::RequiresFpuRegister());
2040 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041 break;
Calin Juravle11351682014-10-23 15:38:15 +01002042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002044 default:
Calin Juravle11351682014-10-23 15:38:15 +01002045 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002046 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002047}
2048
2049void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2050 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 Location first = locations->InAt(0);
2052 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002053 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002054 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002055 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002057 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002058 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002059 __ subl(first.AsRegister<Register>(),
2060 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002062 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002063 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002064 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002065 }
2066
2067 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002068 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002069 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2070 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002071 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002072 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002073 __ sbbl(first.AsRegisterPairHigh<Register>(),
2074 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002075 } else {
2076 DCHECK(second.IsConstant()) << second;
2077 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2078 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2079 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002080 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 break;
2082 }
2083
Calin Juravle11351682014-10-23 15:38:15 +01002084 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002086 break;
Calin Juravle11351682014-10-23 15:38:15 +01002087 }
2088
2089 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002091 break;
2092 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002093
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002094 default:
Calin Juravle11351682014-10-23 15:38:15 +01002095 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002096 }
2097}
2098
Calin Juravle34bacdf2014-10-07 20:23:36 +01002099void LocationsBuilderX86::VisitMul(HMul* mul) {
2100 LocationSummary* locations =
2101 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2102 switch (mul->GetResultType()) {
2103 case Primitive::kPrimInt:
2104 locations->SetInAt(0, Location::RequiresRegister());
2105 locations->SetInAt(1, Location::Any());
2106 locations->SetOut(Location::SameAsFirstInput());
2107 break;
2108 case Primitive::kPrimLong: {
2109 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002110 locations->SetInAt(1, Location::Any());
2111 locations->SetOut(Location::SameAsFirstInput());
2112 // Needed for imul on 32bits with 64bits output.
2113 locations->AddTemp(Location::RegisterLocation(EAX));
2114 locations->AddTemp(Location::RegisterLocation(EDX));
2115 break;
2116 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002117 case Primitive::kPrimFloat:
2118 case Primitive::kPrimDouble: {
2119 locations->SetInAt(0, Location::RequiresFpuRegister());
2120 locations->SetInAt(1, Location::RequiresFpuRegister());
2121 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002122 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002123 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002124
2125 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002126 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002127 }
2128}
2129
2130void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2131 LocationSummary* locations = mul->GetLocations();
2132 Location first = locations->InAt(0);
2133 Location second = locations->InAt(1);
2134 DCHECK(first.Equals(locations->Out()));
2135
2136 switch (mul->GetResultType()) {
2137 case Primitive::kPrimInt: {
2138 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 } else if (second.IsConstant()) {
2141 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002143 } else {
2144 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002146 }
2147 break;
2148 }
2149
2150 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002151 Register in1_hi = first.AsRegisterPairHigh<Register>();
2152 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 Register eax = locations->GetTemp(0).AsRegister<Register>();
2154 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155
2156 DCHECK_EQ(EAX, eax);
2157 DCHECK_EQ(EDX, edx);
2158
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002159 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002160 // output: in1
2161 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2162 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2163 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002164 if (second.IsConstant()) {
2165 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002166
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002167 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2168 int32_t low_value = Low32Bits(value);
2169 int32_t high_value = High32Bits(value);
2170 Immediate low(low_value);
2171 Immediate high(high_value);
2172
2173 __ movl(eax, high);
2174 // eax <- in1.lo * in2.hi
2175 __ imull(eax, in1_lo);
2176 // in1.hi <- in1.hi * in2.lo
2177 __ imull(in1_hi, low);
2178 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2179 __ addl(in1_hi, eax);
2180 // move in2_lo to eax to prepare for double precision
2181 __ movl(eax, low);
2182 // edx:eax <- in1.lo * in2.lo
2183 __ mull(in1_lo);
2184 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2185 __ addl(in1_hi, edx);
2186 // in1.lo <- (in1.lo * in2.lo)[31:0];
2187 __ movl(in1_lo, eax);
2188 } else if (second.IsRegisterPair()) {
2189 Register in2_hi = second.AsRegisterPairHigh<Register>();
2190 Register in2_lo = second.AsRegisterPairLow<Register>();
2191
2192 __ movl(eax, in2_hi);
2193 // eax <- in1.lo * in2.hi
2194 __ imull(eax, in1_lo);
2195 // in1.hi <- in1.hi * in2.lo
2196 __ imull(in1_hi, in2_lo);
2197 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2198 __ addl(in1_hi, eax);
2199 // move in1_lo to eax to prepare for double precision
2200 __ movl(eax, in1_lo);
2201 // edx:eax <- in1.lo * in2.lo
2202 __ mull(in2_lo);
2203 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2204 __ addl(in1_hi, edx);
2205 // in1.lo <- (in1.lo * in2.lo)[31:0];
2206 __ movl(in1_lo, eax);
2207 } else {
2208 DCHECK(second.IsDoubleStackSlot()) << second;
2209 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2210 Address in2_lo(ESP, second.GetStackIndex());
2211
2212 __ movl(eax, in2_hi);
2213 // eax <- in1.lo * in2.hi
2214 __ imull(eax, in1_lo);
2215 // in1.hi <- in1.hi * in2.lo
2216 __ imull(in1_hi, in2_lo);
2217 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2218 __ addl(in1_hi, eax);
2219 // move in1_lo to eax to prepare for double precision
2220 __ movl(eax, in1_lo);
2221 // edx:eax <- in1.lo * in2.lo
2222 __ mull(in2_lo);
2223 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2224 __ addl(in1_hi, edx);
2225 // in1.lo <- (in1.lo * in2.lo)[31:0];
2226 __ movl(in1_lo, eax);
2227 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002228
2229 break;
2230 }
2231
Calin Juravleb5bfa962014-10-21 18:02:24 +01002232 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002233 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002234 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002235 }
2236
2237 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002239 break;
2240 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002241
2242 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002243 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002244 }
2245}
2246
Roland Levillain232ade02015-04-20 15:14:36 +01002247void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2248 uint32_t temp_offset,
2249 uint32_t stack_adjustment,
2250 bool is_fp,
2251 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002252 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002253 DCHECK(!is_wide);
2254 if (is_fp) {
2255 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2256 } else {
2257 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2258 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002259 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002260 DCHECK(is_wide);
2261 if (is_fp) {
2262 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2263 } else {
2264 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2265 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002266 } else {
2267 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002268 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002269 Location stack_temp = Location::StackSlot(temp_offset);
2270 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002271 if (is_fp) {
2272 __ flds(Address(ESP, temp_offset));
2273 } else {
2274 __ filds(Address(ESP, temp_offset));
2275 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002276 } else {
2277 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2278 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002279 if (is_fp) {
2280 __ fldl(Address(ESP, temp_offset));
2281 } else {
2282 __ fildl(Address(ESP, temp_offset));
2283 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002284 }
2285 }
2286}
2287
2288void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2289 Primitive::Type type = rem->GetResultType();
2290 bool is_float = type == Primitive::kPrimFloat;
2291 size_t elem_size = Primitive::ComponentSize(type);
2292 LocationSummary* locations = rem->GetLocations();
2293 Location first = locations->InAt(0);
2294 Location second = locations->InAt(1);
2295 Location out = locations->Out();
2296
2297 // Create stack space for 2 elements.
2298 // TODO: enhance register allocator to ask for stack temporaries.
2299 __ subl(ESP, Immediate(2 * elem_size));
2300
2301 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002302 const bool is_wide = !is_float;
2303 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2304 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002305
2306 // Loop doing FPREM until we stabilize.
2307 Label retry;
2308 __ Bind(&retry);
2309 __ fprem();
2310
2311 // Move FP status to AX.
2312 __ fstsw();
2313
2314 // And see if the argument reduction is complete. This is signaled by the
2315 // C2 FPU flag bit set to 0.
2316 __ andl(EAX, Immediate(kC2ConditionMask));
2317 __ j(kNotEqual, &retry);
2318
2319 // We have settled on the final value. Retrieve it into an XMM register.
2320 // Store FP top of stack to real stack.
2321 if (is_float) {
2322 __ fsts(Address(ESP, 0));
2323 } else {
2324 __ fstl(Address(ESP, 0));
2325 }
2326
2327 // Pop the 2 items from the FP stack.
2328 __ fucompp();
2329
2330 // Load the value from the stack into an XMM register.
2331 DCHECK(out.IsFpuRegister()) << out;
2332 if (is_float) {
2333 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2334 } else {
2335 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2336 }
2337
2338 // And remove the temporary stack space we allocated.
2339 __ addl(ESP, Immediate(2 * elem_size));
2340}
2341
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002342
2343void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2344 DCHECK(instruction->IsDiv() || instruction->IsRem());
2345
2346 LocationSummary* locations = instruction->GetLocations();
2347 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002348 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002349
2350 Register out_register = locations->Out().AsRegister<Register>();
2351 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002352 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002353
2354 DCHECK(imm == 1 || imm == -1);
2355
2356 if (instruction->IsRem()) {
2357 __ xorl(out_register, out_register);
2358 } else {
2359 __ movl(out_register, input_register);
2360 if (imm == -1) {
2361 __ negl(out_register);
2362 }
2363 }
2364}
2365
2366
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002367void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002368 LocationSummary* locations = instruction->GetLocations();
2369
2370 Register out_register = locations->Out().AsRegister<Register>();
2371 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002372 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002373
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002374 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002375 Register num = locations->GetTemp(0).AsRegister<Register>();
2376
2377 __ leal(num, Address(input_register, std::abs(imm) - 1));
2378 __ testl(input_register, input_register);
2379 __ cmovl(kGreaterEqual, num, input_register);
2380 int shift = CTZ(imm);
2381 __ sarl(num, Immediate(shift));
2382
2383 if (imm < 0) {
2384 __ negl(num);
2385 }
2386
2387 __ movl(out_register, num);
2388}
2389
2390void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2391 DCHECK(instruction->IsDiv() || instruction->IsRem());
2392
2393 LocationSummary* locations = instruction->GetLocations();
2394 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2395
2396 Register eax = locations->InAt(0).AsRegister<Register>();
2397 Register out = locations->Out().AsRegister<Register>();
2398 Register num;
2399 Register edx;
2400
2401 if (instruction->IsDiv()) {
2402 edx = locations->GetTemp(0).AsRegister<Register>();
2403 num = locations->GetTemp(1).AsRegister<Register>();
2404 } else {
2405 edx = locations->Out().AsRegister<Register>();
2406 num = locations->GetTemp(0).AsRegister<Register>();
2407 }
2408
2409 DCHECK_EQ(EAX, eax);
2410 DCHECK_EQ(EDX, edx);
2411 if (instruction->IsDiv()) {
2412 DCHECK_EQ(EAX, out);
2413 } else {
2414 DCHECK_EQ(EDX, out);
2415 }
2416
2417 int64_t magic;
2418 int shift;
2419 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2420
2421 Label ndiv;
2422 Label end;
2423 // If numerator is 0, the result is 0, no computation needed.
2424 __ testl(eax, eax);
2425 __ j(kNotEqual, &ndiv);
2426
2427 __ xorl(out, out);
2428 __ jmp(&end);
2429
2430 __ Bind(&ndiv);
2431
2432 // Save the numerator.
2433 __ movl(num, eax);
2434
2435 // EAX = magic
2436 __ movl(eax, Immediate(magic));
2437
2438 // EDX:EAX = magic * numerator
2439 __ imull(num);
2440
2441 if (imm > 0 && magic < 0) {
2442 // EDX += num
2443 __ addl(edx, num);
2444 } else if (imm < 0 && magic > 0) {
2445 __ subl(edx, num);
2446 }
2447
2448 // Shift if needed.
2449 if (shift != 0) {
2450 __ sarl(edx, Immediate(shift));
2451 }
2452
2453 // EDX += 1 if EDX < 0
2454 __ movl(eax, edx);
2455 __ shrl(edx, Immediate(31));
2456 __ addl(edx, eax);
2457
2458 if (instruction->IsRem()) {
2459 __ movl(eax, num);
2460 __ imull(edx, Immediate(imm));
2461 __ subl(eax, edx);
2462 __ movl(edx, eax);
2463 } else {
2464 __ movl(eax, edx);
2465 }
2466 __ Bind(&end);
2467}
2468
Calin Juravlebacfec32014-11-14 15:54:36 +00002469void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2470 DCHECK(instruction->IsDiv() || instruction->IsRem());
2471
2472 LocationSummary* locations = instruction->GetLocations();
2473 Location out = locations->Out();
2474 Location first = locations->InAt(0);
2475 Location second = locations->InAt(1);
2476 bool is_div = instruction->IsDiv();
2477
2478 switch (instruction->GetResultType()) {
2479 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002480 DCHECK_EQ(EAX, first.AsRegister<Register>());
2481 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002482
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002483 if (instruction->InputAt(1)->IsIntConstant()) {
2484 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002485
2486 if (imm == 0) {
2487 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2488 } else if (imm == 1 || imm == -1) {
2489 DivRemOneOrMinusOne(instruction);
2490 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002491 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002492 } else {
2493 DCHECK(imm <= -2 || imm >= 2);
2494 GenerateDivRemWithAnyConstant(instruction);
2495 }
2496 } else {
2497 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002498 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002499 is_div);
2500 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002501
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002502 Register second_reg = second.AsRegister<Register>();
2503 // 0x80000000/-1 triggers an arithmetic exception!
2504 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2505 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002506
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002507 __ cmpl(second_reg, Immediate(-1));
2508 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002509
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002510 // edx:eax <- sign-extended of eax
2511 __ cdq();
2512 // eax = quotient, edx = remainder
2513 __ idivl(second_reg);
2514 __ Bind(slow_path->GetExitLabel());
2515 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002516 break;
2517 }
2518
2519 case Primitive::kPrimLong: {
2520 InvokeRuntimeCallingConvention calling_convention;
2521 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2522 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2523 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2524 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2525 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2526 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2527
2528 if (is_div) {
2529 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2530 } else {
2531 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2532 }
2533 uint32_t dex_pc = is_div
2534 ? instruction->AsDiv()->GetDexPc()
2535 : instruction->AsRem()->GetDexPc();
2536 codegen_->RecordPcInfo(instruction, dex_pc);
2537
2538 break;
2539 }
2540
2541 default:
2542 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2543 }
2544}
2545
Calin Juravle7c4954d2014-10-28 16:57:40 +00002546void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002547 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002548 ? LocationSummary::kCall
2549 : LocationSummary::kNoCall;
2550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2551
Calin Juravle7c4954d2014-10-28 16:57:40 +00002552 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002553 case Primitive::kPrimInt: {
2554 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002555 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002556 locations->SetOut(Location::SameAsFirstInput());
2557 // Intel uses edx:eax as the dividend.
2558 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002559 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2560 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2561 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002562 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002563 locations->AddTemp(Location::RequiresRegister());
2564 }
Calin Juravled0d48522014-11-04 16:40:20 +00002565 break;
2566 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002567 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002568 InvokeRuntimeCallingConvention calling_convention;
2569 locations->SetInAt(0, Location::RegisterPairLocation(
2570 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2571 locations->SetInAt(1, Location::RegisterPairLocation(
2572 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2573 // Runtime helper puts the result in EAX, EDX.
2574 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002575 break;
2576 }
2577 case Primitive::kPrimFloat:
2578 case Primitive::kPrimDouble: {
2579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetInAt(1, Location::RequiresFpuRegister());
2581 locations->SetOut(Location::SameAsFirstInput());
2582 break;
2583 }
2584
2585 default:
2586 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2587 }
2588}
2589
2590void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2591 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002592 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593 Location first = locations->InAt(0);
2594 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002595
2596 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002597 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002599 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002600 break;
2601 }
2602
2603 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002604 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002606 break;
2607 }
2608
2609 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002610 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002612 break;
2613 }
2614
2615 default:
2616 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2617 }
2618}
2619
Calin Juravlebacfec32014-11-14 15:54:36 +00002620void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002621 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002622
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002623 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2624 ? LocationSummary::kCall
2625 : LocationSummary::kNoCall;
2626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002627
Calin Juravled2ec87d2014-12-08 14:24:46 +00002628 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002629 case Primitive::kPrimInt: {
2630 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002631 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002632 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002633 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2634 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2635 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002636 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002637 locations->AddTemp(Location::RequiresRegister());
2638 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002639 break;
2640 }
2641 case Primitive::kPrimLong: {
2642 InvokeRuntimeCallingConvention calling_convention;
2643 locations->SetInAt(0, Location::RegisterPairLocation(
2644 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2645 locations->SetInAt(1, Location::RegisterPairLocation(
2646 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2647 // Runtime helper puts the result in EAX, EDX.
2648 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2649 break;
2650 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002651 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002652 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002653 locations->SetInAt(0, Location::Any());
2654 locations->SetInAt(1, Location::Any());
2655 locations->SetOut(Location::RequiresFpuRegister());
2656 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002657 break;
2658 }
2659
2660 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002661 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002662 }
2663}
2664
2665void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2666 Primitive::Type type = rem->GetResultType();
2667 switch (type) {
2668 case Primitive::kPrimInt:
2669 case Primitive::kPrimLong: {
2670 GenerateDivRemIntegral(rem);
2671 break;
2672 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002673 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002674 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002675 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002676 break;
2677 }
2678 default:
2679 LOG(FATAL) << "Unexpected rem type " << type;
2680 }
2681}
2682
Calin Juravled0d48522014-11-04 16:40:20 +00002683void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2684 LocationSummary* locations =
2685 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002686 switch (instruction->GetType()) {
2687 case Primitive::kPrimInt: {
2688 locations->SetInAt(0, Location::Any());
2689 break;
2690 }
2691 case Primitive::kPrimLong: {
2692 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2693 if (!instruction->IsConstant()) {
2694 locations->AddTemp(Location::RequiresRegister());
2695 }
2696 break;
2697 }
2698 default:
2699 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2700 }
Calin Juravled0d48522014-11-04 16:40:20 +00002701 if (instruction->HasUses()) {
2702 locations->SetOut(Location::SameAsFirstInput());
2703 }
2704}
2705
2706void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2707 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2708 codegen_->AddSlowPath(slow_path);
2709
2710 LocationSummary* locations = instruction->GetLocations();
2711 Location value = locations->InAt(0);
2712
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002713 switch (instruction->GetType()) {
2714 case Primitive::kPrimInt: {
2715 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002717 __ j(kEqual, slow_path->GetEntryLabel());
2718 } else if (value.IsStackSlot()) {
2719 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2720 __ j(kEqual, slow_path->GetEntryLabel());
2721 } else {
2722 DCHECK(value.IsConstant()) << value;
2723 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2724 __ jmp(slow_path->GetEntryLabel());
2725 }
2726 }
2727 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002728 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002729 case Primitive::kPrimLong: {
2730 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002732 __ movl(temp, value.AsRegisterPairLow<Register>());
2733 __ orl(temp, value.AsRegisterPairHigh<Register>());
2734 __ j(kEqual, slow_path->GetEntryLabel());
2735 } else {
2736 DCHECK(value.IsConstant()) << value;
2737 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2738 __ jmp(slow_path->GetEntryLabel());
2739 }
2740 }
2741 break;
2742 }
2743 default:
2744 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002745 }
Calin Juravled0d48522014-11-04 16:40:20 +00002746}
2747
Calin Juravle9aec02f2014-11-18 23:06:35 +00002748void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2749 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2750
2751 LocationSummary* locations =
2752 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2753
2754 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002755 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002756 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002757 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002758 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002759 // The shift count needs to be in CL or a constant.
2760 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002761 locations->SetOut(Location::SameAsFirstInput());
2762 break;
2763 }
2764 default:
2765 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2766 }
2767}
2768
2769void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2770 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2771
2772 LocationSummary* locations = op->GetLocations();
2773 Location first = locations->InAt(0);
2774 Location second = locations->InAt(1);
2775 DCHECK(first.Equals(locations->Out()));
2776
2777 switch (op->GetResultType()) {
2778 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002779 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002780 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002781 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002783 DCHECK_EQ(ECX, second_reg);
2784 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002785 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002786 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002787 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002788 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002789 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002790 }
2791 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002792 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2793 if (shift == 0) {
2794 return;
2795 }
2796 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002797 if (op->IsShl()) {
2798 __ shll(first_reg, imm);
2799 } else if (op->IsShr()) {
2800 __ sarl(first_reg, imm);
2801 } else {
2802 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002803 }
2804 }
2805 break;
2806 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002807 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002808 if (second.IsRegister()) {
2809 Register second_reg = second.AsRegister<Register>();
2810 DCHECK_EQ(ECX, second_reg);
2811 if (op->IsShl()) {
2812 GenerateShlLong(first, second_reg);
2813 } else if (op->IsShr()) {
2814 GenerateShrLong(first, second_reg);
2815 } else {
2816 GenerateUShrLong(first, second_reg);
2817 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002818 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002819 // Shift by a constant.
2820 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2821 // Nothing to do if the shift is 0, as the input is already the output.
2822 if (shift != 0) {
2823 if (op->IsShl()) {
2824 GenerateShlLong(first, shift);
2825 } else if (op->IsShr()) {
2826 GenerateShrLong(first, shift);
2827 } else {
2828 GenerateUShrLong(first, shift);
2829 }
2830 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002831 }
2832 break;
2833 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002834 default:
2835 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2836 }
2837}
2838
Mark P Mendell73945692015-04-29 14:56:17 +00002839void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2840 Register low = loc.AsRegisterPairLow<Register>();
2841 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002842 if (shift == 1) {
2843 // This is just an addition.
2844 __ addl(low, low);
2845 __ adcl(high, high);
2846 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002847 // Shift by 32 is easy. High gets low, and low gets 0.
2848 codegen_->EmitParallelMoves(
2849 loc.ToLow(),
2850 loc.ToHigh(),
2851 Primitive::kPrimInt,
2852 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2853 loc.ToLow(),
2854 Primitive::kPrimInt);
2855 } else if (shift > 32) {
2856 // Low part becomes 0. High part is low part << (shift-32).
2857 __ movl(high, low);
2858 __ shll(high, Immediate(shift - 32));
2859 __ xorl(low, low);
2860 } else {
2861 // Between 1 and 31.
2862 __ shld(high, low, Immediate(shift));
2863 __ shll(low, Immediate(shift));
2864 }
2865}
2866
Calin Juravle9aec02f2014-11-18 23:06:35 +00002867void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2868 Label done;
2869 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2870 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2871 __ testl(shifter, Immediate(32));
2872 __ j(kEqual, &done);
2873 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2874 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2875 __ Bind(&done);
2876}
2877
Mark P Mendell73945692015-04-29 14:56:17 +00002878void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2879 Register low = loc.AsRegisterPairLow<Register>();
2880 Register high = loc.AsRegisterPairHigh<Register>();
2881 if (shift == 32) {
2882 // Need to copy the sign.
2883 DCHECK_NE(low, high);
2884 __ movl(low, high);
2885 __ sarl(high, Immediate(31));
2886 } else if (shift > 32) {
2887 DCHECK_NE(low, high);
2888 // High part becomes sign. Low part is shifted by shift - 32.
2889 __ movl(low, high);
2890 __ sarl(high, Immediate(31));
2891 __ sarl(low, Immediate(shift - 32));
2892 } else {
2893 // Between 1 and 31.
2894 __ shrd(low, high, Immediate(shift));
2895 __ sarl(high, Immediate(shift));
2896 }
2897}
2898
Calin Juravle9aec02f2014-11-18 23:06:35 +00002899void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2900 Label done;
2901 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2902 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2903 __ testl(shifter, Immediate(32));
2904 __ j(kEqual, &done);
2905 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2906 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2907 __ Bind(&done);
2908}
2909
Mark P Mendell73945692015-04-29 14:56:17 +00002910void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2911 Register low = loc.AsRegisterPairLow<Register>();
2912 Register high = loc.AsRegisterPairHigh<Register>();
2913 if (shift == 32) {
2914 // Shift by 32 is easy. Low gets high, and high gets 0.
2915 codegen_->EmitParallelMoves(
2916 loc.ToHigh(),
2917 loc.ToLow(),
2918 Primitive::kPrimInt,
2919 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2920 loc.ToHigh(),
2921 Primitive::kPrimInt);
2922 } else if (shift > 32) {
2923 // Low part is high >> (shift - 32). High part becomes 0.
2924 __ movl(low, high);
2925 __ shrl(low, Immediate(shift - 32));
2926 __ xorl(high, high);
2927 } else {
2928 // Between 1 and 31.
2929 __ shrd(low, high, Immediate(shift));
2930 __ shrl(high, Immediate(shift));
2931 }
2932}
2933
Calin Juravle9aec02f2014-11-18 23:06:35 +00002934void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2935 Label done;
2936 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2937 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2938 __ testl(shifter, Immediate(32));
2939 __ j(kEqual, &done);
2940 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2941 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2942 __ Bind(&done);
2943}
2944
2945void LocationsBuilderX86::VisitShl(HShl* shl) {
2946 HandleShift(shl);
2947}
2948
2949void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2950 HandleShift(shl);
2951}
2952
2953void LocationsBuilderX86::VisitShr(HShr* shr) {
2954 HandleShift(shr);
2955}
2956
2957void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2958 HandleShift(shr);
2959}
2960
2961void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2962 HandleShift(ushr);
2963}
2964
2965void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2966 HandleShift(ushr);
2967}
2968
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002969void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002970 LocationSummary* locations =
2971 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002972 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002973 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002974 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002975 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002976}
2977
2978void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2979 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002980 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002981 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002982
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002983 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002984
Nicolas Geoffray39468442014-09-02 15:17:15 +01002985 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002986 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002987}
2988
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002989void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2990 LocationSummary* locations =
2991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2992 locations->SetOut(Location::RegisterLocation(EAX));
2993 InvokeRuntimeCallingConvention calling_convention;
2994 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00002995 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002996 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002997}
2998
2999void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3000 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray7b0e3532015-06-09 09:25:50 +00003001 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003002 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3003
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003004 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003005
3006 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3007 DCHECK(!codegen_->IsLeafMethod());
3008}
3009
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003010void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003011 LocationSummary* locations =
3012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003013 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3014 if (location.IsStackSlot()) {
3015 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3016 } else if (location.IsDoubleStackSlot()) {
3017 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003018 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003019 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003020}
3021
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003022void InstructionCodeGeneratorX86::VisitParameterValue(
3023 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3024}
3025
3026void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3027 LocationSummary* locations =
3028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3029 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3030}
3031
3032void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003033}
3034
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003035void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003036 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003037 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003040}
3041
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003042void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3043 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003044 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003045 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003046 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003047 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003048 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003049 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003050 break;
3051
3052 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003053 __ notl(out.AsRegisterPairLow<Register>());
3054 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003055 break;
3056
3057 default:
3058 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3059 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003060}
3061
David Brazdil66d126e2015-04-03 16:02:44 +01003062void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3063 LocationSummary* locations =
3064 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3065 locations->SetInAt(0, Location::RequiresRegister());
3066 locations->SetOut(Location::SameAsFirstInput());
3067}
3068
3069void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003070 LocationSummary* locations = bool_not->GetLocations();
3071 Location in = locations->InAt(0);
3072 Location out = locations->Out();
3073 DCHECK(in.Equals(out));
3074 __ xorl(out.AsRegister<Register>(), Immediate(1));
3075}
3076
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003077void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003080 switch (compare->InputAt(0)->GetType()) {
3081 case Primitive::kPrimLong: {
3082 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003083 locations->SetInAt(1, Location::Any());
3084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3085 break;
3086 }
3087 case Primitive::kPrimFloat:
3088 case Primitive::kPrimDouble: {
3089 locations->SetInAt(0, Location::RequiresFpuRegister());
3090 locations->SetInAt(1, Location::RequiresFpuRegister());
3091 locations->SetOut(Location::RequiresRegister());
3092 break;
3093 }
3094 default:
3095 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3096 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003097}
3098
3099void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003100 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003102 Location left = locations->InAt(0);
3103 Location right = locations->InAt(1);
3104
3105 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003106 switch (compare->InputAt(0)->GetType()) {
3107 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003108 Register left_low = left.AsRegisterPairLow<Register>();
3109 Register left_high = left.AsRegisterPairHigh<Register>();
3110 int32_t val_low = 0;
3111 int32_t val_high = 0;
3112 bool right_is_const = false;
3113
3114 if (right.IsConstant()) {
3115 DCHECK(right.GetConstant()->IsLongConstant());
3116 right_is_const = true;
3117 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3118 val_low = Low32Bits(val);
3119 val_high = High32Bits(val);
3120 }
3121
Calin Juravleddb7df22014-11-25 20:56:51 +00003122 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003123 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003124 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003125 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003126 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003127 DCHECK(right_is_const) << right;
3128 if (val_high == 0) {
3129 __ testl(left_high, left_high);
3130 } else {
3131 __ cmpl(left_high, Immediate(val_high));
3132 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003133 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003134 __ j(kLess, &less); // Signed compare.
3135 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003136 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003137 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003138 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003139 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003140 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003141 DCHECK(right_is_const) << right;
3142 if (val_low == 0) {
3143 __ testl(left_low, left_low);
3144 } else {
3145 __ cmpl(left_low, Immediate(val_low));
3146 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003147 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003148 break;
3149 }
3150 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003151 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003152 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3153 break;
3154 }
3155 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003156 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003157 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003158 break;
3159 }
3160 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003161 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003162 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003163 __ movl(out, Immediate(0));
3164 __ j(kEqual, &done);
3165 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3166
3167 __ Bind(&greater);
3168 __ movl(out, Immediate(1));
3169 __ jmp(&done);
3170
3171 __ Bind(&less);
3172 __ movl(out, Immediate(-1));
3173
3174 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003175}
3176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003177void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003178 LocationSummary* locations =
3179 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003180 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3181 locations->SetInAt(i, Location::Any());
3182 }
3183 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003184}
3185
3186void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003187 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003188 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003189}
3190
Calin Juravle52c48962014-12-16 17:02:57 +00003191void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3192 /*
3193 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3194 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3195 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3196 */
3197 switch (kind) {
3198 case MemBarrierKind::kAnyAny: {
3199 __ mfence();
3200 break;
3201 }
3202 case MemBarrierKind::kAnyStore:
3203 case MemBarrierKind::kLoadAny:
3204 case MemBarrierKind::kStoreStore: {
3205 // nop
3206 break;
3207 }
3208 default:
3209 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003210 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003211}
3212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003213
Mark Mendell09ed1a32015-03-25 08:30:06 -04003214void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003215 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003216 // TODO: Implement all kinds of calls:
3217 // 1) boot -> boot
3218 // 2) app -> boot
3219 // 3) app -> app
3220 //
3221 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003222
3223 if (invoke->IsStringInit()) {
3224 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003225 Register reg = temp.AsRegister<Register>();
3226 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003227 // (temp + offset_of_quick_compiled_code)()
3228 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003229 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3230 } else if (invoke->IsRecursive()) {
3231 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003232 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003233 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3234
3235 Register method_reg;
3236 Register reg = temp.AsRegister<Register>();
3237 if (current_method.IsRegister()) {
3238 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003239 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003240 DCHECK(IsBaseline());
3241 DCHECK(!current_method.IsValid());
3242 method_reg = reg;
3243 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003244 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003245 // temp = temp->dex_cache_resolved_methods_;
3246 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3247 // temp = temp[index_in_cache]
3248 __ movl(reg, Address(reg,
3249 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3250 // (temp + offset_of_quick_compiled_code)()
3251 __ call(Address(reg,
3252 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003253 }
3254
3255 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003256}
3257
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003258void CodeGeneratorX86::MarkGCCard(Register temp,
3259 Register card,
3260 Register object,
3261 Register value,
3262 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003263 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003264 if (value_can_be_null) {
3265 __ testl(value, value);
3266 __ j(kEqual, &is_null);
3267 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003268 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3269 __ movl(temp, object);
3270 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003271 __ movb(Address(temp, card, TIMES_1, 0),
3272 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003273 if (value_can_be_null) {
3274 __ Bind(&is_null);
3275 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003276}
3277
Calin Juravle52c48962014-12-16 17:02:57 +00003278void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3279 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003280 LocationSummary* locations =
3281 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003282 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003283
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003284 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3285 locations->SetOut(Location::RequiresFpuRegister());
3286 } else {
3287 // The output overlaps in case of long: we don't want the low move to overwrite
3288 // the object's location.
3289 locations->SetOut(Location::RequiresRegister(),
3290 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3291 : Location::kNoOutputOverlap);
3292 }
Calin Juravle52c48962014-12-16 17:02:57 +00003293
3294 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3295 // Long values can be loaded atomically into an XMM using movsd.
3296 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3297 // and then copy the XMM into the output 32bits at a time).
3298 locations->AddTemp(Location::RequiresFpuRegister());
3299 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003300}
3301
Calin Juravle52c48962014-12-16 17:02:57 +00003302void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3303 const FieldInfo& field_info) {
3304 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003305
Calin Juravle52c48962014-12-16 17:02:57 +00003306 LocationSummary* locations = instruction->GetLocations();
3307 Register base = locations->InAt(0).AsRegister<Register>();
3308 Location out = locations->Out();
3309 bool is_volatile = field_info.IsVolatile();
3310 Primitive::Type field_type = field_info.GetFieldType();
3311 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3312
3313 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003314 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003316 break;
3317 }
3318
3319 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003320 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003321 break;
3322 }
3323
3324 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003325 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003326 break;
3327 }
3328
3329 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003330 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003331 break;
3332 }
3333
3334 case Primitive::kPrimInt:
3335 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003336 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003337 break;
3338 }
3339
3340 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003341 if (is_volatile) {
3342 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3343 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003344 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003345 __ movd(out.AsRegisterPairLow<Register>(), temp);
3346 __ psrlq(temp, Immediate(32));
3347 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3348 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003349 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003350 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003352 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3353 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003354 break;
3355 }
3356
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003357 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003358 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003359 break;
3360 }
3361
3362 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003363 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003364 break;
3365 }
3366
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003367 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003368 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003369 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003370 }
Calin Juravle52c48962014-12-16 17:02:57 +00003371
Calin Juravle77520bc2015-01-12 18:45:46 +00003372 // Longs are handled in the switch.
3373 if (field_type != Primitive::kPrimLong) {
3374 codegen_->MaybeRecordImplicitNullCheck(instruction);
3375 }
3376
Calin Juravle52c48962014-12-16 17:02:57 +00003377 if (is_volatile) {
3378 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3379 }
3380}
3381
3382void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3383 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3384
3385 LocationSummary* locations =
3386 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3387 locations->SetInAt(0, Location::RequiresRegister());
3388 bool is_volatile = field_info.IsVolatile();
3389 Primitive::Type field_type = field_info.GetFieldType();
3390 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3391 || (field_type == Primitive::kPrimByte);
3392
3393 // The register allocator does not support multiple
3394 // inputs that die at entry with one in a specific register.
3395 if (is_byte_type) {
3396 // Ensure the value is in a byte register.
3397 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003398 } else if (Primitive::IsFloatingPointType(field_type)) {
3399 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003400 } else {
3401 locations->SetInAt(1, Location::RequiresRegister());
3402 }
3403 // Temporary registers for the write barrier.
3404 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3405 locations->AddTemp(Location::RequiresRegister());
3406 // Ensure the card is in a byte register.
3407 locations->AddTemp(Location::RegisterLocation(ECX));
3408 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3409 // 64bits value can be atomically written to an address with movsd and an XMM register.
3410 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3411 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3412 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3413 // isolated cases when we need this it isn't worth adding the extra complexity.
3414 locations->AddTemp(Location::RequiresFpuRegister());
3415 locations->AddTemp(Location::RequiresFpuRegister());
3416 }
3417}
3418
3419void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003420 const FieldInfo& field_info,
3421 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003422 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3423
3424 LocationSummary* locations = instruction->GetLocations();
3425 Register base = locations->InAt(0).AsRegister<Register>();
3426 Location value = locations->InAt(1);
3427 bool is_volatile = field_info.IsVolatile();
3428 Primitive::Type field_type = field_info.GetFieldType();
3429 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3430
3431 if (is_volatile) {
3432 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3433 }
3434
3435 switch (field_type) {
3436 case Primitive::kPrimBoolean:
3437 case Primitive::kPrimByte: {
3438 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3439 break;
3440 }
3441
3442 case Primitive::kPrimShort:
3443 case Primitive::kPrimChar: {
3444 __ movw(Address(base, offset), value.AsRegister<Register>());
3445 break;
3446 }
3447
3448 case Primitive::kPrimInt:
3449 case Primitive::kPrimNot: {
3450 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003451 break;
3452 }
3453
3454 case Primitive::kPrimLong: {
3455 if (is_volatile) {
3456 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3457 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3458 __ movd(temp1, value.AsRegisterPairLow<Register>());
3459 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3460 __ punpckldq(temp1, temp2);
3461 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003462 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003463 } else {
3464 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003465 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003466 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3467 }
3468 break;
3469 }
3470
3471 case Primitive::kPrimFloat: {
3472 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3473 break;
3474 }
3475
3476 case Primitive::kPrimDouble: {
3477 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3478 break;
3479 }
3480
3481 case Primitive::kPrimVoid:
3482 LOG(FATAL) << "Unreachable type " << field_type;
3483 UNREACHABLE();
3484 }
3485
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 // Longs are handled in the switch.
3487 if (field_type != Primitive::kPrimLong) {
3488 codegen_->MaybeRecordImplicitNullCheck(instruction);
3489 }
3490
3491 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3492 Register temp = locations->GetTemp(0).AsRegister<Register>();
3493 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003494 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003495 }
3496
Calin Juravle52c48962014-12-16 17:02:57 +00003497 if (is_volatile) {
3498 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3499 }
3500}
3501
3502void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3503 HandleFieldGet(instruction, instruction->GetFieldInfo());
3504}
3505
3506void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3507 HandleFieldGet(instruction, instruction->GetFieldInfo());
3508}
3509
3510void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3511 HandleFieldSet(instruction, instruction->GetFieldInfo());
3512}
3513
3514void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003515 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003516}
3517
3518void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3519 HandleFieldSet(instruction, instruction->GetFieldInfo());
3520}
3521
3522void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003523 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003524}
3525
3526void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3527 HandleFieldGet(instruction, instruction->GetFieldInfo());
3528}
3529
3530void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3531 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003532}
3533
3534void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003535 LocationSummary* locations =
3536 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003537 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3538 ? Location::RequiresRegister()
3539 : Location::Any();
3540 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003541 if (instruction->HasUses()) {
3542 locations->SetOut(Location::SameAsFirstInput());
3543 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544}
3545
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003546void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003547 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3548 return;
3549 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003550 LocationSummary* locations = instruction->GetLocations();
3551 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003552
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003553 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3554 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3555}
3556
3557void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003558 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003559 codegen_->AddSlowPath(slow_path);
3560
3561 LocationSummary* locations = instruction->GetLocations();
3562 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003563
3564 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003565 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003566 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003567 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003568 } else {
3569 DCHECK(obj.IsConstant()) << obj;
3570 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3571 __ jmp(slow_path->GetEntryLabel());
3572 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573 }
3574 __ j(kEqual, slow_path->GetEntryLabel());
3575}
3576
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003577void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3578 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3579 GenerateImplicitNullCheck(instruction);
3580 } else {
3581 GenerateExplicitNullCheck(instruction);
3582 }
3583}
3584
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003585void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003586 LocationSummary* locations =
3587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003588 locations->SetInAt(0, Location::RequiresRegister());
3589 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003590 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3592 } else {
3593 // The output overlaps in case of long: we don't want the low move to overwrite
3594 // the array's location.
3595 locations->SetOut(Location::RequiresRegister(),
3596 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3597 : Location::kNoOutputOverlap);
3598 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003599}
3600
3601void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3602 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 Location index = locations->InAt(1);
3605
Calin Juravle77520bc2015-01-12 18:45:46 +00003606 Primitive::Type type = instruction->GetType();
3607 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 case Primitive::kPrimBoolean: {
3609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 if (index.IsConstant()) {
3612 __ movzxb(out, Address(obj,
3613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3614 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003616 }
3617 break;
3618 }
3619
3620 case Primitive::kPrimByte: {
3621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003623 if (index.IsConstant()) {
3624 __ movsxb(out, Address(obj,
3625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3626 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003628 }
3629 break;
3630 }
3631
3632 case Primitive::kPrimShort: {
3633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 if (index.IsConstant()) {
3636 __ movsxw(out, Address(obj,
3637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3638 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003640 }
3641 break;
3642 }
3643
3644 case Primitive::kPrimChar: {
3645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003647 if (index.IsConstant()) {
3648 __ movzxw(out, Address(obj,
3649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3650 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 }
3653 break;
3654 }
3655
3656 case Primitive::kPrimInt:
3657 case Primitive::kPrimNot: {
3658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003660 if (index.IsConstant()) {
3661 __ movl(out, Address(obj,
3662 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3663 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003664 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003665 }
3666 break;
3667 }
3668
3669 case Primitive::kPrimLong: {
3670 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003671 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003672 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003673 if (index.IsConstant()) {
3674 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003675 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003677 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003678 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003679 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003682 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003684 }
3685 break;
3686 }
3687
Mark Mendell7c8d0092015-01-26 11:21:33 -05003688 case Primitive::kPrimFloat: {
3689 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3690 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3691 if (index.IsConstant()) {
3692 __ movss(out, Address(obj,
3693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3694 } else {
3695 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3696 }
3697 break;
3698 }
3699
3700 case Primitive::kPrimDouble: {
3701 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3702 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3703 if (index.IsConstant()) {
3704 __ movsd(out, Address(obj,
3705 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3706 } else {
3707 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3708 }
3709 break;
3710 }
3711
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003713 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003714 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003715 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003716
3717 if (type != Primitive::kPrimLong) {
3718 codegen_->MaybeRecordImplicitNullCheck(instruction);
3719 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003720}
3721
3722void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003723 // This location builder might end up asking to up to four registers, which is
3724 // not currently possible for baseline. The situation in which we need four
3725 // registers cannot be met by baseline though, because it has not run any
3726 // optimization.
3727
Nicolas Geoffray39468442014-09-02 15:17:15 +01003728 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003729 bool needs_write_barrier =
3730 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3731
Mark Mendell5f874182015-03-04 15:42:45 -05003732 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003733
Nicolas Geoffray39468442014-09-02 15:17:15 +01003734 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3735 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003736 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003737
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003738 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003739 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003740 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3741 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3742 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003743 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003744 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3745 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003746 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003747 // In case of a byte operation, the register allocator does not support multiple
3748 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003749 locations->SetInAt(0, Location::RequiresRegister());
3750 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003751 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003752 // Ensure the value is in a byte register.
3753 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003754 } else if (Primitive::IsFloatingPointType(value_type)) {
3755 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003757 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003759 // Temporary registers for the write barrier.
3760 if (needs_write_barrier) {
3761 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003762 // Ensure the card is in a byte register.
3763 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003764 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003766}
3767
3768void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3769 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003770 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003772 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003773 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003774 bool needs_runtime_call = locations->WillCall();
3775 bool needs_write_barrier =
3776 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003777
3778 switch (value_type) {
3779 case Primitive::kPrimBoolean:
3780 case Primitive::kPrimByte: {
3781 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 if (index.IsConstant()) {
3783 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003784 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003785 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003786 } else {
3787 __ movb(Address(obj, offset),
3788 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3789 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003791 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003793 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003794 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003795 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003796 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3797 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003799 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003800 break;
3801 }
3802
3803 case Primitive::kPrimShort:
3804 case Primitive::kPrimChar: {
3805 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 if (index.IsConstant()) {
3807 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003808 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003810 } else {
3811 __ movw(Address(obj, offset),
3812 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3813 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003815 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003816 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3817 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003818 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003820 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3821 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 break;
3825 }
3826
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003827 case Primitive::kPrimInt:
3828 case Primitive::kPrimNot: {
3829 if (!needs_runtime_call) {
3830 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3831 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003832 size_t offset =
3833 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003834 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003836 } else {
3837 DCHECK(value.IsConstant()) << value;
3838 __ movl(Address(obj, offset),
3839 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3840 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003841 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003842 DCHECK(index.IsRegister()) << index;
3843 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3845 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003846 } else {
3847 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003848 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003849 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3850 }
3851 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003853
3854 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 Register temp = locations->GetTemp(0).AsRegister<Register>();
3856 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003857 codegen_->MarkGCCard(
3858 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003859 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003861 DCHECK_EQ(value_type, Primitive::kPrimNot);
3862 DCHECK(!codegen_->IsLeafMethod());
3863 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3864 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 }
3866 break;
3867 }
3868
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 case Primitive::kPrimLong: {
3870 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 if (index.IsConstant()) {
3872 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003873 if (value.IsRegisterPair()) {
3874 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003875 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003876 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003877 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003878 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003879 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3880 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003882 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3883 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003884 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003885 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003887 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003888 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003889 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003890 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003891 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003892 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003893 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003895 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003896 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003897 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003898 Immediate(High32Bits(val)));
3899 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 }
3901 break;
3902 }
3903
Mark Mendell7c8d0092015-01-26 11:21:33 -05003904 case Primitive::kPrimFloat: {
3905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3906 DCHECK(value.IsFpuRegister());
3907 if (index.IsConstant()) {
3908 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3909 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3910 } else {
3911 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3912 value.AsFpuRegister<XmmRegister>());
3913 }
3914 break;
3915 }
3916
3917 case Primitive::kPrimDouble: {
3918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3919 DCHECK(value.IsFpuRegister());
3920 if (index.IsConstant()) {
3921 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3922 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3923 } else {
3924 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3925 value.AsFpuRegister<XmmRegister>());
3926 }
3927 break;
3928 }
3929
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930 case Primitive::kPrimVoid:
3931 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003932 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 }
3934}
3935
3936void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003938 locations->SetInAt(0, Location::RequiresRegister());
3939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003940}
3941
3942void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3943 LocationSummary* locations = instruction->GetLocations();
3944 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 Register obj = locations->InAt(0).AsRegister<Register>();
3946 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949}
3950
3951void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003952 LocationSummary* locations =
3953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003954 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003955 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003956 if (instruction->HasUses()) {
3957 locations->SetOut(Location::SameAsFirstInput());
3958 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959}
3960
3961void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3962 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003963 Location index_loc = locations->InAt(0);
3964 Location length_loc = locations->InAt(1);
3965 SlowPathCodeX86* slow_path =
3966 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967
Mark Mendell99dbd682015-04-22 16:18:52 -04003968 if (length_loc.IsConstant()) {
3969 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3970 if (index_loc.IsConstant()) {
3971 // BCE will remove the bounds check if we are guarenteed to pass.
3972 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3973 if (index < 0 || index >= length) {
3974 codegen_->AddSlowPath(slow_path);
3975 __ jmp(slow_path->GetEntryLabel());
3976 } else {
3977 // Some optimization after BCE may have generated this, and we should not
3978 // generate a bounds check if it is a valid range.
3979 }
3980 return;
3981 }
3982
3983 // We have to reverse the jump condition because the length is the constant.
3984 Register index_reg = index_loc.AsRegister<Register>();
3985 __ cmpl(index_reg, Immediate(length));
3986 codegen_->AddSlowPath(slow_path);
3987 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003988 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003989 Register length = length_loc.AsRegister<Register>();
3990 if (index_loc.IsConstant()) {
3991 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3992 __ cmpl(length, Immediate(value));
3993 } else {
3994 __ cmpl(length, index_loc.AsRegister<Register>());
3995 }
3996 codegen_->AddSlowPath(slow_path);
3997 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003998 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003999}
4000
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004001void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4002 temp->SetLocations(nullptr);
4003}
4004
4005void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4006 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004007 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008}
4009
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004010void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004011 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004012 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004013}
4014
4015void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004016 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4017}
4018
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004019void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4021}
4022
4023void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004024 HBasicBlock* block = instruction->GetBlock();
4025 if (block->GetLoopInformation() != nullptr) {
4026 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4027 // The back edge will generate the suspend check.
4028 return;
4029 }
4030 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4031 // The goto will generate the suspend check.
4032 return;
4033 }
4034 GenerateSuspendCheck(instruction, nullptr);
4035}
4036
4037void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4038 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004039 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004040 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4041 if (slow_path == nullptr) {
4042 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4043 instruction->SetSlowPath(slow_path);
4044 codegen_->AddSlowPath(slow_path);
4045 if (successor != nullptr) {
4046 DCHECK(successor->IsLoopHeader());
4047 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4048 }
4049 } else {
4050 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4051 }
4052
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004053 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004054 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004055 if (successor == nullptr) {
4056 __ j(kNotEqual, slow_path->GetEntryLabel());
4057 __ Bind(slow_path->GetReturnLabel());
4058 } else {
4059 __ j(kEqual, codegen_->GetLabelOf(successor));
4060 __ jmp(slow_path->GetEntryLabel());
4061 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004062}
4063
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004064X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4065 return codegen_->GetAssembler();
4066}
4067
Mark Mendell7c8d0092015-01-26 11:21:33 -05004068void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004069 ScratchRegisterScope ensure_scratch(
4070 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4071 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4072 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4073 __ movl(temp_reg, Address(ESP, src + stack_offset));
4074 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004075}
4076
4077void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004078 ScratchRegisterScope ensure_scratch(
4079 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4080 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4081 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4082 __ movl(temp_reg, Address(ESP, src + stack_offset));
4083 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4084 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4085 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004086}
4087
4088void ParallelMoveResolverX86::EmitMove(size_t index) {
4089 MoveOperands* move = moves_.Get(index);
4090 Location source = move->GetSource();
4091 Location destination = move->GetDestination();
4092
4093 if (source.IsRegister()) {
4094 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004096 } else {
4097 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004099 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004100 } else if (source.IsFpuRegister()) {
4101 if (destination.IsFpuRegister()) {
4102 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4103 } else if (destination.IsStackSlot()) {
4104 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4105 } else {
4106 DCHECK(destination.IsDoubleStackSlot());
4107 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4108 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004109 } else if (source.IsStackSlot()) {
4110 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004112 } else if (destination.IsFpuRegister()) {
4113 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004114 } else {
4115 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004116 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4117 }
4118 } else if (source.IsDoubleStackSlot()) {
4119 if (destination.IsFpuRegister()) {
4120 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4121 } else {
4122 DCHECK(destination.IsDoubleStackSlot()) << destination;
4123 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004124 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004125 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004126 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004127 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004128 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004129 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004130 if (value == 0) {
4131 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4132 } else {
4133 __ movl(destination.AsRegister<Register>(), Immediate(value));
4134 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004135 } else {
4136 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004137 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004138 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004139 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004140 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004141 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004142 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004143 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004144 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4145 if (value == 0) {
4146 // Easy handling of 0.0.
4147 __ xorps(dest, dest);
4148 } else {
4149 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004150 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4151 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4152 __ movl(temp, Immediate(value));
4153 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004154 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004155 } else {
4156 DCHECK(destination.IsStackSlot()) << destination;
4157 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4158 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004159 } else if (constant->IsLongConstant()) {
4160 int64_t value = constant->AsLongConstant()->GetValue();
4161 int32_t low_value = Low32Bits(value);
4162 int32_t high_value = High32Bits(value);
4163 Immediate low(low_value);
4164 Immediate high(high_value);
4165 if (destination.IsDoubleStackSlot()) {
4166 __ movl(Address(ESP, destination.GetStackIndex()), low);
4167 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4168 } else {
4169 __ movl(destination.AsRegisterPairLow<Register>(), low);
4170 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4171 }
4172 } else {
4173 DCHECK(constant->IsDoubleConstant());
4174 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004175 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004176 int32_t low_value = Low32Bits(value);
4177 int32_t high_value = High32Bits(value);
4178 Immediate low(low_value);
4179 Immediate high(high_value);
4180 if (destination.IsFpuRegister()) {
4181 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4182 if (value == 0) {
4183 // Easy handling of 0.0.
4184 __ xorpd(dest, dest);
4185 } else {
4186 __ pushl(high);
4187 __ pushl(low);
4188 __ movsd(dest, Address(ESP, 0));
4189 __ addl(ESP, Immediate(8));
4190 }
4191 } else {
4192 DCHECK(destination.IsDoubleStackSlot()) << destination;
4193 __ movl(Address(ESP, destination.GetStackIndex()), low);
4194 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4195 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004196 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004197 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004198 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004199 }
4200}
4201
Mark Mendella5c19ce2015-04-01 12:51:05 -04004202void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004203 Register suggested_scratch = reg == EAX ? EBX : EAX;
4204 ScratchRegisterScope ensure_scratch(
4205 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4206
4207 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4208 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4209 __ movl(Address(ESP, mem + stack_offset), reg);
4210 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004211}
4212
Mark Mendell7c8d0092015-01-26 11:21:33 -05004213void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004214 ScratchRegisterScope ensure_scratch(
4215 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4216
4217 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4218 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4219 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4220 __ movss(Address(ESP, mem + stack_offset), reg);
4221 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004222}
4223
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004225 ScratchRegisterScope ensure_scratch1(
4226 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004227
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004228 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4229 ScratchRegisterScope ensure_scratch2(
4230 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004231
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004232 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4233 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4234 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4235 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4236 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4237 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004238}
4239
4240void ParallelMoveResolverX86::EmitSwap(size_t index) {
4241 MoveOperands* move = moves_.Get(index);
4242 Location source = move->GetSource();
4243 Location destination = move->GetDestination();
4244
4245 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004246 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004247 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004248 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004249 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004250 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004251 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4252 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004253 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4254 // Use XOR Swap algorithm to avoid a temporary.
4255 DCHECK_NE(source.reg(), destination.reg());
4256 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4257 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4258 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4259 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4260 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4261 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4262 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004263 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4264 // Take advantage of the 16 bytes in the XMM register.
4265 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4266 Address stack(ESP, destination.GetStackIndex());
4267 // Load the double into the high doubleword.
4268 __ movhpd(reg, stack);
4269
4270 // Store the low double into the destination.
4271 __ movsd(stack, reg);
4272
4273 // Move the high double to the low double.
4274 __ psrldq(reg, Immediate(8));
4275 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4276 // Take advantage of the 16 bytes in the XMM register.
4277 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4278 Address stack(ESP, source.GetStackIndex());
4279 // Load the double into the high doubleword.
4280 __ movhpd(reg, stack);
4281
4282 // Store the low double into the destination.
4283 __ movsd(stack, reg);
4284
4285 // Move the high double to the low double.
4286 __ psrldq(reg, Immediate(8));
4287 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4288 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4289 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004290 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004291 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004292 }
4293}
4294
4295void ParallelMoveResolverX86::SpillScratch(int reg) {
4296 __ pushl(static_cast<Register>(reg));
4297}
4298
4299void ParallelMoveResolverX86::RestoreScratch(int reg) {
4300 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004301}
4302
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004303void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004304 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4305 ? LocationSummary::kCallOnSlowPath
4306 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004307 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004308 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004309 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004310 locations->SetOut(Location::RequiresRegister());
4311}
4312
4313void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004314 LocationSummary* locations = cls->GetLocations();
4315 Register out = locations->Out().AsRegister<Register>();
4316 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004317 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004318 DCHECK(!cls->CanCallRuntime());
4319 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004320 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004321 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004322 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004323 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004324 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004325 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004326
4327 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4328 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4329 codegen_->AddSlowPath(slow_path);
4330 __ testl(out, out);
4331 __ j(kEqual, slow_path->GetEntryLabel());
4332 if (cls->MustGenerateClinitCheck()) {
4333 GenerateClassInitializationCheck(slow_path, out);
4334 } else {
4335 __ Bind(slow_path->GetExitLabel());
4336 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004337 }
4338}
4339
4340void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4341 LocationSummary* locations =
4342 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4343 locations->SetInAt(0, Location::RequiresRegister());
4344 if (check->HasUses()) {
4345 locations->SetOut(Location::SameAsFirstInput());
4346 }
4347}
4348
4349void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004350 // We assume the class to not be null.
4351 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4352 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004353 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004354 GenerateClassInitializationCheck(slow_path,
4355 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004356}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004357
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004358void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4359 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004360 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4361 Immediate(mirror::Class::kStatusInitialized));
4362 __ j(kLess, slow_path->GetEntryLabel());
4363 __ Bind(slow_path->GetExitLabel());
4364 // No need for memory fence, thanks to the X86 memory model.
4365}
4366
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004367void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4368 LocationSummary* locations =
4369 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004370 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004371 locations->SetOut(Location::RequiresRegister());
4372}
4373
4374void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4375 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4376 codegen_->AddSlowPath(slow_path);
4377
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004378 LocationSummary* locations = load->GetLocations();
4379 Register out = locations->Out().AsRegister<Register>();
4380 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004381 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004382 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004383 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4384 __ testl(out, out);
4385 __ j(kEqual, slow_path->GetEntryLabel());
4386 __ Bind(slow_path->GetExitLabel());
4387}
4388
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004389void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4390 LocationSummary* locations =
4391 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4392 locations->SetOut(Location::RequiresRegister());
4393}
4394
4395void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4396 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004397 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004398 __ fs()->movl(address, Immediate(0));
4399}
4400
4401void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4402 LocationSummary* locations =
4403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4404 InvokeRuntimeCallingConvention calling_convention;
4405 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4406}
4407
4408void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4409 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4410 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4411}
4412
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004413void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004414 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4415 ? LocationSummary::kNoCall
4416 : LocationSummary::kCallOnSlowPath;
4417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4418 locations->SetInAt(0, Location::RequiresRegister());
4419 locations->SetInAt(1, Location::Any());
4420 locations->SetOut(Location::RequiresRegister());
4421}
4422
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004423void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004424 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004425 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004426 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4429 Label done, zero;
4430 SlowPathCodeX86* slow_path = nullptr;
4431
4432 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004433 // Avoid null check if we know obj is not null.
4434 if (instruction->MustDoNullCheck()) {
4435 __ testl(obj, obj);
4436 __ j(kEqual, &zero);
4437 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004438 __ movl(out, Address(obj, class_offset));
4439 // Compare the class of `obj` with `cls`.
4440 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004441 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004442 } else {
4443 DCHECK(cls.IsStackSlot()) << cls;
4444 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4445 }
4446
4447 if (instruction->IsClassFinal()) {
4448 // Classes must be equal for the instanceof to succeed.
4449 __ j(kNotEqual, &zero);
4450 __ movl(out, Immediate(1));
4451 __ jmp(&done);
4452 } else {
4453 // If the classes are not equal, we go into a slow path.
4454 DCHECK(locations->OnlyCallsOnSlowPath());
4455 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004456 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004457 codegen_->AddSlowPath(slow_path);
4458 __ j(kNotEqual, slow_path->GetEntryLabel());
4459 __ movl(out, Immediate(1));
4460 __ jmp(&done);
4461 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004462
4463 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4464 __ Bind(&zero);
4465 __ movl(out, Immediate(0));
4466 }
4467
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004468 if (slow_path != nullptr) {
4469 __ Bind(slow_path->GetExitLabel());
4470 }
4471 __ Bind(&done);
4472}
4473
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004474void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4476 instruction, LocationSummary::kCallOnSlowPath);
4477 locations->SetInAt(0, Location::RequiresRegister());
4478 locations->SetInAt(1, Location::Any());
4479 locations->AddTemp(Location::RequiresRegister());
4480}
4481
4482void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4483 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004484 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004485 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004487 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4488 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4489 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4490 codegen_->AddSlowPath(slow_path);
4491
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004492 // Avoid null check if we know obj is not null.
4493 if (instruction->MustDoNullCheck()) {
4494 __ testl(obj, obj);
4495 __ j(kEqual, slow_path->GetExitLabel());
4496 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004497
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004498 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004499 // Compare the class of `obj` with `cls`.
4500 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004502 } else {
4503 DCHECK(cls.IsStackSlot()) << cls;
4504 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4505 }
4506
4507 __ j(kNotEqual, slow_path->GetEntryLabel());
4508 __ Bind(slow_path->GetExitLabel());
4509}
4510
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004511void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4512 LocationSummary* locations =
4513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4514 InvokeRuntimeCallingConvention calling_convention;
4515 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4516}
4517
4518void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4519 __ fs()->call(Address::Absolute(instruction->IsEnter()
4520 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4521 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4522 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4523}
4524
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004525void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4526void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4527void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4528
4529void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4530 LocationSummary* locations =
4531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4532 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4533 || instruction->GetResultType() == Primitive::kPrimLong);
4534 locations->SetInAt(0, Location::RequiresRegister());
4535 locations->SetInAt(1, Location::Any());
4536 locations->SetOut(Location::SameAsFirstInput());
4537}
4538
4539void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4540 HandleBitwiseOperation(instruction);
4541}
4542
4543void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4544 HandleBitwiseOperation(instruction);
4545}
4546
4547void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4548 HandleBitwiseOperation(instruction);
4549}
4550
4551void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4552 LocationSummary* locations = instruction->GetLocations();
4553 Location first = locations->InAt(0);
4554 Location second = locations->InAt(1);
4555 DCHECK(first.Equals(locations->Out()));
4556
4557 if (instruction->GetResultType() == Primitive::kPrimInt) {
4558 if (second.IsRegister()) {
4559 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004560 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004561 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004562 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004563 } else {
4564 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004565 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004566 }
4567 } else if (second.IsConstant()) {
4568 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004569 __ andl(first.AsRegister<Register>(),
4570 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004571 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004572 __ orl(first.AsRegister<Register>(),
4573 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004574 } else {
4575 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004576 __ xorl(first.AsRegister<Register>(),
4577 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004578 }
4579 } else {
4580 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004581 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004582 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004583 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004584 } else {
4585 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004586 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004587 }
4588 }
4589 } else {
4590 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4591 if (second.IsRegisterPair()) {
4592 if (instruction->IsAnd()) {
4593 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4594 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4595 } else if (instruction->IsOr()) {
4596 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4597 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4598 } else {
4599 DCHECK(instruction->IsXor());
4600 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4601 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4602 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004603 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004604 if (instruction->IsAnd()) {
4605 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4606 __ andl(first.AsRegisterPairHigh<Register>(),
4607 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4608 } else if (instruction->IsOr()) {
4609 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4610 __ orl(first.AsRegisterPairHigh<Register>(),
4611 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4612 } else {
4613 DCHECK(instruction->IsXor());
4614 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4615 __ xorl(first.AsRegisterPairHigh<Register>(),
4616 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4617 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004618 } else {
4619 DCHECK(second.IsConstant()) << second;
4620 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004621 int32_t low_value = Low32Bits(value);
4622 int32_t high_value = High32Bits(value);
4623 Immediate low(low_value);
4624 Immediate high(high_value);
4625 Register first_low = first.AsRegisterPairLow<Register>();
4626 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004627 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004628 if (low_value == 0) {
4629 __ xorl(first_low, first_low);
4630 } else if (low_value != -1) {
4631 __ andl(first_low, low);
4632 }
4633 if (high_value == 0) {
4634 __ xorl(first_high, first_high);
4635 } else if (high_value != -1) {
4636 __ andl(first_high, high);
4637 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004638 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004639 if (low_value != 0) {
4640 __ orl(first_low, low);
4641 }
4642 if (high_value != 0) {
4643 __ orl(first_high, high);
4644 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004645 } else {
4646 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004647 if (low_value != 0) {
4648 __ xorl(first_low, low);
4649 }
4650 if (high_value != 0) {
4651 __ xorl(first_high, high);
4652 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004653 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654 }
4655 }
4656}
4657
Calin Juravleb1498f62015-02-16 13:13:29 +00004658void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4659 // Nothing to do, this should be removed during prepare for register allocator.
4660 UNUSED(instruction);
4661 LOG(FATAL) << "Unreachable";
4662}
4663
4664void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4665 // Nothing to do, this should be removed during prepare for register allocator.
4666 UNUSED(instruction);
4667 LOG(FATAL) << "Unreachable";
4668}
4669
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004670} // namespace x86
4671} // namespace art