blob: 8678428bf344f12f9db642490bdb8d5b0ba8b60d [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
Nicolas Geoffraye5038322014-07-04 09:41:32 +010047#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
48
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
59 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
62};
63
Calin Juravled0d48522014-11-04 16:40:20 +000064class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
65 public:
66 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
67
Alexandre Rames2ed20af2015-03-06 13:55:35 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000069 __ Bind(GetEntryLabel());
70 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070071 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000072 }
73
74 private:
75 HDivZeroCheck* const instruction_;
76 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
77};
78
Calin Juravlebacfec32014-11-14 15:54:36 +000079class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000081 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000082
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000085 if (is_div_) {
86 __ negl(reg_);
87 } else {
88 __ movl(reg_, Immediate(0));
89 }
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ jmp(GetExitLabel());
91 }
92
93 private:
94 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +000095 bool is_div_;
96 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +000097};
98
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010099class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100100 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100101 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
102 Location index_location,
103 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000104 : instruction_(instruction),
105 index_location_(index_location),
106 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100110 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000111 // We're moving two locations to locations that could overlap, so we need a parallel
112 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000114 x86_codegen->EmitParallelMoves(
115 index_location_,
116 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100117 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000118 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100119 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
120 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700122 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 }
124
125 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100126 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 const Location index_location_;
128 const Location length_location_;
129
130 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000135 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000137
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000138 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000141 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700143 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100145 if (successor_ == nullptr) {
146 __ jmp(GetReturnLabel());
147 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 }
151
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 Label* GetReturnLabel() {
153 DCHECK(successor_ == nullptr);
154 return &return_label_;
155 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100157 HBasicBlock* GetSuccessor() const {
158 return successor_;
159 }
160
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 private:
162 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 Label return_label_;
165
166 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
167};
168
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000169class LoadStringSlowPathX86 : public SlowPathCodeX86 {
170 public:
171 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
172
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000173 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000174 LocationSummary* locations = instruction_->GetLocations();
175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
176
177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
178 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000179 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000180
181 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800182 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000183 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000185 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000187
188 __ jmp(GetExitLabel());
189 }
190
191 private:
192 HLoadString* const instruction_;
193
194 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
195};
196
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197class LoadClassSlowPathX86 : public SlowPathCodeX86 {
198 public:
199 LoadClassSlowPathX86(HLoadClass* cls,
200 HInstruction* at,
201 uint32_t dex_pc,
202 bool do_clinit)
203 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
204 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
205 }
206
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 LocationSummary* locations = at_->GetLocations();
209 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 InvokeRuntimeCallingConvention calling_convention;
214 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 __ fs()->call(Address::Absolute(do_clinit_
216 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
217 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000218 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219
220 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000221 Location out = locations->Out();
222 if (out.IsValid()) {
223 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
224 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 __ jmp(GetExitLabel());
229 }
230
231 private:
232 // The class this slow path will load.
233 HLoadClass* const cls_;
234
235 // The instruction where this slow path is happening.
236 // (Might be the load class or an initialization check).
237 HInstruction* const at_;
238
239 // The dex PC of `at_`.
240 const uint32_t dex_pc_;
241
242 // Whether to initialize the class.
243 const bool do_clinit_;
244
245 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
246};
247
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
249 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000250 TypeCheckSlowPathX86(HInstruction* instruction,
251 Location class_to_check,
252 Location object_class,
253 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000255 class_to_check_(class_to_check),
256 object_class_(object_class),
257 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000258
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000260 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000261 DCHECK(instruction_->IsCheckCast()
262 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000263
264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
268 // We're moving two locations to locations that could overlap, so we need a parallel
269 // move resolver.
270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000271 x86_codegen->EmitParallelMoves(
272 class_to_check_,
273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100274 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000275 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100276 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
277 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000280 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
281 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 } else {
283 DCHECK(instruction_->IsCheckCast());
284 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 if (instruction_->IsInstanceOf()) {
289 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
290 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 __ jmp(GetExitLabel());
294 }
295
296 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 HInstruction* const instruction_;
298 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
303};
304
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700305class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
306 public:
307 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
308 : instruction_(instruction) {}
309
310 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
311 __ Bind(GetEntryLabel());
312 SaveLiveRegisters(codegen, instruction_->GetLocations());
313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
314 // No need to restore live registers.
315 DCHECK(instruction_->IsDeoptimize());
316 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
317 uint32_t dex_pc = deoptimize->GetDexPc();
318 codegen->RecordPcInfo(instruction_, dex_pc, this);
319 }
320
321 private:
322 HInstruction* const instruction_;
323 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
327#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100344 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100348 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Mark Mendell7c8d0092015-01-26 11:21:33 -0500361size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
362 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
363 return GetFloatingPointSpillSlotSize();
364}
365
366size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
367 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
368 return GetFloatingPointSpillSlotSize();
369}
370
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
372 const X86InstructionSetFeatures& isa_features,
373 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500374 : CodeGenerator(graph,
375 kNumberOfCpuRegisters,
376 kNumberOfXmmRegisters,
377 kNumberOfRegisterPairs,
378 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
379 arraysize(kCoreCalleeSaves))
380 | (1 << kFakeReturnRegister),
381 0,
382 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100383 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 move_resolver_(graph->GetArena(), this),
387 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000388 // Use a fake return address register to mimic Quick.
389 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100390}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 switch (type) {
394 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 X86ManagedRegister pair =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
399 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
401 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100402 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimByte:
407 case Primitive::kPrimBoolean:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
416 X86ManagedRegister current =
417 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
418 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100420 }
421 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 return Location::FpuRegisterLocation(
428 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436}
437
Mark Mendell5f874182015-03-04 15:42:45 -0500438void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441
442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444
Mark Mendell5f874182015-03-04 15:42:45 -0500445 if (is_baseline) {
446 blocked_core_registers_[EBP] = true;
447 blocked_core_registers_[ESI] = true;
448 blocked_core_registers_[EDI] = true;
449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100450
451 UpdateBlockedPairRegisters();
452}
453
454void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
455 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
456 X86ManagedRegister current =
457 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
458 if (blocked_core_registers_[current.AsRegisterPairLow()]
459 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
460 blocked_register_pairs_[i] = true;
461 }
462 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100463}
464
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100465InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
466 : HGraphVisitor(graph),
467 assembler_(codegen->GetAssembler()),
468 codegen_(codegen) {}
469
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100470static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100471 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100472}
473
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100475 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000476 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000477 bool skip_overflow_check =
478 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000479 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000480
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000481 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 }
485
Mark Mendell5f874182015-03-04 15:42:45 -0500486 if (HasEmptyFrame()) {
487 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000488 }
Mark Mendell5f874182015-03-04 15:42:45 -0500489
490 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
491 Register reg = kCoreCalleeSaves[i];
492 if (allocated_registers_.ContainsCoreRegister(reg)) {
493 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100494 __ cfi().AdjustCFAOffset(kX86WordSize);
495 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500496 }
497 }
498
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 int adjust = GetFrameSize() - FrameEntrySpillSize();
500 __ subl(ESP, Immediate(adjust));
501 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100502 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000503}
504
505void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100506 __ cfi().RememberState();
507 if (!HasEmptyFrame()) {
508 int adjust = GetFrameSize() - FrameEntrySpillSize();
509 __ addl(ESP, Immediate(adjust));
510 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500511
David Srbeckyc34dc932015-04-12 09:27:43 +0100512 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
513 Register reg = kCoreCalleeSaves[i];
514 if (allocated_registers_.ContainsCoreRegister(reg)) {
515 __ popl(reg);
516 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
517 __ cfi().Restore(DWARFReg(reg));
518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000520 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100521 __ ret();
522 __ cfi().RestoreState();
523 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524}
525
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100526void CodeGeneratorX86::Bind(HBasicBlock* block) {
527 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528}
529
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100530void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000531 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100532 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000533}
534
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
536 switch (load->GetType()) {
537 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100538 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545
546 case Primitive::kPrimBoolean:
547 case Primitive::kPrimByte:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimShort:
550 case Primitive::kPrimVoid:
551 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700552 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700556 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100557}
558
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100559Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 switch (type) {
561 case Primitive::kPrimBoolean:
562 case Primitive::kPrimByte:
563 case Primitive::kPrimChar:
564 case Primitive::kPrimShort:
565 case Primitive::kPrimInt:
566 case Primitive::kPrimNot: {
567 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000568 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100569 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100571 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000572 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100573 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100575
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000576 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100577 uint32_t index = gp_index_;
578 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000579 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100580 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
582 calling_convention.GetRegisterPairAt(index));
583 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100584 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000585 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
586 }
587 }
588
589 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100590 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000591 stack_index_++;
592 if (index < calling_convention.GetNumberOfFpuRegisters()) {
593 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
594 } else {
595 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
596 }
597 }
598
599 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100600 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000601 stack_index_ += 2;
602 if (index < calling_convention.GetNumberOfFpuRegisters()) {
603 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
604 } else {
605 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100606 }
607 }
608
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100609 case Primitive::kPrimVoid:
610 LOG(FATAL) << "Unexpected parameter type " << type;
611 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100612 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100613 return Location();
614}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100615
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616void CodeGeneratorX86::Move32(Location destination, Location source) {
617 if (source.Equals(destination)) {
618 return;
619 }
620 if (destination.IsRegister()) {
621 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000622 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
626 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000627 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 } else if (destination.IsFpuRegister()) {
630 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000631 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000633 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 } else {
635 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000636 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000639 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000641 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100642 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000643 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500644 } else if (source.IsConstant()) {
645 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500647 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 } else {
649 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100650 __ pushl(Address(ESP, source.GetStackIndex()));
651 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 }
653 }
654}
655
656void CodeGeneratorX86::Move64(Location destination, Location source) {
657 if (source.Equals(destination)) {
658 return;
659 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100660 if (destination.IsRegisterPair()) {
661 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000662 EmitParallelMoves(
663 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
664 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100665 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000666 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100667 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
668 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 } else if (source.IsFpuRegister()) {
670 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000672 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100674 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
675 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
677 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500679 if (source.IsFpuRegister()) {
680 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
681 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 } else {
684 LOG(FATAL) << "Unimplemented";
685 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000687 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100688 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000689 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100690 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100692 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100693 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000695 } else if (source.IsConstant()) {
696 HConstant* constant = source.GetConstant();
697 int64_t value;
698 if (constant->IsLongConstant()) {
699 value = constant->AsLongConstant()->GetValue();
700 } else {
701 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000702 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000703 }
704 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
705 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000707 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000708 EmitParallelMoves(
709 Location::StackSlot(source.GetStackIndex()),
710 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100711 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000712 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100713 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
714 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 }
716 }
717}
718
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100719void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000720 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100721 if (instruction->IsCurrentMethod()) {
722 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
723 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000724 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100725 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000726 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000727 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
728 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000729 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000730 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000731 } else if (location.IsStackSlot()) {
732 __ movl(Address(ESP, location.GetStackIndex()), imm);
733 } else {
734 DCHECK(location.IsConstant());
735 DCHECK_EQ(location.GetConstant(), const_to_move);
736 }
737 } else if (const_to_move->IsLongConstant()) {
738 int64_t value = const_to_move->AsLongConstant()->GetValue();
739 if (location.IsRegisterPair()) {
740 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
741 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
742 } else if (location.IsDoubleStackSlot()) {
743 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000744 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
745 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000746 } else {
747 DCHECK(location.IsConstant());
748 DCHECK_EQ(location.GetConstant(), instruction);
749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000751 } else if (instruction->IsTemporary()) {
752 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000753 if (temp_location.IsStackSlot()) {
754 Move32(location, temp_location);
755 } else {
756 DCHECK(temp_location.IsDoubleStackSlot());
757 Move64(location, temp_location);
758 }
Roland Levillain476df552014-10-09 17:51:36 +0100759 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 switch (instruction->GetType()) {
762 case Primitive::kPrimBoolean:
763 case Primitive::kPrimByte:
764 case Primitive::kPrimChar:
765 case Primitive::kPrimShort:
766 case Primitive::kPrimInt:
767 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 case Primitive::kPrimFloat:
769 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 break;
771
772 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100773 case Primitive::kPrimDouble:
774 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 break;
776
777 default:
778 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
779 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100781 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100782 switch (instruction->GetType()) {
783 case Primitive::kPrimBoolean:
784 case Primitive::kPrimByte:
785 case Primitive::kPrimChar:
786 case Primitive::kPrimShort:
787 case Primitive::kPrimInt:
788 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000790 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 break;
792
793 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000795 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796 break;
797
798 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100799 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801 }
802}
803
804void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000806}
807
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000808void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000809 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100810 DCHECK(!successor->IsExitBlock());
811
812 HBasicBlock* block = got->GetBlock();
813 HInstruction* previous = got->GetPrevious();
814
815 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000816 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100817 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
818 return;
819 }
820
821 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
822 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
823 }
824 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000826 }
827}
828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000831}
832
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000833void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700834 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000835}
836
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700837void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
838 Label* true_target,
839 Label* false_target,
840 Label* always_true_target) {
841 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 if (cond->IsIntConstant()) {
843 // Constant condition, statically compared against 1.
844 int32_t cond_value = cond->AsIntConstant()->GetValue();
845 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700846 if (always_true_target != nullptr) {
847 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100851 DCHECK_EQ(cond_value, 0);
852 }
853 } else {
854 bool materialized =
855 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
856 // Moves do not affect the eflags register, so if the condition is
857 // evaluated just before the if, we don't need to evaluate it
858 // again.
859 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700860 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 if (materialized) {
862 if (!eflags_set) {
863 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700864 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500866 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
868 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
869 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700870 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700872 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100873 }
874 } else {
875 Location lhs = cond->GetLocations()->InAt(0);
876 Location rhs = cond->GetLocations()->InAt(1);
877 // LHS is guaranteed to be in a register (see
878 // LocationsBuilderX86::VisitCondition).
879 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100881 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100882 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500883 if (constant == 0) {
884 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
885 } else {
886 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
887 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100888 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000889 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100890 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700891 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700892 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700894 if (false_target != nullptr) {
895 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896 }
897}
898
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700899void LocationsBuilderX86::VisitIf(HIf* if_instr) {
900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
902 HInstruction* cond = if_instr->InputAt(0);
903 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
904 locations->SetInAt(0, Location::Any());
905 }
906}
907
908void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
909 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
910 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
911 Label* always_true_target = true_target;
912 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
913 if_instr->IfTrueSuccessor())) {
914 always_true_target = nullptr;
915 }
916 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
917 if_instr->IfFalseSuccessor())) {
918 false_target = nullptr;
919 }
920 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
921}
922
923void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
924 LocationSummary* locations = new (GetGraph()->GetArena())
925 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
926 HInstruction* cond = deoptimize->InputAt(0);
927 DCHECK(cond->IsCondition());
928 if (cond->AsCondition()->NeedsMaterialization()) {
929 locations->SetInAt(0, Location::Any());
930 }
931}
932
933void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
934 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
935 DeoptimizationSlowPathX86(deoptimize);
936 codegen_->AddSlowPath(slow_path);
937 Label* slow_path_entry = slow_path->GetEntryLabel();
938 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
939}
940
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000941void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000942 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000943}
944
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000945void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
946 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000947}
948
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100950 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000951}
952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100954 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700955 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000956}
957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100958void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100959 LocationSummary* locations =
960 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100961 switch (store->InputAt(1)->GetType()) {
962 case Primitive::kPrimBoolean:
963 case Primitive::kPrimByte:
964 case Primitive::kPrimChar:
965 case Primitive::kPrimShort:
966 case Primitive::kPrimInt:
967 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100969 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
970 break;
971
972 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100974 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
975 break;
976
977 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100979 }
980 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981}
982
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000985}
986
Roland Levillain0d37cd02015-05-27 16:39:19 +0100987void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100988 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +0100989 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100990 locations->SetInAt(0, Location::RequiresRegister());
991 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +0100992 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500993 // We need a byte register.
994 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100995 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000996}
997
Roland Levillain0d37cd02015-05-27 16:39:19 +0100998void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
999 if (cond->NeedsMaterialization()) {
1000 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001001 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001002 // Clear register: setcc only sets the low byte.
1003 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001004 Location lhs = locations->InAt(0);
1005 Location rhs = locations->InAt(1);
1006 if (rhs.IsRegister()) {
1007 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1008 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001009 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001010 if (constant == 0) {
1011 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1012 } else {
1013 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1014 }
Dave Allison20dfc792014-06-16 20:44:29 -07001015 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001016 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001017 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001018 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001019 }
Dave Allison20dfc792014-06-16 20:44:29 -07001020}
1021
1022void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1023 VisitCondition(comp);
1024}
1025
1026void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1027 VisitCondition(comp);
1028}
1029
1030void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1031 VisitCondition(comp);
1032}
1033
1034void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1035 VisitCondition(comp);
1036}
1037
1038void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1039 VisitCondition(comp);
1040}
1041
1042void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1043 VisitCondition(comp);
1044}
1045
1046void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1047 VisitCondition(comp);
1048}
1049
1050void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1051 VisitCondition(comp);
1052}
1053
1054void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1055 VisitCondition(comp);
1056}
1057
1058void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1059 VisitCondition(comp);
1060}
1061
1062void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1063 VisitCondition(comp);
1064}
1065
1066void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1067 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001068}
1069
1070void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 LocationSummary* locations =
1072 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001073 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001074}
1075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001076void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001078 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001079}
1080
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001081void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1082 LocationSummary* locations =
1083 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1084 locations->SetOut(Location::ConstantLocation(constant));
1085}
1086
1087void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1088 // Will be generated at use site.
1089 UNUSED(constant);
1090}
1091
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001093 LocationSummary* locations =
1094 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001095 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096}
1097
1098void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1099 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001100 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101}
1102
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001103void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1104 LocationSummary* locations =
1105 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1106 locations->SetOut(Location::ConstantLocation(constant));
1107}
1108
1109void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1110 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001111 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001112}
1113
1114void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1115 LocationSummary* locations =
1116 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1117 locations->SetOut(Location::ConstantLocation(constant));
1118}
1119
1120void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1121 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123}
1124
Calin Juravle27df7582015-04-17 19:12:31 +01001125void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1126 memory_barrier->SetLocations(nullptr);
1127}
1128
1129void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1130 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1131}
1132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001134 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135}
1136
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001139 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001140}
1141
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001142void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001143 LocationSummary* locations =
1144 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001145 switch (ret->InputAt(0)->GetType()) {
1146 case Primitive::kPrimBoolean:
1147 case Primitive::kPrimByte:
1148 case Primitive::kPrimChar:
1149 case Primitive::kPrimShort:
1150 case Primitive::kPrimInt:
1151 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154
1155 case Primitive::kPrimLong:
1156 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 break;
1159
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001160 case Primitive::kPrimFloat:
1161 case Primitive::kPrimDouble:
1162 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001164 break;
1165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169}
1170
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001171void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 if (kIsDebugBuild) {
1173 switch (ret->InputAt(0)->GetType()) {
1174 case Primitive::kPrimBoolean:
1175 case Primitive::kPrimByte:
1176 case Primitive::kPrimChar:
1177 case Primitive::kPrimShort:
1178 case Primitive::kPrimInt:
1179 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001180 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1185 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 break;
1187
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001188 case Primitive::kPrimFloat:
1189 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 break;
1192
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
1196 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001200void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001201 // When we do not run baseline, explicit clinit checks triggered by static
1202 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1203 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001204
Mark Mendellfb8d2792015-03-31 22:16:59 -04001205 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001206 if (intrinsic.TryDispatch(invoke)) {
1207 return;
1208 }
1209
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001210 HandleInvoke(invoke);
1211}
1212
Mark Mendell09ed1a32015-03-25 08:30:06 -04001213static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1214 if (invoke->GetLocations()->Intrinsified()) {
1215 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1216 intrinsic.Dispatch(invoke);
1217 return true;
1218 }
1219 return false;
1220}
1221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001223 // When we do not run baseline, explicit clinit checks triggered by static
1224 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1225 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001226
Mark Mendell09ed1a32015-03-25 08:30:06 -04001227 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1228 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001229 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230
Mark Mendell09ed1a32015-03-25 08:30:06 -04001231 codegen_->GenerateStaticOrDirectCall(
1232 invoke, invoke->GetLocations()->GetTemp(0).AsRegister<Register>());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001233 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001234}
1235
1236void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1237 HandleInvoke(invoke);
1238}
1239
1240void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001243 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001244
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001245 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01001246 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001247 HInstruction* input = invoke->InputAt(i);
1248 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1249 }
1250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 switch (invoke->GetType()) {
1252 case Primitive::kPrimBoolean:
1253 case Primitive::kPrimByte:
1254 case Primitive::kPrimChar:
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001258 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001262 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 break;
1264
1265 case Primitive::kPrimVoid:
1266 break;
1267
1268 case Primitive::kPrimDouble:
1269 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001270 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 break;
1272 }
1273
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001274 invoke->SetLocations(locations);
1275}
1276
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001277void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001279 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1280 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001281 LocationSummary* locations = invoke->GetLocations();
1282 Location receiver = locations->InAt(0);
1283 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1284 // temp = object->GetClass();
1285 if (receiver.IsStackSlot()) {
1286 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1287 __ movl(temp, Address(temp, class_offset));
1288 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001290 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001291 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001292 // temp = temp->GetMethodAt(method_offset);
1293 __ movl(temp, Address(temp, method_offset));
1294 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001295 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001296 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001300}
1301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001302void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1303 HandleInvoke(invoke);
1304 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001305 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001306}
1307
1308void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1312 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001313 LocationSummary* locations = invoke->GetLocations();
1314 Location receiver = locations->InAt(0);
1315 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1316
1317 // Set the hidden argument.
1318 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320
1321 // temp = object->GetClass();
1322 if (receiver.IsStackSlot()) {
1323 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1324 __ movl(temp, Address(temp, class_offset));
1325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001326 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001329 // temp = temp->GetImtEntryAt(method_offset);
1330 __ movl(temp, Address(temp, method_offset));
1331 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001332 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001333 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001334
1335 DCHECK(!codegen_->IsLeafMethod());
1336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1337}
1338
Roland Levillain88cb1752014-10-20 16:36:47 +01001339void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1342 switch (neg->GetResultType()) {
1343 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001344 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetOut(Location::SameAsFirstInput());
1347 break;
1348
Roland Levillain88cb1752014-10-20 16:36:47 +01001349 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001350 locations->SetInAt(0, Location::RequiresFpuRegister());
1351 locations->SetOut(Location::SameAsFirstInput());
1352 locations->AddTemp(Location::RequiresRegister());
1353 locations->AddTemp(Location::RequiresFpuRegister());
1354 break;
1355
Roland Levillain88cb1752014-10-20 16:36:47 +01001356 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001357 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001358 locations->SetOut(Location::SameAsFirstInput());
1359 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1364 }
1365}
1366
1367void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1368 LocationSummary* locations = neg->GetLocations();
1369 Location out = locations->Out();
1370 Location in = locations->InAt(0);
1371 switch (neg->GetResultType()) {
1372 case Primitive::kPrimInt:
1373 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001374 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001376 break;
1377
1378 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001379 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001380 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001381 __ negl(out.AsRegisterPairLow<Register>());
1382 // Negation is similar to subtraction from zero. The least
1383 // significant byte triggers a borrow when it is different from
1384 // zero; to take it into account, add 1 to the most significant
1385 // byte if the carry flag (CF) is set to 1 after the first NEGL
1386 // operation.
1387 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1388 __ negl(out.AsRegisterPairHigh<Register>());
1389 break;
1390
Roland Levillain5368c212014-11-27 15:03:41 +00001391 case Primitive::kPrimFloat: {
1392 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 Register constant = locations->GetTemp(0).AsRegister<Register>();
1394 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001395 // Implement float negation with an exclusive or with value
1396 // 0x80000000 (mask for bit 31, representing the sign of a
1397 // single-precision floating-point number).
1398 __ movl(constant, Immediate(INT32_C(0x80000000)));
1399 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001400 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001401 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001402 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001403
Roland Levillain5368c212014-11-27 15:03:41 +00001404 case Primitive::kPrimDouble: {
1405 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001406 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001407 // Implement double negation with an exclusive or with value
1408 // 0x8000000000000000 (mask for bit 63, representing the sign of
1409 // a double-precision floating-point number).
1410 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001411 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001412 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001413 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001414
1415 default:
1416 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1417 }
1418}
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001421 Primitive::Type result_type = conversion->GetResultType();
1422 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001423 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001424
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001425 // The float-to-long and double-to-long type conversions rely on a
1426 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001427 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1429 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001430 ? LocationSummary::kCall
1431 : LocationSummary::kNoCall;
1432 LocationSummary* locations =
1433 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1434
David Brazdilb2bd1c52015-03-25 11:17:37 +00001435 // The Java language does not allow treating boolean as an integral type but
1436 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001437
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001439 case Primitive::kPrimByte:
1440 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001441 case Primitive::kPrimBoolean:
1442 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1448 // Make the output overlap to please the register allocator. This greatly simplifies
1449 // the validation of the linear scan implementation
1450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001451 break;
1452
1453 default:
1454 LOG(FATAL) << "Unexpected type conversion from " << input_type
1455 << " to " << result_type;
1456 }
1457 break;
1458
Roland Levillain01a8d712014-11-14 16:27:39 +00001459 case Primitive::kPrimShort:
1460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001461 case Primitive::kPrimBoolean:
1462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001463 case Primitive::kPrimByte:
1464 case Primitive::kPrimInt:
1465 case Primitive::kPrimChar:
1466 // Processing a Dex `int-to-short' instruction.
1467 locations->SetInAt(0, Location::Any());
1468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillain946e1432014-11-11 17:35:19 +00001477 case Primitive::kPrimInt:
1478 switch (input_type) {
1479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001481 locations->SetInAt(0, Location::Any());
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 break;
1484
1485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001486 // Processing a Dex `float-to-int' instruction.
1487 locations->SetInAt(0, Location::RequiresFpuRegister());
1488 locations->SetOut(Location::RequiresRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 break;
1491
Roland Levillain946e1432014-11-11 17:35:19 +00001492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001493 // Processing a Dex `double-to-int' instruction.
1494 locations->SetInAt(0, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::RequiresRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503 break;
1504
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 case Primitive::kPrimLong:
1506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001507 case Primitive::kPrimBoolean:
1508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001509 case Primitive::kPrimByte:
1510 case Primitive::kPrimShort:
1511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 locations->SetInAt(0, Location::RegisterLocation(EAX));
1515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1516 break;
1517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1524
Vladimir Marko949c91f2015-01-27 10:48:44 +00001525 // The runtime helper puts the result in EAX, EDX.
1526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001529
1530 default:
1531 LOG(FATAL) << "Unexpected type conversion from " << input_type
1532 << " to " << result_type;
1533 }
1534 break;
1535
Roland Levillain981e4542014-11-14 11:47:14 +00001536 case Primitive::kPrimChar:
1537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001538 case Primitive::kPrimBoolean:
1539 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001540 case Primitive::kPrimByte:
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001543 // Processing a Dex `int-to-char' instruction.
1544 locations->SetInAt(0, Location::Any());
1545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1546 break;
1547
1548 default:
1549 LOG(FATAL) << "Unexpected type conversion from " << input_type
1550 << " to " << result_type;
1551 }
1552 break;
1553
Roland Levillaindff1f282014-11-05 14:15:05 +00001554 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001555 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001556 case Primitive::kPrimBoolean:
1557 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001558 case Primitive::kPrimByte:
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimChar:
1562 // Processing a Dex `int-to-float' instruction.
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetOut(Location::RequiresFpuRegister());
1565 break;
1566
1567 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001568 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001569 locations->SetInAt(0, Location::Any());
1570 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001571 break;
1572
Roland Levillaincff13742014-11-17 14:32:17 +00001573 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001574 // Processing a Dex `double-to-float' instruction.
1575 locations->SetInAt(0, Location::RequiresFpuRegister());
1576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 };
1583 break;
1584
Roland Levillaindff1f282014-11-05 14:15:05 +00001585 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001587 case Primitive::kPrimBoolean:
1588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001589 case Primitive::kPrimByte:
1590 case Primitive::kPrimShort:
1591 case Primitive::kPrimInt:
1592 case Primitive::kPrimChar:
1593 // Processing a Dex `int-to-double' instruction.
1594 locations->SetInAt(0, Location::RequiresRegister());
1595 locations->SetOut(Location::RequiresFpuRegister());
1596 break;
1597
1598 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001599 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001600 locations->SetInAt(0, Location::Any());
1601 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001602 break;
1603
Roland Levillaincff13742014-11-17 14:32:17 +00001604 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001605 // Processing a Dex `float-to-double' instruction.
1606 locations->SetInAt(0, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001608 break;
1609
1610 default:
1611 LOG(FATAL) << "Unexpected type conversion from " << input_type
1612 << " to " << result_type;
1613 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620}
1621
1622void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1623 LocationSummary* locations = conversion->GetLocations();
1624 Location out = locations->Out();
1625 Location in = locations->InAt(0);
1626 Primitive::Type result_type = conversion->GetResultType();
1627 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001628 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001630 case Primitive::kPrimByte:
1631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001632 case Primitive::kPrimBoolean:
1633 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001637 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001638 if (in.IsRegister()) {
1639 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001640 } else {
1641 DCHECK(in.GetConstant()->IsIntConstant());
1642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1644 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001645 break;
1646
1647 default:
1648 LOG(FATAL) << "Unexpected type conversion from " << input_type
1649 << " to " << result_type;
1650 }
1651 break;
1652
Roland Levillain01a8d712014-11-14 16:27:39 +00001653 case Primitive::kPrimShort:
1654 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001655 case Primitive::kPrimBoolean:
1656 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001657 case Primitive::kPrimByte:
1658 case Primitive::kPrimInt:
1659 case Primitive::kPrimChar:
1660 // Processing a Dex `int-to-short' instruction.
1661 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001663 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001664 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001665 } else {
1666 DCHECK(in.GetConstant()->IsIntConstant());
1667 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001669 }
1670 break;
1671
1672 default:
1673 LOG(FATAL) << "Unexpected type conversion from " << input_type
1674 << " to " << result_type;
1675 }
1676 break;
1677
Roland Levillain946e1432014-11-11 17:35:19 +00001678 case Primitive::kPrimInt:
1679 switch (input_type) {
1680 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001681 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001682 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001684 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001686 } else {
1687 DCHECK(in.IsConstant());
1688 DCHECK(in.GetConstant()->IsLongConstant());
1689 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001691 }
1692 break;
1693
Roland Levillain3f8f9362014-12-02 17:45:01 +00001694 case Primitive::kPrimFloat: {
1695 // Processing a Dex `float-to-int' instruction.
1696 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1697 Register output = out.AsRegister<Register>();
1698 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1699 Label done, nan;
1700
1701 __ movl(output, Immediate(kPrimIntMax));
1702 // temp = int-to-float(output)
1703 __ cvtsi2ss(temp, output);
1704 // if input >= temp goto done
1705 __ comiss(input, temp);
1706 __ j(kAboveEqual, &done);
1707 // if input == NaN goto nan
1708 __ j(kUnordered, &nan);
1709 // output = float-to-int-truncate(input)
1710 __ cvttss2si(output, input);
1711 __ jmp(&done);
1712 __ Bind(&nan);
1713 // output = 0
1714 __ xorl(output, output);
1715 __ Bind(&done);
1716 break;
1717 }
1718
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001719 case Primitive::kPrimDouble: {
1720 // Processing a Dex `double-to-int' instruction.
1721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1722 Register output = out.AsRegister<Register>();
1723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1724 Label done, nan;
1725
1726 __ movl(output, Immediate(kPrimIntMax));
1727 // temp = int-to-double(output)
1728 __ cvtsi2sd(temp, output);
1729 // if input >= temp goto done
1730 __ comisd(input, temp);
1731 __ j(kAboveEqual, &done);
1732 // if input == NaN goto nan
1733 __ j(kUnordered, &nan);
1734 // output = double-to-int-truncate(input)
1735 __ cvttsd2si(output, input);
1736 __ jmp(&done);
1737 __ Bind(&nan);
1738 // output = 0
1739 __ xorl(output, output);
1740 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001741 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001742 }
Roland Levillain946e1432014-11-11 17:35:19 +00001743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimLong:
1751 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001752 case Primitive::kPrimBoolean:
1753 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001754 case Primitive::kPrimByte:
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001757 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001758 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001759 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1760 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001761 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001762 __ cdq();
1763 break;
1764
1765 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001766 // Processing a Dex `float-to-long' instruction.
1767 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001768 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1769 break;
1770
Roland Levillaindff1f282014-11-05 14:15:05 +00001771 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001772 // Processing a Dex `double-to-long' instruction.
1773 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1774 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 break;
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type conversion from " << input_type
1779 << " to " << result_type;
1780 }
1781 break;
1782
Roland Levillain981e4542014-11-14 11:47:14 +00001783 case Primitive::kPrimChar:
1784 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001785 case Primitive::kPrimBoolean:
1786 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimByte:
1788 case Primitive::kPrimShort:
1789 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1791 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001792 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001793 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001795 } else {
1796 DCHECK(in.GetConstant()->IsIntConstant());
1797 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001799 }
1800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillaindff1f282014-11-05 14:15:05 +00001808 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001816 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001818 break;
1819
Roland Levillain6d0e4832014-11-27 18:31:21 +00001820 case Primitive::kPrimLong: {
1821 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001822 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001823
Roland Levillain232ade02015-04-20 15:14:36 +01001824 // Create stack space for the call to
1825 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1826 // TODO: enhance register allocator to ask for stack temporaries.
1827 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1828 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1829 __ subl(ESP, Immediate(adjustment));
1830 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831
Roland Levillain232ade02015-04-20 15:14:36 +01001832 // Load the value to the FP stack, using temporaries if needed.
1833 PushOntoFPStack(in, 0, adjustment, false, true);
1834
1835 if (out.IsStackSlot()) {
1836 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1837 } else {
1838 __ fstps(Address(ESP, 0));
1839 Location stack_temp = Location::StackSlot(0);
1840 codegen_->Move32(out, stack_temp);
1841 }
1842
1843 // Remove the temporary stack space we allocated.
1844 if (adjustment != 0) {
1845 __ addl(ESP, Immediate(adjustment));
1846 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 break;
1848 }
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 };
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
1868 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001869 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001871 break;
1872
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 case Primitive::kPrimLong: {
1874 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001875 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876
Roland Levillain232ade02015-04-20 15:14:36 +01001877 // Create stack space for the call to
1878 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1879 // TODO: enhance register allocator to ask for stack temporaries.
1880 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1881 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1882 __ subl(ESP, Immediate(adjustment));
1883 }
1884
1885 // Load the value to the FP stack, using temporaries if needed.
1886 PushOntoFPStack(in, 0, adjustment, false, true);
1887
1888 if (out.IsDoubleStackSlot()) {
1889 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1890 } else {
1891 __ fstpl(Address(ESP, 0));
1892 Location stack_temp = Location::DoubleStackSlot(0);
1893 codegen_->Move64(out, stack_temp);
1894 }
1895
1896 // Remove the temporary stack space we allocated.
1897 if (adjustment != 0) {
1898 __ addl(ESP, Immediate(adjustment));
1899 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001900 break;
1901 }
1902
Roland Levillaincff13742014-11-17 14:32:17 +00001903 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001904 // Processing a Dex `float-to-double' instruction.
1905 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001912 break;
1913
1914 default:
1915 LOG(FATAL) << "Unexpected type conversion from " << input_type
1916 << " to " << result_type;
1917 }
1918}
1919
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001920void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001923 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001924 case Primitive::kPrimInt: {
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1928 break;
1929 }
1930
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetInAt(1, Location::Any());
1934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936 }
1937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 case Primitive::kPrimFloat:
1939 case Primitive::kPrimDouble: {
1940 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001941 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1948 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001949 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001950}
1951
1952void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1953 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 Location first = locations->InAt(0);
1955 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001956 Location out = locations->Out();
1957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001958 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001961 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1962 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001963 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1964 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001965 } else {
1966 __ leal(out.AsRegister<Register>(), Address(
1967 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1968 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001970 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1971 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1972 __ addl(out.AsRegister<Register>(), Immediate(value));
1973 } else {
1974 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1975 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001976 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001977 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001978 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001979 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001980 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001981 }
1982
1983 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001984 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001985 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1986 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001987 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001988 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1989 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001990 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001991 } else {
1992 DCHECK(second.IsConstant()) << second;
1993 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1994 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1995 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001996 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997 break;
1998 }
1999
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002000 case Primitive::kPrimFloat: {
2001 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002002 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002003 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002004 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002005 }
2006
2007 case Primitive::kPrimDouble: {
2008 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002009 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002010 }
2011 break;
2012 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002013
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002014 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002015 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002016 }
2017}
2018
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002019void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002022 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002023 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002024 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002025 locations->SetInAt(0, Location::RequiresRegister());
2026 locations->SetInAt(1, Location::Any());
2027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002028 break;
2029 }
Calin Juravle11351682014-10-23 15:38:15 +01002030 case Primitive::kPrimFloat:
2031 case Primitive::kPrimDouble: {
2032 locations->SetInAt(0, Location::RequiresFpuRegister());
2033 locations->SetInAt(1, Location::RequiresFpuRegister());
2034 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035 break;
Calin Juravle11351682014-10-23 15:38:15 +01002036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002038 default:
Calin Juravle11351682014-10-23 15:38:15 +01002039 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002040 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002041}
2042
2043void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2044 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002045 Location first = locations->InAt(0);
2046 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002047 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002048 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002051 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002053 __ subl(first.AsRegister<Register>(),
2054 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002057 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002058 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059 }
2060
2061 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002062 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002063 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2064 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002065 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002066 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 __ sbbl(first.AsRegisterPairHigh<Register>(),
2068 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002069 } else {
2070 DCHECK(second.IsConstant()) << second;
2071 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2072 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2073 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075 break;
2076 }
2077
Calin Juravle11351682014-10-23 15:38:15 +01002078 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002080 break;
Calin Juravle11351682014-10-23 15:38:15 +01002081 }
2082
2083 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002085 break;
2086 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002087
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002088 default:
Calin Juravle11351682014-10-23 15:38:15 +01002089 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002090 }
2091}
2092
Calin Juravle34bacdf2014-10-07 20:23:36 +01002093void LocationsBuilderX86::VisitMul(HMul* mul) {
2094 LocationSummary* locations =
2095 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2096 switch (mul->GetResultType()) {
2097 case Primitive::kPrimInt:
2098 locations->SetInAt(0, Location::RequiresRegister());
2099 locations->SetInAt(1, Location::Any());
2100 locations->SetOut(Location::SameAsFirstInput());
2101 break;
2102 case Primitive::kPrimLong: {
2103 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002104 locations->SetInAt(1, Location::Any());
2105 locations->SetOut(Location::SameAsFirstInput());
2106 // Needed for imul on 32bits with 64bits output.
2107 locations->AddTemp(Location::RegisterLocation(EAX));
2108 locations->AddTemp(Location::RegisterLocation(EDX));
2109 break;
2110 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002111 case Primitive::kPrimFloat:
2112 case Primitive::kPrimDouble: {
2113 locations->SetInAt(0, Location::RequiresFpuRegister());
2114 locations->SetInAt(1, Location::RequiresFpuRegister());
2115 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002116 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002117 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002118
2119 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002120 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002121 }
2122}
2123
2124void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2125 LocationSummary* locations = mul->GetLocations();
2126 Location first = locations->InAt(0);
2127 Location second = locations->InAt(1);
2128 DCHECK(first.Equals(locations->Out()));
2129
2130 switch (mul->GetResultType()) {
2131 case Primitive::kPrimInt: {
2132 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002134 } else if (second.IsConstant()) {
2135 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002137 } else {
2138 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002140 }
2141 break;
2142 }
2143
2144 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002145 Register in1_hi = first.AsRegisterPairHigh<Register>();
2146 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002147 Register eax = locations->GetTemp(0).AsRegister<Register>();
2148 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002149
2150 DCHECK_EQ(EAX, eax);
2151 DCHECK_EQ(EDX, edx);
2152
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002153 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002154 // output: in1
2155 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2156 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2157 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002158 if (second.IsConstant()) {
2159 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002160
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002161 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2162 int32_t low_value = Low32Bits(value);
2163 int32_t high_value = High32Bits(value);
2164 Immediate low(low_value);
2165 Immediate high(high_value);
2166
2167 __ movl(eax, high);
2168 // eax <- in1.lo * in2.hi
2169 __ imull(eax, in1_lo);
2170 // in1.hi <- in1.hi * in2.lo
2171 __ imull(in1_hi, low);
2172 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2173 __ addl(in1_hi, eax);
2174 // move in2_lo to eax to prepare for double precision
2175 __ movl(eax, low);
2176 // edx:eax <- in1.lo * in2.lo
2177 __ mull(in1_lo);
2178 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2179 __ addl(in1_hi, edx);
2180 // in1.lo <- (in1.lo * in2.lo)[31:0];
2181 __ movl(in1_lo, eax);
2182 } else if (second.IsRegisterPair()) {
2183 Register in2_hi = second.AsRegisterPairHigh<Register>();
2184 Register in2_lo = second.AsRegisterPairLow<Register>();
2185
2186 __ movl(eax, in2_hi);
2187 // eax <- in1.lo * in2.hi
2188 __ imull(eax, in1_lo);
2189 // in1.hi <- in1.hi * in2.lo
2190 __ imull(in1_hi, in2_lo);
2191 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2192 __ addl(in1_hi, eax);
2193 // move in1_lo to eax to prepare for double precision
2194 __ movl(eax, in1_lo);
2195 // edx:eax <- in1.lo * in2.lo
2196 __ mull(in2_lo);
2197 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2198 __ addl(in1_hi, edx);
2199 // in1.lo <- (in1.lo * in2.lo)[31:0];
2200 __ movl(in1_lo, eax);
2201 } else {
2202 DCHECK(second.IsDoubleStackSlot()) << second;
2203 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2204 Address in2_lo(ESP, second.GetStackIndex());
2205
2206 __ movl(eax, in2_hi);
2207 // eax <- in1.lo * in2.hi
2208 __ imull(eax, in1_lo);
2209 // in1.hi <- in1.hi * in2.lo
2210 __ imull(in1_hi, in2_lo);
2211 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2212 __ addl(in1_hi, eax);
2213 // move in1_lo to eax to prepare for double precision
2214 __ movl(eax, in1_lo);
2215 // edx:eax <- in1.lo * in2.lo
2216 __ mull(in2_lo);
2217 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2218 __ addl(in1_hi, edx);
2219 // in1.lo <- (in1.lo * in2.lo)[31:0];
2220 __ movl(in1_lo, eax);
2221 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002222
2223 break;
2224 }
2225
Calin Juravleb5bfa962014-10-21 18:02:24 +01002226 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002227 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002228 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002229 }
2230
2231 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002233 break;
2234 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002235
2236 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002237 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002238 }
2239}
2240
Roland Levillain232ade02015-04-20 15:14:36 +01002241void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2242 uint32_t temp_offset,
2243 uint32_t stack_adjustment,
2244 bool is_fp,
2245 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002246 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002247 DCHECK(!is_wide);
2248 if (is_fp) {
2249 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2250 } else {
2251 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2252 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002253 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002254 DCHECK(is_wide);
2255 if (is_fp) {
2256 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2257 } else {
2258 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2259 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002260 } else {
2261 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002262 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002263 Location stack_temp = Location::StackSlot(temp_offset);
2264 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002265 if (is_fp) {
2266 __ flds(Address(ESP, temp_offset));
2267 } else {
2268 __ filds(Address(ESP, temp_offset));
2269 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002270 } else {
2271 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2272 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002273 if (is_fp) {
2274 __ fldl(Address(ESP, temp_offset));
2275 } else {
2276 __ fildl(Address(ESP, temp_offset));
2277 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002278 }
2279 }
2280}
2281
2282void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2283 Primitive::Type type = rem->GetResultType();
2284 bool is_float = type == Primitive::kPrimFloat;
2285 size_t elem_size = Primitive::ComponentSize(type);
2286 LocationSummary* locations = rem->GetLocations();
2287 Location first = locations->InAt(0);
2288 Location second = locations->InAt(1);
2289 Location out = locations->Out();
2290
2291 // Create stack space for 2 elements.
2292 // TODO: enhance register allocator to ask for stack temporaries.
2293 __ subl(ESP, Immediate(2 * elem_size));
2294
2295 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002296 const bool is_wide = !is_float;
2297 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2298 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002299
2300 // Loop doing FPREM until we stabilize.
2301 Label retry;
2302 __ Bind(&retry);
2303 __ fprem();
2304
2305 // Move FP status to AX.
2306 __ fstsw();
2307
2308 // And see if the argument reduction is complete. This is signaled by the
2309 // C2 FPU flag bit set to 0.
2310 __ andl(EAX, Immediate(kC2ConditionMask));
2311 __ j(kNotEqual, &retry);
2312
2313 // We have settled on the final value. Retrieve it into an XMM register.
2314 // Store FP top of stack to real stack.
2315 if (is_float) {
2316 __ fsts(Address(ESP, 0));
2317 } else {
2318 __ fstl(Address(ESP, 0));
2319 }
2320
2321 // Pop the 2 items from the FP stack.
2322 __ fucompp();
2323
2324 // Load the value from the stack into an XMM register.
2325 DCHECK(out.IsFpuRegister()) << out;
2326 if (is_float) {
2327 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2328 } else {
2329 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2330 }
2331
2332 // And remove the temporary stack space we allocated.
2333 __ addl(ESP, Immediate(2 * elem_size));
2334}
2335
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002336
2337void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2338 DCHECK(instruction->IsDiv() || instruction->IsRem());
2339
2340 LocationSummary* locations = instruction->GetLocations();
2341 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002342 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002343
2344 Register out_register = locations->Out().AsRegister<Register>();
2345 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002346 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002347
2348 DCHECK(imm == 1 || imm == -1);
2349
2350 if (instruction->IsRem()) {
2351 __ xorl(out_register, out_register);
2352 } else {
2353 __ movl(out_register, input_register);
2354 if (imm == -1) {
2355 __ negl(out_register);
2356 }
2357 }
2358}
2359
2360
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002361void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002362 LocationSummary* locations = instruction->GetLocations();
2363
2364 Register out_register = locations->Out().AsRegister<Register>();
2365 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002366 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002367
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002368 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002369 Register num = locations->GetTemp(0).AsRegister<Register>();
2370
2371 __ leal(num, Address(input_register, std::abs(imm) - 1));
2372 __ testl(input_register, input_register);
2373 __ cmovl(kGreaterEqual, num, input_register);
2374 int shift = CTZ(imm);
2375 __ sarl(num, Immediate(shift));
2376
2377 if (imm < 0) {
2378 __ negl(num);
2379 }
2380
2381 __ movl(out_register, num);
2382}
2383
2384void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2385 DCHECK(instruction->IsDiv() || instruction->IsRem());
2386
2387 LocationSummary* locations = instruction->GetLocations();
2388 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2389
2390 Register eax = locations->InAt(0).AsRegister<Register>();
2391 Register out = locations->Out().AsRegister<Register>();
2392 Register num;
2393 Register edx;
2394
2395 if (instruction->IsDiv()) {
2396 edx = locations->GetTemp(0).AsRegister<Register>();
2397 num = locations->GetTemp(1).AsRegister<Register>();
2398 } else {
2399 edx = locations->Out().AsRegister<Register>();
2400 num = locations->GetTemp(0).AsRegister<Register>();
2401 }
2402
2403 DCHECK_EQ(EAX, eax);
2404 DCHECK_EQ(EDX, edx);
2405 if (instruction->IsDiv()) {
2406 DCHECK_EQ(EAX, out);
2407 } else {
2408 DCHECK_EQ(EDX, out);
2409 }
2410
2411 int64_t magic;
2412 int shift;
2413 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2414
2415 Label ndiv;
2416 Label end;
2417 // If numerator is 0, the result is 0, no computation needed.
2418 __ testl(eax, eax);
2419 __ j(kNotEqual, &ndiv);
2420
2421 __ xorl(out, out);
2422 __ jmp(&end);
2423
2424 __ Bind(&ndiv);
2425
2426 // Save the numerator.
2427 __ movl(num, eax);
2428
2429 // EAX = magic
2430 __ movl(eax, Immediate(magic));
2431
2432 // EDX:EAX = magic * numerator
2433 __ imull(num);
2434
2435 if (imm > 0 && magic < 0) {
2436 // EDX += num
2437 __ addl(edx, num);
2438 } else if (imm < 0 && magic > 0) {
2439 __ subl(edx, num);
2440 }
2441
2442 // Shift if needed.
2443 if (shift != 0) {
2444 __ sarl(edx, Immediate(shift));
2445 }
2446
2447 // EDX += 1 if EDX < 0
2448 __ movl(eax, edx);
2449 __ shrl(edx, Immediate(31));
2450 __ addl(edx, eax);
2451
2452 if (instruction->IsRem()) {
2453 __ movl(eax, num);
2454 __ imull(edx, Immediate(imm));
2455 __ subl(eax, edx);
2456 __ movl(edx, eax);
2457 } else {
2458 __ movl(eax, edx);
2459 }
2460 __ Bind(&end);
2461}
2462
Calin Juravlebacfec32014-11-14 15:54:36 +00002463void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2464 DCHECK(instruction->IsDiv() || instruction->IsRem());
2465
2466 LocationSummary* locations = instruction->GetLocations();
2467 Location out = locations->Out();
2468 Location first = locations->InAt(0);
2469 Location second = locations->InAt(1);
2470 bool is_div = instruction->IsDiv();
2471
2472 switch (instruction->GetResultType()) {
2473 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002474 DCHECK_EQ(EAX, first.AsRegister<Register>());
2475 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002476
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002477 if (instruction->InputAt(1)->IsIntConstant()) {
2478 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002479
2480 if (imm == 0) {
2481 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2482 } else if (imm == 1 || imm == -1) {
2483 DivRemOneOrMinusOne(instruction);
2484 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002485 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002486 } else {
2487 DCHECK(imm <= -2 || imm >= 2);
2488 GenerateDivRemWithAnyConstant(instruction);
2489 }
2490 } else {
2491 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002492 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002493 is_div);
2494 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002495
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002496 Register second_reg = second.AsRegister<Register>();
2497 // 0x80000000/-1 triggers an arithmetic exception!
2498 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2499 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002500
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002501 __ cmpl(second_reg, Immediate(-1));
2502 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002503
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002504 // edx:eax <- sign-extended of eax
2505 __ cdq();
2506 // eax = quotient, edx = remainder
2507 __ idivl(second_reg);
2508 __ Bind(slow_path->GetExitLabel());
2509 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002510 break;
2511 }
2512
2513 case Primitive::kPrimLong: {
2514 InvokeRuntimeCallingConvention calling_convention;
2515 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2516 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2517 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2518 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2519 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2520 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2521
2522 if (is_div) {
2523 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2524 } else {
2525 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2526 }
2527 uint32_t dex_pc = is_div
2528 ? instruction->AsDiv()->GetDexPc()
2529 : instruction->AsRem()->GetDexPc();
2530 codegen_->RecordPcInfo(instruction, dex_pc);
2531
2532 break;
2533 }
2534
2535 default:
2536 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2537 }
2538}
2539
Calin Juravle7c4954d2014-10-28 16:57:40 +00002540void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002541 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002542 ? LocationSummary::kCall
2543 : LocationSummary::kNoCall;
2544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2545
Calin Juravle7c4954d2014-10-28 16:57:40 +00002546 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002547 case Primitive::kPrimInt: {
2548 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002549 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002550 locations->SetOut(Location::SameAsFirstInput());
2551 // Intel uses edx:eax as the dividend.
2552 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002553 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2554 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2555 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002556 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002557 locations->AddTemp(Location::RequiresRegister());
2558 }
Calin Juravled0d48522014-11-04 16:40:20 +00002559 break;
2560 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002561 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002562 InvokeRuntimeCallingConvention calling_convention;
2563 locations->SetInAt(0, Location::RegisterPairLocation(
2564 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2565 locations->SetInAt(1, Location::RegisterPairLocation(
2566 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2567 // Runtime helper puts the result in EAX, EDX.
2568 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002569 break;
2570 }
2571 case Primitive::kPrimFloat:
2572 case Primitive::kPrimDouble: {
2573 locations->SetInAt(0, Location::RequiresFpuRegister());
2574 locations->SetInAt(1, Location::RequiresFpuRegister());
2575 locations->SetOut(Location::SameAsFirstInput());
2576 break;
2577 }
2578
2579 default:
2580 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2581 }
2582}
2583
2584void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2585 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002586 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587 Location first = locations->InAt(0);
2588 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002589
2590 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002591 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002593 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 break;
2595 }
2596
2597 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002598 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002600 break;
2601 }
2602
2603 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002604 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002606 break;
2607 }
2608
2609 default:
2610 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2611 }
2612}
2613
Calin Juravlebacfec32014-11-14 15:54:36 +00002614void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002615 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002616
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002617 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2618 ? LocationSummary::kCall
2619 : LocationSummary::kNoCall;
2620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002621
Calin Juravled2ec87d2014-12-08 14:24:46 +00002622 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 case Primitive::kPrimInt: {
2624 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002625 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002626 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002627 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2628 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2629 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002630 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002631 locations->AddTemp(Location::RequiresRegister());
2632 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002633 break;
2634 }
2635 case Primitive::kPrimLong: {
2636 InvokeRuntimeCallingConvention calling_convention;
2637 locations->SetInAt(0, Location::RegisterPairLocation(
2638 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2639 locations->SetInAt(1, Location::RegisterPairLocation(
2640 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2641 // Runtime helper puts the result in EAX, EDX.
2642 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2643 break;
2644 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002646 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002647 locations->SetInAt(0, Location::Any());
2648 locations->SetInAt(1, Location::Any());
2649 locations->SetOut(Location::RequiresFpuRegister());
2650 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002651 break;
2652 }
2653
2654 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002655 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002656 }
2657}
2658
2659void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2660 Primitive::Type type = rem->GetResultType();
2661 switch (type) {
2662 case Primitive::kPrimInt:
2663 case Primitive::kPrimLong: {
2664 GenerateDivRemIntegral(rem);
2665 break;
2666 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002667 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002668 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002669 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002670 break;
2671 }
2672 default:
2673 LOG(FATAL) << "Unexpected rem type " << type;
2674 }
2675}
2676
Calin Juravled0d48522014-11-04 16:40:20 +00002677void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002680 switch (instruction->GetType()) {
2681 case Primitive::kPrimInt: {
2682 locations->SetInAt(0, Location::Any());
2683 break;
2684 }
2685 case Primitive::kPrimLong: {
2686 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2687 if (!instruction->IsConstant()) {
2688 locations->AddTemp(Location::RequiresRegister());
2689 }
2690 break;
2691 }
2692 default:
2693 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2694 }
Calin Juravled0d48522014-11-04 16:40:20 +00002695 if (instruction->HasUses()) {
2696 locations->SetOut(Location::SameAsFirstInput());
2697 }
2698}
2699
2700void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2701 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2702 codegen_->AddSlowPath(slow_path);
2703
2704 LocationSummary* locations = instruction->GetLocations();
2705 Location value = locations->InAt(0);
2706
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002707 switch (instruction->GetType()) {
2708 case Primitive::kPrimInt: {
2709 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002711 __ j(kEqual, slow_path->GetEntryLabel());
2712 } else if (value.IsStackSlot()) {
2713 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2714 __ j(kEqual, slow_path->GetEntryLabel());
2715 } else {
2716 DCHECK(value.IsConstant()) << value;
2717 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2718 __ jmp(slow_path->GetEntryLabel());
2719 }
2720 }
2721 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002722 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002723 case Primitive::kPrimLong: {
2724 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002726 __ movl(temp, value.AsRegisterPairLow<Register>());
2727 __ orl(temp, value.AsRegisterPairHigh<Register>());
2728 __ j(kEqual, slow_path->GetEntryLabel());
2729 } else {
2730 DCHECK(value.IsConstant()) << value;
2731 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2732 __ jmp(slow_path->GetEntryLabel());
2733 }
2734 }
2735 break;
2736 }
2737 default:
2738 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002739 }
Calin Juravled0d48522014-11-04 16:40:20 +00002740}
2741
Calin Juravle9aec02f2014-11-18 23:06:35 +00002742void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2743 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2744
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2747
2748 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002749 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002750 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002751 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002752 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002753 // The shift count needs to be in CL or a constant.
2754 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002755 locations->SetOut(Location::SameAsFirstInput());
2756 break;
2757 }
2758 default:
2759 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2760 }
2761}
2762
2763void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2764 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2765
2766 LocationSummary* locations = op->GetLocations();
2767 Location first = locations->InAt(0);
2768 Location second = locations->InAt(1);
2769 DCHECK(first.Equals(locations->Out()));
2770
2771 switch (op->GetResultType()) {
2772 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002773 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002774 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002775 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002777 DCHECK_EQ(ECX, second_reg);
2778 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002779 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002780 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002781 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002783 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002784 }
2785 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002786 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2787 if (shift == 0) {
2788 return;
2789 }
2790 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002791 if (op->IsShl()) {
2792 __ shll(first_reg, imm);
2793 } else if (op->IsShr()) {
2794 __ sarl(first_reg, imm);
2795 } else {
2796 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002797 }
2798 }
2799 break;
2800 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002801 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002802 if (second.IsRegister()) {
2803 Register second_reg = second.AsRegister<Register>();
2804 DCHECK_EQ(ECX, second_reg);
2805 if (op->IsShl()) {
2806 GenerateShlLong(first, second_reg);
2807 } else if (op->IsShr()) {
2808 GenerateShrLong(first, second_reg);
2809 } else {
2810 GenerateUShrLong(first, second_reg);
2811 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002812 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002813 // Shift by a constant.
2814 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2815 // Nothing to do if the shift is 0, as the input is already the output.
2816 if (shift != 0) {
2817 if (op->IsShl()) {
2818 GenerateShlLong(first, shift);
2819 } else if (op->IsShr()) {
2820 GenerateShrLong(first, shift);
2821 } else {
2822 GenerateUShrLong(first, shift);
2823 }
2824 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002825 }
2826 break;
2827 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002828 default:
2829 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2830 }
2831}
2832
Mark P Mendell73945692015-04-29 14:56:17 +00002833void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2834 Register low = loc.AsRegisterPairLow<Register>();
2835 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002836 if (shift == 1) {
2837 // This is just an addition.
2838 __ addl(low, low);
2839 __ adcl(high, high);
2840 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002841 // Shift by 32 is easy. High gets low, and low gets 0.
2842 codegen_->EmitParallelMoves(
2843 loc.ToLow(),
2844 loc.ToHigh(),
2845 Primitive::kPrimInt,
2846 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2847 loc.ToLow(),
2848 Primitive::kPrimInt);
2849 } else if (shift > 32) {
2850 // Low part becomes 0. High part is low part << (shift-32).
2851 __ movl(high, low);
2852 __ shll(high, Immediate(shift - 32));
2853 __ xorl(low, low);
2854 } else {
2855 // Between 1 and 31.
2856 __ shld(high, low, Immediate(shift));
2857 __ shll(low, Immediate(shift));
2858 }
2859}
2860
Calin Juravle9aec02f2014-11-18 23:06:35 +00002861void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2862 Label done;
2863 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2864 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2865 __ testl(shifter, Immediate(32));
2866 __ j(kEqual, &done);
2867 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2868 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2869 __ Bind(&done);
2870}
2871
Mark P Mendell73945692015-04-29 14:56:17 +00002872void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2873 Register low = loc.AsRegisterPairLow<Register>();
2874 Register high = loc.AsRegisterPairHigh<Register>();
2875 if (shift == 32) {
2876 // Need to copy the sign.
2877 DCHECK_NE(low, high);
2878 __ movl(low, high);
2879 __ sarl(high, Immediate(31));
2880 } else if (shift > 32) {
2881 DCHECK_NE(low, high);
2882 // High part becomes sign. Low part is shifted by shift - 32.
2883 __ movl(low, high);
2884 __ sarl(high, Immediate(31));
2885 __ sarl(low, Immediate(shift - 32));
2886 } else {
2887 // Between 1 and 31.
2888 __ shrd(low, high, Immediate(shift));
2889 __ sarl(high, Immediate(shift));
2890 }
2891}
2892
Calin Juravle9aec02f2014-11-18 23:06:35 +00002893void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2894 Label done;
2895 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2896 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2897 __ testl(shifter, Immediate(32));
2898 __ j(kEqual, &done);
2899 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2900 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2901 __ Bind(&done);
2902}
2903
Mark P Mendell73945692015-04-29 14:56:17 +00002904void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2905 Register low = loc.AsRegisterPairLow<Register>();
2906 Register high = loc.AsRegisterPairHigh<Register>();
2907 if (shift == 32) {
2908 // Shift by 32 is easy. Low gets high, and high gets 0.
2909 codegen_->EmitParallelMoves(
2910 loc.ToHigh(),
2911 loc.ToLow(),
2912 Primitive::kPrimInt,
2913 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2914 loc.ToHigh(),
2915 Primitive::kPrimInt);
2916 } else if (shift > 32) {
2917 // Low part is high >> (shift - 32). High part becomes 0.
2918 __ movl(low, high);
2919 __ shrl(low, Immediate(shift - 32));
2920 __ xorl(high, high);
2921 } else {
2922 // Between 1 and 31.
2923 __ shrd(low, high, Immediate(shift));
2924 __ shrl(high, Immediate(shift));
2925 }
2926}
2927
Calin Juravle9aec02f2014-11-18 23:06:35 +00002928void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2929 Label done;
2930 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2931 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2932 __ testl(shifter, Immediate(32));
2933 __ j(kEqual, &done);
2934 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2935 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2936 __ Bind(&done);
2937}
2938
2939void LocationsBuilderX86::VisitShl(HShl* shl) {
2940 HandleShift(shl);
2941}
2942
2943void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2944 HandleShift(shl);
2945}
2946
2947void LocationsBuilderX86::VisitShr(HShr* shr) {
2948 HandleShift(shr);
2949}
2950
2951void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2952 HandleShift(shr);
2953}
2954
2955void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2956 HandleShift(ushr);
2957}
2958
2959void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2960 HandleShift(ushr);
2961}
2962
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002963void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002964 LocationSummary* locations =
2965 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002966 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002967 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002968 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2969 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002970}
2971
2972void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2973 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002974 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002975 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002976
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002977 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002978
Nicolas Geoffray39468442014-09-02 15:17:15 +01002979 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002980 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002981}
2982
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002983void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2984 LocationSummary* locations =
2985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2986 locations->SetOut(Location::RegisterLocation(EAX));
2987 InvokeRuntimeCallingConvention calling_convention;
2988 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002989 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2990 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002991}
2992
2993void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2994 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002995 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002996 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2997
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002998 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002999
3000 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3001 DCHECK(!codegen_->IsLeafMethod());
3002}
3003
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003004void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003005 LocationSummary* locations =
3006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003007 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3008 if (location.IsStackSlot()) {
3009 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3010 } else if (location.IsDoubleStackSlot()) {
3011 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003012 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003013 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003014}
3015
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003016void InstructionCodeGeneratorX86::VisitParameterValue(
3017 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3018}
3019
3020void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3023 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3024}
3025
3026void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003027}
3028
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003029void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003030 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003031 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003032 locations->SetInAt(0, Location::RequiresRegister());
3033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003034}
3035
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003036void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3037 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003038 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003039 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003040 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003041 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003042 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003044 break;
3045
3046 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003047 __ notl(out.AsRegisterPairLow<Register>());
3048 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003049 break;
3050
3051 default:
3052 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3053 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003054}
3055
David Brazdil66d126e2015-04-03 16:02:44 +01003056void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3057 LocationSummary* locations =
3058 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3059 locations->SetInAt(0, Location::RequiresRegister());
3060 locations->SetOut(Location::SameAsFirstInput());
3061}
3062
3063void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003064 LocationSummary* locations = bool_not->GetLocations();
3065 Location in = locations->InAt(0);
3066 Location out = locations->Out();
3067 DCHECK(in.Equals(out));
3068 __ xorl(out.AsRegister<Register>(), Immediate(1));
3069}
3070
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003071void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003072 LocationSummary* locations =
3073 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003074 switch (compare->InputAt(0)->GetType()) {
3075 case Primitive::kPrimLong: {
3076 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003077 locations->SetInAt(1, Location::Any());
3078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3079 break;
3080 }
3081 case Primitive::kPrimFloat:
3082 case Primitive::kPrimDouble: {
3083 locations->SetInAt(0, Location::RequiresFpuRegister());
3084 locations->SetInAt(1, Location::RequiresFpuRegister());
3085 locations->SetOut(Location::RequiresRegister());
3086 break;
3087 }
3088 default:
3089 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3090 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003091}
3092
3093void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003094 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003096 Location left = locations->InAt(0);
3097 Location right = locations->InAt(1);
3098
3099 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003100 switch (compare->InputAt(0)->GetType()) {
3101 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003102 Register left_low = left.AsRegisterPairLow<Register>();
3103 Register left_high = left.AsRegisterPairHigh<Register>();
3104 int32_t val_low = 0;
3105 int32_t val_high = 0;
3106 bool right_is_const = false;
3107
3108 if (right.IsConstant()) {
3109 DCHECK(right.GetConstant()->IsLongConstant());
3110 right_is_const = true;
3111 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3112 val_low = Low32Bits(val);
3113 val_high = High32Bits(val);
3114 }
3115
Calin Juravleddb7df22014-11-25 20:56:51 +00003116 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003117 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003118 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003119 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003120 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003121 DCHECK(right_is_const) << right;
3122 if (val_high == 0) {
3123 __ testl(left_high, left_high);
3124 } else {
3125 __ cmpl(left_high, Immediate(val_high));
3126 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003128 __ j(kLess, &less); // Signed compare.
3129 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003130 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003131 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003132 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003133 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003134 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003135 DCHECK(right_is_const) << right;
3136 if (val_low == 0) {
3137 __ testl(left_low, left_low);
3138 } else {
3139 __ cmpl(left_low, Immediate(val_low));
3140 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003141 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003142 break;
3143 }
3144 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003145 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003146 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3147 break;
3148 }
3149 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003150 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003151 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003152 break;
3153 }
3154 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003155 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003156 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003157 __ movl(out, Immediate(0));
3158 __ j(kEqual, &done);
3159 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3160
3161 __ Bind(&greater);
3162 __ movl(out, Immediate(1));
3163 __ jmp(&done);
3164
3165 __ Bind(&less);
3166 __ movl(out, Immediate(-1));
3167
3168 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003169}
3170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003171void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 LocationSummary* locations =
3173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003174 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3175 locations->SetInAt(i, Location::Any());
3176 }
3177 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003178}
3179
3180void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003181 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003182 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003183}
3184
Calin Juravle52c48962014-12-16 17:02:57 +00003185void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3186 /*
3187 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3188 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3189 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3190 */
3191 switch (kind) {
3192 case MemBarrierKind::kAnyAny: {
3193 __ mfence();
3194 break;
3195 }
3196 case MemBarrierKind::kAnyStore:
3197 case MemBarrierKind::kLoadAny:
3198 case MemBarrierKind::kStoreStore: {
3199 // nop
3200 break;
3201 }
3202 default:
3203 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003204 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003205}
3206
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003207
Mark Mendell09ed1a32015-03-25 08:30:06 -04003208void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3209 Register temp) {
3210 // TODO: Implement all kinds of calls:
3211 // 1) boot -> boot
3212 // 2) app -> boot
3213 // 3) app -> app
3214 //
3215 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003216
3217 if (invoke->IsStringInit()) {
3218 // temp = thread->string_init_entrypoint
3219 __ fs()->movl(temp, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003220 // (temp + offset_of_quick_compiled_code)()
3221 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07003222 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003223 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08003224 // temp = method;
3225 LoadCurrentMethod(temp);
3226 if (!invoke->IsRecursive()) {
3227 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003228 __ movl(temp, Address(temp, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08003229 // temp = temp[index_in_cache]
Mathieu Chartiere401d142015-04-22 13:56:20 -07003230 __ movl(temp, Address(temp,
3231 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
Jeff Hao848f70a2014-01-15 13:49:50 -08003232 // (temp + offset_of_quick_compiled_code)()
3233 __ call(Address(temp,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003234 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08003235 } else {
3236 __ call(GetFrameEntryLabel());
3237 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04003238 }
3239
3240 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003241}
3242
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003243void CodeGeneratorX86::MarkGCCard(Register temp,
3244 Register card,
3245 Register object,
3246 Register value,
3247 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003249 if (value_can_be_null) {
3250 __ testl(value, value);
3251 __ j(kEqual, &is_null);
3252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003253 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3254 __ movl(temp, object);
3255 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003256 __ movb(Address(temp, card, TIMES_1, 0),
3257 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003258 if (value_can_be_null) {
3259 __ Bind(&is_null);
3260 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003261}
3262
Calin Juravle52c48962014-12-16 17:02:57 +00003263void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3264 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003265 LocationSummary* locations =
3266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003267 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003268
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003269 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3270 locations->SetOut(Location::RequiresFpuRegister());
3271 } else {
3272 // The output overlaps in case of long: we don't want the low move to overwrite
3273 // the object's location.
3274 locations->SetOut(Location::RequiresRegister(),
3275 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3276 : Location::kNoOutputOverlap);
3277 }
Calin Juravle52c48962014-12-16 17:02:57 +00003278
3279 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3280 // Long values can be loaded atomically into an XMM using movsd.
3281 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3282 // and then copy the XMM into the output 32bits at a time).
3283 locations->AddTemp(Location::RequiresFpuRegister());
3284 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003285}
3286
Calin Juravle52c48962014-12-16 17:02:57 +00003287void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3288 const FieldInfo& field_info) {
3289 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003290
Calin Juravle52c48962014-12-16 17:02:57 +00003291 LocationSummary* locations = instruction->GetLocations();
3292 Register base = locations->InAt(0).AsRegister<Register>();
3293 Location out = locations->Out();
3294 bool is_volatile = field_info.IsVolatile();
3295 Primitive::Type field_type = field_info.GetFieldType();
3296 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3297
3298 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003300 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003301 break;
3302 }
3303
3304 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003305 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003306 break;
3307 }
3308
3309 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003310 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 break;
3312 }
3313
3314 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003316 break;
3317 }
3318
3319 case Primitive::kPrimInt:
3320 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003322 break;
3323 }
3324
3325 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003326 if (is_volatile) {
3327 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3328 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003330 __ movd(out.AsRegisterPairLow<Register>(), temp);
3331 __ psrlq(temp, Immediate(32));
3332 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3333 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003334 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003335 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003337 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3338 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003339 break;
3340 }
3341
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003342 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003343 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003344 break;
3345 }
3346
3347 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003348 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003349 break;
3350 }
3351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003352 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003353 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003354 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003355 }
Calin Juravle52c48962014-12-16 17:02:57 +00003356
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 // Longs are handled in the switch.
3358 if (field_type != Primitive::kPrimLong) {
3359 codegen_->MaybeRecordImplicitNullCheck(instruction);
3360 }
3361
Calin Juravle52c48962014-12-16 17:02:57 +00003362 if (is_volatile) {
3363 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3364 }
3365}
3366
3367void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3369
3370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3372 locations->SetInAt(0, Location::RequiresRegister());
3373 bool is_volatile = field_info.IsVolatile();
3374 Primitive::Type field_type = field_info.GetFieldType();
3375 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3376 || (field_type == Primitive::kPrimByte);
3377
3378 // The register allocator does not support multiple
3379 // inputs that die at entry with one in a specific register.
3380 if (is_byte_type) {
3381 // Ensure the value is in a byte register.
3382 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003383 } else if (Primitive::IsFloatingPointType(field_type)) {
3384 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003385 } else {
3386 locations->SetInAt(1, Location::RequiresRegister());
3387 }
3388 // Temporary registers for the write barrier.
3389 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3390 locations->AddTemp(Location::RequiresRegister());
3391 // Ensure the card is in a byte register.
3392 locations->AddTemp(Location::RegisterLocation(ECX));
3393 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3394 // 64bits value can be atomically written to an address with movsd and an XMM register.
3395 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3396 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3397 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3398 // isolated cases when we need this it isn't worth adding the extra complexity.
3399 locations->AddTemp(Location::RequiresFpuRegister());
3400 locations->AddTemp(Location::RequiresFpuRegister());
3401 }
3402}
3403
3404void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003405 const FieldInfo& field_info,
3406 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003407 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3408
3409 LocationSummary* locations = instruction->GetLocations();
3410 Register base = locations->InAt(0).AsRegister<Register>();
3411 Location value = locations->InAt(1);
3412 bool is_volatile = field_info.IsVolatile();
3413 Primitive::Type field_type = field_info.GetFieldType();
3414 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3415
3416 if (is_volatile) {
3417 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3418 }
3419
3420 switch (field_type) {
3421 case Primitive::kPrimBoolean:
3422 case Primitive::kPrimByte: {
3423 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3424 break;
3425 }
3426
3427 case Primitive::kPrimShort:
3428 case Primitive::kPrimChar: {
3429 __ movw(Address(base, offset), value.AsRegister<Register>());
3430 break;
3431 }
3432
3433 case Primitive::kPrimInt:
3434 case Primitive::kPrimNot: {
3435 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003436 break;
3437 }
3438
3439 case Primitive::kPrimLong: {
3440 if (is_volatile) {
3441 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3442 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3443 __ movd(temp1, value.AsRegisterPairLow<Register>());
3444 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3445 __ punpckldq(temp1, temp2);
3446 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003447 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003448 } else {
3449 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003450 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003451 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3452 }
3453 break;
3454 }
3455
3456 case Primitive::kPrimFloat: {
3457 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3458 break;
3459 }
3460
3461 case Primitive::kPrimDouble: {
3462 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3463 break;
3464 }
3465
3466 case Primitive::kPrimVoid:
3467 LOG(FATAL) << "Unreachable type " << field_type;
3468 UNREACHABLE();
3469 }
3470
Calin Juravle77520bc2015-01-12 18:45:46 +00003471 // Longs are handled in the switch.
3472 if (field_type != Primitive::kPrimLong) {
3473 codegen_->MaybeRecordImplicitNullCheck(instruction);
3474 }
3475
3476 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3477 Register temp = locations->GetTemp(0).AsRegister<Register>();
3478 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003479 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003480 }
3481
Calin Juravle52c48962014-12-16 17:02:57 +00003482 if (is_volatile) {
3483 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3484 }
3485}
3486
3487void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3488 HandleFieldGet(instruction, instruction->GetFieldInfo());
3489}
3490
3491void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3492 HandleFieldGet(instruction, instruction->GetFieldInfo());
3493}
3494
3495void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3496 HandleFieldSet(instruction, instruction->GetFieldInfo());
3497}
3498
3499void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003500 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003501}
3502
3503void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3504 HandleFieldSet(instruction, instruction->GetFieldInfo());
3505}
3506
3507void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003508 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003509}
3510
3511void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3512 HandleFieldGet(instruction, instruction->GetFieldInfo());
3513}
3514
3515void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3516 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003517}
3518
3519void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003520 LocationSummary* locations =
3521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003522 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3523 ? Location::RequiresRegister()
3524 : Location::Any();
3525 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003526 if (instruction->HasUses()) {
3527 locations->SetOut(Location::SameAsFirstInput());
3528 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003529}
3530
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003531void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003532 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3533 return;
3534 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003535 LocationSummary* locations = instruction->GetLocations();
3536 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003537
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003538 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3539 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3540}
3541
3542void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003543 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544 codegen_->AddSlowPath(slow_path);
3545
3546 LocationSummary* locations = instruction->GetLocations();
3547 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003548
3549 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003550 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003551 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003552 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003553 } else {
3554 DCHECK(obj.IsConstant()) << obj;
3555 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3556 __ jmp(slow_path->GetEntryLabel());
3557 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003558 }
3559 __ j(kEqual, slow_path->GetEntryLabel());
3560}
3561
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003562void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3563 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3564 GenerateImplicitNullCheck(instruction);
3565 } else {
3566 GenerateExplicitNullCheck(instruction);
3567 }
3568}
3569
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003570void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003571 LocationSummary* locations =
3572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003573 locations->SetInAt(0, Location::RequiresRegister());
3574 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003575 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3577 } else {
3578 // The output overlaps in case of long: we don't want the low move to overwrite
3579 // the array's location.
3580 locations->SetOut(Location::RequiresRegister(),
3581 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3582 : Location::kNoOutputOverlap);
3583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003584}
3585
3586void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3587 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003588 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 Location index = locations->InAt(1);
3590
Calin Juravle77520bc2015-01-12 18:45:46 +00003591 Primitive::Type type = instruction->GetType();
3592 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 case Primitive::kPrimBoolean: {
3594 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596 if (index.IsConstant()) {
3597 __ movzxb(out, Address(obj,
3598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3599 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 }
3602 break;
3603 }
3604
3605 case Primitive::kPrimByte: {
3606 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003608 if (index.IsConstant()) {
3609 __ movsxb(out, Address(obj,
3610 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3611 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613 }
3614 break;
3615 }
3616
3617 case Primitive::kPrimShort: {
3618 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 if (index.IsConstant()) {
3621 __ movsxw(out, Address(obj,
3622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3623 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003625 }
3626 break;
3627 }
3628
3629 case Primitive::kPrimChar: {
3630 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 if (index.IsConstant()) {
3633 __ movzxw(out, Address(obj,
3634 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3635 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 }
3638 break;
3639 }
3640
3641 case Primitive::kPrimInt:
3642 case Primitive::kPrimNot: {
3643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 if (index.IsConstant()) {
3646 __ movl(out, Address(obj,
3647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3648 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003650 }
3651 break;
3652 }
3653
3654 case Primitive::kPrimLong: {
3655 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003656 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003657 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003658 if (index.IsConstant()) {
3659 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003660 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003661 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003662 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003664 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003666 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003667 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003669 }
3670 break;
3671 }
3672
Mark Mendell7c8d0092015-01-26 11:21:33 -05003673 case Primitive::kPrimFloat: {
3674 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3675 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3676 if (index.IsConstant()) {
3677 __ movss(out, Address(obj,
3678 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3679 } else {
3680 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3681 }
3682 break;
3683 }
3684
3685 case Primitive::kPrimDouble: {
3686 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3687 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3688 if (index.IsConstant()) {
3689 __ movsd(out, Address(obj,
3690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3691 } else {
3692 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3693 }
3694 break;
3695 }
3696
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003697 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003698 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003699 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003700 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003701
3702 if (type != Primitive::kPrimLong) {
3703 codegen_->MaybeRecordImplicitNullCheck(instruction);
3704 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003705}
3706
3707void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003708 // This location builder might end up asking to up to four registers, which is
3709 // not currently possible for baseline. The situation in which we need four
3710 // registers cannot be met by baseline though, because it has not run any
3711 // optimization.
3712
Nicolas Geoffray39468442014-09-02 15:17:15 +01003713 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003714 bool needs_write_barrier =
3715 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3716
Mark Mendell5f874182015-03-04 15:42:45 -05003717 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718
Nicolas Geoffray39468442014-09-02 15:17:15 +01003719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3720 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003721 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003722
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003723 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003724 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003725 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3726 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3727 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003729 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3730 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003731 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003732 // In case of a byte operation, the register allocator does not support multiple
3733 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003734 locations->SetInAt(0, Location::RequiresRegister());
3735 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003736 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003737 // Ensure the value is in a byte register.
3738 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003739 } else if (Primitive::IsFloatingPointType(value_type)) {
3740 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003741 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003742 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003743 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003744 // Temporary registers for the write barrier.
3745 if (needs_write_barrier) {
3746 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003747 // Ensure the card is in a byte register.
3748 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003749 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003750 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751}
3752
3753void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3754 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003757 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003758 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003759 bool needs_runtime_call = locations->WillCall();
3760 bool needs_write_barrier =
3761 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762
3763 switch (value_type) {
3764 case Primitive::kPrimBoolean:
3765 case Primitive::kPrimByte: {
3766 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 if (index.IsConstant()) {
3768 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003769 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003770 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003771 } else {
3772 __ movb(Address(obj, offset),
3773 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3774 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003776 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003778 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003779 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003781 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3782 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003783 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003784 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 break;
3786 }
3787
3788 case Primitive::kPrimShort:
3789 case Primitive::kPrimChar: {
3790 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003791 if (index.IsConstant()) {
3792 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003793 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003795 } else {
3796 __ movw(Address(obj, offset),
3797 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3798 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003800 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3802 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003803 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003804 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003805 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3806 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003808 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809 break;
3810 }
3811
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003812 case Primitive::kPrimInt:
3813 case Primitive::kPrimNot: {
3814 if (!needs_runtime_call) {
3815 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3816 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003817 size_t offset =
3818 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003819 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003821 } else {
3822 DCHECK(value.IsConstant()) << value;
3823 __ movl(Address(obj, offset),
3824 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3825 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003826 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003827 DCHECK(index.IsRegister()) << index;
3828 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3830 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003831 } else {
3832 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003833 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003834 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3835 }
3836 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003837 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003838
3839 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003840 Register temp = locations->GetTemp(0).AsRegister<Register>();
3841 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003842 codegen_->MarkGCCard(
3843 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003844 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003846 DCHECK_EQ(value_type, Primitive::kPrimNot);
3847 DCHECK(!codegen_->IsLeafMethod());
3848 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3849 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003850 }
3851 break;
3852 }
3853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854 case Primitive::kPrimLong: {
3855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856 if (index.IsConstant()) {
3857 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003858 if (value.IsRegisterPair()) {
3859 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003860 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003861 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003862 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003863 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003864 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3865 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003866 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003867 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3868 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003870 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003872 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003873 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003875 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003876 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003877 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003878 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003880 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003883 Immediate(High32Bits(val)));
3884 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 }
3886 break;
3887 }
3888
Mark Mendell7c8d0092015-01-26 11:21:33 -05003889 case Primitive::kPrimFloat: {
3890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3891 DCHECK(value.IsFpuRegister());
3892 if (index.IsConstant()) {
3893 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3894 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3895 } else {
3896 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3897 value.AsFpuRegister<XmmRegister>());
3898 }
3899 break;
3900 }
3901
3902 case Primitive::kPrimDouble: {
3903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3904 DCHECK(value.IsFpuRegister());
3905 if (index.IsConstant()) {
3906 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3907 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3908 } else {
3909 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3910 value.AsFpuRegister<XmmRegister>());
3911 }
3912 break;
3913 }
3914
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003915 case Primitive::kPrimVoid:
3916 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003917 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 }
3919}
3920
3921void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003923 locations->SetInAt(0, Location::RequiresRegister());
3924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925 instruction->SetLocations(locations);
3926}
3927
3928void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3929 LocationSummary* locations = instruction->GetLocations();
3930 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 Register obj = locations->InAt(0).AsRegister<Register>();
3932 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003934 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935}
3936
3937void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003938 LocationSummary* locations =
3939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003940 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04003941 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003942 if (instruction->HasUses()) {
3943 locations->SetOut(Location::SameAsFirstInput());
3944 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945}
3946
3947void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3948 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003949 Location index_loc = locations->InAt(0);
3950 Location length_loc = locations->InAt(1);
3951 SlowPathCodeX86* slow_path =
3952 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003953
Mark Mendell99dbd682015-04-22 16:18:52 -04003954 if (length_loc.IsConstant()) {
3955 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
3956 if (index_loc.IsConstant()) {
3957 // BCE will remove the bounds check if we are guarenteed to pass.
3958 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3959 if (index < 0 || index >= length) {
3960 codegen_->AddSlowPath(slow_path);
3961 __ jmp(slow_path->GetEntryLabel());
3962 } else {
3963 // Some optimization after BCE may have generated this, and we should not
3964 // generate a bounds check if it is a valid range.
3965 }
3966 return;
3967 }
3968
3969 // We have to reverse the jump condition because the length is the constant.
3970 Register index_reg = index_loc.AsRegister<Register>();
3971 __ cmpl(index_reg, Immediate(length));
3972 codegen_->AddSlowPath(slow_path);
3973 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003974 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04003975 Register length = length_loc.AsRegister<Register>();
3976 if (index_loc.IsConstant()) {
3977 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3978 __ cmpl(length, Immediate(value));
3979 } else {
3980 __ cmpl(length, index_loc.AsRegister<Register>());
3981 }
3982 codegen_->AddSlowPath(slow_path);
3983 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05003984 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003985}
3986
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003987void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3988 temp->SetLocations(nullptr);
3989}
3990
3991void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3992 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003993 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003994}
3995
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003996void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003997 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003998 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003999}
4000
4001void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004002 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4003}
4004
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004005void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4007}
4008
4009void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004010 HBasicBlock* block = instruction->GetBlock();
4011 if (block->GetLoopInformation() != nullptr) {
4012 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4013 // The back edge will generate the suspend check.
4014 return;
4015 }
4016 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4017 // The goto will generate the suspend check.
4018 return;
4019 }
4020 GenerateSuspendCheck(instruction, nullptr);
4021}
4022
4023void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4024 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004025 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004026 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4027 if (slow_path == nullptr) {
4028 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4029 instruction->SetSlowPath(slow_path);
4030 codegen_->AddSlowPath(slow_path);
4031 if (successor != nullptr) {
4032 DCHECK(successor->IsLoopHeader());
4033 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4034 }
4035 } else {
4036 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4037 }
4038
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004039 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004040 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004041 if (successor == nullptr) {
4042 __ j(kNotEqual, slow_path->GetEntryLabel());
4043 __ Bind(slow_path->GetReturnLabel());
4044 } else {
4045 __ j(kEqual, codegen_->GetLabelOf(successor));
4046 __ jmp(slow_path->GetEntryLabel());
4047 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004048}
4049
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004050X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4051 return codegen_->GetAssembler();
4052}
4053
Mark Mendell7c8d0092015-01-26 11:21:33 -05004054void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004055 ScratchRegisterScope ensure_scratch(
4056 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4057 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4058 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4059 __ movl(temp_reg, Address(ESP, src + stack_offset));
4060 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004061}
4062
4063void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004064 ScratchRegisterScope ensure_scratch(
4065 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4066 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4067 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4068 __ movl(temp_reg, Address(ESP, src + stack_offset));
4069 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4070 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4071 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004072}
4073
4074void ParallelMoveResolverX86::EmitMove(size_t index) {
4075 MoveOperands* move = moves_.Get(index);
4076 Location source = move->GetSource();
4077 Location destination = move->GetDestination();
4078
4079 if (source.IsRegister()) {
4080 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004081 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004082 } else {
4083 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004085 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004086 } else if (source.IsFpuRegister()) {
4087 if (destination.IsFpuRegister()) {
4088 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4089 } else if (destination.IsStackSlot()) {
4090 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4091 } else {
4092 DCHECK(destination.IsDoubleStackSlot());
4093 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4094 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004095 } else if (source.IsStackSlot()) {
4096 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004098 } else if (destination.IsFpuRegister()) {
4099 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004100 } else {
4101 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004102 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4103 }
4104 } else if (source.IsDoubleStackSlot()) {
4105 if (destination.IsFpuRegister()) {
4106 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4107 } else {
4108 DCHECK(destination.IsDoubleStackSlot()) << destination;
4109 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004110 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004111 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004112 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004113 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004114 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004115 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004116 if (value == 0) {
4117 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4118 } else {
4119 __ movl(destination.AsRegister<Register>(), Immediate(value));
4120 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004121 } else {
4122 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004123 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004124 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004125 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004126 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004127 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004128 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004129 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004130 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4131 if (value == 0) {
4132 // Easy handling of 0.0.
4133 __ xorps(dest, dest);
4134 } else {
4135 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004136 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4137 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4138 __ movl(temp, Immediate(value));
4139 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004140 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004141 } else {
4142 DCHECK(destination.IsStackSlot()) << destination;
4143 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4144 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004145 } else if (constant->IsLongConstant()) {
4146 int64_t value = constant->AsLongConstant()->GetValue();
4147 int32_t low_value = Low32Bits(value);
4148 int32_t high_value = High32Bits(value);
4149 Immediate low(low_value);
4150 Immediate high(high_value);
4151 if (destination.IsDoubleStackSlot()) {
4152 __ movl(Address(ESP, destination.GetStackIndex()), low);
4153 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4154 } else {
4155 __ movl(destination.AsRegisterPairLow<Register>(), low);
4156 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4157 }
4158 } else {
4159 DCHECK(constant->IsDoubleConstant());
4160 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004161 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004162 int32_t low_value = Low32Bits(value);
4163 int32_t high_value = High32Bits(value);
4164 Immediate low(low_value);
4165 Immediate high(high_value);
4166 if (destination.IsFpuRegister()) {
4167 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4168 if (value == 0) {
4169 // Easy handling of 0.0.
4170 __ xorpd(dest, dest);
4171 } else {
4172 __ pushl(high);
4173 __ pushl(low);
4174 __ movsd(dest, Address(ESP, 0));
4175 __ addl(ESP, Immediate(8));
4176 }
4177 } else {
4178 DCHECK(destination.IsDoubleStackSlot()) << destination;
4179 __ movl(Address(ESP, destination.GetStackIndex()), low);
4180 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4181 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004182 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004183 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004184 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004185 }
4186}
4187
Mark Mendella5c19ce2015-04-01 12:51:05 -04004188void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004189 Register suggested_scratch = reg == EAX ? EBX : EAX;
4190 ScratchRegisterScope ensure_scratch(
4191 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4192
4193 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4194 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4195 __ movl(Address(ESP, mem + stack_offset), reg);
4196 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004197}
4198
Mark Mendell7c8d0092015-01-26 11:21:33 -05004199void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004200 ScratchRegisterScope ensure_scratch(
4201 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4202
4203 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4204 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4205 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4206 __ movss(Address(ESP, mem + stack_offset), reg);
4207 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004208}
4209
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004210void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004211 ScratchRegisterScope ensure_scratch1(
4212 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004213
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004214 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4215 ScratchRegisterScope ensure_scratch2(
4216 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004217
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004218 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4219 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4220 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4221 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4222 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4223 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004224}
4225
4226void ParallelMoveResolverX86::EmitSwap(size_t index) {
4227 MoveOperands* move = moves_.Get(index);
4228 Location source = move->GetSource();
4229 Location destination = move->GetDestination();
4230
4231 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004232 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004233 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004234 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004235 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004236 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004237 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4238 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004239 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4240 // Use XOR Swap algorithm to avoid a temporary.
4241 DCHECK_NE(source.reg(), destination.reg());
4242 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4243 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4244 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4245 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4246 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4247 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4248 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004249 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4250 // Take advantage of the 16 bytes in the XMM register.
4251 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4252 Address stack(ESP, destination.GetStackIndex());
4253 // Load the double into the high doubleword.
4254 __ movhpd(reg, stack);
4255
4256 // Store the low double into the destination.
4257 __ movsd(stack, reg);
4258
4259 // Move the high double to the low double.
4260 __ psrldq(reg, Immediate(8));
4261 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4262 // Take advantage of the 16 bytes in the XMM register.
4263 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4264 Address stack(ESP, source.GetStackIndex());
4265 // Load the double into the high doubleword.
4266 __ movhpd(reg, stack);
4267
4268 // Store the low double into the destination.
4269 __ movsd(stack, reg);
4270
4271 // Move the high double to the low double.
4272 __ psrldq(reg, Immediate(8));
4273 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4274 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4275 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004276 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004277 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004278 }
4279}
4280
4281void ParallelMoveResolverX86::SpillScratch(int reg) {
4282 __ pushl(static_cast<Register>(reg));
4283}
4284
4285void ParallelMoveResolverX86::RestoreScratch(int reg) {
4286 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004287}
4288
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004289void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004290 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4291 ? LocationSummary::kCallOnSlowPath
4292 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004293 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004294 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004295 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004296 locations->SetOut(Location::RequiresRegister());
4297}
4298
4299void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004300 LocationSummary* locations = cls->GetLocations();
4301 Register out = locations->Out().AsRegister<Register>();
4302 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004303 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004304 DCHECK(!cls->CanCallRuntime());
4305 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004306 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004307 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004308 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004309 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004310 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004311 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004312
4313 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4314 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4315 codegen_->AddSlowPath(slow_path);
4316 __ testl(out, out);
4317 __ j(kEqual, slow_path->GetEntryLabel());
4318 if (cls->MustGenerateClinitCheck()) {
4319 GenerateClassInitializationCheck(slow_path, out);
4320 } else {
4321 __ Bind(slow_path->GetExitLabel());
4322 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004323 }
4324}
4325
4326void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4327 LocationSummary* locations =
4328 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4329 locations->SetInAt(0, Location::RequiresRegister());
4330 if (check->HasUses()) {
4331 locations->SetOut(Location::SameAsFirstInput());
4332 }
4333}
4334
4335void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004336 // We assume the class to not be null.
4337 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4338 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004339 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004340 GenerateClassInitializationCheck(slow_path,
4341 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004342}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004343
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004344void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4345 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004346 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4347 Immediate(mirror::Class::kStatusInitialized));
4348 __ j(kLess, slow_path->GetEntryLabel());
4349 __ Bind(slow_path->GetExitLabel());
4350 // No need for memory fence, thanks to the X86 memory model.
4351}
4352
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004353void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4354 LocationSummary* locations =
4355 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004356 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004357 locations->SetOut(Location::RequiresRegister());
4358}
4359
4360void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4361 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4362 codegen_->AddSlowPath(slow_path);
4363
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004364 LocationSummary* locations = load->GetLocations();
4365 Register out = locations->Out().AsRegister<Register>();
4366 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004367 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004368 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004369 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
4370 __ testl(out, out);
4371 __ j(kEqual, slow_path->GetEntryLabel());
4372 __ Bind(slow_path->GetExitLabel());
4373}
4374
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004375void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4376 LocationSummary* locations =
4377 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4378 locations->SetOut(Location::RequiresRegister());
4379}
4380
4381void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4382 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004383 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004384 __ fs()->movl(address, Immediate(0));
4385}
4386
4387void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4388 LocationSummary* locations =
4389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4390 InvokeRuntimeCallingConvention calling_convention;
4391 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4392}
4393
4394void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4395 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4396 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4397}
4398
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004399void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004400 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4401 ? LocationSummary::kNoCall
4402 : LocationSummary::kCallOnSlowPath;
4403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4404 locations->SetInAt(0, Location::RequiresRegister());
4405 locations->SetInAt(1, Location::Any());
4406 locations->SetOut(Location::RequiresRegister());
4407}
4408
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004409void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004410 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004412 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004414 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4415 Label done, zero;
4416 SlowPathCodeX86* slow_path = nullptr;
4417
4418 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004419 // Avoid null check if we know obj is not null.
4420 if (instruction->MustDoNullCheck()) {
4421 __ testl(obj, obj);
4422 __ j(kEqual, &zero);
4423 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004424 __ movl(out, Address(obj, class_offset));
4425 // Compare the class of `obj` with `cls`.
4426 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004428 } else {
4429 DCHECK(cls.IsStackSlot()) << cls;
4430 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4431 }
4432
4433 if (instruction->IsClassFinal()) {
4434 // Classes must be equal for the instanceof to succeed.
4435 __ j(kNotEqual, &zero);
4436 __ movl(out, Immediate(1));
4437 __ jmp(&done);
4438 } else {
4439 // If the classes are not equal, we go into a slow path.
4440 DCHECK(locations->OnlyCallsOnSlowPath());
4441 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004442 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004443 codegen_->AddSlowPath(slow_path);
4444 __ j(kNotEqual, slow_path->GetEntryLabel());
4445 __ movl(out, Immediate(1));
4446 __ jmp(&done);
4447 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004448
4449 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4450 __ Bind(&zero);
4451 __ movl(out, Immediate(0));
4452 }
4453
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004454 if (slow_path != nullptr) {
4455 __ Bind(slow_path->GetExitLabel());
4456 }
4457 __ Bind(&done);
4458}
4459
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004460void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4462 instruction, LocationSummary::kCallOnSlowPath);
4463 locations->SetInAt(0, Location::RequiresRegister());
4464 locations->SetInAt(1, Location::Any());
4465 locations->AddTemp(Location::RequiresRegister());
4466}
4467
4468void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4469 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004470 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004471 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004472 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004473 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4474 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4475 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4476 codegen_->AddSlowPath(slow_path);
4477
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004478 // Avoid null check if we know obj is not null.
4479 if (instruction->MustDoNullCheck()) {
4480 __ testl(obj, obj);
4481 __ j(kEqual, slow_path->GetExitLabel());
4482 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004483
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004484 __ movl(temp, Address(obj, class_offset));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004485 // Compare the class of `obj` with `cls`.
4486 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004487 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004488 } else {
4489 DCHECK(cls.IsStackSlot()) << cls;
4490 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4491 }
4492
4493 __ j(kNotEqual, slow_path->GetEntryLabel());
4494 __ Bind(slow_path->GetExitLabel());
4495}
4496
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004497void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4498 LocationSummary* locations =
4499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4500 InvokeRuntimeCallingConvention calling_convention;
4501 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4502}
4503
4504void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4505 __ fs()->call(Address::Absolute(instruction->IsEnter()
4506 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4507 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4508 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4509}
4510
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004511void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4512void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4513void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4514
4515void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4516 LocationSummary* locations =
4517 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4518 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4519 || instruction->GetResultType() == Primitive::kPrimLong);
4520 locations->SetInAt(0, Location::RequiresRegister());
4521 locations->SetInAt(1, Location::Any());
4522 locations->SetOut(Location::SameAsFirstInput());
4523}
4524
4525void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4526 HandleBitwiseOperation(instruction);
4527}
4528
4529void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4530 HandleBitwiseOperation(instruction);
4531}
4532
4533void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4534 HandleBitwiseOperation(instruction);
4535}
4536
4537void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4538 LocationSummary* locations = instruction->GetLocations();
4539 Location first = locations->InAt(0);
4540 Location second = locations->InAt(1);
4541 DCHECK(first.Equals(locations->Out()));
4542
4543 if (instruction->GetResultType() == Primitive::kPrimInt) {
4544 if (second.IsRegister()) {
4545 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004547 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004548 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004549 } else {
4550 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004551 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004552 }
4553 } else if (second.IsConstant()) {
4554 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004555 __ andl(first.AsRegister<Register>(),
4556 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004557 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004558 __ orl(first.AsRegister<Register>(),
4559 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004560 } else {
4561 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004562 __ xorl(first.AsRegister<Register>(),
4563 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004564 }
4565 } else {
4566 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004567 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004568 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004569 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004570 } else {
4571 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004572 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004573 }
4574 }
4575 } else {
4576 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4577 if (second.IsRegisterPair()) {
4578 if (instruction->IsAnd()) {
4579 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4580 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4581 } else if (instruction->IsOr()) {
4582 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4583 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4584 } else {
4585 DCHECK(instruction->IsXor());
4586 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4587 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4588 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004589 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004590 if (instruction->IsAnd()) {
4591 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4592 __ andl(first.AsRegisterPairHigh<Register>(),
4593 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4594 } else if (instruction->IsOr()) {
4595 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4596 __ orl(first.AsRegisterPairHigh<Register>(),
4597 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4598 } else {
4599 DCHECK(instruction->IsXor());
4600 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4601 __ xorl(first.AsRegisterPairHigh<Register>(),
4602 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4603 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004604 } else {
4605 DCHECK(second.IsConstant()) << second;
4606 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004607 int32_t low_value = Low32Bits(value);
4608 int32_t high_value = High32Bits(value);
4609 Immediate low(low_value);
4610 Immediate high(high_value);
4611 Register first_low = first.AsRegisterPairLow<Register>();
4612 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004613 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004614 if (low_value == 0) {
4615 __ xorl(first_low, first_low);
4616 } else if (low_value != -1) {
4617 __ andl(first_low, low);
4618 }
4619 if (high_value == 0) {
4620 __ xorl(first_high, first_high);
4621 } else if (high_value != -1) {
4622 __ andl(first_high, high);
4623 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004624 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004625 if (low_value != 0) {
4626 __ orl(first_low, low);
4627 }
4628 if (high_value != 0) {
4629 __ orl(first_high, high);
4630 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004631 } else {
4632 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004633 if (low_value != 0) {
4634 __ xorl(first_low, low);
4635 }
4636 if (high_value != 0) {
4637 __ xorl(first_high, high);
4638 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004639 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640 }
4641 }
4642}
4643
Calin Juravleb1498f62015-02-16 13:13:29 +00004644void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4645 // Nothing to do, this should be removed during prepare for register allocator.
4646 UNUSED(instruction);
4647 LOG(FATAL) << "Unreachable";
4648}
4649
4650void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4651 // Nothing to do, this should be removed during prepare for register allocator.
4652 UNUSED(instruction);
4653 LOG(FATAL) << "Unreachable";
4654}
4655
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004656} // namespace x86
4657} // namespace art