blob: 091a3e5d928e2d2c27822e7eb669d46ec626a35c [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,
284 Location object_class,
285 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 class_to_check_(class_to_check),
288 object_class_(object_class),
289 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 DCHECK(instruction_->IsCheckCast()
294 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
296 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
297 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000298 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 x86_codegen->EmitParallelMoves(
304 class_to_check_,
305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100312 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 } else {
317 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100318 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
319 instruction_,
320 instruction_->GetDexPc(),
321 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 }
323
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 if (instruction_->IsInstanceOf()) {
326 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
327 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000328 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 __ jmp(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 HInstruction* const instruction_;
337 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
341 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
342};
343
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700344class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
345 public:
346 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
347 : instruction_(instruction) {}
348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357 // No need to restore live registers.
358 DCHECK(instruction_->IsDeoptimize());
359 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
360 uint32_t dex_pc = deoptimize->GetDexPc();
361 codegen->RecordPcInfo(instruction_, dex_pc, this);
362 }
363
Alexandre Rames9931f312015-06-19 14:47:01 +0100364 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
365
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700366 private:
367 HInstruction* const instruction_;
368 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
369};
370
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100371#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100372#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100373
Roland Levillain4fa13f62015-07-06 18:11:54 +0100374inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700375 switch (cond) {
376 case kCondEQ: return kEqual;
377 case kCondNE: return kNotEqual;
378 case kCondLT: return kLess;
379 case kCondLE: return kLessEqual;
380 case kCondGT: return kGreater;
381 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700382 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100383 LOG(FATAL) << "Unreachable";
384 UNREACHABLE();
385}
386
387inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
388 switch (cond) {
389 case kCondEQ: return kEqual;
390 case kCondNE: return kNotEqual;
391 case kCondLT: return kBelow;
392 case kCondLE: return kBelowEqual;
393 case kCondGT: return kAbove;
394 case kCondGE: return kAboveEqual;
395 }
396 LOG(FATAL) << "Unreachable";
397 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700398}
399
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100400void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100401 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100402}
403
404void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100405 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100406}
407
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
409 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
410 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100411}
412
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100413size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
414 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
415 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100416}
417
Mark Mendell7c8d0092015-01-26 11:21:33 -0500418size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
419 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
420 return GetFloatingPointSpillSlotSize();
421}
422
423size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
424 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
425 return GetFloatingPointSpillSlotSize();
426}
427
Alexandre Rames8158f282015-08-07 10:26:17 +0100428void CodeGeneratorX86::InvokeRuntime(Address entry_point,
429 HInstruction* instruction,
430 uint32_t dex_pc,
431 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100432 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100433 __ fs()->call(entry_point);
434 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100435}
436
Mark Mendellfb8d2792015-03-31 22:16:59 -0400437CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
438 const X86InstructionSetFeatures& isa_features,
439 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500440 : CodeGenerator(graph,
441 kNumberOfCpuRegisters,
442 kNumberOfXmmRegisters,
443 kNumberOfRegisterPairs,
444 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
445 arraysize(kCoreCalleeSaves))
446 | (1 << kFakeReturnRegister),
447 0,
448 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100449 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100450 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100451 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400452 move_resolver_(graph->GetArena(), this),
Vladimir Markob2c431e2015-08-19 12:45:42 +0000453 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000454 // Use a fake return address register to mimic Quick.
455 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100456}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459 switch (type) {
460 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462 X86ManagedRegister pair =
463 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100464 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
465 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100466 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
467 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100468 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100469 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100470 }
471
472 case Primitive::kPrimByte:
473 case Primitive::kPrimBoolean:
474 case Primitive::kPrimChar:
475 case Primitive::kPrimShort:
476 case Primitive::kPrimInt:
477 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100479 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100481 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
482 X86ManagedRegister current =
483 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
484 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100485 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100486 }
487 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100488 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489 }
490
491 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100493 return Location::FpuRegisterLocation(
494 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100495 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100496
497 case Primitive::kPrimVoid:
498 LOG(FATAL) << "Unreachable type " << type;
499 }
500
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100501 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100502}
503
Mark Mendell5f874182015-03-04 15:42:45 -0500504void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100506 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100507
508 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100509 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
Mark Mendell5f874182015-03-04 15:42:45 -0500511 if (is_baseline) {
512 blocked_core_registers_[EBP] = true;
513 blocked_core_registers_[ESI] = true;
514 blocked_core_registers_[EDI] = true;
515 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100516
517 UpdateBlockedPairRegisters();
518}
519
520void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
521 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
522 X86ManagedRegister current =
523 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
524 if (blocked_core_registers_[current.AsRegisterPairLow()]
525 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
526 blocked_register_pairs_[i] = true;
527 }
528 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100529}
530
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100531InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
532 : HGraphVisitor(graph),
533 assembler_(codegen->GetAssembler()),
534 codegen_(codegen) {}
535
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100536static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100537 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100538}
539
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100541 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000542 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000543 bool skip_overflow_check =
544 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000545 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000546
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000547 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100548 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100549 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100550 }
551
Mark Mendell5f874182015-03-04 15:42:45 -0500552 if (HasEmptyFrame()) {
553 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000554 }
Mark Mendell5f874182015-03-04 15:42:45 -0500555
556 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
557 Register reg = kCoreCalleeSaves[i];
558 if (allocated_registers_.ContainsCoreRegister(reg)) {
559 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100560 __ cfi().AdjustCFAOffset(kX86WordSize);
561 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500562 }
563 }
564
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100565 int adjust = GetFrameSize() - FrameEntrySpillSize();
566 __ subl(ESP, Immediate(adjust));
567 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100568 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000569}
570
571void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100572 __ cfi().RememberState();
573 if (!HasEmptyFrame()) {
574 int adjust = GetFrameSize() - FrameEntrySpillSize();
575 __ addl(ESP, Immediate(adjust));
576 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500577
David Srbeckyc34dc932015-04-12 09:27:43 +0100578 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
579 Register reg = kCoreCalleeSaves[i];
580 if (allocated_registers_.ContainsCoreRegister(reg)) {
581 __ popl(reg);
582 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
583 __ cfi().Restore(DWARFReg(reg));
584 }
Mark Mendell5f874182015-03-04 15:42:45 -0500585 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000586 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100587 __ ret();
588 __ cfi().RestoreState();
589 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000590}
591
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100592void CodeGeneratorX86::Bind(HBasicBlock* block) {
593 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000594}
595
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100596Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
597 switch (load->GetType()) {
598 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100600 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601
602 case Primitive::kPrimInt:
603 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100604 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100606
607 case Primitive::kPrimBoolean:
608 case Primitive::kPrimByte:
609 case Primitive::kPrimChar:
610 case Primitive::kPrimShort:
611 case Primitive::kPrimVoid:
612 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700613 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100614 }
615
616 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700617 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100618}
619
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100620Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
621 switch (type) {
622 case Primitive::kPrimBoolean:
623 case Primitive::kPrimByte:
624 case Primitive::kPrimChar:
625 case Primitive::kPrimShort:
626 case Primitive::kPrimInt:
627 case Primitive::kPrimNot:
628 return Location::RegisterLocation(EAX);
629
630 case Primitive::kPrimLong:
631 return Location::RegisterPairLocation(EAX, EDX);
632
633 case Primitive::kPrimVoid:
634 return Location::NoLocation();
635
636 case Primitive::kPrimDouble:
637 case Primitive::kPrimFloat:
638 return Location::FpuRegisterLocation(XMM0);
639 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100640
641 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100642}
643
644Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
645 return Location::RegisterLocation(kMethodRegisterArgument);
646}
647
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100648Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100649 switch (type) {
650 case Primitive::kPrimBoolean:
651 case Primitive::kPrimByte:
652 case Primitive::kPrimChar:
653 case Primitive::kPrimShort:
654 case Primitive::kPrimInt:
655 case Primitive::kPrimNot: {
656 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000657 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100658 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100660 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000661 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100662 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100663 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100664
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000665 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100666 uint32_t index = gp_index_;
667 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000668 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100669 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100670 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
671 calling_convention.GetRegisterPairAt(index));
672 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100673 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000674 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
675 }
676 }
677
678 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100679 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000680 stack_index_++;
681 if (index < calling_convention.GetNumberOfFpuRegisters()) {
682 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
683 } else {
684 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
685 }
686 }
687
688 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100689 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000690 stack_index_ += 2;
691 if (index < calling_convention.GetNumberOfFpuRegisters()) {
692 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
693 } else {
694 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100695 }
696 }
697
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100698 case Primitive::kPrimVoid:
699 LOG(FATAL) << "Unexpected parameter type " << type;
700 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100701 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100702 return Location();
703}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100704
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705void CodeGeneratorX86::Move32(Location destination, Location source) {
706 if (source.Equals(destination)) {
707 return;
708 }
709 if (destination.IsRegister()) {
710 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000711 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000713 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 } else {
715 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000716 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 } else if (destination.IsFpuRegister()) {
719 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000720 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 } else {
724 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000728 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000730 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000732 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500733 } else if (source.IsConstant()) {
734 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000735 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500736 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100737 } else {
738 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100739 __ pushl(Address(ESP, source.GetStackIndex()));
740 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 }
742 }
743}
744
745void CodeGeneratorX86::Move64(Location destination, Location source) {
746 if (source.Equals(destination)) {
747 return;
748 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100749 if (destination.IsRegisterPair()) {
750 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000751 EmitParallelMoves(
752 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
753 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100754 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000755 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100756 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
757 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 } else if (source.IsFpuRegister()) {
759 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000761 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100763 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
764 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
766 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500768 if (source.IsFpuRegister()) {
769 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
770 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000771 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 } else {
773 LOG(FATAL) << "Unimplemented";
774 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000776 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100777 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000778 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100779 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100780 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100781 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100782 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000783 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000784 } else if (source.IsConstant()) {
785 HConstant* constant = source.GetConstant();
786 int64_t value;
787 if (constant->IsLongConstant()) {
788 value = constant->AsLongConstant()->GetValue();
789 } else {
790 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000791 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000792 }
793 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
794 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100795 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000796 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000797 EmitParallelMoves(
798 Location::StackSlot(source.GetStackIndex()),
799 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100800 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000801 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100802 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
803 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 }
805 }
806}
807
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100808void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000809 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100810 if (instruction->IsCurrentMethod()) {
811 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
812 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000813 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100814 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000815 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000816 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
817 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000819 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000820 } else if (location.IsStackSlot()) {
821 __ movl(Address(ESP, location.GetStackIndex()), imm);
822 } else {
823 DCHECK(location.IsConstant());
824 DCHECK_EQ(location.GetConstant(), const_to_move);
825 }
826 } else if (const_to_move->IsLongConstant()) {
827 int64_t value = const_to_move->AsLongConstant()->GetValue();
828 if (location.IsRegisterPair()) {
829 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
830 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
831 } else if (location.IsDoubleStackSlot()) {
832 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000833 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
834 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000835 } else {
836 DCHECK(location.IsConstant());
837 DCHECK_EQ(location.GetConstant(), instruction);
838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000840 } else if (instruction->IsTemporary()) {
841 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000842 if (temp_location.IsStackSlot()) {
843 Move32(location, temp_location);
844 } else {
845 DCHECK(temp_location.IsDoubleStackSlot());
846 Move64(location, temp_location);
847 }
Roland Levillain476df552014-10-09 17:51:36 +0100848 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100850 switch (instruction->GetType()) {
851 case Primitive::kPrimBoolean:
852 case Primitive::kPrimByte:
853 case Primitive::kPrimChar:
854 case Primitive::kPrimShort:
855 case Primitive::kPrimInt:
856 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 case Primitive::kPrimFloat:
858 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 break;
860
861 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100862 case Primitive::kPrimDouble:
863 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 break;
865
866 default:
867 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
868 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000869 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100870 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871 switch (instruction->GetType()) {
872 case Primitive::kPrimBoolean:
873 case Primitive::kPrimByte:
874 case Primitive::kPrimChar:
875 case Primitive::kPrimShort:
876 case Primitive::kPrimInt:
877 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000879 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 break;
881
882 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000884 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100885 break;
886
887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890 }
891}
892
David Brazdilfc6a86a2015-06-26 10:33:45 +0000893void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100894 DCHECK(!successor->IsExitBlock());
895
896 HBasicBlock* block = got->GetBlock();
897 HInstruction* previous = got->GetPrevious();
898
899 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000900 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100901 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
902 return;
903 }
904
905 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
906 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
907 }
908 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000909 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000910 }
911}
912
David Brazdilfc6a86a2015-06-26 10:33:45 +0000913void LocationsBuilderX86::VisitGoto(HGoto* got) {
914 got->SetLocations(nullptr);
915}
916
917void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
918 HandleGoto(got, got->GetSuccessor());
919}
920
921void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
922 try_boundary->SetLocations(nullptr);
923}
924
925void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
926 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
927 if (!successor->IsExitBlock()) {
928 HandleGoto(try_boundary, successor);
929 }
930}
931
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000932void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000934}
935
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000936void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700937 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000938}
939
Mark Mendellc4701932015-04-10 13:18:51 -0400940void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
941 Label* true_label,
942 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943 if (cond->IsFPConditionTrueIfNaN()) {
944 __ j(kUnordered, true_label);
945 } else if (cond->IsFPConditionFalseIfNaN()) {
946 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400947 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400949}
950
951void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
952 Label* true_label,
953 Label* false_label) {
954 LocationSummary* locations = cond->GetLocations();
955 Location left = locations->InAt(0);
956 Location right = locations->InAt(1);
957 IfCondition if_cond = cond->GetCondition();
958
Mark Mendellc4701932015-04-10 13:18:51 -0400959 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400961 IfCondition true_high_cond = if_cond;
962 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100963 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400964
965 // Set the conditions for the test, remembering that == needs to be
966 // decided using the low words.
967 switch (if_cond) {
968 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400969 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100970 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400971 break;
972 case kCondLT:
973 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400974 break;
975 case kCondLE:
976 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400977 break;
978 case kCondGT:
979 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400980 break;
981 case kCondGE:
982 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400983 break;
984 }
985
986 if (right.IsConstant()) {
987 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400988 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100989 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400990
991 if (val_high == 0) {
992 __ testl(left_high, left_high);
993 } else {
994 __ cmpl(left_high, Immediate(val_high));
995 }
996 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100997 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400998 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100999 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001000 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001001 __ j(X86SignedCondition(true_high_cond), true_label);
1002 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001003 }
1004 // Must be equal high, so compare the lows.
1005 if (val_low == 0) {
1006 __ testl(left_low, left_low);
1007 } else {
1008 __ cmpl(left_low, Immediate(val_low));
1009 }
1010 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001011 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001012 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001013
1014 __ cmpl(left_high, right_high);
1015 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001016 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001017 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001018 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001019 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001020 __ j(X86SignedCondition(true_high_cond), true_label);
1021 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001022 }
1023 // Must be equal high, so compare the lows.
1024 __ cmpl(left_low, right_low);
1025 }
1026 // The last comparison might be unsigned.
1027 __ j(final_condition, true_label);
1028}
1029
1030void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1031 HCondition* condition,
1032 Label* true_target,
1033 Label* false_target,
1034 Label* always_true_target) {
1035 LocationSummary* locations = condition->GetLocations();
1036 Location left = locations->InAt(0);
1037 Location right = locations->InAt(1);
1038
1039 // We don't want true_target as a nullptr.
1040 if (true_target == nullptr) {
1041 true_target = always_true_target;
1042 }
1043 bool falls_through = (false_target == nullptr);
1044
1045 // FP compares don't like null false_targets.
1046 if (false_target == nullptr) {
1047 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1048 }
1049
1050 Primitive::Type type = condition->InputAt(0)->GetType();
1051 switch (type) {
1052 case Primitive::kPrimLong:
1053 GenerateLongComparesAndJumps(condition, true_target, false_target);
1054 break;
1055 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001056 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1057 GenerateFPJumps(condition, true_target, false_target);
1058 break;
1059 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001060 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1061 GenerateFPJumps(condition, true_target, false_target);
1062 break;
1063 default:
1064 LOG(FATAL) << "Unexpected compare type " << type;
1065 }
1066
1067 if (!falls_through) {
1068 __ jmp(false_target);
1069 }
1070}
1071
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001072void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1073 Label* true_target,
1074 Label* false_target,
1075 Label* always_true_target) {
1076 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001077 if (cond->IsIntConstant()) {
1078 // Constant condition, statically compared against 1.
1079 int32_t cond_value = cond->AsIntConstant()->GetValue();
1080 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001081 if (always_true_target != nullptr) {
1082 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001083 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001084 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001085 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001086 DCHECK_EQ(cond_value, 0);
1087 }
1088 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001089 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1091 // Moves do not affect the eflags register, so if the condition is
1092 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001093 // again. We can't use the eflags on long/FP conditions if they are
1094 // materialized due to the complex branching.
1095 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001097 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001098 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1099 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 if (!eflags_set) {
1101 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001102 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001103 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001104 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001105 } else {
1106 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1107 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001108 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001109 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001111 }
1112 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001113 // Condition has not been materialized, use its inputs as the
1114 // comparison and its condition as the branch condition.
1115
Mark Mendellc4701932015-04-10 13:18:51 -04001116 // Is this a long or FP comparison that has been folded into the HCondition?
1117 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1118 // Generate the comparison directly.
1119 GenerateCompareTestAndBranch(instruction->AsIf(),
1120 cond->AsCondition(),
1121 true_target,
1122 false_target,
1123 always_true_target);
1124 return;
1125 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001126
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001127 Location lhs = cond->GetLocations()->InAt(0);
1128 Location rhs = cond->GetLocations()->InAt(1);
1129 // LHS is guaranteed to be in a register (see
1130 // LocationsBuilderX86::VisitCondition).
1131 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001132 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001133 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001134 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001135 if (constant == 0) {
1136 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1137 } else {
1138 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1139 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001140 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001142 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001143 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001144 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001145 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001146 if (false_target != nullptr) {
1147 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148 }
1149}
1150
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001151void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1152 LocationSummary* locations =
1153 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1154 HInstruction* cond = if_instr->InputAt(0);
1155 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1156 locations->SetInAt(0, Location::Any());
1157 }
1158}
1159
1160void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1161 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1162 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1163 Label* always_true_target = true_target;
1164 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1165 if_instr->IfTrueSuccessor())) {
1166 always_true_target = nullptr;
1167 }
1168 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1169 if_instr->IfFalseSuccessor())) {
1170 false_target = nullptr;
1171 }
1172 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1173}
1174
1175void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1176 LocationSummary* locations = new (GetGraph()->GetArena())
1177 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1178 HInstruction* cond = deoptimize->InputAt(0);
1179 DCHECK(cond->IsCondition());
1180 if (cond->AsCondition()->NeedsMaterialization()) {
1181 locations->SetInAt(0, Location::Any());
1182 }
1183}
1184
1185void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1186 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1187 DeoptimizationSlowPathX86(deoptimize);
1188 codegen_->AddSlowPath(slow_path);
1189 Label* slow_path_entry = slow_path->GetEntryLabel();
1190 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1191}
1192
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001193void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001195}
1196
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1198 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199}
1200
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001201void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001202 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001203}
1204
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001205void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001206 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001207 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001208}
1209
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001211 LocationSummary* locations =
1212 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 switch (store->InputAt(1)->GetType()) {
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimByte:
1216 case Primitive::kPrimChar:
1217 case Primitive::kPrimShort:
1218 case Primitive::kPrimInt:
1219 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1222 break;
1223
1224 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1227 break;
1228
1229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001231 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001232}
1233
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001234void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001235 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001236}
1237
Roland Levillain0d37cd02015-05-27 16:39:19 +01001238void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001239 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001240 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001241 // Handle the long/FP comparisons made in instruction simplification.
1242 switch (cond->InputAt(0)->GetType()) {
1243 case Primitive::kPrimLong: {
1244 locations->SetInAt(0, Location::RequiresRegister());
1245 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1246 if (cond->NeedsMaterialization()) {
1247 locations->SetOut(Location::RequiresRegister());
1248 }
1249 break;
1250 }
1251 case Primitive::kPrimFloat:
1252 case Primitive::kPrimDouble: {
1253 locations->SetInAt(0, Location::RequiresFpuRegister());
1254 locations->SetInAt(1, Location::RequiresFpuRegister());
1255 if (cond->NeedsMaterialization()) {
1256 locations->SetOut(Location::RequiresRegister());
1257 }
1258 break;
1259 }
1260 default:
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 locations->SetInAt(1, Location::Any());
1263 if (cond->NeedsMaterialization()) {
1264 // We need a byte register.
1265 locations->SetOut(Location::RegisterLocation(ECX));
1266 }
1267 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001268 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001269}
1270
Roland Levillain0d37cd02015-05-27 16:39:19 +01001271void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001272 if (!cond->NeedsMaterialization()) {
1273 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001274 }
Mark Mendellc4701932015-04-10 13:18:51 -04001275
1276 LocationSummary* locations = cond->GetLocations();
1277 Location lhs = locations->InAt(0);
1278 Location rhs = locations->InAt(1);
1279 Register reg = locations->Out().AsRegister<Register>();
1280 Label true_label, false_label;
1281
1282 switch (cond->InputAt(0)->GetType()) {
1283 default: {
1284 // Integer case.
1285
1286 // Clear output register: setcc only sets the low byte.
1287 __ xorl(reg, reg);
1288
1289 if (rhs.IsRegister()) {
1290 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1291 } else if (rhs.IsConstant()) {
1292 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1293 if (constant == 0) {
1294 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1295 } else {
1296 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1297 }
1298 } else {
1299 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1300 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001301 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001302 return;
1303 }
1304 case Primitive::kPrimLong:
1305 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1306 break;
1307 case Primitive::kPrimFloat:
1308 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1309 GenerateFPJumps(cond, &true_label, &false_label);
1310 break;
1311 case Primitive::kPrimDouble:
1312 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1313 GenerateFPJumps(cond, &true_label, &false_label);
1314 break;
1315 }
1316
1317 // Convert the jumps into the result.
1318 Label done_label;
1319
Roland Levillain4fa13f62015-07-06 18:11:54 +01001320 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001321 __ Bind(&false_label);
1322 __ xorl(reg, reg);
1323 __ jmp(&done_label);
1324
Roland Levillain4fa13f62015-07-06 18:11:54 +01001325 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001326 __ Bind(&true_label);
1327 __ movl(reg, Immediate(1));
1328 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001329}
1330
1331void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1332 VisitCondition(comp);
1333}
1334
1335void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1336 VisitCondition(comp);
1337}
1338
1339void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1340 VisitCondition(comp);
1341}
1342
1343void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1344 VisitCondition(comp);
1345}
1346
1347void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1348 VisitCondition(comp);
1349}
1350
1351void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1352 VisitCondition(comp);
1353}
1354
1355void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1356 VisitCondition(comp);
1357}
1358
1359void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1360 VisitCondition(comp);
1361}
1362
1363void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1364 VisitCondition(comp);
1365}
1366
1367void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1368 VisitCondition(comp);
1369}
1370
1371void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1372 VisitCondition(comp);
1373}
1374
1375void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1376 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001377}
1378
1379void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001380 LocationSummary* locations =
1381 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001382 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383}
1384
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001385void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001386 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001387 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388}
1389
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001390void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1391 LocationSummary* locations =
1392 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1393 locations->SetOut(Location::ConstantLocation(constant));
1394}
1395
1396void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1397 // Will be generated at use site.
1398 UNUSED(constant);
1399}
1400
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001401void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001402 LocationSummary* locations =
1403 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001404 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001405}
1406
1407void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1408 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001409 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001410}
1411
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001412void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1413 LocationSummary* locations =
1414 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1415 locations->SetOut(Location::ConstantLocation(constant));
1416}
1417
1418void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1419 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001420 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001421}
1422
1423void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1424 LocationSummary* locations =
1425 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1426 locations->SetOut(Location::ConstantLocation(constant));
1427}
1428
1429void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1430 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001431 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001432}
1433
Calin Juravle27df7582015-04-17 19:12:31 +01001434void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1435 memory_barrier->SetLocations(nullptr);
1436}
1437
1438void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1439 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1440}
1441
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001442void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001443 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001444}
1445
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001446void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001447 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001448 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001449}
1450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001451void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001452 LocationSummary* locations =
1453 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001454 switch (ret->InputAt(0)->GetType()) {
1455 case Primitive::kPrimBoolean:
1456 case Primitive::kPrimByte:
1457 case Primitive::kPrimChar:
1458 case Primitive::kPrimShort:
1459 case Primitive::kPrimInt:
1460 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001461 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 break;
1463
1464 case Primitive::kPrimLong:
1465 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001466 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001467 break;
1468
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001469 case Primitive::kPrimFloat:
1470 case Primitive::kPrimDouble:
1471 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001472 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 break;
1474
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001477 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478}
1479
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001480void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001481 if (kIsDebugBuild) {
1482 switch (ret->InputAt(0)->GetType()) {
1483 case Primitive::kPrimBoolean:
1484 case Primitive::kPrimByte:
1485 case Primitive::kPrimChar:
1486 case Primitive::kPrimShort:
1487 case Primitive::kPrimInt:
1488 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001489 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001490 break;
1491
1492 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001493 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1494 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 break;
1496
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 case Primitive::kPrimFloat:
1498 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001499 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 break;
1501
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001504 }
1505 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001506 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001507}
1508
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001509void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001510 // When we do not run baseline, explicit clinit checks triggered by static
1511 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1512 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001513
Mark Mendellfb8d2792015-03-31 22:16:59 -04001514 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001515 if (intrinsic.TryDispatch(invoke)) {
1516 return;
1517 }
1518
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001519 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001520
1521 if (codegen_->IsBaseline()) {
1522 // Baseline does not have enough registers if the current method also
1523 // needs a register. We therefore do not require a register for it, and let
1524 // the code generation of the invoke handle it.
1525 LocationSummary* locations = invoke->GetLocations();
1526 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1527 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1528 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1529 }
1530 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001531}
1532
Mark Mendell09ed1a32015-03-25 08:30:06 -04001533static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1534 if (invoke->GetLocations()->Intrinsified()) {
1535 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1536 intrinsic.Dispatch(invoke);
1537 return true;
1538 }
1539 return false;
1540}
1541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001542void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001543 // When we do not run baseline, explicit clinit checks triggered by static
1544 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1545 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001546
Mark Mendell09ed1a32015-03-25 08:30:06 -04001547 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1548 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001549 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001550
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001551 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001552 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001553 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001554 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001555}
1556
1557void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1558 HandleInvoke(invoke);
1559}
1560
1561void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001562 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001563 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001564}
1565
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001566void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001567 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001568 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1569 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001570 LocationSummary* locations = invoke->GetLocations();
1571 Location receiver = locations->InAt(0);
1572 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001573 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001574 DCHECK(receiver.IsRegister());
1575 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001576 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001577 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001578 // temp = temp->GetMethodAt(method_offset);
1579 __ movl(temp, Address(temp, method_offset));
1580 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001581 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001582 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001584 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001585 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001586}
1587
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001588void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1589 HandleInvoke(invoke);
1590 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001591 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001592}
1593
1594void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1595 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001596 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001597 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1598 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001599 LocationSummary* locations = invoke->GetLocations();
1600 Location receiver = locations->InAt(0);
1601 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1602
1603 // Set the hidden argument.
1604 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606
1607 // temp = object->GetClass();
1608 if (receiver.IsStackSlot()) {
1609 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1610 __ movl(temp, Address(temp, class_offset));
1611 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001612 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001613 }
Roland Levillain4d027112015-07-01 15:41:14 +01001614 codegen_->MaybeRecordImplicitNullCheck(invoke);
1615 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001616 // temp = temp->GetImtEntryAt(method_offset);
1617 __ movl(temp, Address(temp, method_offset));
1618 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001619 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001620 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001621
1622 DCHECK(!codegen_->IsLeafMethod());
1623 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1624}
1625
Roland Levillain88cb1752014-10-20 16:36:47 +01001626void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1627 LocationSummary* locations =
1628 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1629 switch (neg->GetResultType()) {
1630 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001631 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001632 locations->SetInAt(0, Location::RequiresRegister());
1633 locations->SetOut(Location::SameAsFirstInput());
1634 break;
1635
Roland Levillain88cb1752014-10-20 16:36:47 +01001636 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001637 locations->SetInAt(0, Location::RequiresFpuRegister());
1638 locations->SetOut(Location::SameAsFirstInput());
1639 locations->AddTemp(Location::RequiresRegister());
1640 locations->AddTemp(Location::RequiresFpuRegister());
1641 break;
1642
Roland Levillain88cb1752014-10-20 16:36:47 +01001643 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001644 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001645 locations->SetOut(Location::SameAsFirstInput());
1646 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001647 break;
1648
1649 default:
1650 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1651 }
1652}
1653
1654void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1655 LocationSummary* locations = neg->GetLocations();
1656 Location out = locations->Out();
1657 Location in = locations->InAt(0);
1658 switch (neg->GetResultType()) {
1659 case Primitive::kPrimInt:
1660 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001661 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001663 break;
1664
1665 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001666 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001667 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001668 __ negl(out.AsRegisterPairLow<Register>());
1669 // Negation is similar to subtraction from zero. The least
1670 // significant byte triggers a borrow when it is different from
1671 // zero; to take it into account, add 1 to the most significant
1672 // byte if the carry flag (CF) is set to 1 after the first NEGL
1673 // operation.
1674 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1675 __ negl(out.AsRegisterPairHigh<Register>());
1676 break;
1677
Roland Levillain5368c212014-11-27 15:03:41 +00001678 case Primitive::kPrimFloat: {
1679 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 Register constant = locations->GetTemp(0).AsRegister<Register>();
1681 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001682 // Implement float negation with an exclusive or with value
1683 // 0x80000000 (mask for bit 31, representing the sign of a
1684 // single-precision floating-point number).
1685 __ movl(constant, Immediate(INT32_C(0x80000000)));
1686 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001688 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001689 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001690
Roland Levillain5368c212014-11-27 15:03:41 +00001691 case Primitive::kPrimDouble: {
1692 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001693 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001694 // Implement double negation with an exclusive or with value
1695 // 0x8000000000000000 (mask for bit 63, representing the sign of
1696 // a double-precision floating-point number).
1697 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001698 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001699 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001700 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001701
1702 default:
1703 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1704 }
1705}
1706
Roland Levillaindff1f282014-11-05 14:15:05 +00001707void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001708 Primitive::Type result_type = conversion->GetResultType();
1709 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001710 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001711
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001712 // The float-to-long and double-to-long type conversions rely on a
1713 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001714 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001715 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1716 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001717 ? LocationSummary::kCall
1718 : LocationSummary::kNoCall;
1719 LocationSummary* locations =
1720 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1721
David Brazdilb2bd1c52015-03-25 11:17:37 +00001722 // The Java language does not allow treating boolean as an integral type but
1723 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001724
Roland Levillaindff1f282014-11-05 14:15:05 +00001725 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001726 case Primitive::kPrimByte:
1727 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001728 case Primitive::kPrimBoolean:
1729 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001730 case Primitive::kPrimShort:
1731 case Primitive::kPrimInt:
1732 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001733 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001734 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1735 // Make the output overlap to please the register allocator. This greatly simplifies
1736 // the validation of the linear scan implementation
1737 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001738 break;
1739
1740 default:
1741 LOG(FATAL) << "Unexpected type conversion from " << input_type
1742 << " to " << result_type;
1743 }
1744 break;
1745
Roland Levillain01a8d712014-11-14 16:27:39 +00001746 case Primitive::kPrimShort:
1747 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001748 case Primitive::kPrimBoolean:
1749 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001750 case Primitive::kPrimByte:
1751 case Primitive::kPrimInt:
1752 case Primitive::kPrimChar:
1753 // Processing a Dex `int-to-short' instruction.
1754 locations->SetInAt(0, Location::Any());
1755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1756 break;
1757
1758 default:
1759 LOG(FATAL) << "Unexpected type conversion from " << input_type
1760 << " to " << result_type;
1761 }
1762 break;
1763
Roland Levillain946e1432014-11-11 17:35:19 +00001764 case Primitive::kPrimInt:
1765 switch (input_type) {
1766 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001767 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001768 locations->SetInAt(0, Location::Any());
1769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1770 break;
1771
1772 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001773 // Processing a Dex `float-to-int' instruction.
1774 locations->SetInAt(0, Location::RequiresFpuRegister());
1775 locations->SetOut(Location::RequiresRegister());
1776 locations->AddTemp(Location::RequiresFpuRegister());
1777 break;
1778
Roland Levillain946e1432014-11-11 17:35:19 +00001779 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001780 // Processing a Dex `double-to-int' instruction.
1781 locations->SetInAt(0, Location::RequiresFpuRegister());
1782 locations->SetOut(Location::RequiresRegister());
1783 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001784 break;
1785
1786 default:
1787 LOG(FATAL) << "Unexpected type conversion from " << input_type
1788 << " to " << result_type;
1789 }
1790 break;
1791
Roland Levillaindff1f282014-11-05 14:15:05 +00001792 case Primitive::kPrimLong:
1793 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001794 case Primitive::kPrimBoolean:
1795 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001796 case Primitive::kPrimByte:
1797 case Primitive::kPrimShort:
1798 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001799 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001800 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001801 locations->SetInAt(0, Location::RegisterLocation(EAX));
1802 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1803 break;
1804
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001805 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001806 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001807 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001808 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001809 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1810 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1811
Vladimir Marko949c91f2015-01-27 10:48:44 +00001812 // The runtime helper puts the result in EAX, EDX.
1813 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001814 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001815 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 }
1821 break;
1822
Roland Levillain981e4542014-11-14 11:47:14 +00001823 case Primitive::kPrimChar:
1824 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001825 case Primitive::kPrimBoolean:
1826 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001827 case Primitive::kPrimByte:
1828 case Primitive::kPrimShort:
1829 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001830 // Processing a Dex `int-to-char' instruction.
1831 locations->SetInAt(0, Location::Any());
1832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1833 break;
1834
1835 default:
1836 LOG(FATAL) << "Unexpected type conversion from " << input_type
1837 << " to " << result_type;
1838 }
1839 break;
1840
Roland Levillaindff1f282014-11-05 14:15:05 +00001841 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001842 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001843 case Primitive::kPrimBoolean:
1844 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001845 case Primitive::kPrimByte:
1846 case Primitive::kPrimShort:
1847 case Primitive::kPrimInt:
1848 case Primitive::kPrimChar:
1849 // Processing a Dex `int-to-float' instruction.
1850 locations->SetInAt(0, Location::RequiresRegister());
1851 locations->SetOut(Location::RequiresFpuRegister());
1852 break;
1853
1854 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001855 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001856 locations->SetInAt(0, Location::Any());
1857 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001858 break;
1859
Roland Levillaincff13742014-11-17 14:32:17 +00001860 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001861 // Processing a Dex `double-to-float' instruction.
1862 locations->SetInAt(0, Location::RequiresFpuRegister());
1863 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001864 break;
1865
1866 default:
1867 LOG(FATAL) << "Unexpected type conversion from " << input_type
1868 << " to " << result_type;
1869 };
1870 break;
1871
Roland Levillaindff1f282014-11-05 14:15:05 +00001872 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001873 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001874 case Primitive::kPrimBoolean:
1875 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001876 case Primitive::kPrimByte:
1877 case Primitive::kPrimShort:
1878 case Primitive::kPrimInt:
1879 case Primitive::kPrimChar:
1880 // Processing a Dex `int-to-double' instruction.
1881 locations->SetInAt(0, Location::RequiresRegister());
1882 locations->SetOut(Location::RequiresFpuRegister());
1883 break;
1884
1885 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001886 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001887 locations->SetInAt(0, Location::Any());
1888 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001889 break;
1890
Roland Levillaincff13742014-11-17 14:32:17 +00001891 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001892 // Processing a Dex `float-to-double' instruction.
1893 locations->SetInAt(0, Location::RequiresFpuRegister());
1894 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001895 break;
1896
1897 default:
1898 LOG(FATAL) << "Unexpected type conversion from " << input_type
1899 << " to " << result_type;
1900 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 break;
1902
1903 default:
1904 LOG(FATAL) << "Unexpected type conversion from " << input_type
1905 << " to " << result_type;
1906 }
1907}
1908
1909void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1910 LocationSummary* locations = conversion->GetLocations();
1911 Location out = locations->Out();
1912 Location in = locations->InAt(0);
1913 Primitive::Type result_type = conversion->GetResultType();
1914 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001915 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001916 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001917 case Primitive::kPrimByte:
1918 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001919 case Primitive::kPrimBoolean:
1920 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001921 case Primitive::kPrimShort:
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001924 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001925 if (in.IsRegister()) {
1926 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001927 } else {
1928 DCHECK(in.GetConstant()->IsIntConstant());
1929 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1930 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1931 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001932 break;
1933
1934 default:
1935 LOG(FATAL) << "Unexpected type conversion from " << input_type
1936 << " to " << result_type;
1937 }
1938 break;
1939
Roland Levillain01a8d712014-11-14 16:27:39 +00001940 case Primitive::kPrimShort:
1941 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001942 case Primitive::kPrimBoolean:
1943 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001944 case Primitive::kPrimByte:
1945 case Primitive::kPrimInt:
1946 case Primitive::kPrimChar:
1947 // Processing a Dex `int-to-short' instruction.
1948 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001949 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001950 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001951 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001952 } else {
1953 DCHECK(in.GetConstant()->IsIntConstant());
1954 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001956 }
1957 break;
1958
1959 default:
1960 LOG(FATAL) << "Unexpected type conversion from " << input_type
1961 << " to " << result_type;
1962 }
1963 break;
1964
Roland Levillain946e1432014-11-11 17:35:19 +00001965 case Primitive::kPrimInt:
1966 switch (input_type) {
1967 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001968 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001969 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001970 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001971 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001972 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001973 } else {
1974 DCHECK(in.IsConstant());
1975 DCHECK(in.GetConstant()->IsLongConstant());
1976 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001978 }
1979 break;
1980
Roland Levillain3f8f9362014-12-02 17:45:01 +00001981 case Primitive::kPrimFloat: {
1982 // Processing a Dex `float-to-int' instruction.
1983 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1984 Register output = out.AsRegister<Register>();
1985 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1986 Label done, nan;
1987
1988 __ movl(output, Immediate(kPrimIntMax));
1989 // temp = int-to-float(output)
1990 __ cvtsi2ss(temp, output);
1991 // if input >= temp goto done
1992 __ comiss(input, temp);
1993 __ j(kAboveEqual, &done);
1994 // if input == NaN goto nan
1995 __ j(kUnordered, &nan);
1996 // output = float-to-int-truncate(input)
1997 __ cvttss2si(output, input);
1998 __ jmp(&done);
1999 __ Bind(&nan);
2000 // output = 0
2001 __ xorl(output, output);
2002 __ Bind(&done);
2003 break;
2004 }
2005
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002006 case Primitive::kPrimDouble: {
2007 // Processing a Dex `double-to-int' instruction.
2008 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2009 Register output = out.AsRegister<Register>();
2010 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2011 Label done, nan;
2012
2013 __ movl(output, Immediate(kPrimIntMax));
2014 // temp = int-to-double(output)
2015 __ cvtsi2sd(temp, output);
2016 // if input >= temp goto done
2017 __ comisd(input, temp);
2018 __ j(kAboveEqual, &done);
2019 // if input == NaN goto nan
2020 __ j(kUnordered, &nan);
2021 // output = double-to-int-truncate(input)
2022 __ cvttsd2si(output, input);
2023 __ jmp(&done);
2024 __ Bind(&nan);
2025 // output = 0
2026 __ xorl(output, output);
2027 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002028 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002029 }
Roland Levillain946e1432014-11-11 17:35:19 +00002030
2031 default:
2032 LOG(FATAL) << "Unexpected type conversion from " << input_type
2033 << " to " << result_type;
2034 }
2035 break;
2036
Roland Levillaindff1f282014-11-05 14:15:05 +00002037 case Primitive::kPrimLong:
2038 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002039 case Primitive::kPrimBoolean:
2040 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 case Primitive::kPrimByte:
2042 case Primitive::kPrimShort:
2043 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002044 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002045 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002046 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2047 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002049 __ cdq();
2050 break;
2051
2052 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002053 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002054 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2055 conversion,
2056 conversion->GetDexPc(),
2057 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002058 break;
2059
Roland Levillaindff1f282014-11-05 14:15:05 +00002060 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002061 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002062 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2063 conversion,
2064 conversion->GetDexPc(),
2065 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002066 break;
2067
2068 default:
2069 LOG(FATAL) << "Unexpected type conversion from " << input_type
2070 << " to " << result_type;
2071 }
2072 break;
2073
Roland Levillain981e4542014-11-14 11:47:14 +00002074 case Primitive::kPrimChar:
2075 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002076 case Primitive::kPrimBoolean:
2077 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002078 case Primitive::kPrimByte:
2079 case Primitive::kPrimShort:
2080 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002081 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2082 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002084 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002086 } else {
2087 DCHECK(in.GetConstant()->IsIntConstant());
2088 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002090 }
2091 break;
2092
2093 default:
2094 LOG(FATAL) << "Unexpected type conversion from " << input_type
2095 << " to " << result_type;
2096 }
2097 break;
2098
Roland Levillaindff1f282014-11-05 14:15:05 +00002099 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002100 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002101 case Primitive::kPrimBoolean:
2102 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002103 case Primitive::kPrimByte:
2104 case Primitive::kPrimShort:
2105 case Primitive::kPrimInt:
2106 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002107 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002108 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002109 break;
2110
Roland Levillain6d0e4832014-11-27 18:31:21 +00002111 case Primitive::kPrimLong: {
2112 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002113 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002114
Roland Levillain232ade02015-04-20 15:14:36 +01002115 // Create stack space for the call to
2116 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2117 // TODO: enhance register allocator to ask for stack temporaries.
2118 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2119 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2120 __ subl(ESP, Immediate(adjustment));
2121 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002122
Roland Levillain232ade02015-04-20 15:14:36 +01002123 // Load the value to the FP stack, using temporaries if needed.
2124 PushOntoFPStack(in, 0, adjustment, false, true);
2125
2126 if (out.IsStackSlot()) {
2127 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2128 } else {
2129 __ fstps(Address(ESP, 0));
2130 Location stack_temp = Location::StackSlot(0);
2131 codegen_->Move32(out, stack_temp);
2132 }
2133
2134 // Remove the temporary stack space we allocated.
2135 if (adjustment != 0) {
2136 __ addl(ESP, Immediate(adjustment));
2137 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002138 break;
2139 }
2140
Roland Levillaincff13742014-11-17 14:32:17 +00002141 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002142 // Processing a Dex `double-to-float' instruction.
2143 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002144 break;
2145
2146 default:
2147 LOG(FATAL) << "Unexpected type conversion from " << input_type
2148 << " to " << result_type;
2149 };
2150 break;
2151
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002153 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002154 case Primitive::kPrimBoolean:
2155 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002156 case Primitive::kPrimByte:
2157 case Primitive::kPrimShort:
2158 case Primitive::kPrimInt:
2159 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002160 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002161 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002162 break;
2163
Roland Levillain647b9ed2014-11-27 12:06:00 +00002164 case Primitive::kPrimLong: {
2165 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002166 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002167
Roland Levillain232ade02015-04-20 15:14:36 +01002168 // Create stack space for the call to
2169 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2170 // TODO: enhance register allocator to ask for stack temporaries.
2171 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2172 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2173 __ subl(ESP, Immediate(adjustment));
2174 }
2175
2176 // Load the value to the FP stack, using temporaries if needed.
2177 PushOntoFPStack(in, 0, adjustment, false, true);
2178
2179 if (out.IsDoubleStackSlot()) {
2180 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2181 } else {
2182 __ fstpl(Address(ESP, 0));
2183 Location stack_temp = Location::DoubleStackSlot(0);
2184 codegen_->Move64(out, stack_temp);
2185 }
2186
2187 // Remove the temporary stack space we allocated.
2188 if (adjustment != 0) {
2189 __ addl(ESP, Immediate(adjustment));
2190 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002191 break;
2192 }
2193
Roland Levillaincff13742014-11-17 14:32:17 +00002194 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002195 // Processing a Dex `float-to-double' instruction.
2196 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002197 break;
2198
2199 default:
2200 LOG(FATAL) << "Unexpected type conversion from " << input_type
2201 << " to " << result_type;
2202 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002203 break;
2204
2205 default:
2206 LOG(FATAL) << "Unexpected type conversion from " << input_type
2207 << " to " << result_type;
2208 }
2209}
2210
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002211void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002212 LocationSummary* locations =
2213 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002214 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002215 case Primitive::kPrimInt: {
2216 locations->SetInAt(0, Location::RequiresRegister());
2217 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2218 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2219 break;
2220 }
2221
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002222 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002223 locations->SetInAt(0, Location::RequiresRegister());
2224 locations->SetInAt(1, Location::Any());
2225 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002226 break;
2227 }
2228
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002229 case Primitive::kPrimFloat:
2230 case Primitive::kPrimDouble: {
2231 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002232 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002235 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002236
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002237 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2239 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002240 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002241}
2242
2243void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2244 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 Location first = locations->InAt(0);
2246 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002247 Location out = locations->Out();
2248
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002249 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002250 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002251 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002252 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2253 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002254 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2255 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002256 } else {
2257 __ leal(out.AsRegister<Register>(), Address(
2258 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2259 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002260 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002261 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2262 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2263 __ addl(out.AsRegister<Register>(), Immediate(value));
2264 } else {
2265 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2266 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002267 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002268 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002269 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002270 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002271 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272 }
2273
2274 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002275 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002276 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2277 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002278 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002279 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2280 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002281 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002282 } else {
2283 DCHECK(second.IsConstant()) << second;
2284 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2285 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2286 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002287 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288 break;
2289 }
2290
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002291 case Primitive::kPrimFloat: {
2292 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002293 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002295 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002296 }
2297
2298 case Primitive::kPrimDouble: {
2299 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002300 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002301 }
2302 break;
2303 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002304
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002305 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002307 }
2308}
2309
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002310void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002311 LocationSummary* locations =
2312 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002313 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002314 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002315 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002316 locations->SetInAt(0, Location::RequiresRegister());
2317 locations->SetInAt(1, Location::Any());
2318 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 break;
2320 }
Calin Juravle11351682014-10-23 15:38:15 +01002321 case Primitive::kPrimFloat:
2322 case Primitive::kPrimDouble: {
2323 locations->SetInAt(0, Location::RequiresFpuRegister());
2324 locations->SetInAt(1, Location::RequiresFpuRegister());
2325 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 break;
Calin Juravle11351682014-10-23 15:38:15 +01002327 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002328
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002329 default:
Calin Juravle11351682014-10-23 15:38:15 +01002330 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002331 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002332}
2333
2334void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2335 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002336 Location first = locations->InAt(0);
2337 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002338 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002339 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002340 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002343 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002344 __ subl(first.AsRegister<Register>(),
2345 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002346 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002348 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002349 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002350 }
2351
2352 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002353 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002354 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2355 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002356 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002357 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002358 __ sbbl(first.AsRegisterPairHigh<Register>(),
2359 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002360 } else {
2361 DCHECK(second.IsConstant()) << second;
2362 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2363 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2364 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002365 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002366 break;
2367 }
2368
Calin Juravle11351682014-10-23 15:38:15 +01002369 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002370 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371 break;
Calin Juravle11351682014-10-23 15:38:15 +01002372 }
2373
2374 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002376 break;
2377 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002379 default:
Calin Juravle11351682014-10-23 15:38:15 +01002380 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002381 }
2382}
2383
Calin Juravle34bacdf2014-10-07 20:23:36 +01002384void LocationsBuilderX86::VisitMul(HMul* mul) {
2385 LocationSummary* locations =
2386 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2387 switch (mul->GetResultType()) {
2388 case Primitive::kPrimInt:
2389 locations->SetInAt(0, Location::RequiresRegister());
2390 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002391 if (mul->InputAt(1)->IsIntConstant()) {
2392 // Can use 3 operand multiply.
2393 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2394 } else {
2395 locations->SetOut(Location::SameAsFirstInput());
2396 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002397 break;
2398 case Primitive::kPrimLong: {
2399 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002400 locations->SetInAt(1, Location::Any());
2401 locations->SetOut(Location::SameAsFirstInput());
2402 // Needed for imul on 32bits with 64bits output.
2403 locations->AddTemp(Location::RegisterLocation(EAX));
2404 locations->AddTemp(Location::RegisterLocation(EDX));
2405 break;
2406 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002407 case Primitive::kPrimFloat:
2408 case Primitive::kPrimDouble: {
2409 locations->SetInAt(0, Location::RequiresFpuRegister());
2410 locations->SetInAt(1, Location::RequiresFpuRegister());
2411 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002412 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002413 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002414
2415 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002416 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002417 }
2418}
2419
2420void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2421 LocationSummary* locations = mul->GetLocations();
2422 Location first = locations->InAt(0);
2423 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002424 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002425
2426 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002427 case Primitive::kPrimInt:
2428 // The constant may have ended up in a register, so test explicitly to avoid
2429 // problems where the output may not be the same as the first operand.
2430 if (mul->InputAt(1)->IsIntConstant()) {
2431 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2432 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2433 } else if (second.IsRegister()) {
2434 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002436 } else {
2437 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002438 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002440 }
2441 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002442
2443 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002444 Register in1_hi = first.AsRegisterPairHigh<Register>();
2445 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 Register eax = locations->GetTemp(0).AsRegister<Register>();
2447 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002448
2449 DCHECK_EQ(EAX, eax);
2450 DCHECK_EQ(EDX, edx);
2451
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002452 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002453 // output: in1
2454 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2455 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2456 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002457 if (second.IsConstant()) {
2458 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002459
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002460 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2461 int32_t low_value = Low32Bits(value);
2462 int32_t high_value = High32Bits(value);
2463 Immediate low(low_value);
2464 Immediate high(high_value);
2465
2466 __ movl(eax, high);
2467 // eax <- in1.lo * in2.hi
2468 __ imull(eax, in1_lo);
2469 // in1.hi <- in1.hi * in2.lo
2470 __ imull(in1_hi, low);
2471 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2472 __ addl(in1_hi, eax);
2473 // move in2_lo to eax to prepare for double precision
2474 __ movl(eax, low);
2475 // edx:eax <- in1.lo * in2.lo
2476 __ mull(in1_lo);
2477 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2478 __ addl(in1_hi, edx);
2479 // in1.lo <- (in1.lo * in2.lo)[31:0];
2480 __ movl(in1_lo, eax);
2481 } else if (second.IsRegisterPair()) {
2482 Register in2_hi = second.AsRegisterPairHigh<Register>();
2483 Register in2_lo = second.AsRegisterPairLow<Register>();
2484
2485 __ movl(eax, in2_hi);
2486 // eax <- in1.lo * in2.hi
2487 __ imull(eax, in1_lo);
2488 // in1.hi <- in1.hi * in2.lo
2489 __ imull(in1_hi, in2_lo);
2490 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2491 __ addl(in1_hi, eax);
2492 // move in1_lo to eax to prepare for double precision
2493 __ movl(eax, in1_lo);
2494 // edx:eax <- in1.lo * in2.lo
2495 __ mull(in2_lo);
2496 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2497 __ addl(in1_hi, edx);
2498 // in1.lo <- (in1.lo * in2.lo)[31:0];
2499 __ movl(in1_lo, eax);
2500 } else {
2501 DCHECK(second.IsDoubleStackSlot()) << second;
2502 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2503 Address in2_lo(ESP, second.GetStackIndex());
2504
2505 __ movl(eax, in2_hi);
2506 // eax <- in1.lo * in2.hi
2507 __ imull(eax, in1_lo);
2508 // in1.hi <- in1.hi * in2.lo
2509 __ imull(in1_hi, in2_lo);
2510 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2511 __ addl(in1_hi, eax);
2512 // move in1_lo to eax to prepare for double precision
2513 __ movl(eax, in1_lo);
2514 // edx:eax <- in1.lo * in2.lo
2515 __ mull(in2_lo);
2516 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2517 __ addl(in1_hi, edx);
2518 // in1.lo <- (in1.lo * in2.lo)[31:0];
2519 __ movl(in1_lo, eax);
2520 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002521
2522 break;
2523 }
2524
Calin Juravleb5bfa962014-10-21 18:02:24 +01002525 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002526 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002527 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002528 }
2529
2530 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002531 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002532 break;
2533 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002534
2535 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002536 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002537 }
2538}
2539
Roland Levillain232ade02015-04-20 15:14:36 +01002540void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2541 uint32_t temp_offset,
2542 uint32_t stack_adjustment,
2543 bool is_fp,
2544 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002545 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002546 DCHECK(!is_wide);
2547 if (is_fp) {
2548 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2549 } else {
2550 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2551 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002552 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002553 DCHECK(is_wide);
2554 if (is_fp) {
2555 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2556 } else {
2557 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2558 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002559 } else {
2560 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002561 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002562 Location stack_temp = Location::StackSlot(temp_offset);
2563 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002564 if (is_fp) {
2565 __ flds(Address(ESP, temp_offset));
2566 } else {
2567 __ filds(Address(ESP, temp_offset));
2568 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002569 } else {
2570 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2571 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002572 if (is_fp) {
2573 __ fldl(Address(ESP, temp_offset));
2574 } else {
2575 __ fildl(Address(ESP, temp_offset));
2576 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002577 }
2578 }
2579}
2580
2581void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2582 Primitive::Type type = rem->GetResultType();
2583 bool is_float = type == Primitive::kPrimFloat;
2584 size_t elem_size = Primitive::ComponentSize(type);
2585 LocationSummary* locations = rem->GetLocations();
2586 Location first = locations->InAt(0);
2587 Location second = locations->InAt(1);
2588 Location out = locations->Out();
2589
2590 // Create stack space for 2 elements.
2591 // TODO: enhance register allocator to ask for stack temporaries.
2592 __ subl(ESP, Immediate(2 * elem_size));
2593
2594 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002595 const bool is_wide = !is_float;
2596 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2597 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002598
2599 // Loop doing FPREM until we stabilize.
2600 Label retry;
2601 __ Bind(&retry);
2602 __ fprem();
2603
2604 // Move FP status to AX.
2605 __ fstsw();
2606
2607 // And see if the argument reduction is complete. This is signaled by the
2608 // C2 FPU flag bit set to 0.
2609 __ andl(EAX, Immediate(kC2ConditionMask));
2610 __ j(kNotEqual, &retry);
2611
2612 // We have settled on the final value. Retrieve it into an XMM register.
2613 // Store FP top of stack to real stack.
2614 if (is_float) {
2615 __ fsts(Address(ESP, 0));
2616 } else {
2617 __ fstl(Address(ESP, 0));
2618 }
2619
2620 // Pop the 2 items from the FP stack.
2621 __ fucompp();
2622
2623 // Load the value from the stack into an XMM register.
2624 DCHECK(out.IsFpuRegister()) << out;
2625 if (is_float) {
2626 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2627 } else {
2628 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2629 }
2630
2631 // And remove the temporary stack space we allocated.
2632 __ addl(ESP, Immediate(2 * elem_size));
2633}
2634
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002635
2636void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2637 DCHECK(instruction->IsDiv() || instruction->IsRem());
2638
2639 LocationSummary* locations = instruction->GetLocations();
2640 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002641 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002642
2643 Register out_register = locations->Out().AsRegister<Register>();
2644 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002645 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002646
2647 DCHECK(imm == 1 || imm == -1);
2648
2649 if (instruction->IsRem()) {
2650 __ xorl(out_register, out_register);
2651 } else {
2652 __ movl(out_register, input_register);
2653 if (imm == -1) {
2654 __ negl(out_register);
2655 }
2656 }
2657}
2658
2659
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002660void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002661 LocationSummary* locations = instruction->GetLocations();
2662
2663 Register out_register = locations->Out().AsRegister<Register>();
2664 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002665 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002666
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002667 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002668 Register num = locations->GetTemp(0).AsRegister<Register>();
2669
2670 __ leal(num, Address(input_register, std::abs(imm) - 1));
2671 __ testl(input_register, input_register);
2672 __ cmovl(kGreaterEqual, num, input_register);
2673 int shift = CTZ(imm);
2674 __ sarl(num, Immediate(shift));
2675
2676 if (imm < 0) {
2677 __ negl(num);
2678 }
2679
2680 __ movl(out_register, num);
2681}
2682
2683void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2684 DCHECK(instruction->IsDiv() || instruction->IsRem());
2685
2686 LocationSummary* locations = instruction->GetLocations();
2687 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2688
2689 Register eax = locations->InAt(0).AsRegister<Register>();
2690 Register out = locations->Out().AsRegister<Register>();
2691 Register num;
2692 Register edx;
2693
2694 if (instruction->IsDiv()) {
2695 edx = locations->GetTemp(0).AsRegister<Register>();
2696 num = locations->GetTemp(1).AsRegister<Register>();
2697 } else {
2698 edx = locations->Out().AsRegister<Register>();
2699 num = locations->GetTemp(0).AsRegister<Register>();
2700 }
2701
2702 DCHECK_EQ(EAX, eax);
2703 DCHECK_EQ(EDX, edx);
2704 if (instruction->IsDiv()) {
2705 DCHECK_EQ(EAX, out);
2706 } else {
2707 DCHECK_EQ(EDX, out);
2708 }
2709
2710 int64_t magic;
2711 int shift;
2712 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2713
2714 Label ndiv;
2715 Label end;
2716 // If numerator is 0, the result is 0, no computation needed.
2717 __ testl(eax, eax);
2718 __ j(kNotEqual, &ndiv);
2719
2720 __ xorl(out, out);
2721 __ jmp(&end);
2722
2723 __ Bind(&ndiv);
2724
2725 // Save the numerator.
2726 __ movl(num, eax);
2727
2728 // EAX = magic
2729 __ movl(eax, Immediate(magic));
2730
2731 // EDX:EAX = magic * numerator
2732 __ imull(num);
2733
2734 if (imm > 0 && magic < 0) {
2735 // EDX += num
2736 __ addl(edx, num);
2737 } else if (imm < 0 && magic > 0) {
2738 __ subl(edx, num);
2739 }
2740
2741 // Shift if needed.
2742 if (shift != 0) {
2743 __ sarl(edx, Immediate(shift));
2744 }
2745
2746 // EDX += 1 if EDX < 0
2747 __ movl(eax, edx);
2748 __ shrl(edx, Immediate(31));
2749 __ addl(edx, eax);
2750
2751 if (instruction->IsRem()) {
2752 __ movl(eax, num);
2753 __ imull(edx, Immediate(imm));
2754 __ subl(eax, edx);
2755 __ movl(edx, eax);
2756 } else {
2757 __ movl(eax, edx);
2758 }
2759 __ Bind(&end);
2760}
2761
Calin Juravlebacfec32014-11-14 15:54:36 +00002762void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2763 DCHECK(instruction->IsDiv() || instruction->IsRem());
2764
2765 LocationSummary* locations = instruction->GetLocations();
2766 Location out = locations->Out();
2767 Location first = locations->InAt(0);
2768 Location second = locations->InAt(1);
2769 bool is_div = instruction->IsDiv();
2770
2771 switch (instruction->GetResultType()) {
2772 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 DCHECK_EQ(EAX, first.AsRegister<Register>());
2774 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002775
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002776 if (instruction->InputAt(1)->IsIntConstant()) {
2777 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002778
2779 if (imm == 0) {
2780 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2781 } else if (imm == 1 || imm == -1) {
2782 DivRemOneOrMinusOne(instruction);
2783 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002784 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002785 } else {
2786 DCHECK(imm <= -2 || imm >= 2);
2787 GenerateDivRemWithAnyConstant(instruction);
2788 }
2789 } else {
2790 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002791 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002792 is_div);
2793 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002794
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002795 Register second_reg = second.AsRegister<Register>();
2796 // 0x80000000/-1 triggers an arithmetic exception!
2797 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2798 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002799
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002800 __ cmpl(second_reg, Immediate(-1));
2801 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002802
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002803 // edx:eax <- sign-extended of eax
2804 __ cdq();
2805 // eax = quotient, edx = remainder
2806 __ idivl(second_reg);
2807 __ Bind(slow_path->GetExitLabel());
2808 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002809 break;
2810 }
2811
2812 case Primitive::kPrimLong: {
2813 InvokeRuntimeCallingConvention calling_convention;
2814 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2815 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2816 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2817 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2818 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2819 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2820
2821 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002822 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2823 instruction,
2824 instruction->GetDexPc(),
2825 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002826 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002827 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2828 instruction,
2829 instruction->GetDexPc(),
2830 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002831 }
2832 uint32_t dex_pc = is_div
2833 ? instruction->AsDiv()->GetDexPc()
2834 : instruction->AsRem()->GetDexPc();
2835 codegen_->RecordPcInfo(instruction, dex_pc);
2836
2837 break;
2838 }
2839
2840 default:
2841 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2842 }
2843}
2844
Calin Juravle7c4954d2014-10-28 16:57:40 +00002845void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002846 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002847 ? LocationSummary::kCall
2848 : LocationSummary::kNoCall;
2849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2850
Calin Juravle7c4954d2014-10-28 16:57:40 +00002851 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002852 case Primitive::kPrimInt: {
2853 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002854 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002855 locations->SetOut(Location::SameAsFirstInput());
2856 // Intel uses edx:eax as the dividend.
2857 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002858 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2859 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2860 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002861 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002862 locations->AddTemp(Location::RequiresRegister());
2863 }
Calin Juravled0d48522014-11-04 16:40:20 +00002864 break;
2865 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002867 InvokeRuntimeCallingConvention calling_convention;
2868 locations->SetInAt(0, Location::RegisterPairLocation(
2869 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2870 locations->SetInAt(1, Location::RegisterPairLocation(
2871 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2872 // Runtime helper puts the result in EAX, EDX.
2873 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002874 break;
2875 }
2876 case Primitive::kPrimFloat:
2877 case Primitive::kPrimDouble: {
2878 locations->SetInAt(0, Location::RequiresFpuRegister());
2879 locations->SetInAt(1, Location::RequiresFpuRegister());
2880 locations->SetOut(Location::SameAsFirstInput());
2881 break;
2882 }
2883
2884 default:
2885 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2886 }
2887}
2888
2889void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2890 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002891 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002892 Location first = locations->InAt(0);
2893 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002894
2895 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002896 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002897 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002898 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002899 break;
2900 }
2901
2902 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002903 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002904 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002905 break;
2906 }
2907
2908 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002909 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002910 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002911 break;
2912 }
2913
2914 default:
2915 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2916 }
2917}
2918
Calin Juravlebacfec32014-11-14 15:54:36 +00002919void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002920 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002921
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002922 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2923 ? LocationSummary::kCall
2924 : LocationSummary::kNoCall;
2925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002926
Calin Juravled2ec87d2014-12-08 14:24:46 +00002927 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002928 case Primitive::kPrimInt: {
2929 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002930 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002931 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002932 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2933 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2934 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002935 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002936 locations->AddTemp(Location::RequiresRegister());
2937 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002938 break;
2939 }
2940 case Primitive::kPrimLong: {
2941 InvokeRuntimeCallingConvention calling_convention;
2942 locations->SetInAt(0, Location::RegisterPairLocation(
2943 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2944 locations->SetInAt(1, Location::RegisterPairLocation(
2945 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2946 // Runtime helper puts the result in EAX, EDX.
2947 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2948 break;
2949 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002950 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002951 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002952 locations->SetInAt(0, Location::Any());
2953 locations->SetInAt(1, Location::Any());
2954 locations->SetOut(Location::RequiresFpuRegister());
2955 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002956 break;
2957 }
2958
2959 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002960 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002961 }
2962}
2963
2964void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2965 Primitive::Type type = rem->GetResultType();
2966 switch (type) {
2967 case Primitive::kPrimInt:
2968 case Primitive::kPrimLong: {
2969 GenerateDivRemIntegral(rem);
2970 break;
2971 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002972 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002973 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002974 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002975 break;
2976 }
2977 default:
2978 LOG(FATAL) << "Unexpected rem type " << type;
2979 }
2980}
2981
Calin Juravled0d48522014-11-04 16:40:20 +00002982void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002985 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002986 case Primitive::kPrimByte:
2987 case Primitive::kPrimChar:
2988 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002989 case Primitive::kPrimInt: {
2990 locations->SetInAt(0, Location::Any());
2991 break;
2992 }
2993 case Primitive::kPrimLong: {
2994 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2995 if (!instruction->IsConstant()) {
2996 locations->AddTemp(Location::RequiresRegister());
2997 }
2998 break;
2999 }
3000 default:
3001 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3002 }
Calin Juravled0d48522014-11-04 16:40:20 +00003003 if (instruction->HasUses()) {
3004 locations->SetOut(Location::SameAsFirstInput());
3005 }
3006}
3007
3008void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3009 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3010 codegen_->AddSlowPath(slow_path);
3011
3012 LocationSummary* locations = instruction->GetLocations();
3013 Location value = locations->InAt(0);
3014
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003015 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003016 case Primitive::kPrimByte:
3017 case Primitive::kPrimChar:
3018 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003019 case Primitive::kPrimInt: {
3020 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003022 __ j(kEqual, slow_path->GetEntryLabel());
3023 } else if (value.IsStackSlot()) {
3024 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3025 __ j(kEqual, slow_path->GetEntryLabel());
3026 } else {
3027 DCHECK(value.IsConstant()) << value;
3028 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3029 __ jmp(slow_path->GetEntryLabel());
3030 }
3031 }
3032 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003033 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003034 case Primitive::kPrimLong: {
3035 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003037 __ movl(temp, value.AsRegisterPairLow<Register>());
3038 __ orl(temp, value.AsRegisterPairHigh<Register>());
3039 __ j(kEqual, slow_path->GetEntryLabel());
3040 } else {
3041 DCHECK(value.IsConstant()) << value;
3042 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3043 __ jmp(slow_path->GetEntryLabel());
3044 }
3045 }
3046 break;
3047 }
3048 default:
3049 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003050 }
Calin Juravled0d48522014-11-04 16:40:20 +00003051}
3052
Calin Juravle9aec02f2014-11-18 23:06:35 +00003053void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3054 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3055
3056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3058
3059 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003060 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003061 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003062 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003063 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003064 // The shift count needs to be in CL or a constant.
3065 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003066 locations->SetOut(Location::SameAsFirstInput());
3067 break;
3068 }
3069 default:
3070 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3071 }
3072}
3073
3074void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3075 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3076
3077 LocationSummary* locations = op->GetLocations();
3078 Location first = locations->InAt(0);
3079 Location second = locations->InAt(1);
3080 DCHECK(first.Equals(locations->Out()));
3081
3082 switch (op->GetResultType()) {
3083 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003084 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003085 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003086 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003088 DCHECK_EQ(ECX, second_reg);
3089 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003090 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003091 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003092 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003093 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003094 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003095 }
3096 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003097 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3098 if (shift == 0) {
3099 return;
3100 }
3101 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003102 if (op->IsShl()) {
3103 __ shll(first_reg, imm);
3104 } else if (op->IsShr()) {
3105 __ sarl(first_reg, imm);
3106 } else {
3107 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003108 }
3109 }
3110 break;
3111 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003112 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003113 if (second.IsRegister()) {
3114 Register second_reg = second.AsRegister<Register>();
3115 DCHECK_EQ(ECX, second_reg);
3116 if (op->IsShl()) {
3117 GenerateShlLong(first, second_reg);
3118 } else if (op->IsShr()) {
3119 GenerateShrLong(first, second_reg);
3120 } else {
3121 GenerateUShrLong(first, second_reg);
3122 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003123 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003124 // Shift by a constant.
3125 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3126 // Nothing to do if the shift is 0, as the input is already the output.
3127 if (shift != 0) {
3128 if (op->IsShl()) {
3129 GenerateShlLong(first, shift);
3130 } else if (op->IsShr()) {
3131 GenerateShrLong(first, shift);
3132 } else {
3133 GenerateUShrLong(first, shift);
3134 }
3135 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003136 }
3137 break;
3138 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003139 default:
3140 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3141 }
3142}
3143
Mark P Mendell73945692015-04-29 14:56:17 +00003144void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3145 Register low = loc.AsRegisterPairLow<Register>();
3146 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003147 if (shift == 1) {
3148 // This is just an addition.
3149 __ addl(low, low);
3150 __ adcl(high, high);
3151 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003152 // Shift by 32 is easy. High gets low, and low gets 0.
3153 codegen_->EmitParallelMoves(
3154 loc.ToLow(),
3155 loc.ToHigh(),
3156 Primitive::kPrimInt,
3157 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3158 loc.ToLow(),
3159 Primitive::kPrimInt);
3160 } else if (shift > 32) {
3161 // Low part becomes 0. High part is low part << (shift-32).
3162 __ movl(high, low);
3163 __ shll(high, Immediate(shift - 32));
3164 __ xorl(low, low);
3165 } else {
3166 // Between 1 and 31.
3167 __ shld(high, low, Immediate(shift));
3168 __ shll(low, Immediate(shift));
3169 }
3170}
3171
Calin Juravle9aec02f2014-11-18 23:06:35 +00003172void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3173 Label done;
3174 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3175 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3176 __ testl(shifter, Immediate(32));
3177 __ j(kEqual, &done);
3178 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3179 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3180 __ Bind(&done);
3181}
3182
Mark P Mendell73945692015-04-29 14:56:17 +00003183void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3184 Register low = loc.AsRegisterPairLow<Register>();
3185 Register high = loc.AsRegisterPairHigh<Register>();
3186 if (shift == 32) {
3187 // Need to copy the sign.
3188 DCHECK_NE(low, high);
3189 __ movl(low, high);
3190 __ sarl(high, Immediate(31));
3191 } else if (shift > 32) {
3192 DCHECK_NE(low, high);
3193 // High part becomes sign. Low part is shifted by shift - 32.
3194 __ movl(low, high);
3195 __ sarl(high, Immediate(31));
3196 __ sarl(low, Immediate(shift - 32));
3197 } else {
3198 // Between 1 and 31.
3199 __ shrd(low, high, Immediate(shift));
3200 __ sarl(high, Immediate(shift));
3201 }
3202}
3203
Calin Juravle9aec02f2014-11-18 23:06:35 +00003204void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3205 Label done;
3206 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3207 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3208 __ testl(shifter, Immediate(32));
3209 __ j(kEqual, &done);
3210 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3211 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3212 __ Bind(&done);
3213}
3214
Mark P Mendell73945692015-04-29 14:56:17 +00003215void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3216 Register low = loc.AsRegisterPairLow<Register>();
3217 Register high = loc.AsRegisterPairHigh<Register>();
3218 if (shift == 32) {
3219 // Shift by 32 is easy. Low gets high, and high gets 0.
3220 codegen_->EmitParallelMoves(
3221 loc.ToHigh(),
3222 loc.ToLow(),
3223 Primitive::kPrimInt,
3224 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3225 loc.ToHigh(),
3226 Primitive::kPrimInt);
3227 } else if (shift > 32) {
3228 // Low part is high >> (shift - 32). High part becomes 0.
3229 __ movl(low, high);
3230 __ shrl(low, Immediate(shift - 32));
3231 __ xorl(high, high);
3232 } else {
3233 // Between 1 and 31.
3234 __ shrd(low, high, Immediate(shift));
3235 __ shrl(high, Immediate(shift));
3236 }
3237}
3238
Calin Juravle9aec02f2014-11-18 23:06:35 +00003239void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3240 Label done;
3241 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3242 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3243 __ testl(shifter, Immediate(32));
3244 __ j(kEqual, &done);
3245 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3246 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3247 __ Bind(&done);
3248}
3249
3250void LocationsBuilderX86::VisitShl(HShl* shl) {
3251 HandleShift(shl);
3252}
3253
3254void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3255 HandleShift(shl);
3256}
3257
3258void LocationsBuilderX86::VisitShr(HShr* shr) {
3259 HandleShift(shr);
3260}
3261
3262void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3263 HandleShift(shr);
3264}
3265
3266void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3267 HandleShift(ushr);
3268}
3269
3270void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3271 HandleShift(ushr);
3272}
3273
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003274void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003275 LocationSummary* locations =
3276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003277 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003278 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003279 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003280 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003281}
3282
3283void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003285 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003286 // Note: if heap poisoning is enabled, the entry point takes cares
3287 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003288 codegen_->InvokeRuntime(
3289 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3290 instruction,
3291 instruction->GetDexPc(),
3292 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003293 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003294}
3295
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003296void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3299 locations->SetOut(Location::RegisterLocation(EAX));
3300 InvokeRuntimeCallingConvention calling_convention;
3301 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003302 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003303 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003304}
3305
3306void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3307 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003308 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3309
Roland Levillain4d027112015-07-01 15:41:14 +01003310 // Note: if heap poisoning is enabled, the entry point takes cares
3311 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003312 codegen_->InvokeRuntime(
3313 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3314 instruction,
3315 instruction->GetDexPc(),
3316 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003317 DCHECK(!codegen_->IsLeafMethod());
3318}
3319
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003320void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003321 LocationSummary* locations =
3322 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003323 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3324 if (location.IsStackSlot()) {
3325 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3326 } else if (location.IsDoubleStackSlot()) {
3327 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003328 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003329 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003330}
3331
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003332void InstructionCodeGeneratorX86::VisitParameterValue(
3333 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3334}
3335
3336void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3337 LocationSummary* locations =
3338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3339 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3340}
3341
3342void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003343}
3344
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003345void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003346 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003347 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003348 locations->SetInAt(0, Location::RequiresRegister());
3349 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003350}
3351
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003352void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3353 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003354 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003355 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003356 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003357 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003358 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003360 break;
3361
3362 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003363 __ notl(out.AsRegisterPairLow<Register>());
3364 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003365 break;
3366
3367 default:
3368 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3369 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003370}
3371
David Brazdil66d126e2015-04-03 16:02:44 +01003372void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3373 LocationSummary* locations =
3374 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3375 locations->SetInAt(0, Location::RequiresRegister());
3376 locations->SetOut(Location::SameAsFirstInput());
3377}
3378
3379void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003380 LocationSummary* locations = bool_not->GetLocations();
3381 Location in = locations->InAt(0);
3382 Location out = locations->Out();
3383 DCHECK(in.Equals(out));
3384 __ xorl(out.AsRegister<Register>(), Immediate(1));
3385}
3386
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003387void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003390 switch (compare->InputAt(0)->GetType()) {
3391 case Primitive::kPrimLong: {
3392 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003393 locations->SetInAt(1, Location::Any());
3394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 break;
3396 }
3397 case Primitive::kPrimFloat:
3398 case Primitive::kPrimDouble: {
3399 locations->SetInAt(0, Location::RequiresFpuRegister());
3400 locations->SetInAt(1, Location::RequiresFpuRegister());
3401 locations->SetOut(Location::RequiresRegister());
3402 break;
3403 }
3404 default:
3405 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3406 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003407}
3408
3409void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003410 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003411 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003412 Location left = locations->InAt(0);
3413 Location right = locations->InAt(1);
3414
3415 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003416 switch (compare->InputAt(0)->GetType()) {
3417 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003418 Register left_low = left.AsRegisterPairLow<Register>();
3419 Register left_high = left.AsRegisterPairHigh<Register>();
3420 int32_t val_low = 0;
3421 int32_t val_high = 0;
3422 bool right_is_const = false;
3423
3424 if (right.IsConstant()) {
3425 DCHECK(right.GetConstant()->IsLongConstant());
3426 right_is_const = true;
3427 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3428 val_low = Low32Bits(val);
3429 val_high = High32Bits(val);
3430 }
3431
Calin Juravleddb7df22014-11-25 20:56:51 +00003432 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003433 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003434 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003435 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003436 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003437 DCHECK(right_is_const) << right;
3438 if (val_high == 0) {
3439 __ testl(left_high, left_high);
3440 } else {
3441 __ cmpl(left_high, Immediate(val_high));
3442 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003443 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003444 __ j(kLess, &less); // Signed compare.
3445 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003446 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003447 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003448 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003449 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003450 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003451 DCHECK(right_is_const) << right;
3452 if (val_low == 0) {
3453 __ testl(left_low, left_low);
3454 } else {
3455 __ cmpl(left_low, Immediate(val_low));
3456 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003457 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003458 break;
3459 }
3460 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003461 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003462 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3463 break;
3464 }
3465 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003467 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003468 break;
3469 }
3470 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003471 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003472 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003473 __ movl(out, Immediate(0));
3474 __ j(kEqual, &done);
3475 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3476
3477 __ Bind(&greater);
3478 __ movl(out, Immediate(1));
3479 __ jmp(&done);
3480
3481 __ Bind(&less);
3482 __ movl(out, Immediate(-1));
3483
3484 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003485}
3486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003487void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003488 LocationSummary* locations =
3489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003490 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3491 locations->SetInAt(i, Location::Any());
3492 }
3493 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003494}
3495
3496void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003497 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003498 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003499}
3500
Calin Juravle52c48962014-12-16 17:02:57 +00003501void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3502 /*
3503 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3504 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3505 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3506 */
3507 switch (kind) {
3508 case MemBarrierKind::kAnyAny: {
3509 __ mfence();
3510 break;
3511 }
3512 case MemBarrierKind::kAnyStore:
3513 case MemBarrierKind::kLoadAny:
3514 case MemBarrierKind::kStoreStore: {
3515 // nop
3516 break;
3517 }
3518 default:
3519 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003521}
3522
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003523
Vladimir Markob2c431e2015-08-19 12:45:42 +00003524void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3525 Location temp) {
3526 // TODO: Implement all kinds of calls:
3527 // 1) boot -> boot
3528 // 2) app -> boot
3529 // 3) app -> app
3530 //
3531 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Vladimir Marko9b688a02015-05-06 14:12:42 +01003532
Vladimir Markob2c431e2015-08-19 12:45:42 +00003533 if (invoke->IsStringInit()) {
3534 // temp = thread->string_init_entrypoint
3535 Register reg = temp.AsRegister<Register>();
3536 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
3537 // (temp + offset_of_quick_compiled_code)()
3538 __ call(Address(
3539 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3540 } else if (invoke->IsRecursive()) {
3541 __ call(GetFrameEntryLabel());
3542 } else {
3543 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3544
3545 Register method_reg;
3546 Register reg = temp.AsRegister<Register>();
3547 if (current_method.IsRegister()) {
3548 method_reg = current_method.AsRegister<Register>();
3549 } else {
3550 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3551 DCHECK(!current_method.IsValid());
3552 method_reg = reg;
3553 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Vladimir Marko9b688a02015-05-06 14:12:42 +01003554 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003555 // temp = temp->dex_cache_resolved_methods_;
3556 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3557 // temp = temp[index_in_cache]
3558 __ movl(reg, Address(reg,
3559 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3560 // (temp + offset_of_quick_compiled_code)()
3561 __ call(Address(reg,
3562 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003563 }
3564
3565 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003566}
3567
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003568void CodeGeneratorX86::MarkGCCard(Register temp,
3569 Register card,
3570 Register object,
3571 Register value,
3572 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003574 if (value_can_be_null) {
3575 __ testl(value, value);
3576 __ j(kEqual, &is_null);
3577 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3579 __ movl(temp, object);
3580 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003581 __ movb(Address(temp, card, TIMES_1, 0),
3582 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003583 if (value_can_be_null) {
3584 __ Bind(&is_null);
3585 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586}
3587
Calin Juravle52c48962014-12-16 17:02:57 +00003588void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3589 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003590 LocationSummary* locations =
3591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003592 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003593
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003594 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3595 locations->SetOut(Location::RequiresFpuRegister());
3596 } else {
3597 // The output overlaps in case of long: we don't want the low move to overwrite
3598 // the object's location.
3599 locations->SetOut(Location::RequiresRegister(),
3600 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3601 : Location::kNoOutputOverlap);
3602 }
Calin Juravle52c48962014-12-16 17:02:57 +00003603
3604 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3605 // Long values can be loaded atomically into an XMM using movsd.
3606 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3607 // and then copy the XMM into the output 32bits at a time).
3608 locations->AddTemp(Location::RequiresFpuRegister());
3609 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003610}
3611
Calin Juravle52c48962014-12-16 17:02:57 +00003612void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3613 const FieldInfo& field_info) {
3614 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003615
Calin Juravle52c48962014-12-16 17:02:57 +00003616 LocationSummary* locations = instruction->GetLocations();
3617 Register base = locations->InAt(0).AsRegister<Register>();
3618 Location out = locations->Out();
3619 bool is_volatile = field_info.IsVolatile();
3620 Primitive::Type field_type = field_info.GetFieldType();
3621 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3622
3623 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003624 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003625 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003626 break;
3627 }
3628
3629 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003630 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003631 break;
3632 }
3633
3634 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003635 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003636 break;
3637 }
3638
3639 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003640 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641 break;
3642 }
3643
3644 case Primitive::kPrimInt:
3645 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003646 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003647 break;
3648 }
3649
3650 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003651 if (is_volatile) {
3652 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3653 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003654 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003655 __ movd(out.AsRegisterPairLow<Register>(), temp);
3656 __ psrlq(temp, Immediate(32));
3657 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3658 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003659 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003660 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003661 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003662 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3663 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003664 break;
3665 }
3666
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003667 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003668 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003669 break;
3670 }
3671
3672 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003673 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003674 break;
3675 }
3676
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003678 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003679 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003680 }
Calin Juravle52c48962014-12-16 17:02:57 +00003681
Calin Juravle77520bc2015-01-12 18:45:46 +00003682 // Longs are handled in the switch.
3683 if (field_type != Primitive::kPrimLong) {
3684 codegen_->MaybeRecordImplicitNullCheck(instruction);
3685 }
3686
Calin Juravle52c48962014-12-16 17:02:57 +00003687 if (is_volatile) {
3688 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3689 }
Roland Levillain4d027112015-07-01 15:41:14 +01003690
3691 if (field_type == Primitive::kPrimNot) {
3692 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3693 }
Calin Juravle52c48962014-12-16 17:02:57 +00003694}
3695
3696void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3697 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3698
3699 LocationSummary* locations =
3700 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3701 locations->SetInAt(0, Location::RequiresRegister());
3702 bool is_volatile = field_info.IsVolatile();
3703 Primitive::Type field_type = field_info.GetFieldType();
3704 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3705 || (field_type == Primitive::kPrimByte);
3706
3707 // The register allocator does not support multiple
3708 // inputs that die at entry with one in a specific register.
3709 if (is_byte_type) {
3710 // Ensure the value is in a byte register.
3711 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003712 } else if (Primitive::IsFloatingPointType(field_type)) {
3713 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003714 } else {
3715 locations->SetInAt(1, Location::RequiresRegister());
3716 }
Calin Juravle52c48962014-12-16 17:02:57 +00003717 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003718 // Temporary registers for the write barrier.
3719 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003720 // Ensure the card is in a byte register.
3721 locations->AddTemp(Location::RegisterLocation(ECX));
3722 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3723 // 64bits value can be atomically written to an address with movsd and an XMM register.
3724 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3725 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3726 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3727 // isolated cases when we need this it isn't worth adding the extra complexity.
3728 locations->AddTemp(Location::RequiresFpuRegister());
3729 locations->AddTemp(Location::RequiresFpuRegister());
3730 }
3731}
3732
3733void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003734 const FieldInfo& field_info,
3735 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003736 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3737
3738 LocationSummary* locations = instruction->GetLocations();
3739 Register base = locations->InAt(0).AsRegister<Register>();
3740 Location value = locations->InAt(1);
3741 bool is_volatile = field_info.IsVolatile();
3742 Primitive::Type field_type = field_info.GetFieldType();
3743 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003744 bool needs_write_barrier =
3745 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003746
3747 if (is_volatile) {
3748 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3749 }
3750
3751 switch (field_type) {
3752 case Primitive::kPrimBoolean:
3753 case Primitive::kPrimByte: {
3754 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3755 break;
3756 }
3757
3758 case Primitive::kPrimShort:
3759 case Primitive::kPrimChar: {
3760 __ movw(Address(base, offset), value.AsRegister<Register>());
3761 break;
3762 }
3763
3764 case Primitive::kPrimInt:
3765 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003766 if (kPoisonHeapReferences && needs_write_barrier) {
3767 // Note that in the case where `value` is a null reference,
3768 // we do not enter this block, as the reference does not
3769 // need poisoning.
3770 DCHECK_EQ(field_type, Primitive::kPrimNot);
3771 Register temp = locations->GetTemp(0).AsRegister<Register>();
3772 __ movl(temp, value.AsRegister<Register>());
3773 __ PoisonHeapReference(temp);
3774 __ movl(Address(base, offset), temp);
3775 } else {
3776 __ movl(Address(base, offset), value.AsRegister<Register>());
3777 }
Calin Juravle52c48962014-12-16 17:02:57 +00003778 break;
3779 }
3780
3781 case Primitive::kPrimLong: {
3782 if (is_volatile) {
3783 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3784 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3785 __ movd(temp1, value.AsRegisterPairLow<Register>());
3786 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3787 __ punpckldq(temp1, temp2);
3788 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003790 } else {
3791 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003793 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3794 }
3795 break;
3796 }
3797
3798 case Primitive::kPrimFloat: {
3799 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3800 break;
3801 }
3802
3803 case Primitive::kPrimDouble: {
3804 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3805 break;
3806 }
3807
3808 case Primitive::kPrimVoid:
3809 LOG(FATAL) << "Unreachable type " << field_type;
3810 UNREACHABLE();
3811 }
3812
Calin Juravle77520bc2015-01-12 18:45:46 +00003813 // Longs are handled in the switch.
3814 if (field_type != Primitive::kPrimLong) {
3815 codegen_->MaybeRecordImplicitNullCheck(instruction);
3816 }
3817
Roland Levillain4d027112015-07-01 15:41:14 +01003818 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003819 Register temp = locations->GetTemp(0).AsRegister<Register>();
3820 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003821 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003822 }
3823
Calin Juravle52c48962014-12-16 17:02:57 +00003824 if (is_volatile) {
3825 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3826 }
3827}
3828
3829void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3830 HandleFieldGet(instruction, instruction->GetFieldInfo());
3831}
3832
3833void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3834 HandleFieldGet(instruction, instruction->GetFieldInfo());
3835}
3836
3837void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3838 HandleFieldSet(instruction, instruction->GetFieldInfo());
3839}
3840
3841void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003842 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003843}
3844
3845void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3846 HandleFieldSet(instruction, instruction->GetFieldInfo());
3847}
3848
3849void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003851}
3852
3853void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3854 HandleFieldGet(instruction, instruction->GetFieldInfo());
3855}
3856
3857void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3858 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003859}
3860
3861void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003862 LocationSummary* locations =
3863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003864 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3865 ? Location::RequiresRegister()
3866 : Location::Any();
3867 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003868 if (instruction->HasUses()) {
3869 locations->SetOut(Location::SameAsFirstInput());
3870 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003871}
3872
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003873void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003874 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3875 return;
3876 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003877 LocationSummary* locations = instruction->GetLocations();
3878 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003879
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003880 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3881 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3882}
3883
3884void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003885 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003886 codegen_->AddSlowPath(slow_path);
3887
3888 LocationSummary* locations = instruction->GetLocations();
3889 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003890
3891 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003892 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003893 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003894 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003895 } else {
3896 DCHECK(obj.IsConstant()) << obj;
3897 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3898 __ jmp(slow_path->GetEntryLabel());
3899 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003900 }
3901 __ j(kEqual, slow_path->GetEntryLabel());
3902}
3903
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003904void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3905 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3906 GenerateImplicitNullCheck(instruction);
3907 } else {
3908 GenerateExplicitNullCheck(instruction);
3909 }
3910}
3911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003912void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003915 locations->SetInAt(0, Location::RequiresRegister());
3916 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003917 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3918 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3919 } else {
3920 // The output overlaps in case of long: we don't want the low move to overwrite
3921 // the array's location.
3922 locations->SetOut(Location::RequiresRegister(),
3923 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3924 : Location::kNoOutputOverlap);
3925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003926}
3927
3928void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3929 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 Location index = locations->InAt(1);
3932
Calin Juravle77520bc2015-01-12 18:45:46 +00003933 Primitive::Type type = instruction->GetType();
3934 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003935 case Primitive::kPrimBoolean: {
3936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003937 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 if (index.IsConstant()) {
3939 __ movzxb(out, Address(obj,
3940 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3941 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003942 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 }
3944 break;
3945 }
3946
3947 case Primitive::kPrimByte: {
3948 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003950 if (index.IsConstant()) {
3951 __ movsxb(out, Address(obj,
3952 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3953 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955 }
3956 break;
3957 }
3958
3959 case Primitive::kPrimShort: {
3960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003961 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962 if (index.IsConstant()) {
3963 __ movsxw(out, Address(obj,
3964 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3965 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003966 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967 }
3968 break;
3969 }
3970
3971 case Primitive::kPrimChar: {
3972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
3976 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 }
3980 break;
3981 }
3982
3983 case Primitive::kPrimInt:
3984 case Primitive::kPrimNot: {
3985 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003986 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987 if (index.IsConstant()) {
3988 __ movl(out, Address(obj,
3989 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3990 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003992 }
3993 break;
3994 }
3995
3996 case Primitive::kPrimLong: {
3997 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003998 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003999 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004000 if (index.IsConstant()) {
4001 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004002 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004003 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004004 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004005 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004006 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004007 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004008 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004009 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011 }
4012 break;
4013 }
4014
Mark Mendell7c8d0092015-01-26 11:21:33 -05004015 case Primitive::kPrimFloat: {
4016 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4017 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4018 if (index.IsConstant()) {
4019 __ movss(out, Address(obj,
4020 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4021 } else {
4022 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4023 }
4024 break;
4025 }
4026
4027 case Primitive::kPrimDouble: {
4028 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4029 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4030 if (index.IsConstant()) {
4031 __ movsd(out, Address(obj,
4032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4033 } else {
4034 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4035 }
4036 break;
4037 }
4038
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004039 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004041 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004042 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004043
4044 if (type != Primitive::kPrimLong) {
4045 codegen_->MaybeRecordImplicitNullCheck(instruction);
4046 }
Roland Levillain4d027112015-07-01 15:41:14 +01004047
4048 if (type == Primitive::kPrimNot) {
4049 Register out = locations->Out().AsRegister<Register>();
4050 __ MaybeUnpoisonHeapReference(out);
4051 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004052}
4053
4054void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004055 // This location builder might end up asking to up to four registers, which is
4056 // not currently possible for baseline. The situation in which we need four
4057 // registers cannot be met by baseline though, because it has not run any
4058 // optimization.
4059
Nicolas Geoffray39468442014-09-02 15:17:15 +01004060 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004061 bool needs_write_barrier =
4062 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4063
Mark Mendell5f874182015-03-04 15:42:45 -05004064 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004065
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4067 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004068 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004069
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004070 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004072 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4073 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4074 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004075 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004076 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4077 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004078 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004079 // In case of a byte operation, the register allocator does not support multiple
4080 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004081 locations->SetInAt(0, Location::RequiresRegister());
4082 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004083 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004084 // Ensure the value is in a byte register.
4085 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004086 } else if (Primitive::IsFloatingPointType(value_type)) {
4087 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004088 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004089 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004090 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004091 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004092 // Temporary registers for the write barrier.
4093 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004094 // Ensure the card is in a byte register.
4095 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004097 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098}
4099
4100void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4101 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004104 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004105 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004106 bool needs_runtime_call = locations->WillCall();
4107 bool needs_write_barrier =
4108 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004109
4110 switch (value_type) {
4111 case Primitive::kPrimBoolean:
4112 case Primitive::kPrimByte: {
4113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114 if (index.IsConstant()) {
4115 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004116 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004117 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004118 } else {
4119 __ movb(Address(obj, offset),
4120 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4121 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004123 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004124 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004125 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004126 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004127 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004128 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4129 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004130 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004131 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004132 break;
4133 }
4134
4135 case Primitive::kPrimShort:
4136 case Primitive::kPrimChar: {
4137 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004138 if (index.IsConstant()) {
4139 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004140 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004141 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004142 } else {
4143 __ movw(Address(obj, offset),
4144 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4145 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004146 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004147 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004148 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4149 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004150 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004151 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004152 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4153 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004154 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156 break;
4157 }
4158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004159 case Primitive::kPrimInt:
4160 case Primitive::kPrimNot: {
4161 if (!needs_runtime_call) {
4162 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4163 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004164 size_t offset =
4165 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004166 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004167 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4168 Register temp = locations->GetTemp(0).AsRegister<Register>();
4169 __ movl(temp, value.AsRegister<Register>());
4170 __ PoisonHeapReference(temp);
4171 __ movl(Address(obj, offset), temp);
4172 } else {
4173 __ movl(Address(obj, offset), value.AsRegister<Register>());
4174 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004175 } else {
4176 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004177 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4178 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4179 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4180 // Note: if heap poisoning is enabled, no need to poison
4181 // (negate) `v` if it is a reference, as it would be null.
4182 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004183 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004184 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004185 DCHECK(index.IsRegister()) << index;
4186 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004187 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4188 Register temp = locations->GetTemp(0).AsRegister<Register>();
4189 __ movl(temp, value.AsRegister<Register>());
4190 __ PoisonHeapReference(temp);
4191 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4192 } else {
4193 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4194 value.AsRegister<Register>());
4195 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004196 } else {
4197 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004198 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4199 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4200 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4201 // Note: if heap poisoning is enabled, no need to poison
4202 // (negate) `v` if it is a reference, as it would be null.
4203 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004204 }
4205 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004207
4208 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004209 Register temp = locations->GetTemp(0).AsRegister<Register>();
4210 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004211 codegen_->MarkGCCard(
4212 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004213 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004215 DCHECK_EQ(value_type, Primitive::kPrimNot);
4216 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004217 // Note: if heap poisoning is enabled, pAputObject takes cares
4218 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004219 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4220 instruction,
4221 instruction->GetDexPc(),
4222 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223 }
4224 break;
4225 }
4226
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227 case Primitive::kPrimLong: {
4228 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229 if (index.IsConstant()) {
4230 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004231 if (value.IsRegisterPair()) {
4232 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004234 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004235 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004236 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004237 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4238 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004239 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004240 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4241 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004243 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004244 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004245 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004246 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004247 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004248 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004249 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004250 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004251 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004252 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004253 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004254 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004255 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004256 Immediate(High32Bits(val)));
4257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004258 }
4259 break;
4260 }
4261
Mark Mendell7c8d0092015-01-26 11:21:33 -05004262 case Primitive::kPrimFloat: {
4263 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4264 DCHECK(value.IsFpuRegister());
4265 if (index.IsConstant()) {
4266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4267 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4268 } else {
4269 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4270 value.AsFpuRegister<XmmRegister>());
4271 }
4272 break;
4273 }
4274
4275 case Primitive::kPrimDouble: {
4276 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4277 DCHECK(value.IsFpuRegister());
4278 if (index.IsConstant()) {
4279 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4280 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4281 } else {
4282 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4283 value.AsFpuRegister<XmmRegister>());
4284 }
4285 break;
4286 }
4287
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004288 case Primitive::kPrimVoid:
4289 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004290 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004291 }
4292}
4293
4294void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004296 locations->SetInAt(0, Location::RequiresRegister());
4297 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004298}
4299
4300void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4301 LocationSummary* locations = instruction->GetLocations();
4302 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004303 Register obj = locations->InAt(0).AsRegister<Register>();
4304 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004306 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004307}
4308
4309void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004310 LocationSummary* locations =
4311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004312 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004313 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004314 if (instruction->HasUses()) {
4315 locations->SetOut(Location::SameAsFirstInput());
4316 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317}
4318
4319void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4320 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004321 Location index_loc = locations->InAt(0);
4322 Location length_loc = locations->InAt(1);
4323 SlowPathCodeX86* slow_path =
4324 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325
Mark Mendell99dbd682015-04-22 16:18:52 -04004326 if (length_loc.IsConstant()) {
4327 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4328 if (index_loc.IsConstant()) {
4329 // BCE will remove the bounds check if we are guarenteed to pass.
4330 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4331 if (index < 0 || index >= length) {
4332 codegen_->AddSlowPath(slow_path);
4333 __ jmp(slow_path->GetEntryLabel());
4334 } else {
4335 // Some optimization after BCE may have generated this, and we should not
4336 // generate a bounds check if it is a valid range.
4337 }
4338 return;
4339 }
4340
4341 // We have to reverse the jump condition because the length is the constant.
4342 Register index_reg = index_loc.AsRegister<Register>();
4343 __ cmpl(index_reg, Immediate(length));
4344 codegen_->AddSlowPath(slow_path);
4345 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004346 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004347 Register length = length_loc.AsRegister<Register>();
4348 if (index_loc.IsConstant()) {
4349 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4350 __ cmpl(length, Immediate(value));
4351 } else {
4352 __ cmpl(length, index_loc.AsRegister<Register>());
4353 }
4354 codegen_->AddSlowPath(slow_path);
4355 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004356 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004357}
4358
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4360 temp->SetLocations(nullptr);
4361}
4362
4363void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4364 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004365 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366}
4367
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004368void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004369 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004370 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004371}
4372
4373void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004374 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4375}
4376
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004377void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4379}
4380
4381void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004382 HBasicBlock* block = instruction->GetBlock();
4383 if (block->GetLoopInformation() != nullptr) {
4384 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4385 // The back edge will generate the suspend check.
4386 return;
4387 }
4388 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4389 // The goto will generate the suspend check.
4390 return;
4391 }
4392 GenerateSuspendCheck(instruction, nullptr);
4393}
4394
4395void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4396 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004397 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004398 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4399 if (slow_path == nullptr) {
4400 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4401 instruction->SetSlowPath(slow_path);
4402 codegen_->AddSlowPath(slow_path);
4403 if (successor != nullptr) {
4404 DCHECK(successor->IsLoopHeader());
4405 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4406 }
4407 } else {
4408 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4409 }
4410
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004411 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004412 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004413 if (successor == nullptr) {
4414 __ j(kNotEqual, slow_path->GetEntryLabel());
4415 __ Bind(slow_path->GetReturnLabel());
4416 } else {
4417 __ j(kEqual, codegen_->GetLabelOf(successor));
4418 __ jmp(slow_path->GetEntryLabel());
4419 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004420}
4421
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004422X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4423 return codegen_->GetAssembler();
4424}
4425
Mark Mendell7c8d0092015-01-26 11:21:33 -05004426void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004427 ScratchRegisterScope ensure_scratch(
4428 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4429 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4430 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4431 __ movl(temp_reg, Address(ESP, src + stack_offset));
4432 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004433}
4434
4435void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004436 ScratchRegisterScope ensure_scratch(
4437 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4438 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4439 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4440 __ movl(temp_reg, Address(ESP, src + stack_offset));
4441 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4442 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4443 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004444}
4445
4446void ParallelMoveResolverX86::EmitMove(size_t index) {
4447 MoveOperands* move = moves_.Get(index);
4448 Location source = move->GetSource();
4449 Location destination = move->GetDestination();
4450
4451 if (source.IsRegister()) {
4452 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004453 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004454 } else {
4455 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004456 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004457 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004458 } else if (source.IsFpuRegister()) {
4459 if (destination.IsFpuRegister()) {
4460 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4461 } else if (destination.IsStackSlot()) {
4462 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4463 } else {
4464 DCHECK(destination.IsDoubleStackSlot());
4465 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4466 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004467 } else if (source.IsStackSlot()) {
4468 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004469 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004470 } else if (destination.IsFpuRegister()) {
4471 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004472 } else {
4473 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004474 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4475 }
4476 } else if (source.IsDoubleStackSlot()) {
4477 if (destination.IsFpuRegister()) {
4478 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4479 } else {
4480 DCHECK(destination.IsDoubleStackSlot()) << destination;
4481 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004482 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004483 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004484 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004485 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004486 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004487 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004488 if (value == 0) {
4489 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4490 } else {
4491 __ movl(destination.AsRegister<Register>(), Immediate(value));
4492 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004493 } else {
4494 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004495 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004496 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004497 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004498 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004499 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004500 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004501 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004502 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4503 if (value == 0) {
4504 // Easy handling of 0.0.
4505 __ xorps(dest, dest);
4506 } else {
4507 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004508 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4509 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4510 __ movl(temp, Immediate(value));
4511 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004512 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004513 } else {
4514 DCHECK(destination.IsStackSlot()) << destination;
4515 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4516 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004517 } else if (constant->IsLongConstant()) {
4518 int64_t value = constant->AsLongConstant()->GetValue();
4519 int32_t low_value = Low32Bits(value);
4520 int32_t high_value = High32Bits(value);
4521 Immediate low(low_value);
4522 Immediate high(high_value);
4523 if (destination.IsDoubleStackSlot()) {
4524 __ movl(Address(ESP, destination.GetStackIndex()), low);
4525 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4526 } else {
4527 __ movl(destination.AsRegisterPairLow<Register>(), low);
4528 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4529 }
4530 } else {
4531 DCHECK(constant->IsDoubleConstant());
4532 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004533 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004534 int32_t low_value = Low32Bits(value);
4535 int32_t high_value = High32Bits(value);
4536 Immediate low(low_value);
4537 Immediate high(high_value);
4538 if (destination.IsFpuRegister()) {
4539 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4540 if (value == 0) {
4541 // Easy handling of 0.0.
4542 __ xorpd(dest, dest);
4543 } else {
4544 __ pushl(high);
4545 __ pushl(low);
4546 __ movsd(dest, Address(ESP, 0));
4547 __ addl(ESP, Immediate(8));
4548 }
4549 } else {
4550 DCHECK(destination.IsDoubleStackSlot()) << destination;
4551 __ movl(Address(ESP, destination.GetStackIndex()), low);
4552 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4553 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004554 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004555 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004556 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004557 }
4558}
4559
Mark Mendella5c19ce2015-04-01 12:51:05 -04004560void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004561 Register suggested_scratch = reg == EAX ? EBX : EAX;
4562 ScratchRegisterScope ensure_scratch(
4563 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4564
4565 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4566 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4567 __ movl(Address(ESP, mem + stack_offset), reg);
4568 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004569}
4570
Mark Mendell7c8d0092015-01-26 11:21:33 -05004571void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004572 ScratchRegisterScope ensure_scratch(
4573 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4574
4575 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4576 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4577 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4578 __ movss(Address(ESP, mem + stack_offset), reg);
4579 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004580}
4581
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004582void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004583 ScratchRegisterScope ensure_scratch1(
4584 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004585
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004586 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4587 ScratchRegisterScope ensure_scratch2(
4588 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004589
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004590 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4591 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4592 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4593 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4594 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4595 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004596}
4597
4598void ParallelMoveResolverX86::EmitSwap(size_t index) {
4599 MoveOperands* move = moves_.Get(index);
4600 Location source = move->GetSource();
4601 Location destination = move->GetDestination();
4602
4603 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004604 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4605 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4606 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4607 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4608 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004609 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004610 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004611 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004612 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004613 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4614 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004615 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4616 // Use XOR Swap algorithm to avoid a temporary.
4617 DCHECK_NE(source.reg(), destination.reg());
4618 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4619 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4620 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4621 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4622 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4623 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4624 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004625 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4626 // Take advantage of the 16 bytes in the XMM register.
4627 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4628 Address stack(ESP, destination.GetStackIndex());
4629 // Load the double into the high doubleword.
4630 __ movhpd(reg, stack);
4631
4632 // Store the low double into the destination.
4633 __ movsd(stack, reg);
4634
4635 // Move the high double to the low double.
4636 __ psrldq(reg, Immediate(8));
4637 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4638 // Take advantage of the 16 bytes in the XMM register.
4639 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4640 Address stack(ESP, source.GetStackIndex());
4641 // Load the double into the high doubleword.
4642 __ movhpd(reg, stack);
4643
4644 // Store the low double into the destination.
4645 __ movsd(stack, reg);
4646
4647 // Move the high double to the low double.
4648 __ psrldq(reg, Immediate(8));
4649 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4650 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4651 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004652 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004653 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004654 }
4655}
4656
4657void ParallelMoveResolverX86::SpillScratch(int reg) {
4658 __ pushl(static_cast<Register>(reg));
4659}
4660
4661void ParallelMoveResolverX86::RestoreScratch(int reg) {
4662 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004663}
4664
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004665void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004666 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4667 ? LocationSummary::kCallOnSlowPath
4668 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004669 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004670 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004671 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004672 locations->SetOut(Location::RequiresRegister());
4673}
4674
4675void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004676 LocationSummary* locations = cls->GetLocations();
4677 Register out = locations->Out().AsRegister<Register>();
4678 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004679 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004680 DCHECK(!cls->CanCallRuntime());
4681 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004682 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004683 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004684 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004685 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004686 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004687 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004688 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004689
4690 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4691 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4692 codegen_->AddSlowPath(slow_path);
4693 __ testl(out, out);
4694 __ j(kEqual, slow_path->GetEntryLabel());
4695 if (cls->MustGenerateClinitCheck()) {
4696 GenerateClassInitializationCheck(slow_path, out);
4697 } else {
4698 __ Bind(slow_path->GetExitLabel());
4699 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004700 }
4701}
4702
4703void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4704 LocationSummary* locations =
4705 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4706 locations->SetInAt(0, Location::RequiresRegister());
4707 if (check->HasUses()) {
4708 locations->SetOut(Location::SameAsFirstInput());
4709 }
4710}
4711
4712void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004713 // We assume the class to not be null.
4714 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4715 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004716 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004717 GenerateClassInitializationCheck(slow_path,
4718 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004719}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004720
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004721void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4722 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004723 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4724 Immediate(mirror::Class::kStatusInitialized));
4725 __ j(kLess, slow_path->GetEntryLabel());
4726 __ Bind(slow_path->GetExitLabel());
4727 // No need for memory fence, thanks to the X86 memory model.
4728}
4729
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004730void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4731 LocationSummary* locations =
4732 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004733 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004734 locations->SetOut(Location::RequiresRegister());
4735}
4736
4737void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4738 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4739 codegen_->AddSlowPath(slow_path);
4740
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004741 LocationSummary* locations = load->GetLocations();
4742 Register out = locations->Out().AsRegister<Register>();
4743 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004744 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004745 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004746 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004747 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004748 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004749 __ testl(out, out);
4750 __ j(kEqual, slow_path->GetEntryLabel());
4751 __ Bind(slow_path->GetExitLabel());
4752}
4753
David Brazdilcb1c0552015-08-04 16:22:25 +01004754static Address GetExceptionTlsAddress() {
4755 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4756}
4757
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004758void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4759 LocationSummary* locations =
4760 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4761 locations->SetOut(Location::RequiresRegister());
4762}
4763
4764void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004765 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4766}
4767
4768void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4769 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4770}
4771
4772void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4773 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004774}
4775
4776void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4777 LocationSummary* locations =
4778 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4779 InvokeRuntimeCallingConvention calling_convention;
4780 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4781}
4782
4783void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004784 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4785 instruction,
4786 instruction->GetDexPc(),
4787 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004788}
4789
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004790void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004791 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4792 ? LocationSummary::kNoCall
4793 : LocationSummary::kCallOnSlowPath;
4794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4795 locations->SetInAt(0, Location::RequiresRegister());
4796 locations->SetInAt(1, Location::Any());
4797 locations->SetOut(Location::RequiresRegister());
4798}
4799
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004800void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004801 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004802 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004803 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004804 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004805 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4806 Label done, zero;
4807 SlowPathCodeX86* slow_path = nullptr;
4808
4809 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004810 // Avoid null check if we know obj is not null.
4811 if (instruction->MustDoNullCheck()) {
4812 __ testl(obj, obj);
4813 __ j(kEqual, &zero);
4814 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004815 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004816 __ movl(out, Address(obj, class_offset));
4817 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004818 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004819 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004820 } else {
4821 DCHECK(cls.IsStackSlot()) << cls;
4822 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4823 }
4824
4825 if (instruction->IsClassFinal()) {
4826 // Classes must be equal for the instanceof to succeed.
4827 __ j(kNotEqual, &zero);
4828 __ movl(out, Immediate(1));
4829 __ jmp(&done);
4830 } else {
4831 // If the classes are not equal, we go into a slow path.
4832 DCHECK(locations->OnlyCallsOnSlowPath());
4833 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004834 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004835 codegen_->AddSlowPath(slow_path);
4836 __ j(kNotEqual, slow_path->GetEntryLabel());
4837 __ movl(out, Immediate(1));
4838 __ jmp(&done);
4839 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004840
4841 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4842 __ Bind(&zero);
4843 __ movl(out, Immediate(0));
4844 }
4845
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004846 if (slow_path != nullptr) {
4847 __ Bind(slow_path->GetExitLabel());
4848 }
4849 __ Bind(&done);
4850}
4851
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004852void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4854 instruction, LocationSummary::kCallOnSlowPath);
4855 locations->SetInAt(0, Location::RequiresRegister());
4856 locations->SetInAt(1, Location::Any());
4857 locations->AddTemp(Location::RequiresRegister());
4858}
4859
4860void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4861 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004862 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004863 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004864 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004865 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4866 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4867 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4868 codegen_->AddSlowPath(slow_path);
4869
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004870 // Avoid null check if we know obj is not null.
4871 if (instruction->MustDoNullCheck()) {
4872 __ testl(obj, obj);
4873 __ j(kEqual, slow_path->GetExitLabel());
4874 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004875 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004876 __ movl(temp, Address(obj, class_offset));
4877 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004878 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004879 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004880 } else {
4881 DCHECK(cls.IsStackSlot()) << cls;
4882 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4883 }
Roland Levillain4d027112015-07-01 15:41:14 +01004884 // The checkcast succeeds if the classes are equal (fast path).
4885 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004886 __ j(kNotEqual, slow_path->GetEntryLabel());
4887 __ Bind(slow_path->GetExitLabel());
4888}
4889
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004890void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4893 InvokeRuntimeCallingConvention calling_convention;
4894 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4895}
4896
4897void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004898 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
4899 : QUICK_ENTRY_POINT(pUnlockObject),
4900 instruction,
4901 instruction->GetDexPc(),
4902 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004903}
4904
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004905void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4906void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4907void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4908
4909void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4910 LocationSummary* locations =
4911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4912 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4913 || instruction->GetResultType() == Primitive::kPrimLong);
4914 locations->SetInAt(0, Location::RequiresRegister());
4915 locations->SetInAt(1, Location::Any());
4916 locations->SetOut(Location::SameAsFirstInput());
4917}
4918
4919void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4920 HandleBitwiseOperation(instruction);
4921}
4922
4923void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4924 HandleBitwiseOperation(instruction);
4925}
4926
4927void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4928 HandleBitwiseOperation(instruction);
4929}
4930
4931void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4932 LocationSummary* locations = instruction->GetLocations();
4933 Location first = locations->InAt(0);
4934 Location second = locations->InAt(1);
4935 DCHECK(first.Equals(locations->Out()));
4936
4937 if (instruction->GetResultType() == Primitive::kPrimInt) {
4938 if (second.IsRegister()) {
4939 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004940 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004941 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004942 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004943 } else {
4944 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004946 }
4947 } else if (second.IsConstant()) {
4948 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004949 __ andl(first.AsRegister<Register>(),
4950 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004951 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004952 __ orl(first.AsRegister<Register>(),
4953 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004954 } else {
4955 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004956 __ xorl(first.AsRegister<Register>(),
4957 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004958 }
4959 } else {
4960 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004961 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004962 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004963 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004964 } else {
4965 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004966 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004967 }
4968 }
4969 } else {
4970 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4971 if (second.IsRegisterPair()) {
4972 if (instruction->IsAnd()) {
4973 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4974 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4975 } else if (instruction->IsOr()) {
4976 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4977 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4978 } else {
4979 DCHECK(instruction->IsXor());
4980 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4981 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4982 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004983 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004984 if (instruction->IsAnd()) {
4985 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4986 __ andl(first.AsRegisterPairHigh<Register>(),
4987 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4988 } else if (instruction->IsOr()) {
4989 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4990 __ orl(first.AsRegisterPairHigh<Register>(),
4991 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4992 } else {
4993 DCHECK(instruction->IsXor());
4994 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4995 __ xorl(first.AsRegisterPairHigh<Register>(),
4996 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4997 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004998 } else {
4999 DCHECK(second.IsConstant()) << second;
5000 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005001 int32_t low_value = Low32Bits(value);
5002 int32_t high_value = High32Bits(value);
5003 Immediate low(low_value);
5004 Immediate high(high_value);
5005 Register first_low = first.AsRegisterPairLow<Register>();
5006 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005007 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005008 if (low_value == 0) {
5009 __ xorl(first_low, first_low);
5010 } else if (low_value != -1) {
5011 __ andl(first_low, low);
5012 }
5013 if (high_value == 0) {
5014 __ xorl(first_high, first_high);
5015 } else if (high_value != -1) {
5016 __ andl(first_high, high);
5017 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005018 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005019 if (low_value != 0) {
5020 __ orl(first_low, low);
5021 }
5022 if (high_value != 0) {
5023 __ orl(first_high, high);
5024 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005025 } else {
5026 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005027 if (low_value != 0) {
5028 __ xorl(first_low, low);
5029 }
5030 if (high_value != 0) {
5031 __ xorl(first_high, high);
5032 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005033 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005034 }
5035 }
5036}
5037
Calin Juravleb1498f62015-02-16 13:13:29 +00005038void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5039 // Nothing to do, this should be removed during prepare for register allocator.
5040 UNUSED(instruction);
5041 LOG(FATAL) << "Unreachable";
5042}
5043
5044void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5045 // Nothing to do, this should be removed during prepare for register allocator.
5046 UNUSED(instruction);
5047 LOG(FATAL) << "Unreachable";
5048}
5049
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005050void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5051 DCHECK(codegen_->IsBaseline());
5052 LocationSummary* locations =
5053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5054 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5055}
5056
5057void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5058 DCHECK(codegen_->IsBaseline());
5059 // Will be generated at use site.
5060}
5061
Roland Levillain4d027112015-07-01 15:41:14 +01005062#undef __
5063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005064} // namespace x86
5065} // namespace art