blob: 285cbb4ab69e01dd316abfb05c7003111406e80f [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010048#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x))
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010050class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010052 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Alexandre Rames2ed20af2015-03-06 13:55:35 +000054 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010055 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010057 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
58 instruction_,
59 instruction_->GetDexPc(),
60 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 }
62
Alexandre Rames8158f282015-08-07 10:26:17 +010063 bool IsFatal() const OVERRIDE { return true; }
64
Alexandre Rames9931f312015-06-19 14:47:01 +010065 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
66
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010068 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
70};
71
Calin Juravled0d48522014-11-04 16:40:20 +000072class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
73 public:
74 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
75
Alexandre Rames2ed20af2015-03-06 13:55:35 +000076 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010077 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000078 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010079 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
80 instruction_,
81 instruction_->GetDexPc(),
82 this);
Calin Juravled0d48522014-11-04 16:40:20 +000083 }
84
Alexandre Rames8158f282015-08-07 10:26:17 +010085 bool IsFatal() const OVERRIDE { return true; }
86
Alexandre Rames9931f312015-06-19 14:47:01 +010087 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
88
Calin Juravled0d48522014-11-04 16:40:20 +000089 private:
90 HDivZeroCheck* const instruction_;
91 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
92};
93
Calin Juravlebacfec32014-11-14 15:54:36 +000094class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000095 public:
Roland Levillain3887c462015-08-12 18:15:42 +010096 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000097
Alexandre Rames2ed20af2015-03-06 13:55:35 +000098 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000099 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000100 if (is_div_) {
101 __ negl(reg_);
102 } else {
103 __ movl(reg_, Immediate(0));
104 }
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ jmp(GetExitLabel());
106 }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 bool is_div_;
113 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000114};
115
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100117 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100118 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
119 Location index_location,
120 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000121 : instruction_(instruction),
122 index_location_(index_location),
123 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100124
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000125 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000131 x86_codegen->EmitParallelMoves(
132 index_location_,
133 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100134 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000135 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100136 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
137 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100138 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
139 instruction_,
140 instruction_->GetDexPc(),
141 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 }
143
Alexandre Rames8158f282015-08-07 10:26:17 +0100144 bool IsFatal() const OVERRIDE { return true; }
145
Alexandre Rames9931f312015-06-19 14:47:01 +0100146 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 const Location index_location_;
151 const Location length_location_;
152
153 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
154};
155
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000158 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000161 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000164 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
166 instruction_,
167 instruction_->GetDexPc(),
168 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000169 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100182 HBasicBlock* GetSuccessor() const {
183 return successor_;
184 }
185
Alexandre Rames9931f312015-06-19 14:47:01 +0100186 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
187
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 private:
189 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191 Label return_label_;
192
193 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
194};
195
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000196class LoadStringSlowPathX86 : public SlowPathCodeX86 {
197 public:
198 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
199
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000200 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000201 LocationSummary* locations = instruction_->GetLocations();
202 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
203
204 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207
208 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800209 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100210 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
211 instruction_,
212 instruction_->GetDexPc(),
213 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000214 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000215 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216
217 __ jmp(GetExitLabel());
218 }
219
Alexandre Rames9931f312015-06-19 14:47:01 +0100220 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
221
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222 private:
223 HLoadString* const instruction_;
224
225 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
226};
227
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228class LoadClassSlowPathX86 : public SlowPathCodeX86 {
229 public:
230 LoadClassSlowPathX86(HLoadClass* cls,
231 HInstruction* at,
232 uint32_t dex_pc,
233 bool do_clinit)
234 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
235 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
236 }
237
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000238 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 LocationSummary* locations = at_->GetLocations();
240 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
241 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243
244 InvokeRuntimeCallingConvention calling_convention;
245 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
247 : QUICK_ENTRY_POINT(pInitializeType),
248 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249
250 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 Location out = locations->Out();
252 if (out.IsValid()) {
253 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
254 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000256
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 __ jmp(GetExitLabel());
259 }
260
Alexandre Rames9931f312015-06-19 14:47:01 +0100261 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
262
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 private:
264 // The class this slow path will load.
265 HLoadClass* const cls_;
266
267 // The instruction where this slow path is happening.
268 // (Might be the load class or an initialization check).
269 HInstruction* const at_;
270
271 // The dex PC of `at_`.
272 const uint32_t dex_pc_;
273
274 // Whether to initialize the class.
275 const bool do_clinit_;
276
277 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
278};
279
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
281 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000282 TypeCheckSlowPathX86(HInstruction* instruction,
283 Location class_to_check,
Alexandre Rames98596202015-08-19 11:33:36 +0100284 Location object_class)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 class_to_check_(class_to_check),
Alexandre Rames98596202015-08-19 11:33:36 +0100287 object_class_(object_class) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 DCHECK(instruction_->IsCheckCast()
292 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
294 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
295 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000296 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
298 // We're moving two locations to locations that could overlap, so we need a parallel
299 // move resolver.
300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000301 x86_codegen->EmitParallelMoves(
302 class_to_check_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100304 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
307 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100310 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
311 instruction_,
312 instruction_->GetDexPc(),
313 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 } else {
315 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100316 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
317 instruction_,
318 instruction_->GetDexPc(),
319 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 }
321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 if (instruction_->IsInstanceOf()) {
323 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
324 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000325 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 __ jmp(GetExitLabel());
328 }
329
Alexandre Rames9931f312015-06-19 14:47:01 +0100330 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
331
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 HInstruction* const instruction_;
334 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 const Location object_class_;
336
337 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
338};
339
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700340class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
341 public:
342 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
343 : instruction_(instruction) {}
344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100346 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100347 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
351 instruction_,
352 instruction_->GetDexPc(),
353 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
357
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 private:
359 HInstruction* const instruction_;
360 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
361};
362
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100363#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100364#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100365
Roland Levillain4fa13f62015-07-06 18:11:54 +0100366inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700367 switch (cond) {
368 case kCondEQ: return kEqual;
369 case kCondNE: return kNotEqual;
370 case kCondLT: return kLess;
371 case kCondLE: return kLessEqual;
372 case kCondGT: return kGreater;
373 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700374 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100375 LOG(FATAL) << "Unreachable";
376 UNREACHABLE();
377}
378
379inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
380 switch (cond) {
381 case kCondEQ: return kEqual;
382 case kCondNE: return kNotEqual;
383 case kCondLT: return kBelow;
384 case kCondLE: return kBelowEqual;
385 case kCondGT: return kAbove;
386 case kCondGE: return kAboveEqual;
387 }
388 LOG(FATAL) << "Unreachable";
389 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700390}
391
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100392void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100393 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100394}
395
396void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100397 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100398}
399
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100400size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
401 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
402 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100403}
404
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
406 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
407 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100408}
409
Mark Mendell7c8d0092015-01-26 11:21:33 -0500410size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
411 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
412 return GetFloatingPointSpillSlotSize();
413}
414
415size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
416 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
417 return GetFloatingPointSpillSlotSize();
418}
419
Alexandre Rames8158f282015-08-07 10:26:17 +0100420void CodeGeneratorX86::InvokeRuntime(Address entry_point,
421 HInstruction* instruction,
422 uint32_t dex_pc,
423 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100424 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100425 __ fs()->call(entry_point);
426 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100427}
428
Mark Mendellfb8d2792015-03-31 22:16:59 -0400429CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
430 const X86InstructionSetFeatures& isa_features,
431 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500432 : CodeGenerator(graph,
433 kNumberOfCpuRegisters,
434 kNumberOfXmmRegisters,
435 kNumberOfRegisterPairs,
436 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
437 arraysize(kCoreCalleeSaves))
438 | (1 << kFakeReturnRegister),
439 0,
440 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100441 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100443 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400444 move_resolver_(graph->GetArena(), this),
445 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000446 // Use a fake return address register to mimic Quick.
447 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100448}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100449
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100451 switch (type) {
452 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454 X86ManagedRegister pair =
455 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100456 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
457 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
459 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100460 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100461 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462 }
463
464 case Primitive::kPrimByte:
465 case Primitive::kPrimBoolean:
466 case Primitive::kPrimChar:
467 case Primitive::kPrimShort:
468 case Primitive::kPrimInt:
469 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100470 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100471 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
474 X86ManagedRegister current =
475 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
476 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100477 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 }
479 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100480 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481 }
482
483 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100485 return Location::FpuRegisterLocation(
486 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100488
489 case Primitive::kPrimVoid:
490 LOG(FATAL) << "Unreachable type " << type;
491 }
492
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100493 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494}
495
Mark Mendell5f874182015-03-04 15:42:45 -0500496void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100497 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100498 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499
500 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100501 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100502
Mark Mendell5f874182015-03-04 15:42:45 -0500503 if (is_baseline) {
504 blocked_core_registers_[EBP] = true;
505 blocked_core_registers_[ESI] = true;
506 blocked_core_registers_[EDI] = true;
507 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100508
509 UpdateBlockedPairRegisters();
510}
511
512void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
513 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
514 X86ManagedRegister current =
515 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
516 if (blocked_core_registers_[current.AsRegisterPairLow()]
517 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
518 blocked_register_pairs_[i] = true;
519 }
520 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100521}
522
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100523InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
524 : HGraphVisitor(graph),
525 assembler_(codegen->GetAssembler()),
526 codegen_(codegen) {}
527
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100528static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100529 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530}
531
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000532void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100533 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000534 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000535 bool skip_overflow_check =
536 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000537 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000538
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000539 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100540 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100541 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100542 }
543
Mark Mendell5f874182015-03-04 15:42:45 -0500544 if (HasEmptyFrame()) {
545 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000546 }
Mark Mendell5f874182015-03-04 15:42:45 -0500547
548 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
549 Register reg = kCoreCalleeSaves[i];
550 if (allocated_registers_.ContainsCoreRegister(reg)) {
551 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100552 __ cfi().AdjustCFAOffset(kX86WordSize);
553 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500554 }
555 }
556
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100557 int adjust = GetFrameSize() - FrameEntrySpillSize();
558 __ subl(ESP, Immediate(adjust));
559 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100560 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000561}
562
563void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100564 __ cfi().RememberState();
565 if (!HasEmptyFrame()) {
566 int adjust = GetFrameSize() - FrameEntrySpillSize();
567 __ addl(ESP, Immediate(adjust));
568 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500569
David Srbeckyc34dc932015-04-12 09:27:43 +0100570 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
571 Register reg = kCoreCalleeSaves[i];
572 if (allocated_registers_.ContainsCoreRegister(reg)) {
573 __ popl(reg);
574 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
575 __ cfi().Restore(DWARFReg(reg));
576 }
Mark Mendell5f874182015-03-04 15:42:45 -0500577 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000578 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100579 __ ret();
580 __ cfi().RestoreState();
581 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582}
583
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100584void CodeGeneratorX86::Bind(HBasicBlock* block) {
585 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000586}
587
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
589 switch (load->GetType()) {
590 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100592 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593
594 case Primitive::kPrimInt:
595 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100596 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598
599 case Primitive::kPrimBoolean:
600 case Primitive::kPrimByte:
601 case Primitive::kPrimChar:
602 case Primitive::kPrimShort:
603 case Primitive::kPrimVoid:
604 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700605 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100606 }
607
608 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700609 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100610}
611
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100612Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
613 switch (type) {
614 case Primitive::kPrimBoolean:
615 case Primitive::kPrimByte:
616 case Primitive::kPrimChar:
617 case Primitive::kPrimShort:
618 case Primitive::kPrimInt:
619 case Primitive::kPrimNot:
620 return Location::RegisterLocation(EAX);
621
622 case Primitive::kPrimLong:
623 return Location::RegisterPairLocation(EAX, EDX);
624
625 case Primitive::kPrimVoid:
626 return Location::NoLocation();
627
628 case Primitive::kPrimDouble:
629 case Primitive::kPrimFloat:
630 return Location::FpuRegisterLocation(XMM0);
631 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100632
633 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100634}
635
636Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
637 return Location::RegisterLocation(kMethodRegisterArgument);
638}
639
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100640Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100641 switch (type) {
642 case Primitive::kPrimBoolean:
643 case Primitive::kPrimByte:
644 case Primitive::kPrimChar:
645 case Primitive::kPrimShort:
646 case Primitive::kPrimInt:
647 case Primitive::kPrimNot: {
648 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000649 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100650 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100651 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100652 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000653 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100654 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100655 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100656
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000657 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100658 uint32_t index = gp_index_;
659 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000660 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100661 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
663 calling_convention.GetRegisterPairAt(index));
664 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100665 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000666 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
667 }
668 }
669
670 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100671 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000672 stack_index_++;
673 if (index < calling_convention.GetNumberOfFpuRegisters()) {
674 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
675 } else {
676 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
677 }
678 }
679
680 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100681 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000682 stack_index_ += 2;
683 if (index < calling_convention.GetNumberOfFpuRegisters()) {
684 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
685 } else {
686 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100687 }
688 }
689
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100690 case Primitive::kPrimVoid:
691 LOG(FATAL) << "Unexpected parameter type " << type;
692 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100693 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100694 return Location();
695}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100696
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697void CodeGeneratorX86::Move32(Location destination, Location source) {
698 if (source.Equals(destination)) {
699 return;
700 }
701 if (destination.IsRegister()) {
702 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 } else {
707 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000708 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100709 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 } else if (destination.IsFpuRegister()) {
711 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000712 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000714 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 } else {
716 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000720 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000724 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500725 } else if (source.IsConstant()) {
726 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000727 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500728 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 } else {
730 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100731 __ pushl(Address(ESP, source.GetStackIndex()));
732 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100733 }
734 }
735}
736
737void CodeGeneratorX86::Move64(Location destination, Location source) {
738 if (source.Equals(destination)) {
739 return;
740 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100741 if (destination.IsRegisterPair()) {
742 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000743 EmitParallelMoves(
744 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
745 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100746 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000747 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100748 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
749 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100750 } else if (source.IsFpuRegister()) {
751 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000753 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100754 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100755 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
756 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
758 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500760 if (source.IsFpuRegister()) {
761 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
762 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000763 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 } else {
765 LOG(FATAL) << "Unimplemented";
766 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000768 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100769 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000770 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100771 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100773 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000775 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000776 } else if (source.IsConstant()) {
777 HConstant* constant = source.GetConstant();
778 int64_t value;
779 if (constant->IsLongConstant()) {
780 value = constant->AsLongConstant()->GetValue();
781 } else {
782 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000783 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000784 }
785 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
786 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000788 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000789 EmitParallelMoves(
790 Location::StackSlot(source.GetStackIndex()),
791 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100792 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000793 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100794 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
795 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100796 }
797 }
798}
799
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100800void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000801 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100802 if (instruction->IsCurrentMethod()) {
803 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
804 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000805 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100806 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000807 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000808 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
809 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000810 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000811 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000812 } else if (location.IsStackSlot()) {
813 __ movl(Address(ESP, location.GetStackIndex()), imm);
814 } else {
815 DCHECK(location.IsConstant());
816 DCHECK_EQ(location.GetConstant(), const_to_move);
817 }
818 } else if (const_to_move->IsLongConstant()) {
819 int64_t value = const_to_move->AsLongConstant()->GetValue();
820 if (location.IsRegisterPair()) {
821 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
822 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
823 } else if (location.IsDoubleStackSlot()) {
824 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000825 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
826 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000827 } else {
828 DCHECK(location.IsConstant());
829 DCHECK_EQ(location.GetConstant(), instruction);
830 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000832 } else if (instruction->IsTemporary()) {
833 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000834 if (temp_location.IsStackSlot()) {
835 Move32(location, temp_location);
836 } else {
837 DCHECK(temp_location.IsDoubleStackSlot());
838 Move64(location, temp_location);
839 }
Roland Levillain476df552014-10-09 17:51:36 +0100840 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100841 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 switch (instruction->GetType()) {
843 case Primitive::kPrimBoolean:
844 case Primitive::kPrimByte:
845 case Primitive::kPrimChar:
846 case Primitive::kPrimShort:
847 case Primitive::kPrimInt:
848 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 case Primitive::kPrimFloat:
850 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 break;
852
853 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 case Primitive::kPrimDouble:
855 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100856 break;
857
858 default:
859 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
860 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000861 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100862 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 switch (instruction->GetType()) {
864 case Primitive::kPrimBoolean:
865 case Primitive::kPrimByte:
866 case Primitive::kPrimChar:
867 case Primitive::kPrimShort:
868 case Primitive::kPrimInt:
869 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100870 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000871 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872 break;
873
874 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000876 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877 break;
878
879 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100880 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882 }
883}
884
David Brazdilfc6a86a2015-06-26 10:33:45 +0000885void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100886 DCHECK(!successor->IsExitBlock());
887
888 HBasicBlock* block = got->GetBlock();
889 HInstruction* previous = got->GetPrevious();
890
891 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000892 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100893 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
894 return;
895 }
896
897 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
898 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
899 }
900 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902 }
903}
904
David Brazdilfc6a86a2015-06-26 10:33:45 +0000905void LocationsBuilderX86::VisitGoto(HGoto* got) {
906 got->SetLocations(nullptr);
907}
908
909void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
910 HandleGoto(got, got->GetSuccessor());
911}
912
913void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
914 try_boundary->SetLocations(nullptr);
915}
916
917void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
918 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
919 if (!successor->IsExitBlock()) {
920 HandleGoto(try_boundary, successor);
921 }
922}
923
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000925 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000926}
927
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000928void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700929 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000930}
931
Mark Mendellc4701932015-04-10 13:18:51 -0400932void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
933 Label* true_label,
934 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100935 if (cond->IsFPConditionTrueIfNaN()) {
936 __ j(kUnordered, true_label);
937 } else if (cond->IsFPConditionFalseIfNaN()) {
938 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400941}
942
943void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
944 Label* true_label,
945 Label* false_label) {
946 LocationSummary* locations = cond->GetLocations();
947 Location left = locations->InAt(0);
948 Location right = locations->InAt(1);
949 IfCondition if_cond = cond->GetCondition();
950
Mark Mendellc4701932015-04-10 13:18:51 -0400951 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100952 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400953 IfCondition true_high_cond = if_cond;
954 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400956
957 // Set the conditions for the test, remembering that == needs to be
958 // decided using the low words.
959 switch (if_cond) {
960 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400961 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400963 break;
964 case kCondLT:
965 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400966 break;
967 case kCondLE:
968 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400969 break;
970 case kCondGT:
971 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400972 break;
973 case kCondGE:
974 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400975 break;
976 }
977
978 if (right.IsConstant()) {
979 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400980 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100981 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400982
983 if (val_high == 0) {
984 __ testl(left_high, left_high);
985 } else {
986 __ cmpl(left_high, Immediate(val_high));
987 }
988 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100989 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400990 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100991 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400992 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100993 __ j(X86SignedCondition(true_high_cond), true_label);
994 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400995 }
996 // Must be equal high, so compare the lows.
997 if (val_low == 0) {
998 __ testl(left_low, left_low);
999 } else {
1000 __ cmpl(left_low, Immediate(val_low));
1001 }
1002 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001003 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001004 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001005
1006 __ cmpl(left_high, right_high);
1007 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001008 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001009 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001010 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001011 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001012 __ j(X86SignedCondition(true_high_cond), true_label);
1013 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001014 }
1015 // Must be equal high, so compare the lows.
1016 __ cmpl(left_low, right_low);
1017 }
1018 // The last comparison might be unsigned.
1019 __ j(final_condition, true_label);
1020}
1021
1022void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1023 HCondition* condition,
1024 Label* true_target,
1025 Label* false_target,
1026 Label* always_true_target) {
1027 LocationSummary* locations = condition->GetLocations();
1028 Location left = locations->InAt(0);
1029 Location right = locations->InAt(1);
1030
1031 // We don't want true_target as a nullptr.
1032 if (true_target == nullptr) {
1033 true_target = always_true_target;
1034 }
1035 bool falls_through = (false_target == nullptr);
1036
1037 // FP compares don't like null false_targets.
1038 if (false_target == nullptr) {
1039 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1040 }
1041
1042 Primitive::Type type = condition->InputAt(0)->GetType();
1043 switch (type) {
1044 case Primitive::kPrimLong:
1045 GenerateLongComparesAndJumps(condition, true_target, false_target);
1046 break;
1047 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001048 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1049 GenerateFPJumps(condition, true_target, false_target);
1050 break;
1051 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001052 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1053 GenerateFPJumps(condition, true_target, false_target);
1054 break;
1055 default:
1056 LOG(FATAL) << "Unexpected compare type " << type;
1057 }
1058
1059 if (!falls_through) {
1060 __ jmp(false_target);
1061 }
1062}
1063
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001064void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1065 Label* true_target,
1066 Label* false_target,
1067 Label* always_true_target) {
1068 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001069 if (cond->IsIntConstant()) {
1070 // Constant condition, statically compared against 1.
1071 int32_t cond_value = cond->AsIntConstant()->GetValue();
1072 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001073 if (always_true_target != nullptr) {
1074 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001075 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001076 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001077 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001078 DCHECK_EQ(cond_value, 0);
1079 }
1080 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001081 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001082 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1083 // Moves do not affect the eflags register, so if the condition is
1084 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001085 // again. We can't use the eflags on long/FP conditions if they are
1086 // materialized due to the complex branching.
1087 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001088 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001089 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001090 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1091 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001092 if (!eflags_set) {
1093 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001094 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001095 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001096 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001097 } else {
1098 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1099 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001100 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001101 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001102 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001103 }
1104 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001105 // Condition has not been materialized, use its inputs as the
1106 // comparison and its condition as the branch condition.
1107
Mark Mendellc4701932015-04-10 13:18:51 -04001108 // Is this a long or FP comparison that has been folded into the HCondition?
1109 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1110 // Generate the comparison directly.
1111 GenerateCompareTestAndBranch(instruction->AsIf(),
1112 cond->AsCondition(),
1113 true_target,
1114 false_target,
1115 always_true_target);
1116 return;
1117 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001118
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001119 Location lhs = cond->GetLocations()->InAt(0);
1120 Location rhs = cond->GetLocations()->InAt(1);
1121 // LHS is guaranteed to be in a register (see
1122 // LocationsBuilderX86::VisitCondition).
1123 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001125 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001126 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001127 if (constant == 0) {
1128 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1129 } else {
1130 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1131 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001132 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001133 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001134 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001135 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001136 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001137 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001138 if (false_target != nullptr) {
1139 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140 }
1141}
1142
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001143void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1144 LocationSummary* locations =
1145 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1146 HInstruction* cond = if_instr->InputAt(0);
1147 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1148 locations->SetInAt(0, Location::Any());
1149 }
1150}
1151
1152void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1153 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1154 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1155 Label* always_true_target = true_target;
1156 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1157 if_instr->IfTrueSuccessor())) {
1158 always_true_target = nullptr;
1159 }
1160 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1161 if_instr->IfFalseSuccessor())) {
1162 false_target = nullptr;
1163 }
1164 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1165}
1166
1167void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1168 LocationSummary* locations = new (GetGraph()->GetArena())
1169 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1170 HInstruction* cond = deoptimize->InputAt(0);
1171 DCHECK(cond->IsCondition());
1172 if (cond->AsCondition()->NeedsMaterialization()) {
1173 locations->SetInAt(0, Location::Any());
1174 }
1175}
1176
1177void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1178 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1179 DeoptimizationSlowPathX86(deoptimize);
1180 codegen_->AddSlowPath(slow_path);
1181 Label* slow_path_entry = slow_path->GetEntryLabel();
1182 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1183}
1184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001185void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001186 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001187}
1188
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001189void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1190 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001191}
1192
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001193void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001194 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001195}
1196
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001198 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001199 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200}
1201
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 LocationSummary* locations =
1204 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 switch (store->InputAt(1)->GetType()) {
1206 case Primitive::kPrimBoolean:
1207 case Primitive::kPrimByte:
1208 case Primitive::kPrimChar:
1209 case Primitive::kPrimShort:
1210 case Primitive::kPrimInt:
1211 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001212 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1214 break;
1215
1216 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1219 break;
1220
1221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001224}
1225
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001226void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001227 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001228}
1229
Roland Levillain0d37cd02015-05-27 16:39:19 +01001230void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001231 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001232 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001233 // Handle the long/FP comparisons made in instruction simplification.
1234 switch (cond->InputAt(0)->GetType()) {
1235 case Primitive::kPrimLong: {
1236 locations->SetInAt(0, Location::RequiresRegister());
1237 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1238 if (cond->NeedsMaterialization()) {
1239 locations->SetOut(Location::RequiresRegister());
1240 }
1241 break;
1242 }
1243 case Primitive::kPrimFloat:
1244 case Primitive::kPrimDouble: {
1245 locations->SetInAt(0, Location::RequiresFpuRegister());
1246 locations->SetInAt(1, Location::RequiresFpuRegister());
1247 if (cond->NeedsMaterialization()) {
1248 locations->SetOut(Location::RequiresRegister());
1249 }
1250 break;
1251 }
1252 default:
1253 locations->SetInAt(0, Location::RequiresRegister());
1254 locations->SetInAt(1, Location::Any());
1255 if (cond->NeedsMaterialization()) {
1256 // We need a byte register.
1257 locations->SetOut(Location::RegisterLocation(ECX));
1258 }
1259 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001260 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001261}
1262
Roland Levillain0d37cd02015-05-27 16:39:19 +01001263void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001264 if (!cond->NeedsMaterialization()) {
1265 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001266 }
Mark Mendellc4701932015-04-10 13:18:51 -04001267
1268 LocationSummary* locations = cond->GetLocations();
1269 Location lhs = locations->InAt(0);
1270 Location rhs = locations->InAt(1);
1271 Register reg = locations->Out().AsRegister<Register>();
1272 Label true_label, false_label;
1273
1274 switch (cond->InputAt(0)->GetType()) {
1275 default: {
1276 // Integer case.
1277
1278 // Clear output register: setcc only sets the low byte.
1279 __ xorl(reg, reg);
1280
1281 if (rhs.IsRegister()) {
1282 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1283 } else if (rhs.IsConstant()) {
1284 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1285 if (constant == 0) {
1286 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1287 } else {
1288 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1289 }
1290 } else {
1291 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1292 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001293 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001294 return;
1295 }
1296 case Primitive::kPrimLong:
1297 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1298 break;
1299 case Primitive::kPrimFloat:
1300 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1301 GenerateFPJumps(cond, &true_label, &false_label);
1302 break;
1303 case Primitive::kPrimDouble:
1304 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1305 GenerateFPJumps(cond, &true_label, &false_label);
1306 break;
1307 }
1308
1309 // Convert the jumps into the result.
1310 Label done_label;
1311
Roland Levillain4fa13f62015-07-06 18:11:54 +01001312 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001313 __ Bind(&false_label);
1314 __ xorl(reg, reg);
1315 __ jmp(&done_label);
1316
Roland Levillain4fa13f62015-07-06 18:11:54 +01001317 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001318 __ Bind(&true_label);
1319 __ movl(reg, Immediate(1));
1320 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001321}
1322
1323void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1324 VisitCondition(comp);
1325}
1326
1327void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1328 VisitCondition(comp);
1329}
1330
1331void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1332 VisitCondition(comp);
1333}
1334
1335void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1336 VisitCondition(comp);
1337}
1338
1339void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1340 VisitCondition(comp);
1341}
1342
1343void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1344 VisitCondition(comp);
1345}
1346
1347void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1348 VisitCondition(comp);
1349}
1350
1351void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1352 VisitCondition(comp);
1353}
1354
1355void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1356 VisitCondition(comp);
1357}
1358
1359void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1360 VisitCondition(comp);
1361}
1362
1363void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1364 VisitCondition(comp);
1365}
1366
1367void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1368 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001369}
1370
1371void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001374 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375}
1376
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001377void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001378 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001379 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380}
1381
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001382void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1383 LocationSummary* locations =
1384 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1385 locations->SetOut(Location::ConstantLocation(constant));
1386}
1387
1388void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1389 // Will be generated at use site.
1390 UNUSED(constant);
1391}
1392
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001393void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 LocationSummary* locations =
1395 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001396 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397}
1398
1399void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1400 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001401 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001402}
1403
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001404void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1407 locations->SetOut(Location::ConstantLocation(constant));
1408}
1409
1410void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1411 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001412 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001413}
1414
1415void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1416 LocationSummary* locations =
1417 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1418 locations->SetOut(Location::ConstantLocation(constant));
1419}
1420
1421void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1422 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001423 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001424}
1425
Calin Juravle27df7582015-04-17 19:12:31 +01001426void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1427 memory_barrier->SetLocations(nullptr);
1428}
1429
1430void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1431 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1432}
1433
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001434void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001435 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001436}
1437
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001438void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001439 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001440 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001441}
1442
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001443void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001444 LocationSummary* locations =
1445 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001446 switch (ret->InputAt(0)->GetType()) {
1447 case Primitive::kPrimBoolean:
1448 case Primitive::kPrimByte:
1449 case Primitive::kPrimChar:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
1452 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001453 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001454 break;
1455
1456 case Primitive::kPrimLong:
1457 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001458 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 break;
1460
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 case Primitive::kPrimFloat:
1462 case Primitive::kPrimDouble:
1463 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001464 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001465 break;
1466
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001467 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001468 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001469 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470}
1471
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001472void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473 if (kIsDebugBuild) {
1474 switch (ret->InputAt(0)->GetType()) {
1475 case Primitive::kPrimBoolean:
1476 case Primitive::kPrimByte:
1477 case Primitive::kPrimChar:
1478 case Primitive::kPrimShort:
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001481 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482 break;
1483
1484 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001485 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1486 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 break;
1488
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001489 case Primitive::kPrimFloat:
1490 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001491 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001492 break;
1493
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001496 }
1497 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001498 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001499}
1500
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001501void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001502 // When we do not run baseline, explicit clinit checks triggered by static
1503 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1504 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001505
Mark Mendellfb8d2792015-03-31 22:16:59 -04001506 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001507 if (intrinsic.TryDispatch(invoke)) {
1508 return;
1509 }
1510
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001511 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001512
1513 if (codegen_->IsBaseline()) {
1514 // Baseline does not have enough registers if the current method also
1515 // needs a register. We therefore do not require a register for it, and let
1516 // the code generation of the invoke handle it.
1517 LocationSummary* locations = invoke->GetLocations();
1518 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1519 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1520 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1521 }
1522 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001523}
1524
Mark Mendell09ed1a32015-03-25 08:30:06 -04001525static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1526 if (invoke->GetLocations()->Intrinsified()) {
1527 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1528 intrinsic.Dispatch(invoke);
1529 return true;
1530 }
1531 return false;
1532}
1533
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001534void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001535 // When we do not run baseline, explicit clinit checks triggered by static
1536 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1537 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001538
Mark Mendell09ed1a32015-03-25 08:30:06 -04001539 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1540 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001541 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001542
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001543 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001544 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001545 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001546 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001547}
1548
1549void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1550 HandleInvoke(invoke);
1551}
1552
1553void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001554 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001555 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001556}
1557
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001558void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001559 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001560 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1561 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001562 LocationSummary* locations = invoke->GetLocations();
1563 Location receiver = locations->InAt(0);
1564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001565 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001566 DCHECK(receiver.IsRegister());
1567 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001568 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001569 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001570 // temp = temp->GetMethodAt(method_offset);
1571 __ movl(temp, Address(temp, method_offset));
1572 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001573 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001574 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001575
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001576 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001578}
1579
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001580void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1581 HandleInvoke(invoke);
1582 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001583 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001584}
1585
1586void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1587 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001588 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1590 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001591 LocationSummary* locations = invoke->GetLocations();
1592 Location receiver = locations->InAt(0);
1593 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1594
1595 // Set the hidden argument.
1596 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001598
1599 // temp = object->GetClass();
1600 if (receiver.IsStackSlot()) {
1601 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1602 __ movl(temp, Address(temp, class_offset));
1603 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001604 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001605 }
Roland Levillain4d027112015-07-01 15:41:14 +01001606 codegen_->MaybeRecordImplicitNullCheck(invoke);
1607 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001608 // temp = temp->GetImtEntryAt(method_offset);
1609 __ movl(temp, Address(temp, method_offset));
1610 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001611 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001612 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001613
1614 DCHECK(!codegen_->IsLeafMethod());
1615 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1616}
1617
Roland Levillain88cb1752014-10-20 16:36:47 +01001618void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1619 LocationSummary* locations =
1620 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1621 switch (neg->GetResultType()) {
1622 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001623 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001624 locations->SetInAt(0, Location::RequiresRegister());
1625 locations->SetOut(Location::SameAsFirstInput());
1626 break;
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001629 locations->SetInAt(0, Location::RequiresFpuRegister());
1630 locations->SetOut(Location::SameAsFirstInput());
1631 locations->AddTemp(Location::RequiresRegister());
1632 locations->AddTemp(Location::RequiresFpuRegister());
1633 break;
1634
Roland Levillain88cb1752014-10-20 16:36:47 +01001635 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001636 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001637 locations->SetOut(Location::SameAsFirstInput());
1638 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001639 break;
1640
1641 default:
1642 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1643 }
1644}
1645
1646void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1647 LocationSummary* locations = neg->GetLocations();
1648 Location out = locations->Out();
1649 Location in = locations->InAt(0);
1650 switch (neg->GetResultType()) {
1651 case Primitive::kPrimInt:
1652 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001653 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001655 break;
1656
1657 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001658 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001659 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001660 __ negl(out.AsRegisterPairLow<Register>());
1661 // Negation is similar to subtraction from zero. The least
1662 // significant byte triggers a borrow when it is different from
1663 // zero; to take it into account, add 1 to the most significant
1664 // byte if the carry flag (CF) is set to 1 after the first NEGL
1665 // operation.
1666 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1667 __ negl(out.AsRegisterPairHigh<Register>());
1668 break;
1669
Roland Levillain5368c212014-11-27 15:03:41 +00001670 case Primitive::kPrimFloat: {
1671 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 Register constant = locations->GetTemp(0).AsRegister<Register>();
1673 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001674 // Implement float negation with an exclusive or with value
1675 // 0x80000000 (mask for bit 31, representing the sign of a
1676 // single-precision floating-point number).
1677 __ movl(constant, Immediate(INT32_C(0x80000000)));
1678 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001680 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001681 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001682
Roland Levillain5368c212014-11-27 15:03:41 +00001683 case Primitive::kPrimDouble: {
1684 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001686 // Implement double negation with an exclusive or with value
1687 // 0x8000000000000000 (mask for bit 63, representing the sign of
1688 // a double-precision floating-point number).
1689 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001690 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001691 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001692 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001693
1694 default:
1695 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1696 }
1697}
1698
Roland Levillaindff1f282014-11-05 14:15:05 +00001699void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001700 Primitive::Type result_type = conversion->GetResultType();
1701 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001702 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001703
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001704 // The float-to-long and double-to-long type conversions rely on a
1705 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001706 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001707 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1708 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001709 ? LocationSummary::kCall
1710 : LocationSummary::kNoCall;
1711 LocationSummary* locations =
1712 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1713
David Brazdilb2bd1c52015-03-25 11:17:37 +00001714 // The Java language does not allow treating boolean as an integral type but
1715 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001716
Roland Levillaindff1f282014-11-05 14:15:05 +00001717 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001718 case Primitive::kPrimByte:
1719 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001720 case Primitive::kPrimBoolean:
1721 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001722 case Primitive::kPrimShort:
1723 case Primitive::kPrimInt:
1724 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001725 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001726 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1727 // Make the output overlap to please the register allocator. This greatly simplifies
1728 // the validation of the linear scan implementation
1729 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001730 break;
1731
1732 default:
1733 LOG(FATAL) << "Unexpected type conversion from " << input_type
1734 << " to " << result_type;
1735 }
1736 break;
1737
Roland Levillain01a8d712014-11-14 16:27:39 +00001738 case Primitive::kPrimShort:
1739 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001740 case Primitive::kPrimBoolean:
1741 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001742 case Primitive::kPrimByte:
1743 case Primitive::kPrimInt:
1744 case Primitive::kPrimChar:
1745 // Processing a Dex `int-to-short' instruction.
1746 locations->SetInAt(0, Location::Any());
1747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1748 break;
1749
1750 default:
1751 LOG(FATAL) << "Unexpected type conversion from " << input_type
1752 << " to " << result_type;
1753 }
1754 break;
1755
Roland Levillain946e1432014-11-11 17:35:19 +00001756 case Primitive::kPrimInt:
1757 switch (input_type) {
1758 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001759 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001760 locations->SetInAt(0, Location::Any());
1761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1762 break;
1763
1764 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001765 // Processing a Dex `float-to-int' instruction.
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetOut(Location::RequiresRegister());
1768 locations->AddTemp(Location::RequiresFpuRegister());
1769 break;
1770
Roland Levillain946e1432014-11-11 17:35:19 +00001771 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001772 // Processing a Dex `double-to-int' instruction.
1773 locations->SetInAt(0, Location::RequiresFpuRegister());
1774 locations->SetOut(Location::RequiresRegister());
1775 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001776 break;
1777
1778 default:
1779 LOG(FATAL) << "Unexpected type conversion from " << input_type
1780 << " to " << result_type;
1781 }
1782 break;
1783
Roland Levillaindff1f282014-11-05 14:15:05 +00001784 case Primitive::kPrimLong:
1785 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001786 case Primitive::kPrimBoolean:
1787 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001788 case Primitive::kPrimByte:
1789 case Primitive::kPrimShort:
1790 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001791 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001792 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 locations->SetInAt(0, Location::RegisterLocation(EAX));
1794 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1795 break;
1796
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001797 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001798 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001799 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001800 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001801 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1802 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1803
Vladimir Marko949c91f2015-01-27 10:48:44 +00001804 // The runtime helper puts the result in EAX, EDX.
1805 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001806 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001807 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 }
1813 break;
1814
Roland Levillain981e4542014-11-14 11:47:14 +00001815 case Primitive::kPrimChar:
1816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001817 case Primitive::kPrimBoolean:
1818 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001819 case Primitive::kPrimByte:
1820 case Primitive::kPrimShort:
1821 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001822 // Processing a Dex `int-to-char' instruction.
1823 locations->SetInAt(0, Location::Any());
1824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1825 break;
1826
1827 default:
1828 LOG(FATAL) << "Unexpected type conversion from " << input_type
1829 << " to " << result_type;
1830 }
1831 break;
1832
Roland Levillaindff1f282014-11-05 14:15:05 +00001833 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001834 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001835 case Primitive::kPrimBoolean:
1836 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001837 case Primitive::kPrimByte:
1838 case Primitive::kPrimShort:
1839 case Primitive::kPrimInt:
1840 case Primitive::kPrimChar:
1841 // Processing a Dex `int-to-float' instruction.
1842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetOut(Location::RequiresFpuRegister());
1844 break;
1845
1846 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001848 locations->SetInAt(0, Location::Any());
1849 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001850 break;
1851
Roland Levillaincff13742014-11-17 14:32:17 +00001852 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001853 // Processing a Dex `double-to-float' instruction.
1854 locations->SetInAt(0, Location::RequiresFpuRegister());
1855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001856 break;
1857
1858 default:
1859 LOG(FATAL) << "Unexpected type conversion from " << input_type
1860 << " to " << result_type;
1861 };
1862 break;
1863
Roland Levillaindff1f282014-11-05 14:15:05 +00001864 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001865 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001866 case Primitive::kPrimBoolean:
1867 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001868 case Primitive::kPrimByte:
1869 case Primitive::kPrimShort:
1870 case Primitive::kPrimInt:
1871 case Primitive::kPrimChar:
1872 // Processing a Dex `int-to-double' instruction.
1873 locations->SetInAt(0, Location::RequiresRegister());
1874 locations->SetOut(Location::RequiresFpuRegister());
1875 break;
1876
1877 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001878 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001879 locations->SetInAt(0, Location::Any());
1880 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 break;
1882
Roland Levillaincff13742014-11-17 14:32:17 +00001883 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001884 // Processing a Dex `float-to-double' instruction.
1885 locations->SetInAt(0, Location::RequiresFpuRegister());
1886 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001887 break;
1888
1889 default:
1890 LOG(FATAL) << "Unexpected type conversion from " << input_type
1891 << " to " << result_type;
1892 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 }
1899}
1900
1901void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1902 LocationSummary* locations = conversion->GetLocations();
1903 Location out = locations->Out();
1904 Location in = locations->InAt(0);
1905 Primitive::Type result_type = conversion->GetResultType();
1906 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001907 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001908 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001909 case Primitive::kPrimByte:
1910 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001911 case Primitive::kPrimBoolean:
1912 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001913 case Primitive::kPrimShort:
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001916 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001917 if (in.IsRegister()) {
1918 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001919 } else {
1920 DCHECK(in.GetConstant()->IsIntConstant());
1921 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1922 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1923 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001924 break;
1925
1926 default:
1927 LOG(FATAL) << "Unexpected type conversion from " << input_type
1928 << " to " << result_type;
1929 }
1930 break;
1931
Roland Levillain01a8d712014-11-14 16:27:39 +00001932 case Primitive::kPrimShort:
1933 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001934 case Primitive::kPrimBoolean:
1935 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001936 case Primitive::kPrimByte:
1937 case Primitive::kPrimInt:
1938 case Primitive::kPrimChar:
1939 // Processing a Dex `int-to-short' instruction.
1940 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001942 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001944 } else {
1945 DCHECK(in.GetConstant()->IsIntConstant());
1946 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001947 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001948 }
1949 break;
1950
1951 default:
1952 LOG(FATAL) << "Unexpected type conversion from " << input_type
1953 << " to " << result_type;
1954 }
1955 break;
1956
Roland Levillain946e1432014-11-11 17:35:19 +00001957 case Primitive::kPrimInt:
1958 switch (input_type) {
1959 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001960 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001961 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001963 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001965 } else {
1966 DCHECK(in.IsConstant());
1967 DCHECK(in.GetConstant()->IsLongConstant());
1968 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001969 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001970 }
1971 break;
1972
Roland Levillain3f8f9362014-12-02 17:45:01 +00001973 case Primitive::kPrimFloat: {
1974 // Processing a Dex `float-to-int' instruction.
1975 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1976 Register output = out.AsRegister<Register>();
1977 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1978 Label done, nan;
1979
1980 __ movl(output, Immediate(kPrimIntMax));
1981 // temp = int-to-float(output)
1982 __ cvtsi2ss(temp, output);
1983 // if input >= temp goto done
1984 __ comiss(input, temp);
1985 __ j(kAboveEqual, &done);
1986 // if input == NaN goto nan
1987 __ j(kUnordered, &nan);
1988 // output = float-to-int-truncate(input)
1989 __ cvttss2si(output, input);
1990 __ jmp(&done);
1991 __ Bind(&nan);
1992 // output = 0
1993 __ xorl(output, output);
1994 __ Bind(&done);
1995 break;
1996 }
1997
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001998 case Primitive::kPrimDouble: {
1999 // Processing a Dex `double-to-int' instruction.
2000 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2001 Register output = out.AsRegister<Register>();
2002 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2003 Label done, nan;
2004
2005 __ movl(output, Immediate(kPrimIntMax));
2006 // temp = int-to-double(output)
2007 __ cvtsi2sd(temp, output);
2008 // if input >= temp goto done
2009 __ comisd(input, temp);
2010 __ j(kAboveEqual, &done);
2011 // if input == NaN goto nan
2012 __ j(kUnordered, &nan);
2013 // output = double-to-int-truncate(input)
2014 __ cvttsd2si(output, input);
2015 __ jmp(&done);
2016 __ Bind(&nan);
2017 // output = 0
2018 __ xorl(output, output);
2019 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002020 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002021 }
Roland Levillain946e1432014-11-11 17:35:19 +00002022
2023 default:
2024 LOG(FATAL) << "Unexpected type conversion from " << input_type
2025 << " to " << result_type;
2026 }
2027 break;
2028
Roland Levillaindff1f282014-11-05 14:15:05 +00002029 case Primitive::kPrimLong:
2030 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002031 case Primitive::kPrimBoolean:
2032 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 case Primitive::kPrimByte:
2034 case Primitive::kPrimShort:
2035 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002036 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002037 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002038 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2039 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 __ cdq();
2042 break;
2043
2044 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002045 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002046 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2047 conversion,
2048 conversion->GetDexPc(),
2049 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002050 break;
2051
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002053 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002054 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2055 conversion,
2056 conversion->GetDexPc(),
2057 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected type conversion from " << input_type
2062 << " to " << result_type;
2063 }
2064 break;
2065
Roland Levillain981e4542014-11-14 11:47:14 +00002066 case Primitive::kPrimChar:
2067 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002068 case Primitive::kPrimBoolean:
2069 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002070 case Primitive::kPrimByte:
2071 case Primitive::kPrimShort:
2072 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002073 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2074 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002075 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002076 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002077 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002078 } else {
2079 DCHECK(in.GetConstant()->IsIntConstant());
2080 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002082 }
2083 break;
2084
2085 default:
2086 LOG(FATAL) << "Unexpected type conversion from " << input_type
2087 << " to " << result_type;
2088 }
2089 break;
2090
Roland Levillaindff1f282014-11-05 14:15:05 +00002091 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002092 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002093 case Primitive::kPrimBoolean:
2094 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002095 case Primitive::kPrimByte:
2096 case Primitive::kPrimShort:
2097 case Primitive::kPrimInt:
2098 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002099 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002101 break;
2102
Roland Levillain6d0e4832014-11-27 18:31:21 +00002103 case Primitive::kPrimLong: {
2104 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002105 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002106
Roland Levillain232ade02015-04-20 15:14:36 +01002107 // Create stack space for the call to
2108 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2109 // TODO: enhance register allocator to ask for stack temporaries.
2110 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2111 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2112 __ subl(ESP, Immediate(adjustment));
2113 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002114
Roland Levillain232ade02015-04-20 15:14:36 +01002115 // Load the value to the FP stack, using temporaries if needed.
2116 PushOntoFPStack(in, 0, adjustment, false, true);
2117
2118 if (out.IsStackSlot()) {
2119 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2120 } else {
2121 __ fstps(Address(ESP, 0));
2122 Location stack_temp = Location::StackSlot(0);
2123 codegen_->Move32(out, stack_temp);
2124 }
2125
2126 // Remove the temporary stack space we allocated.
2127 if (adjustment != 0) {
2128 __ addl(ESP, Immediate(adjustment));
2129 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002130 break;
2131 }
2132
Roland Levillaincff13742014-11-17 14:32:17 +00002133 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002134 // Processing a Dex `double-to-float' instruction.
2135 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected type conversion from " << input_type
2140 << " to " << result_type;
2141 };
2142 break;
2143
Roland Levillaindff1f282014-11-05 14:15:05 +00002144 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002145 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002146 case Primitive::kPrimBoolean:
2147 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002148 case Primitive::kPrimByte:
2149 case Primitive::kPrimShort:
2150 case Primitive::kPrimInt:
2151 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002152 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002154 break;
2155
Roland Levillain647b9ed2014-11-27 12:06:00 +00002156 case Primitive::kPrimLong: {
2157 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002158 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002159
Roland Levillain232ade02015-04-20 15:14:36 +01002160 // Create stack space for the call to
2161 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2162 // TODO: enhance register allocator to ask for stack temporaries.
2163 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2164 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2165 __ subl(ESP, Immediate(adjustment));
2166 }
2167
2168 // Load the value to the FP stack, using temporaries if needed.
2169 PushOntoFPStack(in, 0, adjustment, false, true);
2170
2171 if (out.IsDoubleStackSlot()) {
2172 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2173 } else {
2174 __ fstpl(Address(ESP, 0));
2175 Location stack_temp = Location::DoubleStackSlot(0);
2176 codegen_->Move64(out, stack_temp);
2177 }
2178
2179 // Remove the temporary stack space we allocated.
2180 if (adjustment != 0) {
2181 __ addl(ESP, Immediate(adjustment));
2182 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002183 break;
2184 }
2185
Roland Levillaincff13742014-11-17 14:32:17 +00002186 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002187 // Processing a Dex `float-to-double' instruction.
2188 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002189 break;
2190
2191 default:
2192 LOG(FATAL) << "Unexpected type conversion from " << input_type
2193 << " to " << result_type;
2194 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002195 break;
2196
2197 default:
2198 LOG(FATAL) << "Unexpected type conversion from " << input_type
2199 << " to " << result_type;
2200 }
2201}
2202
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002203void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 LocationSummary* locations =
2205 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002206 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002207 case Primitive::kPrimInt: {
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2211 break;
2212 }
2213
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002214 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002215 locations->SetInAt(0, Location::RequiresRegister());
2216 locations->SetInAt(1, Location::Any());
2217 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002218 break;
2219 }
2220
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002221 case Primitive::kPrimFloat:
2222 case Primitive::kPrimDouble: {
2223 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002224 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002225 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002226 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002227 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002228
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2231 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002232 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002233}
2234
2235void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2236 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 Location first = locations->InAt(0);
2238 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002239 Location out = locations->Out();
2240
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002241 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002242 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002243 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002244 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2245 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002246 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2247 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002248 } else {
2249 __ leal(out.AsRegister<Register>(), Address(
2250 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2251 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002252 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002253 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2254 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2255 __ addl(out.AsRegister<Register>(), Immediate(value));
2256 } else {
2257 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2258 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002259 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002260 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002262 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002263 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002264 }
2265
2266 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002267 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002268 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2269 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002270 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002271 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2272 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002273 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002274 } else {
2275 DCHECK(second.IsConstant()) << second;
2276 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2277 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2278 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002279 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002280 break;
2281 }
2282
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002283 case Primitive::kPrimFloat: {
2284 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002285 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002286 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002287 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002288 }
2289
2290 case Primitive::kPrimDouble: {
2291 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002293 }
2294 break;
2295 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002297 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002298 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002299 }
2300}
2301
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002302void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002303 LocationSummary* locations =
2304 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002305 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002306 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002307 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002308 locations->SetInAt(0, Location::RequiresRegister());
2309 locations->SetInAt(1, Location::Any());
2310 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311 break;
2312 }
Calin Juravle11351682014-10-23 15:38:15 +01002313 case Primitive::kPrimFloat:
2314 case Primitive::kPrimDouble: {
2315 locations->SetInAt(0, Location::RequiresFpuRegister());
2316 locations->SetInAt(1, Location::RequiresFpuRegister());
2317 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318 break;
Calin Juravle11351682014-10-23 15:38:15 +01002319 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002321 default:
Calin Juravle11351682014-10-23 15:38:15 +01002322 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002323 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002324}
2325
2326void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2327 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002328 Location first = locations->InAt(0);
2329 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002330 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002331 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002332 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002333 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002334 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002335 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002336 __ subl(first.AsRegister<Register>(),
2337 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002338 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002339 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002340 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002341 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002342 }
2343
2344 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002345 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002346 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2347 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002348 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002349 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002350 __ sbbl(first.AsRegisterPairHigh<Register>(),
2351 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002352 } else {
2353 DCHECK(second.IsConstant()) << second;
2354 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2355 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2356 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002357 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002358 break;
2359 }
2360
Calin Juravle11351682014-10-23 15:38:15 +01002361 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002363 break;
Calin Juravle11351682014-10-23 15:38:15 +01002364 }
2365
2366 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002368 break;
2369 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002370
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002371 default:
Calin Juravle11351682014-10-23 15:38:15 +01002372 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002373 }
2374}
2375
Calin Juravle34bacdf2014-10-07 20:23:36 +01002376void LocationsBuilderX86::VisitMul(HMul* mul) {
2377 LocationSummary* locations =
2378 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2379 switch (mul->GetResultType()) {
2380 case Primitive::kPrimInt:
2381 locations->SetInAt(0, Location::RequiresRegister());
2382 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002383 if (mul->InputAt(1)->IsIntConstant()) {
2384 // Can use 3 operand multiply.
2385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2386 } else {
2387 locations->SetOut(Location::SameAsFirstInput());
2388 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002389 break;
2390 case Primitive::kPrimLong: {
2391 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002392 locations->SetInAt(1, Location::Any());
2393 locations->SetOut(Location::SameAsFirstInput());
2394 // Needed for imul on 32bits with 64bits output.
2395 locations->AddTemp(Location::RegisterLocation(EAX));
2396 locations->AddTemp(Location::RegisterLocation(EDX));
2397 break;
2398 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002399 case Primitive::kPrimFloat:
2400 case Primitive::kPrimDouble: {
2401 locations->SetInAt(0, Location::RequiresFpuRegister());
2402 locations->SetInAt(1, Location::RequiresFpuRegister());
2403 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002404 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002405 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002406
2407 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002408 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002409 }
2410}
2411
2412void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2413 LocationSummary* locations = mul->GetLocations();
2414 Location first = locations->InAt(0);
2415 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002416 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002417
2418 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002419 case Primitive::kPrimInt:
2420 // The constant may have ended up in a register, so test explicitly to avoid
2421 // problems where the output may not be the same as the first operand.
2422 if (mul->InputAt(1)->IsIntConstant()) {
2423 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2424 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2425 } else if (second.IsRegister()) {
2426 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002427 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002428 } else {
2429 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002430 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002431 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002432 }
2433 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002434
2435 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002436 Register in1_hi = first.AsRegisterPairHigh<Register>();
2437 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 Register eax = locations->GetTemp(0).AsRegister<Register>();
2439 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002440
2441 DCHECK_EQ(EAX, eax);
2442 DCHECK_EQ(EDX, edx);
2443
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002444 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002445 // output: in1
2446 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2447 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2448 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002449 if (second.IsConstant()) {
2450 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002451
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002452 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2453 int32_t low_value = Low32Bits(value);
2454 int32_t high_value = High32Bits(value);
2455 Immediate low(low_value);
2456 Immediate high(high_value);
2457
2458 __ movl(eax, high);
2459 // eax <- in1.lo * in2.hi
2460 __ imull(eax, in1_lo);
2461 // in1.hi <- in1.hi * in2.lo
2462 __ imull(in1_hi, low);
2463 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2464 __ addl(in1_hi, eax);
2465 // move in2_lo to eax to prepare for double precision
2466 __ movl(eax, low);
2467 // edx:eax <- in1.lo * in2.lo
2468 __ mull(in1_lo);
2469 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2470 __ addl(in1_hi, edx);
2471 // in1.lo <- (in1.lo * in2.lo)[31:0];
2472 __ movl(in1_lo, eax);
2473 } else if (second.IsRegisterPair()) {
2474 Register in2_hi = second.AsRegisterPairHigh<Register>();
2475 Register in2_lo = second.AsRegisterPairLow<Register>();
2476
2477 __ movl(eax, in2_hi);
2478 // eax <- in1.lo * in2.hi
2479 __ imull(eax, in1_lo);
2480 // in1.hi <- in1.hi * in2.lo
2481 __ imull(in1_hi, in2_lo);
2482 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2483 __ addl(in1_hi, eax);
2484 // move in1_lo to eax to prepare for double precision
2485 __ movl(eax, in1_lo);
2486 // edx:eax <- in1.lo * in2.lo
2487 __ mull(in2_lo);
2488 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2489 __ addl(in1_hi, edx);
2490 // in1.lo <- (in1.lo * in2.lo)[31:0];
2491 __ movl(in1_lo, eax);
2492 } else {
2493 DCHECK(second.IsDoubleStackSlot()) << second;
2494 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2495 Address in2_lo(ESP, second.GetStackIndex());
2496
2497 __ movl(eax, in2_hi);
2498 // eax <- in1.lo * in2.hi
2499 __ imull(eax, in1_lo);
2500 // in1.hi <- in1.hi * in2.lo
2501 __ imull(in1_hi, in2_lo);
2502 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2503 __ addl(in1_hi, eax);
2504 // move in1_lo to eax to prepare for double precision
2505 __ movl(eax, in1_lo);
2506 // edx:eax <- in1.lo * in2.lo
2507 __ mull(in2_lo);
2508 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2509 __ addl(in1_hi, edx);
2510 // in1.lo <- (in1.lo * in2.lo)[31:0];
2511 __ movl(in1_lo, eax);
2512 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002513
2514 break;
2515 }
2516
Calin Juravleb5bfa962014-10-21 18:02:24 +01002517 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002519 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002520 }
2521
2522 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002524 break;
2525 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002526
2527 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002528 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002529 }
2530}
2531
Roland Levillain232ade02015-04-20 15:14:36 +01002532void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2533 uint32_t temp_offset,
2534 uint32_t stack_adjustment,
2535 bool is_fp,
2536 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002537 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002538 DCHECK(!is_wide);
2539 if (is_fp) {
2540 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2541 } else {
2542 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2543 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002544 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002545 DCHECK(is_wide);
2546 if (is_fp) {
2547 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2548 } else {
2549 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2550 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002551 } else {
2552 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002553 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002554 Location stack_temp = Location::StackSlot(temp_offset);
2555 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002556 if (is_fp) {
2557 __ flds(Address(ESP, temp_offset));
2558 } else {
2559 __ filds(Address(ESP, temp_offset));
2560 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002561 } else {
2562 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2563 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002564 if (is_fp) {
2565 __ fldl(Address(ESP, temp_offset));
2566 } else {
2567 __ fildl(Address(ESP, temp_offset));
2568 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002569 }
2570 }
2571}
2572
2573void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2574 Primitive::Type type = rem->GetResultType();
2575 bool is_float = type == Primitive::kPrimFloat;
2576 size_t elem_size = Primitive::ComponentSize(type);
2577 LocationSummary* locations = rem->GetLocations();
2578 Location first = locations->InAt(0);
2579 Location second = locations->InAt(1);
2580 Location out = locations->Out();
2581
2582 // Create stack space for 2 elements.
2583 // TODO: enhance register allocator to ask for stack temporaries.
2584 __ subl(ESP, Immediate(2 * elem_size));
2585
2586 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002587 const bool is_wide = !is_float;
2588 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2589 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002590
2591 // Loop doing FPREM until we stabilize.
2592 Label retry;
2593 __ Bind(&retry);
2594 __ fprem();
2595
2596 // Move FP status to AX.
2597 __ fstsw();
2598
2599 // And see if the argument reduction is complete. This is signaled by the
2600 // C2 FPU flag bit set to 0.
2601 __ andl(EAX, Immediate(kC2ConditionMask));
2602 __ j(kNotEqual, &retry);
2603
2604 // We have settled on the final value. Retrieve it into an XMM register.
2605 // Store FP top of stack to real stack.
2606 if (is_float) {
2607 __ fsts(Address(ESP, 0));
2608 } else {
2609 __ fstl(Address(ESP, 0));
2610 }
2611
2612 // Pop the 2 items from the FP stack.
2613 __ fucompp();
2614
2615 // Load the value from the stack into an XMM register.
2616 DCHECK(out.IsFpuRegister()) << out;
2617 if (is_float) {
2618 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2619 } else {
2620 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2621 }
2622
2623 // And remove the temporary stack space we allocated.
2624 __ addl(ESP, Immediate(2 * elem_size));
2625}
2626
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002627
2628void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2629 DCHECK(instruction->IsDiv() || instruction->IsRem());
2630
2631 LocationSummary* locations = instruction->GetLocations();
2632 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002633 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002634
2635 Register out_register = locations->Out().AsRegister<Register>();
2636 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002637 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002638
2639 DCHECK(imm == 1 || imm == -1);
2640
2641 if (instruction->IsRem()) {
2642 __ xorl(out_register, out_register);
2643 } else {
2644 __ movl(out_register, input_register);
2645 if (imm == -1) {
2646 __ negl(out_register);
2647 }
2648 }
2649}
2650
2651
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002652void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002653 LocationSummary* locations = instruction->GetLocations();
2654
2655 Register out_register = locations->Out().AsRegister<Register>();
2656 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002657 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002658
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002659 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002660 Register num = locations->GetTemp(0).AsRegister<Register>();
2661
2662 __ leal(num, Address(input_register, std::abs(imm) - 1));
2663 __ testl(input_register, input_register);
2664 __ cmovl(kGreaterEqual, num, input_register);
2665 int shift = CTZ(imm);
2666 __ sarl(num, Immediate(shift));
2667
2668 if (imm < 0) {
2669 __ negl(num);
2670 }
2671
2672 __ movl(out_register, num);
2673}
2674
2675void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2676 DCHECK(instruction->IsDiv() || instruction->IsRem());
2677
2678 LocationSummary* locations = instruction->GetLocations();
2679 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2680
2681 Register eax = locations->InAt(0).AsRegister<Register>();
2682 Register out = locations->Out().AsRegister<Register>();
2683 Register num;
2684 Register edx;
2685
2686 if (instruction->IsDiv()) {
2687 edx = locations->GetTemp(0).AsRegister<Register>();
2688 num = locations->GetTemp(1).AsRegister<Register>();
2689 } else {
2690 edx = locations->Out().AsRegister<Register>();
2691 num = locations->GetTemp(0).AsRegister<Register>();
2692 }
2693
2694 DCHECK_EQ(EAX, eax);
2695 DCHECK_EQ(EDX, edx);
2696 if (instruction->IsDiv()) {
2697 DCHECK_EQ(EAX, out);
2698 } else {
2699 DCHECK_EQ(EDX, out);
2700 }
2701
2702 int64_t magic;
2703 int shift;
2704 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2705
2706 Label ndiv;
2707 Label end;
2708 // If numerator is 0, the result is 0, no computation needed.
2709 __ testl(eax, eax);
2710 __ j(kNotEqual, &ndiv);
2711
2712 __ xorl(out, out);
2713 __ jmp(&end);
2714
2715 __ Bind(&ndiv);
2716
2717 // Save the numerator.
2718 __ movl(num, eax);
2719
2720 // EAX = magic
2721 __ movl(eax, Immediate(magic));
2722
2723 // EDX:EAX = magic * numerator
2724 __ imull(num);
2725
2726 if (imm > 0 && magic < 0) {
2727 // EDX += num
2728 __ addl(edx, num);
2729 } else if (imm < 0 && magic > 0) {
2730 __ subl(edx, num);
2731 }
2732
2733 // Shift if needed.
2734 if (shift != 0) {
2735 __ sarl(edx, Immediate(shift));
2736 }
2737
2738 // EDX += 1 if EDX < 0
2739 __ movl(eax, edx);
2740 __ shrl(edx, Immediate(31));
2741 __ addl(edx, eax);
2742
2743 if (instruction->IsRem()) {
2744 __ movl(eax, num);
2745 __ imull(edx, Immediate(imm));
2746 __ subl(eax, edx);
2747 __ movl(edx, eax);
2748 } else {
2749 __ movl(eax, edx);
2750 }
2751 __ Bind(&end);
2752}
2753
Calin Juravlebacfec32014-11-14 15:54:36 +00002754void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2755 DCHECK(instruction->IsDiv() || instruction->IsRem());
2756
2757 LocationSummary* locations = instruction->GetLocations();
2758 Location out = locations->Out();
2759 Location first = locations->InAt(0);
2760 Location second = locations->InAt(1);
2761 bool is_div = instruction->IsDiv();
2762
2763 switch (instruction->GetResultType()) {
2764 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 DCHECK_EQ(EAX, first.AsRegister<Register>());
2766 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002767
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002768 if (instruction->InputAt(1)->IsIntConstant()) {
2769 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002770
2771 if (imm == 0) {
2772 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2773 } else if (imm == 1 || imm == -1) {
2774 DivRemOneOrMinusOne(instruction);
2775 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002776 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002777 } else {
2778 DCHECK(imm <= -2 || imm >= 2);
2779 GenerateDivRemWithAnyConstant(instruction);
2780 }
2781 } else {
2782 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002783 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002784 is_div);
2785 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002786
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002787 Register second_reg = second.AsRegister<Register>();
2788 // 0x80000000/-1 triggers an arithmetic exception!
2789 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2790 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002791
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002792 __ cmpl(second_reg, Immediate(-1));
2793 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002794
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002795 // edx:eax <- sign-extended of eax
2796 __ cdq();
2797 // eax = quotient, edx = remainder
2798 __ idivl(second_reg);
2799 __ Bind(slow_path->GetExitLabel());
2800 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002801 break;
2802 }
2803
2804 case Primitive::kPrimLong: {
2805 InvokeRuntimeCallingConvention calling_convention;
2806 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2807 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2808 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2809 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2810 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2811 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2812
2813 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002814 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2815 instruction,
2816 instruction->GetDexPc(),
2817 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002818 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002819 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2820 instruction,
2821 instruction->GetDexPc(),
2822 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002823 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002824 break;
2825 }
2826
2827 default:
2828 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2829 }
2830}
2831
Calin Juravle7c4954d2014-10-28 16:57:40 +00002832void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002833 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002834 ? LocationSummary::kCall
2835 : LocationSummary::kNoCall;
2836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2837
Calin Juravle7c4954d2014-10-28 16:57:40 +00002838 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002839 case Primitive::kPrimInt: {
2840 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002841 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002842 locations->SetOut(Location::SameAsFirstInput());
2843 // Intel uses edx:eax as the dividend.
2844 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002845 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2846 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2847 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002848 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002849 locations->AddTemp(Location::RequiresRegister());
2850 }
Calin Juravled0d48522014-11-04 16:40:20 +00002851 break;
2852 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002853 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002854 InvokeRuntimeCallingConvention calling_convention;
2855 locations->SetInAt(0, Location::RegisterPairLocation(
2856 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2857 locations->SetInAt(1, Location::RegisterPairLocation(
2858 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2859 // Runtime helper puts the result in EAX, EDX.
2860 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002861 break;
2862 }
2863 case Primitive::kPrimFloat:
2864 case Primitive::kPrimDouble: {
2865 locations->SetInAt(0, Location::RequiresFpuRegister());
2866 locations->SetInAt(1, Location::RequiresFpuRegister());
2867 locations->SetOut(Location::SameAsFirstInput());
2868 break;
2869 }
2870
2871 default:
2872 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2873 }
2874}
2875
2876void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2877 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002878 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002879 Location first = locations->InAt(0);
2880 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002881
2882 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002883 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002884 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002885 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002886 break;
2887 }
2888
2889 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002890 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002892 break;
2893 }
2894
2895 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002896 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002898 break;
2899 }
2900
2901 default:
2902 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2903 }
2904}
2905
Calin Juravlebacfec32014-11-14 15:54:36 +00002906void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002907 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002908
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002909 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2910 ? LocationSummary::kCall
2911 : LocationSummary::kNoCall;
2912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002913
Calin Juravled2ec87d2014-12-08 14:24:46 +00002914 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002915 case Primitive::kPrimInt: {
2916 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002917 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002918 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002919 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2920 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2921 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002922 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002923 locations->AddTemp(Location::RequiresRegister());
2924 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002925 break;
2926 }
2927 case Primitive::kPrimLong: {
2928 InvokeRuntimeCallingConvention calling_convention;
2929 locations->SetInAt(0, Location::RegisterPairLocation(
2930 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2931 locations->SetInAt(1, Location::RegisterPairLocation(
2932 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2933 // Runtime helper puts the result in EAX, EDX.
2934 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2935 break;
2936 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002937 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002938 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002939 locations->SetInAt(0, Location::Any());
2940 locations->SetInAt(1, Location::Any());
2941 locations->SetOut(Location::RequiresFpuRegister());
2942 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002943 break;
2944 }
2945
2946 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002947 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002948 }
2949}
2950
2951void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2952 Primitive::Type type = rem->GetResultType();
2953 switch (type) {
2954 case Primitive::kPrimInt:
2955 case Primitive::kPrimLong: {
2956 GenerateDivRemIntegral(rem);
2957 break;
2958 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002959 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002960 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002961 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002962 break;
2963 }
2964 default:
2965 LOG(FATAL) << "Unexpected rem type " << type;
2966 }
2967}
2968
Calin Juravled0d48522014-11-04 16:40:20 +00002969void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2970 LocationSummary* locations =
2971 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002972 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002973 case Primitive::kPrimByte:
2974 case Primitive::kPrimChar:
2975 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002976 case Primitive::kPrimInt: {
2977 locations->SetInAt(0, Location::Any());
2978 break;
2979 }
2980 case Primitive::kPrimLong: {
2981 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2982 if (!instruction->IsConstant()) {
2983 locations->AddTemp(Location::RequiresRegister());
2984 }
2985 break;
2986 }
2987 default:
2988 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2989 }
Calin Juravled0d48522014-11-04 16:40:20 +00002990 if (instruction->HasUses()) {
2991 locations->SetOut(Location::SameAsFirstInput());
2992 }
2993}
2994
2995void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2996 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2997 codegen_->AddSlowPath(slow_path);
2998
2999 LocationSummary* locations = instruction->GetLocations();
3000 Location value = locations->InAt(0);
3001
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003002 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003003 case Primitive::kPrimByte:
3004 case Primitive::kPrimChar:
3005 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003006 case Primitive::kPrimInt: {
3007 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003008 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003009 __ j(kEqual, slow_path->GetEntryLabel());
3010 } else if (value.IsStackSlot()) {
3011 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3012 __ j(kEqual, slow_path->GetEntryLabel());
3013 } else {
3014 DCHECK(value.IsConstant()) << value;
3015 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3016 __ jmp(slow_path->GetEntryLabel());
3017 }
3018 }
3019 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003020 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003021 case Primitive::kPrimLong: {
3022 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003024 __ movl(temp, value.AsRegisterPairLow<Register>());
3025 __ orl(temp, value.AsRegisterPairHigh<Register>());
3026 __ j(kEqual, slow_path->GetEntryLabel());
3027 } else {
3028 DCHECK(value.IsConstant()) << value;
3029 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3030 __ jmp(slow_path->GetEntryLabel());
3031 }
3032 }
3033 break;
3034 }
3035 default:
3036 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003037 }
Calin Juravled0d48522014-11-04 16:40:20 +00003038}
3039
Calin Juravle9aec02f2014-11-18 23:06:35 +00003040void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3041 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3042
3043 LocationSummary* locations =
3044 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3045
3046 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003047 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003048 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003049 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003050 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003051 // The shift count needs to be in CL or a constant.
3052 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003053 locations->SetOut(Location::SameAsFirstInput());
3054 break;
3055 }
3056 default:
3057 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3058 }
3059}
3060
3061void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3062 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3063
3064 LocationSummary* locations = op->GetLocations();
3065 Location first = locations->InAt(0);
3066 Location second = locations->InAt(1);
3067 DCHECK(first.Equals(locations->Out()));
3068
3069 switch (op->GetResultType()) {
3070 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003071 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003072 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003073 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003075 DCHECK_EQ(ECX, second_reg);
3076 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003077 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003078 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003079 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003080 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003081 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003082 }
3083 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003084 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3085 if (shift == 0) {
3086 return;
3087 }
3088 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003089 if (op->IsShl()) {
3090 __ shll(first_reg, imm);
3091 } else if (op->IsShr()) {
3092 __ sarl(first_reg, imm);
3093 } else {
3094 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003095 }
3096 }
3097 break;
3098 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003099 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003100 if (second.IsRegister()) {
3101 Register second_reg = second.AsRegister<Register>();
3102 DCHECK_EQ(ECX, second_reg);
3103 if (op->IsShl()) {
3104 GenerateShlLong(first, second_reg);
3105 } else if (op->IsShr()) {
3106 GenerateShrLong(first, second_reg);
3107 } else {
3108 GenerateUShrLong(first, second_reg);
3109 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003110 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003111 // Shift by a constant.
3112 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3113 // Nothing to do if the shift is 0, as the input is already the output.
3114 if (shift != 0) {
3115 if (op->IsShl()) {
3116 GenerateShlLong(first, shift);
3117 } else if (op->IsShr()) {
3118 GenerateShrLong(first, shift);
3119 } else {
3120 GenerateUShrLong(first, shift);
3121 }
3122 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003123 }
3124 break;
3125 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003126 default:
3127 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3128 }
3129}
3130
Mark P Mendell73945692015-04-29 14:56:17 +00003131void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3132 Register low = loc.AsRegisterPairLow<Register>();
3133 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003134 if (shift == 1) {
3135 // This is just an addition.
3136 __ addl(low, low);
3137 __ adcl(high, high);
3138 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003139 // Shift by 32 is easy. High gets low, and low gets 0.
3140 codegen_->EmitParallelMoves(
3141 loc.ToLow(),
3142 loc.ToHigh(),
3143 Primitive::kPrimInt,
3144 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3145 loc.ToLow(),
3146 Primitive::kPrimInt);
3147 } else if (shift > 32) {
3148 // Low part becomes 0. High part is low part << (shift-32).
3149 __ movl(high, low);
3150 __ shll(high, Immediate(shift - 32));
3151 __ xorl(low, low);
3152 } else {
3153 // Between 1 and 31.
3154 __ shld(high, low, Immediate(shift));
3155 __ shll(low, Immediate(shift));
3156 }
3157}
3158
Calin Juravle9aec02f2014-11-18 23:06:35 +00003159void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3160 Label done;
3161 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3162 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3163 __ testl(shifter, Immediate(32));
3164 __ j(kEqual, &done);
3165 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3166 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3167 __ Bind(&done);
3168}
3169
Mark P Mendell73945692015-04-29 14:56:17 +00003170void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3171 Register low = loc.AsRegisterPairLow<Register>();
3172 Register high = loc.AsRegisterPairHigh<Register>();
3173 if (shift == 32) {
3174 // Need to copy the sign.
3175 DCHECK_NE(low, high);
3176 __ movl(low, high);
3177 __ sarl(high, Immediate(31));
3178 } else if (shift > 32) {
3179 DCHECK_NE(low, high);
3180 // High part becomes sign. Low part is shifted by shift - 32.
3181 __ movl(low, high);
3182 __ sarl(high, Immediate(31));
3183 __ sarl(low, Immediate(shift - 32));
3184 } else {
3185 // Between 1 and 31.
3186 __ shrd(low, high, Immediate(shift));
3187 __ sarl(high, Immediate(shift));
3188 }
3189}
3190
Calin Juravle9aec02f2014-11-18 23:06:35 +00003191void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3192 Label done;
3193 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3194 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3195 __ testl(shifter, Immediate(32));
3196 __ j(kEqual, &done);
3197 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3198 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3199 __ Bind(&done);
3200}
3201
Mark P Mendell73945692015-04-29 14:56:17 +00003202void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3203 Register low = loc.AsRegisterPairLow<Register>();
3204 Register high = loc.AsRegisterPairHigh<Register>();
3205 if (shift == 32) {
3206 // Shift by 32 is easy. Low gets high, and high gets 0.
3207 codegen_->EmitParallelMoves(
3208 loc.ToHigh(),
3209 loc.ToLow(),
3210 Primitive::kPrimInt,
3211 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3212 loc.ToHigh(),
3213 Primitive::kPrimInt);
3214 } else if (shift > 32) {
3215 // Low part is high >> (shift - 32). High part becomes 0.
3216 __ movl(low, high);
3217 __ shrl(low, Immediate(shift - 32));
3218 __ xorl(high, high);
3219 } else {
3220 // Between 1 and 31.
3221 __ shrd(low, high, Immediate(shift));
3222 __ shrl(high, Immediate(shift));
3223 }
3224}
3225
Calin Juravle9aec02f2014-11-18 23:06:35 +00003226void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3227 Label done;
3228 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3229 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3230 __ testl(shifter, Immediate(32));
3231 __ j(kEqual, &done);
3232 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3233 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3234 __ Bind(&done);
3235}
3236
3237void LocationsBuilderX86::VisitShl(HShl* shl) {
3238 HandleShift(shl);
3239}
3240
3241void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3242 HandleShift(shl);
3243}
3244
3245void LocationsBuilderX86::VisitShr(HShr* shr) {
3246 HandleShift(shr);
3247}
3248
3249void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3250 HandleShift(shr);
3251}
3252
3253void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3254 HandleShift(ushr);
3255}
3256
3257void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3258 HandleShift(ushr);
3259}
3260
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003261void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003264 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003265 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003266 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003267 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003268}
3269
3270void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3271 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003272 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003273 // Note: if heap poisoning is enabled, the entry point takes cares
3274 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003275 codegen_->InvokeRuntime(
3276 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3277 instruction,
3278 instruction->GetDexPc(),
3279 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003280 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003281}
3282
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003283void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3284 LocationSummary* locations =
3285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3286 locations->SetOut(Location::RegisterLocation(EAX));
3287 InvokeRuntimeCallingConvention calling_convention;
3288 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003289 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003290 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003291}
3292
3293void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003295 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3296
Roland Levillain4d027112015-07-01 15:41:14 +01003297 // Note: if heap poisoning is enabled, the entry point takes cares
3298 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003299 codegen_->InvokeRuntime(
3300 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3301 instruction,
3302 instruction->GetDexPc(),
3303 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003304 DCHECK(!codegen_->IsLeafMethod());
3305}
3306
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003307void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003308 LocationSummary* locations =
3309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003310 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3311 if (location.IsStackSlot()) {
3312 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3313 } else if (location.IsDoubleStackSlot()) {
3314 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003315 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003316 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003317}
3318
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003319void InstructionCodeGeneratorX86::VisitParameterValue(
3320 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3321}
3322
3323void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3324 LocationSummary* locations =
3325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3326 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3327}
3328
3329void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003330}
3331
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003332void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003333 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003334 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003335 locations->SetInAt(0, Location::RequiresRegister());
3336 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003337}
3338
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003339void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3340 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003341 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003342 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003343 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003344 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003345 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003347 break;
3348
3349 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003350 __ notl(out.AsRegisterPairLow<Register>());
3351 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003352 break;
3353
3354 default:
3355 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3356 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003357}
3358
David Brazdil66d126e2015-04-03 16:02:44 +01003359void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3362 locations->SetInAt(0, Location::RequiresRegister());
3363 locations->SetOut(Location::SameAsFirstInput());
3364}
3365
3366void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003367 LocationSummary* locations = bool_not->GetLocations();
3368 Location in = locations->InAt(0);
3369 Location out = locations->Out();
3370 DCHECK(in.Equals(out));
3371 __ xorl(out.AsRegister<Register>(), Immediate(1));
3372}
3373
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003374void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003375 LocationSummary* locations =
3376 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003377 switch (compare->InputAt(0)->GetType()) {
3378 case Primitive::kPrimLong: {
3379 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003380 locations->SetInAt(1, Location::Any());
3381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3382 break;
3383 }
3384 case Primitive::kPrimFloat:
3385 case Primitive::kPrimDouble: {
3386 locations->SetInAt(0, Location::RequiresFpuRegister());
3387 locations->SetInAt(1, Location::RequiresFpuRegister());
3388 locations->SetOut(Location::RequiresRegister());
3389 break;
3390 }
3391 default:
3392 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3393 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003394}
3395
3396void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003397 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003398 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003399 Location left = locations->InAt(0);
3400 Location right = locations->InAt(1);
3401
3402 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003403 switch (compare->InputAt(0)->GetType()) {
3404 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003405 Register left_low = left.AsRegisterPairLow<Register>();
3406 Register left_high = left.AsRegisterPairHigh<Register>();
3407 int32_t val_low = 0;
3408 int32_t val_high = 0;
3409 bool right_is_const = false;
3410
3411 if (right.IsConstant()) {
3412 DCHECK(right.GetConstant()->IsLongConstant());
3413 right_is_const = true;
3414 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3415 val_low = Low32Bits(val);
3416 val_high = High32Bits(val);
3417 }
3418
Calin Juravleddb7df22014-11-25 20:56:51 +00003419 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003420 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003421 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003422 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003423 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003424 DCHECK(right_is_const) << right;
3425 if (val_high == 0) {
3426 __ testl(left_high, left_high);
3427 } else {
3428 __ cmpl(left_high, Immediate(val_high));
3429 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003431 __ j(kLess, &less); // Signed compare.
3432 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003433 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003434 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003435 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003436 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003437 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003438 DCHECK(right_is_const) << right;
3439 if (val_low == 0) {
3440 __ testl(left_low, left_low);
3441 } else {
3442 __ cmpl(left_low, Immediate(val_low));
3443 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003444 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003445 break;
3446 }
3447 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003449 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3450 break;
3451 }
3452 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003454 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003455 break;
3456 }
3457 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003458 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003459 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003460 __ movl(out, Immediate(0));
3461 __ j(kEqual, &done);
3462 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3463
3464 __ Bind(&greater);
3465 __ movl(out, Immediate(1));
3466 __ jmp(&done);
3467
3468 __ Bind(&less);
3469 __ movl(out, Immediate(-1));
3470
3471 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003472}
3473
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003474void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003475 LocationSummary* locations =
3476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003477 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3478 locations->SetInAt(i, Location::Any());
3479 }
3480 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003481}
3482
3483void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003484 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003485 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003486}
3487
Calin Juravle52c48962014-12-16 17:02:57 +00003488void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3489 /*
3490 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3491 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3492 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3493 */
3494 switch (kind) {
3495 case MemBarrierKind::kAnyAny: {
3496 __ mfence();
3497 break;
3498 }
3499 case MemBarrierKind::kAnyStore:
3500 case MemBarrierKind::kLoadAny:
3501 case MemBarrierKind::kStoreStore: {
3502 // nop
3503 break;
3504 }
3505 default:
3506 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003507 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003508}
3509
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003510
Mark Mendell09ed1a32015-03-25 08:30:06 -04003511void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003512 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003513 // TODO: Implement all kinds of calls:
3514 // 1) boot -> boot
3515 // 2) app -> boot
3516 // 3) app -> app
3517 //
3518 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003519
3520 if (invoke->IsStringInit()) {
3521 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003522 Register reg = temp.AsRegister<Register>();
3523 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003524 // (temp + offset_of_quick_compiled_code)()
3525 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003526 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3527 } else if (invoke->IsRecursive()) {
3528 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003529 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003530 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3531
3532 Register method_reg;
3533 Register reg = temp.AsRegister<Register>();
3534 if (current_method.IsRegister()) {
3535 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003536 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003537 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003538 DCHECK(!current_method.IsValid());
3539 method_reg = reg;
3540 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003541 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003542 // temp = temp->dex_cache_resolved_methods_;
3543 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3544 // temp = temp[index_in_cache]
3545 __ movl(reg, Address(reg,
3546 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3547 // (temp + offset_of_quick_compiled_code)()
3548 __ call(Address(reg,
3549 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003550 }
3551
3552 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003553}
3554
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003555void CodeGeneratorX86::MarkGCCard(Register temp,
3556 Register card,
3557 Register object,
3558 Register value,
3559 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003561 if (value_can_be_null) {
3562 __ testl(value, value);
3563 __ j(kEqual, &is_null);
3564 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3566 __ movl(temp, object);
3567 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003568 __ movb(Address(temp, card, TIMES_1, 0),
3569 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003570 if (value_can_be_null) {
3571 __ Bind(&is_null);
3572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573}
3574
Calin Juravle52c48962014-12-16 17:02:57 +00003575void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3576 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003577 LocationSummary* locations =
3578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003579 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003580
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003581 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3582 locations->SetOut(Location::RequiresFpuRegister());
3583 } else {
3584 // The output overlaps in case of long: we don't want the low move to overwrite
3585 // the object's location.
3586 locations->SetOut(Location::RequiresRegister(),
3587 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3588 : Location::kNoOutputOverlap);
3589 }
Calin Juravle52c48962014-12-16 17:02:57 +00003590
3591 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3592 // Long values can be loaded atomically into an XMM using movsd.
3593 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3594 // and then copy the XMM into the output 32bits at a time).
3595 locations->AddTemp(Location::RequiresFpuRegister());
3596 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003597}
3598
Calin Juravle52c48962014-12-16 17:02:57 +00003599void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3600 const FieldInfo& field_info) {
3601 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003602
Calin Juravle52c48962014-12-16 17:02:57 +00003603 LocationSummary* locations = instruction->GetLocations();
3604 Register base = locations->InAt(0).AsRegister<Register>();
3605 Location out = locations->Out();
3606 bool is_volatile = field_info.IsVolatile();
3607 Primitive::Type field_type = field_info.GetFieldType();
3608 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3609
3610 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003611 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003612 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003613 break;
3614 }
3615
3616 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003617 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003618 break;
3619 }
3620
3621 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003622 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003623 break;
3624 }
3625
3626 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003627 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003628 break;
3629 }
3630
3631 case Primitive::kPrimInt:
3632 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003633 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003634 break;
3635 }
3636
3637 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003638 if (is_volatile) {
3639 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3640 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003641 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003642 __ movd(out.AsRegisterPairLow<Register>(), temp);
3643 __ psrlq(temp, Immediate(32));
3644 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3645 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003646 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003647 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003648 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003649 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3650 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003651 break;
3652 }
3653
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003654 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003655 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003656 break;
3657 }
3658
3659 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003660 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003661 break;
3662 }
3663
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003664 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003665 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003666 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667 }
Calin Juravle52c48962014-12-16 17:02:57 +00003668
Calin Juravle77520bc2015-01-12 18:45:46 +00003669 // Longs are handled in the switch.
3670 if (field_type != Primitive::kPrimLong) {
3671 codegen_->MaybeRecordImplicitNullCheck(instruction);
3672 }
3673
Calin Juravle52c48962014-12-16 17:02:57 +00003674 if (is_volatile) {
3675 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3676 }
Roland Levillain4d027112015-07-01 15:41:14 +01003677
3678 if (field_type == Primitive::kPrimNot) {
3679 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3680 }
Calin Juravle52c48962014-12-16 17:02:57 +00003681}
3682
3683void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3684 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3685
3686 LocationSummary* locations =
3687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3688 locations->SetInAt(0, Location::RequiresRegister());
3689 bool is_volatile = field_info.IsVolatile();
3690 Primitive::Type field_type = field_info.GetFieldType();
3691 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3692 || (field_type == Primitive::kPrimByte);
3693
3694 // The register allocator does not support multiple
3695 // inputs that die at entry with one in a specific register.
3696 if (is_byte_type) {
3697 // Ensure the value is in a byte register.
3698 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003699 } else if (Primitive::IsFloatingPointType(field_type)) {
3700 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003701 } else {
3702 locations->SetInAt(1, Location::RequiresRegister());
3703 }
Calin Juravle52c48962014-12-16 17:02:57 +00003704 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003705 // Temporary registers for the write barrier.
3706 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003707 // Ensure the card is in a byte register.
3708 locations->AddTemp(Location::RegisterLocation(ECX));
3709 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3710 // 64bits value can be atomically written to an address with movsd and an XMM register.
3711 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3712 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3713 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3714 // isolated cases when we need this it isn't worth adding the extra complexity.
3715 locations->AddTemp(Location::RequiresFpuRegister());
3716 locations->AddTemp(Location::RequiresFpuRegister());
3717 }
3718}
3719
3720void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003721 const FieldInfo& field_info,
3722 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003723 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3724
3725 LocationSummary* locations = instruction->GetLocations();
3726 Register base = locations->InAt(0).AsRegister<Register>();
3727 Location value = locations->InAt(1);
3728 bool is_volatile = field_info.IsVolatile();
3729 Primitive::Type field_type = field_info.GetFieldType();
3730 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003731 bool needs_write_barrier =
3732 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003733
3734 if (is_volatile) {
3735 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3736 }
3737
3738 switch (field_type) {
3739 case Primitive::kPrimBoolean:
3740 case Primitive::kPrimByte: {
3741 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3742 break;
3743 }
3744
3745 case Primitive::kPrimShort:
3746 case Primitive::kPrimChar: {
3747 __ movw(Address(base, offset), value.AsRegister<Register>());
3748 break;
3749 }
3750
3751 case Primitive::kPrimInt:
3752 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003753 if (kPoisonHeapReferences && needs_write_barrier) {
3754 // Note that in the case where `value` is a null reference,
3755 // we do not enter this block, as the reference does not
3756 // need poisoning.
3757 DCHECK_EQ(field_type, Primitive::kPrimNot);
3758 Register temp = locations->GetTemp(0).AsRegister<Register>();
3759 __ movl(temp, value.AsRegister<Register>());
3760 __ PoisonHeapReference(temp);
3761 __ movl(Address(base, offset), temp);
3762 } else {
3763 __ movl(Address(base, offset), value.AsRegister<Register>());
3764 }
Calin Juravle52c48962014-12-16 17:02:57 +00003765 break;
3766 }
3767
3768 case Primitive::kPrimLong: {
3769 if (is_volatile) {
3770 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3771 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3772 __ movd(temp1, value.AsRegisterPairLow<Register>());
3773 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3774 __ punpckldq(temp1, temp2);
3775 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003776 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003777 } else {
3778 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003780 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3781 }
3782 break;
3783 }
3784
3785 case Primitive::kPrimFloat: {
3786 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3787 break;
3788 }
3789
3790 case Primitive::kPrimDouble: {
3791 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3792 break;
3793 }
3794
3795 case Primitive::kPrimVoid:
3796 LOG(FATAL) << "Unreachable type " << field_type;
3797 UNREACHABLE();
3798 }
3799
Calin Juravle77520bc2015-01-12 18:45:46 +00003800 // Longs are handled in the switch.
3801 if (field_type != Primitive::kPrimLong) {
3802 codegen_->MaybeRecordImplicitNullCheck(instruction);
3803 }
3804
Roland Levillain4d027112015-07-01 15:41:14 +01003805 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003806 Register temp = locations->GetTemp(0).AsRegister<Register>();
3807 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003808 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003809 }
3810
Calin Juravle52c48962014-12-16 17:02:57 +00003811 if (is_volatile) {
3812 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3813 }
3814}
3815
3816void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3817 HandleFieldGet(instruction, instruction->GetFieldInfo());
3818}
3819
3820void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3821 HandleFieldGet(instruction, instruction->GetFieldInfo());
3822}
3823
3824void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3825 HandleFieldSet(instruction, instruction->GetFieldInfo());
3826}
3827
3828void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003829 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003830}
3831
3832void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3833 HandleFieldSet(instruction, instruction->GetFieldInfo());
3834}
3835
3836void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003837 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003838}
3839
3840void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3841 HandleFieldGet(instruction, instruction->GetFieldInfo());
3842}
3843
3844void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3845 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846}
3847
3848void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003849 LocationSummary* locations =
3850 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003851 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3852 ? Location::RequiresRegister()
3853 : Location::Any();
3854 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003855 if (instruction->HasUses()) {
3856 locations->SetOut(Location::SameAsFirstInput());
3857 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858}
3859
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003860void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003861 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3862 return;
3863 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003864 LocationSummary* locations = instruction->GetLocations();
3865 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003866
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003867 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3868 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3869}
3870
3871void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003872 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003873 codegen_->AddSlowPath(slow_path);
3874
3875 LocationSummary* locations = instruction->GetLocations();
3876 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003877
3878 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003879 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003880 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003881 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003882 } else {
3883 DCHECK(obj.IsConstant()) << obj;
3884 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3885 __ jmp(slow_path->GetEntryLabel());
3886 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003887 }
3888 __ j(kEqual, slow_path->GetEntryLabel());
3889}
3890
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003891void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3892 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3893 GenerateImplicitNullCheck(instruction);
3894 } else {
3895 GenerateExplicitNullCheck(instruction);
3896 }
3897}
3898
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003899void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003900 LocationSummary* locations =
3901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003902 locations->SetInAt(0, Location::RequiresRegister());
3903 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003904 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3905 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3906 } else {
3907 // The output overlaps in case of long: we don't want the low move to overwrite
3908 // the array's location.
3909 locations->SetOut(Location::RequiresRegister(),
3910 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3911 : Location::kNoOutputOverlap);
3912 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913}
3914
3915void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3916 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003917 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 Location index = locations->InAt(1);
3919
Calin Juravle77520bc2015-01-12 18:45:46 +00003920 Primitive::Type type = instruction->GetType();
3921 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922 case Primitive::kPrimBoolean: {
3923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003924 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925 if (index.IsConstant()) {
3926 __ movzxb(out, Address(obj,
3927 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930 }
3931 break;
3932 }
3933
3934 case Primitive::kPrimByte: {
3935 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003937 if (index.IsConstant()) {
3938 __ movsxb(out, Address(obj,
3939 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3940 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 }
3943 break;
3944 }
3945
3946 case Primitive::kPrimShort: {
3947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003948 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 if (index.IsConstant()) {
3950 __ movsxw(out, Address(obj,
3951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3952 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003953 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 }
3955 break;
3956 }
3957
3958 case Primitive::kPrimChar: {
3959 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003960 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 if (index.IsConstant()) {
3962 __ movzxw(out, Address(obj,
3963 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3964 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003965 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003966 }
3967 break;
3968 }
3969
3970 case Primitive::kPrimInt:
3971 case Primitive::kPrimNot: {
3972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003973 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003974 if (index.IsConstant()) {
3975 __ movl(out, Address(obj,
3976 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 }
3980 break;
3981 }
3982
3983 case Primitive::kPrimLong: {
3984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003985 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003986 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987 if (index.IsConstant()) {
3988 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003989 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003991 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003992 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003993 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003994 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003995 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003996 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 }
3999 break;
4000 }
4001
Mark Mendell7c8d0092015-01-26 11:21:33 -05004002 case Primitive::kPrimFloat: {
4003 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4004 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4005 if (index.IsConstant()) {
4006 __ movss(out, Address(obj,
4007 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4008 } else {
4009 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4010 }
4011 break;
4012 }
4013
4014 case Primitive::kPrimDouble: {
4015 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4016 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4017 if (index.IsConstant()) {
4018 __ movsd(out, Address(obj,
4019 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4020 } else {
4021 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4022 }
4023 break;
4024 }
4025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004026 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004027 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004028 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004030
4031 if (type != Primitive::kPrimLong) {
4032 codegen_->MaybeRecordImplicitNullCheck(instruction);
4033 }
Roland Levillain4d027112015-07-01 15:41:14 +01004034
4035 if (type == Primitive::kPrimNot) {
4036 Register out = locations->Out().AsRegister<Register>();
4037 __ MaybeUnpoisonHeapReference(out);
4038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039}
4040
4041void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004042 // This location builder might end up asking to up to four registers, which is
4043 // not currently possible for baseline. The situation in which we need four
4044 // registers cannot be met by baseline though, because it has not run any
4045 // optimization.
4046
Nicolas Geoffray39468442014-09-02 15:17:15 +01004047 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004048 bool needs_write_barrier =
4049 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4050
Mark Mendell5f874182015-03-04 15:42:45 -05004051 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004052
Nicolas Geoffray39468442014-09-02 15:17:15 +01004053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4054 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004055 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004056
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004057 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004058 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004059 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4060 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4061 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004062 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004063 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4064 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004065 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004066 // In case of a byte operation, the register allocator does not support multiple
4067 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004068 locations->SetInAt(0, Location::RequiresRegister());
4069 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004070 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004071 // Ensure the value is in a byte register.
4072 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004073 } else if (Primitive::IsFloatingPointType(value_type)) {
4074 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004075 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004076 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004078 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004079 // Temporary registers for the write barrier.
4080 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004081 // Ensure the card is in a byte register.
4082 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004084 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085}
4086
4087void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4088 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004089 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004090 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004091 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004092 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004093 bool needs_runtime_call = locations->WillCall();
4094 bool needs_write_barrier =
4095 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096
4097 switch (value_type) {
4098 case Primitive::kPrimBoolean:
4099 case Primitive::kPrimByte: {
4100 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004101 if (index.IsConstant()) {
4102 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004103 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004104 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004105 } else {
4106 __ movb(Address(obj, offset),
4107 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4108 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004109 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004110 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004112 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004113 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004115 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4116 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004117 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004118 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004119 break;
4120 }
4121
4122 case Primitive::kPrimShort:
4123 case Primitive::kPrimChar: {
4124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125 if (index.IsConstant()) {
4126 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004127 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004129 } else {
4130 __ movw(Address(obj, offset),
4131 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004133 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004134 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4136 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004137 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004139 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4140 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 break;
4144 }
4145
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004146 case Primitive::kPrimInt:
4147 case Primitive::kPrimNot: {
4148 if (!needs_runtime_call) {
4149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4150 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004151 size_t offset =
4152 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004153 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004154 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4155 Register temp = locations->GetTemp(0).AsRegister<Register>();
4156 __ movl(temp, value.AsRegister<Register>());
4157 __ PoisonHeapReference(temp);
4158 __ movl(Address(obj, offset), temp);
4159 } else {
4160 __ movl(Address(obj, offset), value.AsRegister<Register>());
4161 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004162 } else {
4163 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004164 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4165 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4166 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4167 // Note: if heap poisoning is enabled, no need to poison
4168 // (negate) `v` if it is a reference, as it would be null.
4169 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004170 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004171 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004172 DCHECK(index.IsRegister()) << index;
4173 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004174 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4175 Register temp = locations->GetTemp(0).AsRegister<Register>();
4176 __ movl(temp, value.AsRegister<Register>());
4177 __ PoisonHeapReference(temp);
4178 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4179 } else {
4180 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4181 value.AsRegister<Register>());
4182 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004183 } else {
4184 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004185 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4186 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4187 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4188 // Note: if heap poisoning is enabled, no need to poison
4189 // (negate) `v` if it is a reference, as it would be null.
4190 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004191 }
4192 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004194
4195 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004196 Register temp = locations->GetTemp(0).AsRegister<Register>();
4197 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004198 codegen_->MarkGCCard(
4199 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004200 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004201 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004202 DCHECK_EQ(value_type, Primitive::kPrimNot);
4203 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004204 // Note: if heap poisoning is enabled, pAputObject takes cares
4205 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004206 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4207 instruction,
4208 instruction->GetDexPc(),
4209 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004210 }
4211 break;
4212 }
4213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 case Primitive::kPrimLong: {
4215 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216 if (index.IsConstant()) {
4217 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004218 if (value.IsRegisterPair()) {
4219 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004220 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004221 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004222 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004223 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004224 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4225 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004226 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004227 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004230 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004231 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004232 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004234 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004235 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004236 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004237 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004238 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004239 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004240 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004241 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004242 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004243 Immediate(High32Bits(val)));
4244 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245 }
4246 break;
4247 }
4248
Mark Mendell7c8d0092015-01-26 11:21:33 -05004249 case Primitive::kPrimFloat: {
4250 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4251 DCHECK(value.IsFpuRegister());
4252 if (index.IsConstant()) {
4253 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4254 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4255 } else {
4256 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4257 value.AsFpuRegister<XmmRegister>());
4258 }
4259 break;
4260 }
4261
4262 case Primitive::kPrimDouble: {
4263 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4264 DCHECK(value.IsFpuRegister());
4265 if (index.IsConstant()) {
4266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4267 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4268 } else {
4269 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4270 value.AsFpuRegister<XmmRegister>());
4271 }
4272 break;
4273 }
4274
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 case Primitive::kPrimVoid:
4276 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004277 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004278 }
4279}
4280
4281void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4282 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004283 locations->SetInAt(0, Location::RequiresRegister());
4284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004285}
4286
4287void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4288 LocationSummary* locations = instruction->GetLocations();
4289 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004290 Register obj = locations->InAt(0).AsRegister<Register>();
4291 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004292 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294}
4295
4296void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004297 LocationSummary* locations =
4298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004299 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004300 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004301 if (instruction->HasUses()) {
4302 locations->SetOut(Location::SameAsFirstInput());
4303 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304}
4305
4306void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4307 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004308 Location index_loc = locations->InAt(0);
4309 Location length_loc = locations->InAt(1);
4310 SlowPathCodeX86* slow_path =
4311 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004312
Mark Mendell99dbd682015-04-22 16:18:52 -04004313 if (length_loc.IsConstant()) {
4314 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4315 if (index_loc.IsConstant()) {
4316 // BCE will remove the bounds check if we are guarenteed to pass.
4317 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4318 if (index < 0 || index >= length) {
4319 codegen_->AddSlowPath(slow_path);
4320 __ jmp(slow_path->GetEntryLabel());
4321 } else {
4322 // Some optimization after BCE may have generated this, and we should not
4323 // generate a bounds check if it is a valid range.
4324 }
4325 return;
4326 }
4327
4328 // We have to reverse the jump condition because the length is the constant.
4329 Register index_reg = index_loc.AsRegister<Register>();
4330 __ cmpl(index_reg, Immediate(length));
4331 codegen_->AddSlowPath(slow_path);
4332 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004333 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004334 Register length = length_loc.AsRegister<Register>();
4335 if (index_loc.IsConstant()) {
4336 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4337 __ cmpl(length, Immediate(value));
4338 } else {
4339 __ cmpl(length, index_loc.AsRegister<Register>());
4340 }
4341 codegen_->AddSlowPath(slow_path);
4342 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004343 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344}
4345
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004346void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4347 temp->SetLocations(nullptr);
4348}
4349
4350void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4351 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004352 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353}
4354
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004355void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004356 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004357 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004358}
4359
4360void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004361 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4362}
4363
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004364void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4366}
4367
4368void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004369 HBasicBlock* block = instruction->GetBlock();
4370 if (block->GetLoopInformation() != nullptr) {
4371 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4372 // The back edge will generate the suspend check.
4373 return;
4374 }
4375 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4376 // The goto will generate the suspend check.
4377 return;
4378 }
4379 GenerateSuspendCheck(instruction, nullptr);
4380}
4381
4382void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4383 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004384 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004385 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4386 if (slow_path == nullptr) {
4387 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4388 instruction->SetSlowPath(slow_path);
4389 codegen_->AddSlowPath(slow_path);
4390 if (successor != nullptr) {
4391 DCHECK(successor->IsLoopHeader());
4392 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4393 }
4394 } else {
4395 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4396 }
4397
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004398 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004399 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004400 if (successor == nullptr) {
4401 __ j(kNotEqual, slow_path->GetEntryLabel());
4402 __ Bind(slow_path->GetReturnLabel());
4403 } else {
4404 __ j(kEqual, codegen_->GetLabelOf(successor));
4405 __ jmp(slow_path->GetEntryLabel());
4406 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004407}
4408
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004409X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4410 return codegen_->GetAssembler();
4411}
4412
Mark Mendell7c8d0092015-01-26 11:21:33 -05004413void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004414 ScratchRegisterScope ensure_scratch(
4415 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4416 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4417 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4418 __ movl(temp_reg, Address(ESP, src + stack_offset));
4419 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004420}
4421
4422void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004423 ScratchRegisterScope ensure_scratch(
4424 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4425 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4426 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4427 __ movl(temp_reg, Address(ESP, src + stack_offset));
4428 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4429 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4430 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004431}
4432
4433void ParallelMoveResolverX86::EmitMove(size_t index) {
4434 MoveOperands* move = moves_.Get(index);
4435 Location source = move->GetSource();
4436 Location destination = move->GetDestination();
4437
4438 if (source.IsRegister()) {
4439 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004440 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004441 } else {
4442 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004444 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004445 } else if (source.IsFpuRegister()) {
4446 if (destination.IsFpuRegister()) {
4447 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4448 } else if (destination.IsStackSlot()) {
4449 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4450 } else {
4451 DCHECK(destination.IsDoubleStackSlot());
4452 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4453 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004454 } else if (source.IsStackSlot()) {
4455 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004456 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004457 } else if (destination.IsFpuRegister()) {
4458 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004459 } else {
4460 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004461 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4462 }
4463 } else if (source.IsDoubleStackSlot()) {
4464 if (destination.IsFpuRegister()) {
4465 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4466 } else {
4467 DCHECK(destination.IsDoubleStackSlot()) << destination;
4468 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004469 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004470 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004471 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004472 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004473 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004474 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004475 if (value == 0) {
4476 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4477 } else {
4478 __ movl(destination.AsRegister<Register>(), Immediate(value));
4479 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004480 } else {
4481 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004482 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004483 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004484 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004485 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004486 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004487 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004488 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004489 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4490 if (value == 0) {
4491 // Easy handling of 0.0.
4492 __ xorps(dest, dest);
4493 } else {
4494 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004495 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4496 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4497 __ movl(temp, Immediate(value));
4498 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004499 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004500 } else {
4501 DCHECK(destination.IsStackSlot()) << destination;
4502 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4503 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004504 } else if (constant->IsLongConstant()) {
4505 int64_t value = constant->AsLongConstant()->GetValue();
4506 int32_t low_value = Low32Bits(value);
4507 int32_t high_value = High32Bits(value);
4508 Immediate low(low_value);
4509 Immediate high(high_value);
4510 if (destination.IsDoubleStackSlot()) {
4511 __ movl(Address(ESP, destination.GetStackIndex()), low);
4512 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4513 } else {
4514 __ movl(destination.AsRegisterPairLow<Register>(), low);
4515 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4516 }
4517 } else {
4518 DCHECK(constant->IsDoubleConstant());
4519 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004520 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004521 int32_t low_value = Low32Bits(value);
4522 int32_t high_value = High32Bits(value);
4523 Immediate low(low_value);
4524 Immediate high(high_value);
4525 if (destination.IsFpuRegister()) {
4526 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4527 if (value == 0) {
4528 // Easy handling of 0.0.
4529 __ xorpd(dest, dest);
4530 } else {
4531 __ pushl(high);
4532 __ pushl(low);
4533 __ movsd(dest, Address(ESP, 0));
4534 __ addl(ESP, Immediate(8));
4535 }
4536 } else {
4537 DCHECK(destination.IsDoubleStackSlot()) << destination;
4538 __ movl(Address(ESP, destination.GetStackIndex()), low);
4539 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4540 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004541 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004542 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004543 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004544 }
4545}
4546
Mark Mendella5c19ce2015-04-01 12:51:05 -04004547void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004548 Register suggested_scratch = reg == EAX ? EBX : EAX;
4549 ScratchRegisterScope ensure_scratch(
4550 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4551
4552 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4553 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4554 __ movl(Address(ESP, mem + stack_offset), reg);
4555 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004556}
4557
Mark Mendell7c8d0092015-01-26 11:21:33 -05004558void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004559 ScratchRegisterScope ensure_scratch(
4560 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4561
4562 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4563 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4564 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4565 __ movss(Address(ESP, mem + stack_offset), reg);
4566 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004567}
4568
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004569void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004570 ScratchRegisterScope ensure_scratch1(
4571 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004572
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004573 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4574 ScratchRegisterScope ensure_scratch2(
4575 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004576
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004577 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4578 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4579 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4580 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4581 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4582 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004583}
4584
4585void ParallelMoveResolverX86::EmitSwap(size_t index) {
4586 MoveOperands* move = moves_.Get(index);
4587 Location source = move->GetSource();
4588 Location destination = move->GetDestination();
4589
4590 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004591 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4592 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4593 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4594 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4595 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004596 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004598 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004599 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004600 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4601 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004602 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4603 // Use XOR Swap algorithm to avoid a temporary.
4604 DCHECK_NE(source.reg(), destination.reg());
4605 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4606 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4607 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4608 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4609 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4610 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4611 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004612 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4613 // Take advantage of the 16 bytes in the XMM register.
4614 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4615 Address stack(ESP, destination.GetStackIndex());
4616 // Load the double into the high doubleword.
4617 __ movhpd(reg, stack);
4618
4619 // Store the low double into the destination.
4620 __ movsd(stack, reg);
4621
4622 // Move the high double to the low double.
4623 __ psrldq(reg, Immediate(8));
4624 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4625 // Take advantage of the 16 bytes in the XMM register.
4626 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4627 Address stack(ESP, source.GetStackIndex());
4628 // Load the double into the high doubleword.
4629 __ movhpd(reg, stack);
4630
4631 // Store the low double into the destination.
4632 __ movsd(stack, reg);
4633
4634 // Move the high double to the low double.
4635 __ psrldq(reg, Immediate(8));
4636 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4637 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4638 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004639 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004640 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004641 }
4642}
4643
4644void ParallelMoveResolverX86::SpillScratch(int reg) {
4645 __ pushl(static_cast<Register>(reg));
4646}
4647
4648void ParallelMoveResolverX86::RestoreScratch(int reg) {
4649 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004650}
4651
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004652void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004653 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4654 ? LocationSummary::kCallOnSlowPath
4655 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004656 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004657 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004658 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004659 locations->SetOut(Location::RequiresRegister());
4660}
4661
4662void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004663 LocationSummary* locations = cls->GetLocations();
4664 Register out = locations->Out().AsRegister<Register>();
4665 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004666 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004667 DCHECK(!cls->CanCallRuntime());
4668 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004669 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004670 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004671 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004672 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004673 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004674 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004675 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004676
4677 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4678 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4679 codegen_->AddSlowPath(slow_path);
4680 __ testl(out, out);
4681 __ j(kEqual, slow_path->GetEntryLabel());
4682 if (cls->MustGenerateClinitCheck()) {
4683 GenerateClassInitializationCheck(slow_path, out);
4684 } else {
4685 __ Bind(slow_path->GetExitLabel());
4686 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004687 }
4688}
4689
4690void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4691 LocationSummary* locations =
4692 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4693 locations->SetInAt(0, Location::RequiresRegister());
4694 if (check->HasUses()) {
4695 locations->SetOut(Location::SameAsFirstInput());
4696 }
4697}
4698
4699void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004700 // We assume the class to not be null.
4701 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4702 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004703 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004704 GenerateClassInitializationCheck(slow_path,
4705 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004706}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004707
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004708void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4709 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004710 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4711 Immediate(mirror::Class::kStatusInitialized));
4712 __ j(kLess, slow_path->GetEntryLabel());
4713 __ Bind(slow_path->GetExitLabel());
4714 // No need for memory fence, thanks to the X86 memory model.
4715}
4716
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004717void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4718 LocationSummary* locations =
4719 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004720 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004721 locations->SetOut(Location::RequiresRegister());
4722}
4723
4724void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4725 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4726 codegen_->AddSlowPath(slow_path);
4727
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004728 LocationSummary* locations = load->GetLocations();
4729 Register out = locations->Out().AsRegister<Register>();
4730 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004731 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004732 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004733 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004734 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004735 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004736 __ testl(out, out);
4737 __ j(kEqual, slow_path->GetEntryLabel());
4738 __ Bind(slow_path->GetExitLabel());
4739}
4740
David Brazdilcb1c0552015-08-04 16:22:25 +01004741static Address GetExceptionTlsAddress() {
4742 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4743}
4744
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004745void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4746 LocationSummary* locations =
4747 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4748 locations->SetOut(Location::RequiresRegister());
4749}
4750
4751void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004752 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4753}
4754
4755void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4756 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4757}
4758
4759void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4760 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004761}
4762
4763void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4764 LocationSummary* locations =
4765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4766 InvokeRuntimeCallingConvention calling_convention;
4767 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4768}
4769
4770void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004771 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4772 instruction,
4773 instruction->GetDexPc(),
4774 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004775}
4776
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004777void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004778 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4779 ? LocationSummary::kNoCall
4780 : LocationSummary::kCallOnSlowPath;
4781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4782 locations->SetInAt(0, Location::RequiresRegister());
4783 locations->SetInAt(1, Location::Any());
4784 locations->SetOut(Location::RequiresRegister());
4785}
4786
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004787void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004788 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004789 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004790 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004791 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004792 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4793 Label done, zero;
4794 SlowPathCodeX86* slow_path = nullptr;
4795
4796 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004797 // Avoid null check if we know obj is not null.
4798 if (instruction->MustDoNullCheck()) {
4799 __ testl(obj, obj);
4800 __ j(kEqual, &zero);
4801 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004802 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004803 __ movl(out, Address(obj, class_offset));
4804 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004805 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004806 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004807 } else {
4808 DCHECK(cls.IsStackSlot()) << cls;
4809 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4810 }
4811
4812 if (instruction->IsClassFinal()) {
4813 // Classes must be equal for the instanceof to succeed.
4814 __ j(kNotEqual, &zero);
4815 __ movl(out, Immediate(1));
4816 __ jmp(&done);
4817 } else {
4818 // If the classes are not equal, we go into a slow path.
4819 DCHECK(locations->OnlyCallsOnSlowPath());
4820 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Alexandre Rames98596202015-08-19 11:33:36 +01004821 instruction, locations->InAt(1), locations->Out());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004822 codegen_->AddSlowPath(slow_path);
4823 __ j(kNotEqual, slow_path->GetEntryLabel());
4824 __ movl(out, Immediate(1));
4825 __ jmp(&done);
4826 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004827
4828 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4829 __ Bind(&zero);
4830 __ movl(out, Immediate(0));
4831 }
4832
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004833 if (slow_path != nullptr) {
4834 __ Bind(slow_path->GetExitLabel());
4835 }
4836 __ Bind(&done);
4837}
4838
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004839void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4841 instruction, LocationSummary::kCallOnSlowPath);
4842 locations->SetInAt(0, Location::RequiresRegister());
4843 locations->SetInAt(1, Location::Any());
4844 locations->AddTemp(Location::RequiresRegister());
4845}
4846
4847void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4848 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004849 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004850 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004851 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004852 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4853 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Alexandre Rames98596202015-08-19 11:33:36 +01004854 instruction, locations->InAt(1), locations->GetTemp(0));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004855 codegen_->AddSlowPath(slow_path);
4856
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004857 // Avoid null check if we know obj is not null.
4858 if (instruction->MustDoNullCheck()) {
4859 __ testl(obj, obj);
4860 __ j(kEqual, slow_path->GetExitLabel());
4861 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004862 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004863 __ movl(temp, Address(obj, class_offset));
4864 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004865 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004866 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004867 } else {
4868 DCHECK(cls.IsStackSlot()) << cls;
4869 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4870 }
Roland Levillain4d027112015-07-01 15:41:14 +01004871 // The checkcast succeeds if the classes are equal (fast path).
4872 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004873 __ j(kNotEqual, slow_path->GetEntryLabel());
4874 __ Bind(slow_path->GetExitLabel());
4875}
4876
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004877void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4878 LocationSummary* locations =
4879 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4880 InvokeRuntimeCallingConvention calling_convention;
4881 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4882}
4883
4884void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004885 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4886 : QUICK_ENTRY_POINT(pUnlockObject),
4887 instruction,
4888 instruction->GetDexPc(),
4889 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004890}
4891
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004892void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4893void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4894void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4895
4896void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4897 LocationSummary* locations =
4898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4899 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4900 || instruction->GetResultType() == Primitive::kPrimLong);
4901 locations->SetInAt(0, Location::RequiresRegister());
4902 locations->SetInAt(1, Location::Any());
4903 locations->SetOut(Location::SameAsFirstInput());
4904}
4905
4906void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4907 HandleBitwiseOperation(instruction);
4908}
4909
4910void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4911 HandleBitwiseOperation(instruction);
4912}
4913
4914void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4915 HandleBitwiseOperation(instruction);
4916}
4917
4918void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4919 LocationSummary* locations = instruction->GetLocations();
4920 Location first = locations->InAt(0);
4921 Location second = locations->InAt(1);
4922 DCHECK(first.Equals(locations->Out()));
4923
4924 if (instruction->GetResultType() == Primitive::kPrimInt) {
4925 if (second.IsRegister()) {
4926 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004927 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004928 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004929 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004930 } else {
4931 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004933 }
4934 } else if (second.IsConstant()) {
4935 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004936 __ andl(first.AsRegister<Register>(),
4937 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004939 __ orl(first.AsRegister<Register>(),
4940 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004941 } else {
4942 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004943 __ xorl(first.AsRegister<Register>(),
4944 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004945 }
4946 } else {
4947 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004948 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004949 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004950 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004951 } else {
4952 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004953 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004954 }
4955 }
4956 } else {
4957 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4958 if (second.IsRegisterPair()) {
4959 if (instruction->IsAnd()) {
4960 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4961 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4962 } else if (instruction->IsOr()) {
4963 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4964 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4965 } else {
4966 DCHECK(instruction->IsXor());
4967 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4968 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4969 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004970 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004971 if (instruction->IsAnd()) {
4972 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4973 __ andl(first.AsRegisterPairHigh<Register>(),
4974 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4975 } else if (instruction->IsOr()) {
4976 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4977 __ orl(first.AsRegisterPairHigh<Register>(),
4978 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4979 } else {
4980 DCHECK(instruction->IsXor());
4981 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4982 __ xorl(first.AsRegisterPairHigh<Register>(),
4983 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4984 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004985 } else {
4986 DCHECK(second.IsConstant()) << second;
4987 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004988 int32_t low_value = Low32Bits(value);
4989 int32_t high_value = High32Bits(value);
4990 Immediate low(low_value);
4991 Immediate high(high_value);
4992 Register first_low = first.AsRegisterPairLow<Register>();
4993 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004994 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004995 if (low_value == 0) {
4996 __ xorl(first_low, first_low);
4997 } else if (low_value != -1) {
4998 __ andl(first_low, low);
4999 }
5000 if (high_value == 0) {
5001 __ xorl(first_high, first_high);
5002 } else if (high_value != -1) {
5003 __ andl(first_high, high);
5004 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005005 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005006 if (low_value != 0) {
5007 __ orl(first_low, low);
5008 }
5009 if (high_value != 0) {
5010 __ orl(first_high, high);
5011 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005012 } else {
5013 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005014 if (low_value != 0) {
5015 __ xorl(first_low, low);
5016 }
5017 if (high_value != 0) {
5018 __ xorl(first_high, high);
5019 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005020 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005021 }
5022 }
5023}
5024
Calin Juravleb1498f62015-02-16 13:13:29 +00005025void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5026 // Nothing to do, this should be removed during prepare for register allocator.
5027 UNUSED(instruction);
5028 LOG(FATAL) << "Unreachable";
5029}
5030
5031void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5032 // Nothing to do, this should be removed during prepare for register allocator.
5033 UNUSED(instruction);
5034 LOG(FATAL) << "Unreachable";
5035}
5036
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005037void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5038 DCHECK(codegen_->IsBaseline());
5039 LocationSummary* locations =
5040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5041 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5042}
5043
5044void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5045 DCHECK(codegen_->IsBaseline());
5046 // Will be generated at use site.
5047}
5048
Roland Levillain4d027112015-07-01 15:41:14 +01005049#undef __
5050
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005051} // namespace x86
5052} // namespace art