blob: ecf5eedcd5647fab6de02897765b8eb2bea88a36 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Mark Mendell0616ae02015-04-17 12:49:27 -040022#include "constant_area_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040026#include "intrinsics.h"
27#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace x86 {
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010041static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042
Mark Mendell5f874182015-03-04 15:42:45 -050043static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Alexandre Rames8158f282015-08-07 10:26:17 +010050#define QUICK_ENTRY_POINT(x) Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x))
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010052class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055
Alexandre Rames2ed20af2015-03-06 13:55:35 +000056 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010057 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010059 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
60 instruction_,
61 instruction_->GetDexPc(),
62 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 }
64
Alexandre Rames8158f282015-08-07 10:26:17 +010065 bool IsFatal() const OVERRIDE { return true; }
66
Alexandre Rames9931f312015-06-19 14:47:01 +010067 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
68
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010070 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
72};
73
Calin Juravled0d48522014-11-04 16:40:20 +000074class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
75 public:
76 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
77
Alexandre Rames2ed20af2015-03-06 13:55:35 +000078 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000080 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +010081 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
82 instruction_,
83 instruction_->GetDexPc(),
84 this);
Calin Juravled0d48522014-11-04 16:40:20 +000085 }
86
Alexandre Rames8158f282015-08-07 10:26:17 +010087 bool IsFatal() const OVERRIDE { return true; }
88
Alexandre Rames9931f312015-06-19 14:47:01 +010089 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
90
Calin Juravled0d48522014-11-04 16:40:20 +000091 private:
92 HDivZeroCheck* const instruction_;
93 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
94};
95
Calin Juravlebacfec32014-11-14 15:54:36 +000096class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000097 public:
Roland Levillain3887c462015-08-12 18:15:42 +010098 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000099
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000100 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000102 if (is_div_) {
103 __ negl(reg_);
104 } else {
105 __ movl(reg_, Immediate(0));
106 }
Calin Juravled0d48522014-11-04 16:40:20 +0000107 __ jmp(GetExitLabel());
108 }
109
Alexandre Rames9931f312015-06-19 14:47:01 +0100110 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
111
Calin Juravled0d48522014-11-04 16:40:20 +0000112 private:
113 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 bool is_div_;
115 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000116};
117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100118class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100119 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100121
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000122 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000126 // We're moving two locations to locations that could overlap, so we need a parallel
127 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000129 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100130 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000131 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100132 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100134 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
135 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100136 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
137 instruction_,
138 instruction_->GetDexPc(),
139 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 }
141
Alexandre Rames8158f282015-08-07 10:26:17 +0100142 bool IsFatal() const OVERRIDE { return true; }
143
Alexandre Rames9931f312015-06-19 14:47:01 +0100144 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148
149 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
150};
151
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100152class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000153 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000154 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100155 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000160 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100161 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
162 instruction_,
163 instruction_->GetDexPc(),
164 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000165 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 if (successor_ == nullptr) {
167 __ jmp(GetReturnLabel());
168 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100169 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 }
172
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 Label* GetReturnLabel() {
174 DCHECK(successor_ == nullptr);
175 return &return_label_;
176 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100178 HBasicBlock* GetSuccessor() const {
179 return successor_;
180 }
181
Alexandre Rames9931f312015-06-19 14:47:01 +0100182 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
183
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184 private:
185 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
190};
191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000192class LoadStringSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
195
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000196 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000197 LocationSummary* locations = instruction_->GetLocations();
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199
200 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
201 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000203
204 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100206 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
207 instruction_,
208 instruction_->GetDexPc(),
209 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000210 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000212
213 __ jmp(GetExitLabel());
214 }
215
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
217
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000218 private:
219 HLoadString* const instruction_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
222};
223
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224class LoadClassSlowPathX86 : public SlowPathCodeX86 {
225 public:
226 LoadClassSlowPathX86(HLoadClass* cls,
227 HInstruction* at,
228 uint32_t dex_pc,
229 bool do_clinit)
230 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
231 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
232 }
233
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000234 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 LocationSummary* locations = at_->GetLocations();
236 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
237 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000238 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239
240 InvokeRuntimeCallingConvention calling_convention;
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100242 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
243 : QUICK_ENTRY_POINT(pInitializeType),
244 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245
246 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000247 Location out = locations->Out();
248 if (out.IsValid()) {
249 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
250 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000252
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000253 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 __ jmp(GetExitLabel());
255 }
256
Alexandre Rames9931f312015-06-19 14:47:01 +0100257 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
258
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 private:
260 // The class this slow path will load.
261 HLoadClass* const cls_;
262
263 // The instruction where this slow path is happening.
264 // (Might be the load class or an initialization check).
265 HInstruction* const at_;
266
267 // The dex PC of `at_`.
268 const uint32_t dex_pc_;
269
270 // Whether to initialize the class.
271 const bool do_clinit_;
272
273 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
274};
275
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
277 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100278 explicit TypeCheckSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000280 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100282 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
283 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000284 DCHECK(instruction_->IsCheckCast()
285 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
288 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000289 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
291 // We're moving two locations to locations that could overlap, so we need a parallel
292 // move resolver.
293 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000294 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100295 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100297 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100298 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100299 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
300 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100303 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
304 instruction_,
305 instruction_->GetDexPc(),
306 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 } else {
308 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100309 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
310 instruction_,
311 instruction_->GetDexPc(),
312 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 }
314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
316 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
317 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000318 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 __ jmp(GetExitLabel());
321 }
322
Alexandre Rames9931f312015-06-19 14:47:01 +0100323 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
324
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
328 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
329};
330
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700331class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
332 public:
333 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
334 : instruction_(instruction) {}
335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100337 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100338 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700339 __ Bind(GetEntryLabel());
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100341 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
342 instruction_,
343 instruction_->GetDexPc(),
344 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700345 }
346
Alexandre Rames9931f312015-06-19 14:47:01 +0100347 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
348
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700349 private:
350 HInstruction* const instruction_;
351 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
352};
353
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100354#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100355#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100356
Roland Levillain4fa13f62015-07-06 18:11:54 +0100357inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700358 switch (cond) {
359 case kCondEQ: return kEqual;
360 case kCondNE: return kNotEqual;
361 case kCondLT: return kLess;
362 case kCondLE: return kLessEqual;
363 case kCondGT: return kGreater;
364 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700365 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100366 LOG(FATAL) << "Unreachable";
367 UNREACHABLE();
368}
369
370inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
371 switch (cond) {
372 case kCondEQ: return kEqual;
373 case kCondNE: return kNotEqual;
374 case kCondLT: return kBelow;
375 case kCondLE: return kBelowEqual;
376 case kCondGT: return kAbove;
377 case kCondGE: return kAboveEqual;
378 }
379 LOG(FATAL) << "Unreachable";
380 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700381}
382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100383void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100384 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100385}
386
387void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100388 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100389}
390
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100391size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
392 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
393 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100394}
395
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100396size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
397 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
398 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100399}
400
Mark Mendell7c8d0092015-01-26 11:21:33 -0500401size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
402 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
403 return GetFloatingPointSpillSlotSize();
404}
405
406size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
407 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
408 return GetFloatingPointSpillSlotSize();
409}
410
Alexandre Rames8158f282015-08-07 10:26:17 +0100411void CodeGeneratorX86::InvokeRuntime(Address entry_point,
412 HInstruction* instruction,
413 uint32_t dex_pc,
414 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100415 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100416 __ fs()->call(entry_point);
417 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100418}
419
Mark Mendellfb8d2792015-03-31 22:16:59 -0400420CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
421 const X86InstructionSetFeatures& isa_features,
422 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500423 : CodeGenerator(graph,
424 kNumberOfCpuRegisters,
425 kNumberOfXmmRegisters,
426 kNumberOfRegisterPairs,
427 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
428 arraysize(kCoreCalleeSaves))
429 | (1 << kFakeReturnRegister),
430 0,
431 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100432 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100434 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400435 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000436 isa_features_(isa_features),
437 method_patches_(graph->GetArena()->Adapter()),
438 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000439 // Use a fake return address register to mimic Quick.
440 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100441}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444 switch (type) {
445 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100447 X86ManagedRegister pair =
448 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100449 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
450 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
452 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100453 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100454 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455 }
456
457 case Primitive::kPrimByte:
458 case Primitive::kPrimBoolean:
459 case Primitive::kPrimChar:
460 case Primitive::kPrimShort:
461 case Primitive::kPrimInt:
462 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100463 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100464 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100465 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100466 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
467 X86ManagedRegister current =
468 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
469 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100470 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100471 }
472 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100473 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 }
475
476 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100477 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 return Location::FpuRegisterLocation(
479 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100480 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481
482 case Primitive::kPrimVoid:
483 LOG(FATAL) << "Unreachable type " << type;
484 }
485
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100486 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100487}
488
Mark Mendell5f874182015-03-04 15:42:45 -0500489void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100490 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100491 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100492
493 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100495
Mark Mendell5f874182015-03-04 15:42:45 -0500496 if (is_baseline) {
497 blocked_core_registers_[EBP] = true;
498 blocked_core_registers_[ESI] = true;
499 blocked_core_registers_[EDI] = true;
500 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100501
502 UpdateBlockedPairRegisters();
503}
504
505void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
506 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
507 X86ManagedRegister current =
508 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
509 if (blocked_core_registers_[current.AsRegisterPairLow()]
510 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
511 blocked_register_pairs_[i] = true;
512 }
513 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514}
515
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100516InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
517 : HGraphVisitor(graph),
518 assembler_(codegen->GetAssembler()),
519 codegen_(codegen) {}
520
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100521static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100522 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100523}
524
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100526 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000527 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000528 bool skip_overflow_check =
529 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000530 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000531
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000532 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100533 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100534 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100535 }
536
Mark Mendell5f874182015-03-04 15:42:45 -0500537 if (HasEmptyFrame()) {
538 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000539 }
Mark Mendell5f874182015-03-04 15:42:45 -0500540
541 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
542 Register reg = kCoreCalleeSaves[i];
543 if (allocated_registers_.ContainsCoreRegister(reg)) {
544 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100545 __ cfi().AdjustCFAOffset(kX86WordSize);
546 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500547 }
548 }
549
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100550 int adjust = GetFrameSize() - FrameEntrySpillSize();
551 __ subl(ESP, Immediate(adjust));
552 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100553 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000554}
555
556void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100557 __ cfi().RememberState();
558 if (!HasEmptyFrame()) {
559 int adjust = GetFrameSize() - FrameEntrySpillSize();
560 __ addl(ESP, Immediate(adjust));
561 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500562
David Srbeckyc34dc932015-04-12 09:27:43 +0100563 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
564 Register reg = kCoreCalleeSaves[i];
565 if (allocated_registers_.ContainsCoreRegister(reg)) {
566 __ popl(reg);
567 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
568 __ cfi().Restore(DWARFReg(reg));
569 }
Mark Mendell5f874182015-03-04 15:42:45 -0500570 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000571 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100572 __ ret();
573 __ cfi().RestoreState();
574 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000575}
576
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100577void CodeGeneratorX86::Bind(HBasicBlock* block) {
578 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000579}
580
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100581Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
582 switch (load->GetType()) {
583 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100585 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100586
587 case Primitive::kPrimInt:
588 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100589 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100591
592 case Primitive::kPrimBoolean:
593 case Primitive::kPrimByte:
594 case Primitive::kPrimChar:
595 case Primitive::kPrimShort:
596 case Primitive::kPrimVoid:
597 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700598 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100599 }
600
601 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700602 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603}
604
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100605Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
606 switch (type) {
607 case Primitive::kPrimBoolean:
608 case Primitive::kPrimByte:
609 case Primitive::kPrimChar:
610 case Primitive::kPrimShort:
611 case Primitive::kPrimInt:
612 case Primitive::kPrimNot:
613 return Location::RegisterLocation(EAX);
614
615 case Primitive::kPrimLong:
616 return Location::RegisterPairLocation(EAX, EDX);
617
618 case Primitive::kPrimVoid:
619 return Location::NoLocation();
620
621 case Primitive::kPrimDouble:
622 case Primitive::kPrimFloat:
623 return Location::FpuRegisterLocation(XMM0);
624 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100625
626 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100627}
628
629Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
630 return Location::RegisterLocation(kMethodRegisterArgument);
631}
632
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100633Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100634 switch (type) {
635 case Primitive::kPrimBoolean:
636 case Primitive::kPrimByte:
637 case Primitive::kPrimChar:
638 case Primitive::kPrimShort:
639 case Primitive::kPrimInt:
640 case Primitive::kPrimNot: {
641 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000642 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100643 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100644 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100645 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000646 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100647 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100648 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100649
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000650 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100651 uint32_t index = gp_index_;
652 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000653 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100654 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
656 calling_convention.GetRegisterPairAt(index));
657 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100658 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000659 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
660 }
661 }
662
663 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100664 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000665 stack_index_++;
666 if (index < calling_convention.GetNumberOfFpuRegisters()) {
667 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
668 } else {
669 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
670 }
671 }
672
673 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100674 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000675 stack_index_ += 2;
676 if (index < calling_convention.GetNumberOfFpuRegisters()) {
677 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
678 } else {
679 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100680 }
681 }
682
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100683 case Primitive::kPrimVoid:
684 LOG(FATAL) << "Unexpected parameter type " << type;
685 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100686 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100687 return Location();
688}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100689
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690void CodeGeneratorX86::Move32(Location destination, Location source) {
691 if (source.Equals(destination)) {
692 return;
693 }
694 if (destination.IsRegister()) {
695 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100697 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000698 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 } else {
700 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000701 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 } else if (destination.IsFpuRegister()) {
704 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000705 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100706 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000707 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100708 } else {
709 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000713 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000715 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500718 } else if (source.IsConstant()) {
719 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000720 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500721 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100722 } else {
723 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100724 __ pushl(Address(ESP, source.GetStackIndex()));
725 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 }
727 }
728}
729
730void CodeGeneratorX86::Move64(Location destination, Location source) {
731 if (source.Equals(destination)) {
732 return;
733 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100734 if (destination.IsRegisterPair()) {
735 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000736 EmitParallelMoves(
737 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
738 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100739 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000740 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100741 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
742 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 } else if (source.IsFpuRegister()) {
744 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000746 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100748 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
749 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
751 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500753 if (source.IsFpuRegister()) {
754 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
755 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000756 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 } else {
758 LOG(FATAL) << "Unimplemented";
759 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000761 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100762 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000763 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100764 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100766 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000768 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000769 } else if (source.IsConstant()) {
770 HConstant* constant = source.GetConstant();
771 int64_t value;
772 if (constant->IsLongConstant()) {
773 value = constant->AsLongConstant()->GetValue();
774 } else {
775 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000776 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000777 }
778 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
779 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100780 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000781 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000782 EmitParallelMoves(
783 Location::StackSlot(source.GetStackIndex()),
784 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100785 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000786 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100787 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
788 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 }
790 }
791}
792
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100793void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000794 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100795 if (instruction->IsCurrentMethod()) {
796 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
797 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000798 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100799 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000800 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000801 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
802 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000803 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000805 } else if (location.IsStackSlot()) {
806 __ movl(Address(ESP, location.GetStackIndex()), imm);
807 } else {
808 DCHECK(location.IsConstant());
809 DCHECK_EQ(location.GetConstant(), const_to_move);
810 }
811 } else if (const_to_move->IsLongConstant()) {
812 int64_t value = const_to_move->AsLongConstant()->GetValue();
813 if (location.IsRegisterPair()) {
814 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
815 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
816 } else if (location.IsDoubleStackSlot()) {
817 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000818 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
819 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000820 } else {
821 DCHECK(location.IsConstant());
822 DCHECK_EQ(location.GetConstant(), instruction);
823 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000825 } else if (instruction->IsTemporary()) {
826 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000827 if (temp_location.IsStackSlot()) {
828 Move32(location, temp_location);
829 } else {
830 DCHECK(temp_location.IsDoubleStackSlot());
831 Move64(location, temp_location);
832 }
Roland Levillain476df552014-10-09 17:51:36 +0100833 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100834 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100835 switch (instruction->GetType()) {
836 case Primitive::kPrimBoolean:
837 case Primitive::kPrimByte:
838 case Primitive::kPrimChar:
839 case Primitive::kPrimShort:
840 case Primitive::kPrimInt:
841 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 case Primitive::kPrimFloat:
843 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 break;
845
846 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 case Primitive::kPrimDouble:
848 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 break;
850
851 default:
852 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
853 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100855 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100856 switch (instruction->GetType()) {
857 case Primitive::kPrimBoolean:
858 case Primitive::kPrimByte:
859 case Primitive::kPrimChar:
860 case Primitive::kPrimShort:
861 case Primitive::kPrimInt:
862 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000864 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 break;
866
867 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000869 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 break;
871
872 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100874 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000875 }
876}
877
David Brazdilfc6a86a2015-06-26 10:33:45 +0000878void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100879 DCHECK(!successor->IsExitBlock());
880
881 HBasicBlock* block = got->GetBlock();
882 HInstruction* previous = got->GetPrevious();
883
884 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000885 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100886 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
887 return;
888 }
889
890 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
891 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
892 }
893 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895 }
896}
897
David Brazdilfc6a86a2015-06-26 10:33:45 +0000898void LocationsBuilderX86::VisitGoto(HGoto* got) {
899 got->SetLocations(nullptr);
900}
901
902void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
903 HandleGoto(got, got->GetSuccessor());
904}
905
906void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
907 try_boundary->SetLocations(nullptr);
908}
909
910void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
911 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
912 if (!successor->IsExitBlock()) {
913 HandleGoto(try_boundary, successor);
914 }
915}
916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000918 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000919}
920
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700922 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000923}
924
Mark Mendellc4701932015-04-10 13:18:51 -0400925void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
926 Label* true_label,
927 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928 if (cond->IsFPConditionTrueIfNaN()) {
929 __ j(kUnordered, true_label);
930 } else if (cond->IsFPConditionFalseIfNaN()) {
931 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400932 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400934}
935
936void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
937 Label* true_label,
938 Label* false_label) {
939 LocationSummary* locations = cond->GetLocations();
940 Location left = locations->InAt(0);
941 Location right = locations->InAt(1);
942 IfCondition if_cond = cond->GetCondition();
943
Mark Mendellc4701932015-04-10 13:18:51 -0400944 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400946 IfCondition true_high_cond = if_cond;
947 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400949
950 // Set the conditions for the test, remembering that == needs to be
951 // decided using the low words.
952 switch (if_cond) {
953 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400954 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100955 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400956 break;
957 case kCondLT:
958 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400959 break;
960 case kCondLE:
961 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400962 break;
963 case kCondGT:
964 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400965 break;
966 case kCondGE:
967 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400968 break;
969 }
970
971 if (right.IsConstant()) {
972 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400973 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100974 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400975
976 if (val_high == 0) {
977 __ testl(left_high, left_high);
978 } else {
979 __ cmpl(left_high, Immediate(val_high));
980 }
981 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100982 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400983 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100984 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400985 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100986 __ j(X86SignedCondition(true_high_cond), true_label);
987 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400988 }
989 // Must be equal high, so compare the lows.
990 if (val_low == 0) {
991 __ testl(left_low, left_low);
992 } else {
993 __ cmpl(left_low, Immediate(val_low));
994 }
995 } else {
Mark Mendellc4701932015-04-10 13:18:51 -0400996 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100997 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400998
999 __ cmpl(left_high, right_high);
1000 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001001 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001002 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001003 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001004 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001005 __ j(X86SignedCondition(true_high_cond), true_label);
1006 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001007 }
1008 // Must be equal high, so compare the lows.
1009 __ cmpl(left_low, right_low);
1010 }
1011 // The last comparison might be unsigned.
1012 __ j(final_condition, true_label);
1013}
1014
1015void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1016 HCondition* condition,
1017 Label* true_target,
1018 Label* false_target,
1019 Label* always_true_target) {
1020 LocationSummary* locations = condition->GetLocations();
1021 Location left = locations->InAt(0);
1022 Location right = locations->InAt(1);
1023
1024 // We don't want true_target as a nullptr.
1025 if (true_target == nullptr) {
1026 true_target = always_true_target;
1027 }
1028 bool falls_through = (false_target == nullptr);
1029
1030 // FP compares don't like null false_targets.
1031 if (false_target == nullptr) {
1032 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1033 }
1034
1035 Primitive::Type type = condition->InputAt(0)->GetType();
1036 switch (type) {
1037 case Primitive::kPrimLong:
1038 GenerateLongComparesAndJumps(condition, true_target, false_target);
1039 break;
1040 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001041 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1042 GenerateFPJumps(condition, true_target, false_target);
1043 break;
1044 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001045 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1046 GenerateFPJumps(condition, true_target, false_target);
1047 break;
1048 default:
1049 LOG(FATAL) << "Unexpected compare type " << type;
1050 }
1051
1052 if (!falls_through) {
1053 __ jmp(false_target);
1054 }
1055}
1056
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001057void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1058 Label* true_target,
1059 Label* false_target,
1060 Label* always_true_target) {
1061 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001062 if (cond->IsIntConstant()) {
1063 // Constant condition, statically compared against 1.
1064 int32_t cond_value = cond->AsIntConstant()->GetValue();
1065 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001066 if (always_true_target != nullptr) {
1067 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001068 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001069 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001070 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001071 DCHECK_EQ(cond_value, 0);
1072 }
1073 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001074 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1076 // Moves do not affect the eflags register, so if the condition is
1077 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001078 // again. We can't use the eflags on long/FP conditions if they are
1079 // materialized due to the complex branching.
1080 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001081 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001082 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001083 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1084 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001085 if (!eflags_set) {
1086 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001087 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001088 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001089 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 } else {
1091 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1092 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001093 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001094 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001095 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001096 }
1097 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001098 // Condition has not been materialized, use its inputs as the
1099 // comparison and its condition as the branch condition.
1100
Mark Mendellc4701932015-04-10 13:18:51 -04001101 // Is this a long or FP comparison that has been folded into the HCondition?
1102 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1103 // Generate the comparison directly.
1104 GenerateCompareTestAndBranch(instruction->AsIf(),
1105 cond->AsCondition(),
1106 true_target,
1107 false_target,
1108 always_true_target);
1109 return;
1110 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001111
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001112 Location lhs = cond->GetLocations()->InAt(0);
1113 Location rhs = cond->GetLocations()->InAt(1);
1114 // LHS is guaranteed to be in a register (see
1115 // LocationsBuilderX86::VisitCondition).
1116 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001117 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001118 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001119 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001120 if (constant == 0) {
1121 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1122 } else {
1123 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1124 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001127 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001128 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001129 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001130 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001131 if (false_target != nullptr) {
1132 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133 }
1134}
1135
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001136void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1137 LocationSummary* locations =
1138 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1139 HInstruction* cond = if_instr->InputAt(0);
1140 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1141 locations->SetInAt(0, Location::Any());
1142 }
1143}
1144
1145void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1146 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1147 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1148 Label* always_true_target = true_target;
1149 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1150 if_instr->IfTrueSuccessor())) {
1151 always_true_target = nullptr;
1152 }
1153 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1154 if_instr->IfFalseSuccessor())) {
1155 false_target = nullptr;
1156 }
1157 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1158}
1159
1160void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1161 LocationSummary* locations = new (GetGraph()->GetArena())
1162 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1163 HInstruction* cond = deoptimize->InputAt(0);
1164 DCHECK(cond->IsCondition());
1165 if (cond->AsCondition()->NeedsMaterialization()) {
1166 locations->SetInAt(0, Location::Any());
1167 }
1168}
1169
1170void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1171 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1172 DeoptimizationSlowPathX86(deoptimize);
1173 codegen_->AddSlowPath(slow_path);
1174 Label* slow_path_entry = slow_path->GetEntryLabel();
1175 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1176}
1177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001178void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001179 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001180}
1181
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1183 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001184}
1185
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001186void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001187 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188}
1189
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001190void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001191 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001192 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001193}
1194
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001196 LocationSummary* locations =
1197 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198 switch (store->InputAt(1)->GetType()) {
1199 case Primitive::kPrimBoolean:
1200 case Primitive::kPrimByte:
1201 case Primitive::kPrimChar:
1202 case Primitive::kPrimShort:
1203 case Primitive::kPrimInt:
1204 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1207 break;
1208
1209 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1212 break;
1213
1214 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217}
1218
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001219void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001220 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001221}
1222
Roland Levillain0d37cd02015-05-27 16:39:19 +01001223void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001224 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001225 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001226 // Handle the long/FP comparisons made in instruction simplification.
1227 switch (cond->InputAt(0)->GetType()) {
1228 case Primitive::kPrimLong: {
1229 locations->SetInAt(0, Location::RequiresRegister());
1230 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1231 if (cond->NeedsMaterialization()) {
1232 locations->SetOut(Location::RequiresRegister());
1233 }
1234 break;
1235 }
1236 case Primitive::kPrimFloat:
1237 case Primitive::kPrimDouble: {
1238 locations->SetInAt(0, Location::RequiresFpuRegister());
1239 locations->SetInAt(1, Location::RequiresFpuRegister());
1240 if (cond->NeedsMaterialization()) {
1241 locations->SetOut(Location::RequiresRegister());
1242 }
1243 break;
1244 }
1245 default:
1246 locations->SetInAt(0, Location::RequiresRegister());
1247 locations->SetInAt(1, Location::Any());
1248 if (cond->NeedsMaterialization()) {
1249 // We need a byte register.
1250 locations->SetOut(Location::RegisterLocation(ECX));
1251 }
1252 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001253 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254}
1255
Roland Levillain0d37cd02015-05-27 16:39:19 +01001256void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001257 if (!cond->NeedsMaterialization()) {
1258 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001259 }
Mark Mendellc4701932015-04-10 13:18:51 -04001260
1261 LocationSummary* locations = cond->GetLocations();
1262 Location lhs = locations->InAt(0);
1263 Location rhs = locations->InAt(1);
1264 Register reg = locations->Out().AsRegister<Register>();
1265 Label true_label, false_label;
1266
1267 switch (cond->InputAt(0)->GetType()) {
1268 default: {
1269 // Integer case.
1270
1271 // Clear output register: setcc only sets the low byte.
1272 __ xorl(reg, reg);
1273
1274 if (rhs.IsRegister()) {
1275 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1276 } else if (rhs.IsConstant()) {
1277 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1278 if (constant == 0) {
1279 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1280 } else {
1281 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1282 }
1283 } else {
1284 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1285 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001286 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001287 return;
1288 }
1289 case Primitive::kPrimLong:
1290 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1291 break;
1292 case Primitive::kPrimFloat:
1293 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1294 GenerateFPJumps(cond, &true_label, &false_label);
1295 break;
1296 case Primitive::kPrimDouble:
1297 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1298 GenerateFPJumps(cond, &true_label, &false_label);
1299 break;
1300 }
1301
1302 // Convert the jumps into the result.
1303 Label done_label;
1304
Roland Levillain4fa13f62015-07-06 18:11:54 +01001305 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001306 __ Bind(&false_label);
1307 __ xorl(reg, reg);
1308 __ jmp(&done_label);
1309
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001311 __ Bind(&true_label);
1312 __ movl(reg, Immediate(1));
1313 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001314}
1315
1316void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1317 VisitCondition(comp);
1318}
1319
1320void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1321 VisitCondition(comp);
1322}
1323
1324void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1325 VisitCondition(comp);
1326}
1327
1328void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1329 VisitCondition(comp);
1330}
1331
1332void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1333 VisitCondition(comp);
1334}
1335
1336void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1337 VisitCondition(comp);
1338}
1339
1340void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1341 VisitCondition(comp);
1342}
1343
1344void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1345 VisitCondition(comp);
1346}
1347
1348void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1349 VisitCondition(comp);
1350}
1351
1352void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1353 VisitCondition(comp);
1354}
1355
1356void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1357 VisitCondition(comp);
1358}
1359
1360void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1361 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001362}
1363
1364void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001365 LocationSummary* locations =
1366 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001367 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368}
1369
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001370void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001371 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001372 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001373}
1374
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001375void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1376 LocationSummary* locations =
1377 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1378 locations->SetOut(Location::ConstantLocation(constant));
1379}
1380
1381void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1382 // Will be generated at use site.
1383 UNUSED(constant);
1384}
1385
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 LocationSummary* locations =
1388 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001389 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390}
1391
1392void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1393 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001394 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395}
1396
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001397void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1398 LocationSummary* locations =
1399 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1400 locations->SetOut(Location::ConstantLocation(constant));
1401}
1402
1403void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1404 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001405 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001406}
1407
1408void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1409 LocationSummary* locations =
1410 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1411 locations->SetOut(Location::ConstantLocation(constant));
1412}
1413
1414void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1415 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001416 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001417}
1418
Calin Juravle27df7582015-04-17 19:12:31 +01001419void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1420 memory_barrier->SetLocations(nullptr);
1421}
1422
1423void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1424 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1425}
1426
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001427void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001428 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001429}
1430
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001431void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001432 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001433 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001434}
1435
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001436void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001437 LocationSummary* locations =
1438 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439 switch (ret->InputAt(0)->GetType()) {
1440 case Primitive::kPrimBoolean:
1441 case Primitive::kPrimByte:
1442 case Primitive::kPrimChar:
1443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001446 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001447 break;
1448
1449 case Primitive::kPrimLong:
1450 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001451 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 break;
1453
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001454 case Primitive::kPrimFloat:
1455 case Primitive::kPrimDouble:
1456 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001457 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001458 break;
1459
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001460 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463}
1464
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001465void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001466 if (kIsDebugBuild) {
1467 switch (ret->InputAt(0)->GetType()) {
1468 case Primitive::kPrimBoolean:
1469 case Primitive::kPrimByte:
1470 case Primitive::kPrimChar:
1471 case Primitive::kPrimShort:
1472 case Primitive::kPrimInt:
1473 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001474 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475 break;
1476
1477 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1479 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480 break;
1481
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001482 case Primitive::kPrimFloat:
1483 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001484 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 break;
1486
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001488 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 }
1490 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001491 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001492}
1493
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001494void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001495 // When we do not run baseline, explicit clinit checks triggered by static
1496 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1497 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001498
Mark Mendellfb8d2792015-03-31 22:16:59 -04001499 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001500 if (intrinsic.TryDispatch(invoke)) {
1501 return;
1502 }
1503
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001504 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001505
1506 if (codegen_->IsBaseline()) {
1507 // Baseline does not have enough registers if the current method also
1508 // needs a register. We therefore do not require a register for it, and let
1509 // the code generation of the invoke handle it.
1510 LocationSummary* locations = invoke->GetLocations();
1511 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1512 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1513 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1514 }
1515 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001516}
1517
Mark Mendell09ed1a32015-03-25 08:30:06 -04001518static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1519 if (invoke->GetLocations()->Intrinsified()) {
1520 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1521 intrinsic.Dispatch(invoke);
1522 return true;
1523 }
1524 return false;
1525}
1526
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001527void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001528 // When we do not run baseline, explicit clinit checks triggered by static
1529 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1530 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001531
Mark Mendell09ed1a32015-03-25 08:30:06 -04001532 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1533 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001534 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001535
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001536 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001537 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001538 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001539 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001540}
1541
1542void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1543 HandleInvoke(invoke);
1544}
1545
1546void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001547 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001548 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001549}
1550
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001551void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001552 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001553 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1554 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001555 LocationSummary* locations = invoke->GetLocations();
1556 Location receiver = locations->InAt(0);
1557 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001558 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001559 DCHECK(receiver.IsRegister());
1560 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001561 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001562 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001563 // temp = temp->GetMethodAt(method_offset);
1564 __ movl(temp, Address(temp, method_offset));
1565 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001566 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001567 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001568
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001569 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001570 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001571}
1572
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001573void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1574 HandleInvoke(invoke);
1575 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001576 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001577}
1578
1579void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1580 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001582 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1583 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001584 LocationSummary* locations = invoke->GetLocations();
1585 Location receiver = locations->InAt(0);
1586 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1587
1588 // Set the hidden argument.
1589 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001590 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001591
1592 // temp = object->GetClass();
1593 if (receiver.IsStackSlot()) {
1594 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1595 __ movl(temp, Address(temp, class_offset));
1596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001598 }
Roland Levillain4d027112015-07-01 15:41:14 +01001599 codegen_->MaybeRecordImplicitNullCheck(invoke);
1600 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001601 // temp = temp->GetImtEntryAt(method_offset);
1602 __ movl(temp, Address(temp, method_offset));
1603 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001605 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606
1607 DCHECK(!codegen_->IsLeafMethod());
1608 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1609}
1610
Roland Levillain88cb1752014-10-20 16:36:47 +01001611void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1612 LocationSummary* locations =
1613 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1614 switch (neg->GetResultType()) {
1615 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001616 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetOut(Location::SameAsFirstInput());
1619 break;
1620
Roland Levillain88cb1752014-10-20 16:36:47 +01001621 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001622 locations->SetInAt(0, Location::RequiresFpuRegister());
1623 locations->SetOut(Location::SameAsFirstInput());
1624 locations->AddTemp(Location::RequiresRegister());
1625 locations->AddTemp(Location::RequiresFpuRegister());
1626 break;
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001629 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001630 locations->SetOut(Location::SameAsFirstInput());
1631 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001632 break;
1633
1634 default:
1635 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1636 }
1637}
1638
1639void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1640 LocationSummary* locations = neg->GetLocations();
1641 Location out = locations->Out();
1642 Location in = locations->InAt(0);
1643 switch (neg->GetResultType()) {
1644 case Primitive::kPrimInt:
1645 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001646 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001648 break;
1649
1650 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001651 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001652 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001653 __ negl(out.AsRegisterPairLow<Register>());
1654 // Negation is similar to subtraction from zero. The least
1655 // significant byte triggers a borrow when it is different from
1656 // zero; to take it into account, add 1 to the most significant
1657 // byte if the carry flag (CF) is set to 1 after the first NEGL
1658 // operation.
1659 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1660 __ negl(out.AsRegisterPairHigh<Register>());
1661 break;
1662
Roland Levillain5368c212014-11-27 15:03:41 +00001663 case Primitive::kPrimFloat: {
1664 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 Register constant = locations->GetTemp(0).AsRegister<Register>();
1666 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001667 // Implement float negation with an exclusive or with value
1668 // 0x80000000 (mask for bit 31, representing the sign of a
1669 // single-precision floating-point number).
1670 __ movl(constant, Immediate(INT32_C(0x80000000)));
1671 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001673 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001674 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001675
Roland Levillain5368c212014-11-27 15:03:41 +00001676 case Primitive::kPrimDouble: {
1677 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001678 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001679 // Implement double negation with an exclusive or with value
1680 // 0x8000000000000000 (mask for bit 63, representing the sign of
1681 // a double-precision floating-point number).
1682 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001684 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001685 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001686
1687 default:
1688 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1689 }
1690}
1691
Roland Levillaindff1f282014-11-05 14:15:05 +00001692void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001693 Primitive::Type result_type = conversion->GetResultType();
1694 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001695 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001696
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001697 // The float-to-long and double-to-long type conversions rely on a
1698 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001699 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001700 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1701 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001702 ? LocationSummary::kCall
1703 : LocationSummary::kNoCall;
1704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1706
David Brazdilb2bd1c52015-03-25 11:17:37 +00001707 // The Java language does not allow treating boolean as an integral type but
1708 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001709
Roland Levillaindff1f282014-11-05 14:15:05 +00001710 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001711 case Primitive::kPrimByte:
1712 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001713 case Primitive::kPrimBoolean:
1714 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001715 case Primitive::kPrimShort:
1716 case Primitive::kPrimInt:
1717 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001718 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001719 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1720 // Make the output overlap to please the register allocator. This greatly simplifies
1721 // the validation of the linear scan implementation
1722 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001723 break;
1724
1725 default:
1726 LOG(FATAL) << "Unexpected type conversion from " << input_type
1727 << " to " << result_type;
1728 }
1729 break;
1730
Roland Levillain01a8d712014-11-14 16:27:39 +00001731 case Primitive::kPrimShort:
1732 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001733 case Primitive::kPrimBoolean:
1734 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 case Primitive::kPrimByte:
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimChar:
1738 // Processing a Dex `int-to-short' instruction.
1739 locations->SetInAt(0, Location::Any());
1740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1741 break;
1742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillain946e1432014-11-11 17:35:19 +00001749 case Primitive::kPrimInt:
1750 switch (input_type) {
1751 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001752 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001753 locations->SetInAt(0, Location::Any());
1754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1755 break;
1756
1757 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001758 // Processing a Dex `float-to-int' instruction.
1759 locations->SetInAt(0, Location::RequiresFpuRegister());
1760 locations->SetOut(Location::RequiresRegister());
1761 locations->AddTemp(Location::RequiresFpuRegister());
1762 break;
1763
Roland Levillain946e1432014-11-11 17:35:19 +00001764 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001765 // Processing a Dex `double-to-int' instruction.
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetOut(Location::RequiresRegister());
1768 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimLong:
1778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001784 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001785 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 locations->SetInAt(0, Location::RegisterLocation(EAX));
1787 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1788 break;
1789
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001790 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001791 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001792 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001793 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001794 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1795 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1796
Vladimir Marko949c91f2015-01-27 10:48:44 +00001797 // The runtime helper puts the result in EAX, EDX.
1798 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001799 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001800 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillain981e4542014-11-14 11:47:14 +00001808 case Primitive::kPrimChar:
1809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001815 // Processing a Dex `int-to-char' instruction.
1816 locations->SetInAt(0, Location::Any());
1817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 }
1824 break;
1825
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001827 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001828 case Primitive::kPrimBoolean:
1829 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001830 case Primitive::kPrimByte:
1831 case Primitive::kPrimShort:
1832 case Primitive::kPrimInt:
1833 case Primitive::kPrimChar:
1834 // Processing a Dex `int-to-float' instruction.
1835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetOut(Location::RequiresFpuRegister());
1837 break;
1838
1839 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001840 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001841 locations->SetInAt(0, Location::Any());
1842 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001843 break;
1844
Roland Levillaincff13742014-11-17 14:32:17 +00001845 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001846 // Processing a Dex `double-to-float' instruction.
1847 locations->SetInAt(0, Location::RequiresFpuRegister());
1848 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001849 break;
1850
1851 default:
1852 LOG(FATAL) << "Unexpected type conversion from " << input_type
1853 << " to " << result_type;
1854 };
1855 break;
1856
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001859 case Primitive::kPrimBoolean:
1860 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001861 case Primitive::kPrimByte:
1862 case Primitive::kPrimShort:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
1865 // Processing a Dex `int-to-double' instruction.
1866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetOut(Location::RequiresFpuRegister());
1868 break;
1869
1870 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001871 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001872 locations->SetInAt(0, Location::Any());
1873 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001874 break;
1875
Roland Levillaincff13742014-11-17 14:32:17 +00001876 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001877 // Processing a Dex `float-to-double' instruction.
1878 locations->SetInAt(0, Location::RequiresFpuRegister());
1879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001880 break;
1881
1882 default:
1883 LOG(FATAL) << "Unexpected type conversion from " << input_type
1884 << " to " << result_type;
1885 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001886 break;
1887
1888 default:
1889 LOG(FATAL) << "Unexpected type conversion from " << input_type
1890 << " to " << result_type;
1891 }
1892}
1893
1894void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1895 LocationSummary* locations = conversion->GetLocations();
1896 Location out = locations->Out();
1897 Location in = locations->InAt(0);
1898 Primitive::Type result_type = conversion->GetResultType();
1899 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001900 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001902 case Primitive::kPrimByte:
1903 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001904 case Primitive::kPrimBoolean:
1905 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001906 case Primitive::kPrimShort:
1907 case Primitive::kPrimInt:
1908 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001909 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001910 if (in.IsRegister()) {
1911 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001912 } else {
1913 DCHECK(in.GetConstant()->IsIntConstant());
1914 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1915 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1916 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001917 break;
1918
1919 default:
1920 LOG(FATAL) << "Unexpected type conversion from " << input_type
1921 << " to " << result_type;
1922 }
1923 break;
1924
Roland Levillain01a8d712014-11-14 16:27:39 +00001925 case Primitive::kPrimShort:
1926 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001927 case Primitive::kPrimBoolean:
1928 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001929 case Primitive::kPrimByte:
1930 case Primitive::kPrimInt:
1931 case Primitive::kPrimChar:
1932 // Processing a Dex `int-to-short' instruction.
1933 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001935 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001937 } else {
1938 DCHECK(in.GetConstant()->IsIntConstant());
1939 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001941 }
1942 break;
1943
1944 default:
1945 LOG(FATAL) << "Unexpected type conversion from " << input_type
1946 << " to " << result_type;
1947 }
1948 break;
1949
Roland Levillain946e1432014-11-11 17:35:19 +00001950 case Primitive::kPrimInt:
1951 switch (input_type) {
1952 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001953 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001954 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001956 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001958 } else {
1959 DCHECK(in.IsConstant());
1960 DCHECK(in.GetConstant()->IsLongConstant());
1961 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001963 }
1964 break;
1965
Roland Levillain3f8f9362014-12-02 17:45:01 +00001966 case Primitive::kPrimFloat: {
1967 // Processing a Dex `float-to-int' instruction.
1968 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1969 Register output = out.AsRegister<Register>();
1970 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1971 Label done, nan;
1972
1973 __ movl(output, Immediate(kPrimIntMax));
1974 // temp = int-to-float(output)
1975 __ cvtsi2ss(temp, output);
1976 // if input >= temp goto done
1977 __ comiss(input, temp);
1978 __ j(kAboveEqual, &done);
1979 // if input == NaN goto nan
1980 __ j(kUnordered, &nan);
1981 // output = float-to-int-truncate(input)
1982 __ cvttss2si(output, input);
1983 __ jmp(&done);
1984 __ Bind(&nan);
1985 // output = 0
1986 __ xorl(output, output);
1987 __ Bind(&done);
1988 break;
1989 }
1990
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001991 case Primitive::kPrimDouble: {
1992 // Processing a Dex `double-to-int' instruction.
1993 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1994 Register output = out.AsRegister<Register>();
1995 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1996 Label done, nan;
1997
1998 __ movl(output, Immediate(kPrimIntMax));
1999 // temp = int-to-double(output)
2000 __ cvtsi2sd(temp, output);
2001 // if input >= temp goto done
2002 __ comisd(input, temp);
2003 __ j(kAboveEqual, &done);
2004 // if input == NaN goto nan
2005 __ j(kUnordered, &nan);
2006 // output = double-to-int-truncate(input)
2007 __ cvttsd2si(output, input);
2008 __ jmp(&done);
2009 __ Bind(&nan);
2010 // output = 0
2011 __ xorl(output, output);
2012 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002013 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002014 }
Roland Levillain946e1432014-11-11 17:35:19 +00002015
2016 default:
2017 LOG(FATAL) << "Unexpected type conversion from " << input_type
2018 << " to " << result_type;
2019 }
2020 break;
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 case Primitive::kPrimLong:
2023 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002024 case Primitive::kPrimBoolean:
2025 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002026 case Primitive::kPrimByte:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002029 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002030 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002031 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2032 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002033 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002034 __ cdq();
2035 break;
2036
2037 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002038 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002039 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2040 conversion,
2041 conversion->GetDexPc(),
2042 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002043 break;
2044
Roland Levillaindff1f282014-11-05 14:15:05 +00002045 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002046 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002047 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2048 conversion,
2049 conversion->GetDexPc(),
2050 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002051 break;
2052
2053 default:
2054 LOG(FATAL) << "Unexpected type conversion from " << input_type
2055 << " to " << result_type;
2056 }
2057 break;
2058
Roland Levillain981e4542014-11-14 11:47:14 +00002059 case Primitive::kPrimChar:
2060 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002061 case Primitive::kPrimBoolean:
2062 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002063 case Primitive::kPrimByte:
2064 case Primitive::kPrimShort:
2065 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002066 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2067 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002069 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002070 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002071 } else {
2072 DCHECK(in.GetConstant()->IsIntConstant());
2073 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002075 }
2076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 }
2082 break;
2083
Roland Levillaindff1f282014-11-05 14:15:05 +00002084 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002085 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002086 case Primitive::kPrimBoolean:
2087 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002088 case Primitive::kPrimByte:
2089 case Primitive::kPrimShort:
2090 case Primitive::kPrimInt:
2091 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002092 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002094 break;
2095
Roland Levillain6d0e4832014-11-27 18:31:21 +00002096 case Primitive::kPrimLong: {
2097 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002098 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002099
Roland Levillain232ade02015-04-20 15:14:36 +01002100 // Create stack space for the call to
2101 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2102 // TODO: enhance register allocator to ask for stack temporaries.
2103 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2104 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2105 __ subl(ESP, Immediate(adjustment));
2106 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002107
Roland Levillain232ade02015-04-20 15:14:36 +01002108 // Load the value to the FP stack, using temporaries if needed.
2109 PushOntoFPStack(in, 0, adjustment, false, true);
2110
2111 if (out.IsStackSlot()) {
2112 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2113 } else {
2114 __ fstps(Address(ESP, 0));
2115 Location stack_temp = Location::StackSlot(0);
2116 codegen_->Move32(out, stack_temp);
2117 }
2118
2119 // Remove the temporary stack space we allocated.
2120 if (adjustment != 0) {
2121 __ addl(ESP, Immediate(adjustment));
2122 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002123 break;
2124 }
2125
Roland Levillaincff13742014-11-17 14:32:17 +00002126 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002127 // Processing a Dex `double-to-float' instruction.
2128 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002129 break;
2130
2131 default:
2132 LOG(FATAL) << "Unexpected type conversion from " << input_type
2133 << " to " << result_type;
2134 };
2135 break;
2136
Roland Levillaindff1f282014-11-05 14:15:05 +00002137 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002138 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002139 case Primitive::kPrimBoolean:
2140 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002141 case Primitive::kPrimByte:
2142 case Primitive::kPrimShort:
2143 case Primitive::kPrimInt:
2144 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002145 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002147 break;
2148
Roland Levillain647b9ed2014-11-27 12:06:00 +00002149 case Primitive::kPrimLong: {
2150 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002151 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002152
Roland Levillain232ade02015-04-20 15:14:36 +01002153 // Create stack space for the call to
2154 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2155 // TODO: enhance register allocator to ask for stack temporaries.
2156 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2157 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2158 __ subl(ESP, Immediate(adjustment));
2159 }
2160
2161 // Load the value to the FP stack, using temporaries if needed.
2162 PushOntoFPStack(in, 0, adjustment, false, true);
2163
2164 if (out.IsDoubleStackSlot()) {
2165 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2166 } else {
2167 __ fstpl(Address(ESP, 0));
2168 Location stack_temp = Location::DoubleStackSlot(0);
2169 codegen_->Move64(out, stack_temp);
2170 }
2171
2172 // Remove the temporary stack space we allocated.
2173 if (adjustment != 0) {
2174 __ addl(ESP, Immediate(adjustment));
2175 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002176 break;
2177 }
2178
Roland Levillaincff13742014-11-17 14:32:17 +00002179 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002180 // Processing a Dex `float-to-double' instruction.
2181 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002182 break;
2183
2184 default:
2185 LOG(FATAL) << "Unexpected type conversion from " << input_type
2186 << " to " << result_type;
2187 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 break;
2189
2190 default:
2191 LOG(FATAL) << "Unexpected type conversion from " << input_type
2192 << " to " << result_type;
2193 }
2194}
2195
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002196void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002197 LocationSummary* locations =
2198 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002199 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002200 case Primitive::kPrimInt: {
2201 locations->SetInAt(0, Location::RequiresRegister());
2202 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2204 break;
2205 }
2206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetInAt(1, Location::Any());
2210 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 break;
2212 }
2213
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble: {
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002217 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002218 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002219 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002220 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002221
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002222 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002223 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2224 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002225 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002226}
2227
2228void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2229 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 Location first = locations->InAt(0);
2231 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002232 Location out = locations->Out();
2233
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002234 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002235 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002236 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002237 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2238 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002239 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2240 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002241 } else {
2242 __ leal(out.AsRegister<Register>(), Address(
2243 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2244 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002246 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2247 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2248 __ addl(out.AsRegister<Register>(), Immediate(value));
2249 } else {
2250 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2251 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002252 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002253 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002254 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002255 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002256 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002257 }
2258
2259 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002260 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002261 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2262 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002263 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2265 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002266 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002267 } else {
2268 DCHECK(second.IsConstant()) << second;
2269 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2270 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2271 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002273 break;
2274 }
2275
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002276 case Primitive::kPrimFloat: {
2277 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002278 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002279 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2280 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2281 DCHECK(!const_area->NeedsMaterialization());
2282 __ addss(first.AsFpuRegister<XmmRegister>(),
2283 codegen_->LiteralFloatAddress(
2284 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2285 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2286 } else {
2287 DCHECK(second.IsStackSlot());
2288 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002291 }
2292
2293 case Primitive::kPrimDouble: {
2294 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002296 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2297 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2298 DCHECK(!const_area->NeedsMaterialization());
2299 __ addsd(first.AsFpuRegister<XmmRegister>(),
2300 codegen_->LiteralDoubleAddress(
2301 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2302 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2303 } else {
2304 DCHECK(second.IsDoubleStackSlot());
2305 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 }
2307 break;
2308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002309
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002312 }
2313}
2314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002316 LocationSummary* locations =
2317 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002318 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002319 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002321 locations->SetInAt(0, Location::RequiresRegister());
2322 locations->SetInAt(1, Location::Any());
2323 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324 break;
2325 }
Calin Juravle11351682014-10-23 15:38:15 +01002326 case Primitive::kPrimFloat:
2327 case Primitive::kPrimDouble: {
2328 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002329 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002330 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 break;
Calin Juravle11351682014-10-23 15:38:15 +01002332 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002334 default:
Calin Juravle11351682014-10-23 15:38:15 +01002335 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002336 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002337}
2338
2339void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2340 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 Location first = locations->InAt(0);
2342 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002343 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002344 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002345 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002349 __ subl(first.AsRegister<Register>(),
2350 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002351 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002352 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002353 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002354 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 }
2356
2357 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002358 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002359 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2360 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002361 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002362 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002363 __ sbbl(first.AsRegisterPairHigh<Register>(),
2364 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002365 } else {
2366 DCHECK(second.IsConstant()) << second;
2367 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2368 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2369 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002370 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371 break;
2372 }
2373
Calin Juravle11351682014-10-23 15:38:15 +01002374 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002375 if (second.IsFpuRegister()) {
2376 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2377 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2378 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2379 DCHECK(!const_area->NeedsMaterialization());
2380 __ subss(first.AsFpuRegister<XmmRegister>(),
2381 codegen_->LiteralFloatAddress(
2382 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2383 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2384 } else {
2385 DCHECK(second.IsStackSlot());
2386 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388 break;
Calin Juravle11351682014-10-23 15:38:15 +01002389 }
2390
2391 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002392 if (second.IsFpuRegister()) {
2393 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2394 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2395 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2396 DCHECK(!const_area->NeedsMaterialization());
2397 __ subsd(first.AsFpuRegister<XmmRegister>(),
2398 codegen_->LiteralDoubleAddress(
2399 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2400 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2401 } else {
2402 DCHECK(second.IsDoubleStackSlot());
2403 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2404 }
Calin Juravle11351682014-10-23 15:38:15 +01002405 break;
2406 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002408 default:
Calin Juravle11351682014-10-23 15:38:15 +01002409 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002410 }
2411}
2412
Calin Juravle34bacdf2014-10-07 20:23:36 +01002413void LocationsBuilderX86::VisitMul(HMul* mul) {
2414 LocationSummary* locations =
2415 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2416 switch (mul->GetResultType()) {
2417 case Primitive::kPrimInt:
2418 locations->SetInAt(0, Location::RequiresRegister());
2419 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002420 if (mul->InputAt(1)->IsIntConstant()) {
2421 // Can use 3 operand multiply.
2422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423 } else {
2424 locations->SetOut(Location::SameAsFirstInput());
2425 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002426 break;
2427 case Primitive::kPrimLong: {
2428 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002429 locations->SetInAt(1, Location::Any());
2430 locations->SetOut(Location::SameAsFirstInput());
2431 // Needed for imul on 32bits with 64bits output.
2432 locations->AddTemp(Location::RegisterLocation(EAX));
2433 locations->AddTemp(Location::RegisterLocation(EDX));
2434 break;
2435 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002436 case Primitive::kPrimFloat:
2437 case Primitive::kPrimDouble: {
2438 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002439 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002440 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002442 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443
2444 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002445 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002446 }
2447}
2448
2449void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2450 LocationSummary* locations = mul->GetLocations();
2451 Location first = locations->InAt(0);
2452 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002453 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454
2455 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002456 case Primitive::kPrimInt:
2457 // The constant may have ended up in a register, so test explicitly to avoid
2458 // problems where the output may not be the same as the first operand.
2459 if (mul->InputAt(1)->IsIntConstant()) {
2460 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2461 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2462 } else if (second.IsRegister()) {
2463 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002465 } else {
2466 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002467 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002469 }
2470 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471
2472 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002473 Register in1_hi = first.AsRegisterPairHigh<Register>();
2474 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 Register eax = locations->GetTemp(0).AsRegister<Register>();
2476 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002477
2478 DCHECK_EQ(EAX, eax);
2479 DCHECK_EQ(EDX, edx);
2480
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002481 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002482 // output: in1
2483 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2484 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2485 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002486 if (second.IsConstant()) {
2487 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002488
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002489 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2490 int32_t low_value = Low32Bits(value);
2491 int32_t high_value = High32Bits(value);
2492 Immediate low(low_value);
2493 Immediate high(high_value);
2494
2495 __ movl(eax, high);
2496 // eax <- in1.lo * in2.hi
2497 __ imull(eax, in1_lo);
2498 // in1.hi <- in1.hi * in2.lo
2499 __ imull(in1_hi, low);
2500 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2501 __ addl(in1_hi, eax);
2502 // move in2_lo to eax to prepare for double precision
2503 __ movl(eax, low);
2504 // edx:eax <- in1.lo * in2.lo
2505 __ mull(in1_lo);
2506 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2507 __ addl(in1_hi, edx);
2508 // in1.lo <- (in1.lo * in2.lo)[31:0];
2509 __ movl(in1_lo, eax);
2510 } else if (second.IsRegisterPair()) {
2511 Register in2_hi = second.AsRegisterPairHigh<Register>();
2512 Register in2_lo = second.AsRegisterPairLow<Register>();
2513
2514 __ movl(eax, in2_hi);
2515 // eax <- in1.lo * in2.hi
2516 __ imull(eax, in1_lo);
2517 // in1.hi <- in1.hi * in2.lo
2518 __ imull(in1_hi, in2_lo);
2519 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2520 __ addl(in1_hi, eax);
2521 // move in1_lo to eax to prepare for double precision
2522 __ movl(eax, in1_lo);
2523 // edx:eax <- in1.lo * in2.lo
2524 __ mull(in2_lo);
2525 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2526 __ addl(in1_hi, edx);
2527 // in1.lo <- (in1.lo * in2.lo)[31:0];
2528 __ movl(in1_lo, eax);
2529 } else {
2530 DCHECK(second.IsDoubleStackSlot()) << second;
2531 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2532 Address in2_lo(ESP, second.GetStackIndex());
2533
2534 __ movl(eax, in2_hi);
2535 // eax <- in1.lo * in2.hi
2536 __ imull(eax, in1_lo);
2537 // in1.hi <- in1.hi * in2.lo
2538 __ imull(in1_hi, in2_lo);
2539 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2540 __ addl(in1_hi, eax);
2541 // move in1_lo to eax to prepare for double precision
2542 __ movl(eax, in1_lo);
2543 // edx:eax <- in1.lo * in2.lo
2544 __ mull(in2_lo);
2545 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2546 __ addl(in1_hi, edx);
2547 // in1.lo <- (in1.lo * in2.lo)[31:0];
2548 __ movl(in1_lo, eax);
2549 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002550
2551 break;
2552 }
2553
Calin Juravleb5bfa962014-10-21 18:02:24 +01002554 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002555 DCHECK(first.Equals(locations->Out()));
2556 if (second.IsFpuRegister()) {
2557 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2558 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2559 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2560 DCHECK(!const_area->NeedsMaterialization());
2561 __ mulss(first.AsFpuRegister<XmmRegister>(),
2562 codegen_->LiteralFloatAddress(
2563 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2564 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2565 } else {
2566 DCHECK(second.IsStackSlot());
2567 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2568 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002569 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002570 }
2571
2572 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002573 DCHECK(first.Equals(locations->Out()));
2574 if (second.IsFpuRegister()) {
2575 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2576 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2577 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2578 DCHECK(!const_area->NeedsMaterialization());
2579 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2580 codegen_->LiteralDoubleAddress(
2581 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2582 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2583 } else {
2584 DCHECK(second.IsDoubleStackSlot());
2585 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2586 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002587 break;
2588 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002589
2590 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002591 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592 }
2593}
2594
Roland Levillain232ade02015-04-20 15:14:36 +01002595void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2596 uint32_t temp_offset,
2597 uint32_t stack_adjustment,
2598 bool is_fp,
2599 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002600 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002601 DCHECK(!is_wide);
2602 if (is_fp) {
2603 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2604 } else {
2605 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2606 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002607 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002608 DCHECK(is_wide);
2609 if (is_fp) {
2610 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2611 } else {
2612 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2613 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002614 } else {
2615 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002616 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002617 Location stack_temp = Location::StackSlot(temp_offset);
2618 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002619 if (is_fp) {
2620 __ flds(Address(ESP, temp_offset));
2621 } else {
2622 __ filds(Address(ESP, temp_offset));
2623 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002624 } else {
2625 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2626 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002627 if (is_fp) {
2628 __ fldl(Address(ESP, temp_offset));
2629 } else {
2630 __ fildl(Address(ESP, temp_offset));
2631 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002632 }
2633 }
2634}
2635
2636void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2637 Primitive::Type type = rem->GetResultType();
2638 bool is_float = type == Primitive::kPrimFloat;
2639 size_t elem_size = Primitive::ComponentSize(type);
2640 LocationSummary* locations = rem->GetLocations();
2641 Location first = locations->InAt(0);
2642 Location second = locations->InAt(1);
2643 Location out = locations->Out();
2644
2645 // Create stack space for 2 elements.
2646 // TODO: enhance register allocator to ask for stack temporaries.
2647 __ subl(ESP, Immediate(2 * elem_size));
2648
2649 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002650 const bool is_wide = !is_float;
2651 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2652 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002653
2654 // Loop doing FPREM until we stabilize.
2655 Label retry;
2656 __ Bind(&retry);
2657 __ fprem();
2658
2659 // Move FP status to AX.
2660 __ fstsw();
2661
2662 // And see if the argument reduction is complete. This is signaled by the
2663 // C2 FPU flag bit set to 0.
2664 __ andl(EAX, Immediate(kC2ConditionMask));
2665 __ j(kNotEqual, &retry);
2666
2667 // We have settled on the final value. Retrieve it into an XMM register.
2668 // Store FP top of stack to real stack.
2669 if (is_float) {
2670 __ fsts(Address(ESP, 0));
2671 } else {
2672 __ fstl(Address(ESP, 0));
2673 }
2674
2675 // Pop the 2 items from the FP stack.
2676 __ fucompp();
2677
2678 // Load the value from the stack into an XMM register.
2679 DCHECK(out.IsFpuRegister()) << out;
2680 if (is_float) {
2681 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2682 } else {
2683 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2684 }
2685
2686 // And remove the temporary stack space we allocated.
2687 __ addl(ESP, Immediate(2 * elem_size));
2688}
2689
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002690
2691void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2692 DCHECK(instruction->IsDiv() || instruction->IsRem());
2693
2694 LocationSummary* locations = instruction->GetLocations();
2695 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002696 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002697
2698 Register out_register = locations->Out().AsRegister<Register>();
2699 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002700 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002701
2702 DCHECK(imm == 1 || imm == -1);
2703
2704 if (instruction->IsRem()) {
2705 __ xorl(out_register, out_register);
2706 } else {
2707 __ movl(out_register, input_register);
2708 if (imm == -1) {
2709 __ negl(out_register);
2710 }
2711 }
2712}
2713
2714
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002715void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002716 LocationSummary* locations = instruction->GetLocations();
2717
2718 Register out_register = locations->Out().AsRegister<Register>();
2719 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002720 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002721
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002722 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002723 Register num = locations->GetTemp(0).AsRegister<Register>();
2724
2725 __ leal(num, Address(input_register, std::abs(imm) - 1));
2726 __ testl(input_register, input_register);
2727 __ cmovl(kGreaterEqual, num, input_register);
2728 int shift = CTZ(imm);
2729 __ sarl(num, Immediate(shift));
2730
2731 if (imm < 0) {
2732 __ negl(num);
2733 }
2734
2735 __ movl(out_register, num);
2736}
2737
2738void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2739 DCHECK(instruction->IsDiv() || instruction->IsRem());
2740
2741 LocationSummary* locations = instruction->GetLocations();
2742 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2743
2744 Register eax = locations->InAt(0).AsRegister<Register>();
2745 Register out = locations->Out().AsRegister<Register>();
2746 Register num;
2747 Register edx;
2748
2749 if (instruction->IsDiv()) {
2750 edx = locations->GetTemp(0).AsRegister<Register>();
2751 num = locations->GetTemp(1).AsRegister<Register>();
2752 } else {
2753 edx = locations->Out().AsRegister<Register>();
2754 num = locations->GetTemp(0).AsRegister<Register>();
2755 }
2756
2757 DCHECK_EQ(EAX, eax);
2758 DCHECK_EQ(EDX, edx);
2759 if (instruction->IsDiv()) {
2760 DCHECK_EQ(EAX, out);
2761 } else {
2762 DCHECK_EQ(EDX, out);
2763 }
2764
2765 int64_t magic;
2766 int shift;
2767 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2768
2769 Label ndiv;
2770 Label end;
2771 // If numerator is 0, the result is 0, no computation needed.
2772 __ testl(eax, eax);
2773 __ j(kNotEqual, &ndiv);
2774
2775 __ xorl(out, out);
2776 __ jmp(&end);
2777
2778 __ Bind(&ndiv);
2779
2780 // Save the numerator.
2781 __ movl(num, eax);
2782
2783 // EAX = magic
2784 __ movl(eax, Immediate(magic));
2785
2786 // EDX:EAX = magic * numerator
2787 __ imull(num);
2788
2789 if (imm > 0 && magic < 0) {
2790 // EDX += num
2791 __ addl(edx, num);
2792 } else if (imm < 0 && magic > 0) {
2793 __ subl(edx, num);
2794 }
2795
2796 // Shift if needed.
2797 if (shift != 0) {
2798 __ sarl(edx, Immediate(shift));
2799 }
2800
2801 // EDX += 1 if EDX < 0
2802 __ movl(eax, edx);
2803 __ shrl(edx, Immediate(31));
2804 __ addl(edx, eax);
2805
2806 if (instruction->IsRem()) {
2807 __ movl(eax, num);
2808 __ imull(edx, Immediate(imm));
2809 __ subl(eax, edx);
2810 __ movl(edx, eax);
2811 } else {
2812 __ movl(eax, edx);
2813 }
2814 __ Bind(&end);
2815}
2816
Calin Juravlebacfec32014-11-14 15:54:36 +00002817void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2818 DCHECK(instruction->IsDiv() || instruction->IsRem());
2819
2820 LocationSummary* locations = instruction->GetLocations();
2821 Location out = locations->Out();
2822 Location first = locations->InAt(0);
2823 Location second = locations->InAt(1);
2824 bool is_div = instruction->IsDiv();
2825
2826 switch (instruction->GetResultType()) {
2827 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 DCHECK_EQ(EAX, first.AsRegister<Register>());
2829 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002830
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002831 if (instruction->InputAt(1)->IsIntConstant()) {
2832 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002833
2834 if (imm == 0) {
2835 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2836 } else if (imm == 1 || imm == -1) {
2837 DivRemOneOrMinusOne(instruction);
2838 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002839 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002840 } else {
2841 DCHECK(imm <= -2 || imm >= 2);
2842 GenerateDivRemWithAnyConstant(instruction);
2843 }
2844 } else {
2845 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002846 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002847 is_div);
2848 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002849
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002850 Register second_reg = second.AsRegister<Register>();
2851 // 0x80000000/-1 triggers an arithmetic exception!
2852 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2853 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002854
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002855 __ cmpl(second_reg, Immediate(-1));
2856 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002857
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002858 // edx:eax <- sign-extended of eax
2859 __ cdq();
2860 // eax = quotient, edx = remainder
2861 __ idivl(second_reg);
2862 __ Bind(slow_path->GetExitLabel());
2863 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002864 break;
2865 }
2866
2867 case Primitive::kPrimLong: {
2868 InvokeRuntimeCallingConvention calling_convention;
2869 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2870 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2871 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2872 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2873 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2874 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2875
2876 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002877 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2878 instruction,
2879 instruction->GetDexPc(),
2880 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002881 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002882 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2883 instruction,
2884 instruction->GetDexPc(),
2885 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002886 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002887 break;
2888 }
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2892 }
2893}
2894
Calin Juravle7c4954d2014-10-28 16:57:40 +00002895void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002896 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002897 ? LocationSummary::kCall
2898 : LocationSummary::kNoCall;
2899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2900
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002902 case Primitive::kPrimInt: {
2903 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002904 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002905 locations->SetOut(Location::SameAsFirstInput());
2906 // Intel uses edx:eax as the dividend.
2907 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002908 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2909 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2910 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002911 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002912 locations->AddTemp(Location::RequiresRegister());
2913 }
Calin Juravled0d48522014-11-04 16:40:20 +00002914 break;
2915 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002916 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002917 InvokeRuntimeCallingConvention calling_convention;
2918 locations->SetInAt(0, Location::RegisterPairLocation(
2919 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2920 locations->SetInAt(1, Location::RegisterPairLocation(
2921 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2922 // Runtime helper puts the result in EAX, EDX.
2923 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002924 break;
2925 }
2926 case Primitive::kPrimFloat:
2927 case Primitive::kPrimDouble: {
2928 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002929 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002930 locations->SetOut(Location::SameAsFirstInput());
2931 break;
2932 }
2933
2934 default:
2935 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2936 }
2937}
2938
2939void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2940 LocationSummary* locations = div->GetLocations();
2941 Location first = locations->InAt(0);
2942 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002943
2944 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002945 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002946 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002947 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002948 break;
2949 }
2950
2951 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002952 if (second.IsFpuRegister()) {
2953 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2954 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2955 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2956 DCHECK(!const_area->NeedsMaterialization());
2957 __ divss(first.AsFpuRegister<XmmRegister>(),
2958 codegen_->LiteralFloatAddress(
2959 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2960 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2961 } else {
2962 DCHECK(second.IsStackSlot());
2963 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2964 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002965 break;
2966 }
2967
2968 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002969 if (second.IsFpuRegister()) {
2970 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2971 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2972 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2973 DCHECK(!const_area->NeedsMaterialization());
2974 __ divsd(first.AsFpuRegister<XmmRegister>(),
2975 codegen_->LiteralDoubleAddress(
2976 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2977 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2978 } else {
2979 DCHECK(second.IsDoubleStackSlot());
2980 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2981 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002982 break;
2983 }
2984
2985 default:
2986 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2987 }
2988}
2989
Calin Juravlebacfec32014-11-14 15:54:36 +00002990void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002991 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002992
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002993 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2994 ? LocationSummary::kCall
2995 : LocationSummary::kNoCall;
2996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002997
Calin Juravled2ec87d2014-12-08 14:24:46 +00002998 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002999 case Primitive::kPrimInt: {
3000 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003001 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003002 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3004 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3005 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003006 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003007 locations->AddTemp(Location::RequiresRegister());
3008 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003009 break;
3010 }
3011 case Primitive::kPrimLong: {
3012 InvokeRuntimeCallingConvention calling_convention;
3013 locations->SetInAt(0, Location::RegisterPairLocation(
3014 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3015 locations->SetInAt(1, Location::RegisterPairLocation(
3016 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3017 // Runtime helper puts the result in EAX, EDX.
3018 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3019 break;
3020 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003021 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003022 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003023 locations->SetInAt(0, Location::Any());
3024 locations->SetInAt(1, Location::Any());
3025 locations->SetOut(Location::RequiresFpuRegister());
3026 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003027 break;
3028 }
3029
3030 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003031 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003032 }
3033}
3034
3035void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3036 Primitive::Type type = rem->GetResultType();
3037 switch (type) {
3038 case Primitive::kPrimInt:
3039 case Primitive::kPrimLong: {
3040 GenerateDivRemIntegral(rem);
3041 break;
3042 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003043 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003044 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003045 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003046 break;
3047 }
3048 default:
3049 LOG(FATAL) << "Unexpected rem type " << type;
3050 }
3051}
3052
Calin Juravled0d48522014-11-04 16:40:20 +00003053void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3054 LocationSummary* locations =
3055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003056 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003057 case Primitive::kPrimByte:
3058 case Primitive::kPrimChar:
3059 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003060 case Primitive::kPrimInt: {
3061 locations->SetInAt(0, Location::Any());
3062 break;
3063 }
3064 case Primitive::kPrimLong: {
3065 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3066 if (!instruction->IsConstant()) {
3067 locations->AddTemp(Location::RequiresRegister());
3068 }
3069 break;
3070 }
3071 default:
3072 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3073 }
Calin Juravled0d48522014-11-04 16:40:20 +00003074 if (instruction->HasUses()) {
3075 locations->SetOut(Location::SameAsFirstInput());
3076 }
3077}
3078
3079void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3080 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3081 codegen_->AddSlowPath(slow_path);
3082
3083 LocationSummary* locations = instruction->GetLocations();
3084 Location value = locations->InAt(0);
3085
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003086 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003087 case Primitive::kPrimByte:
3088 case Primitive::kPrimChar:
3089 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003090 case Primitive::kPrimInt: {
3091 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003092 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003093 __ j(kEqual, slow_path->GetEntryLabel());
3094 } else if (value.IsStackSlot()) {
3095 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3096 __ j(kEqual, slow_path->GetEntryLabel());
3097 } else {
3098 DCHECK(value.IsConstant()) << value;
3099 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3100 __ jmp(slow_path->GetEntryLabel());
3101 }
3102 }
3103 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003104 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003105 case Primitive::kPrimLong: {
3106 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003107 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003108 __ movl(temp, value.AsRegisterPairLow<Register>());
3109 __ orl(temp, value.AsRegisterPairHigh<Register>());
3110 __ j(kEqual, slow_path->GetEntryLabel());
3111 } else {
3112 DCHECK(value.IsConstant()) << value;
3113 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3114 __ jmp(slow_path->GetEntryLabel());
3115 }
3116 }
3117 break;
3118 }
3119 default:
3120 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003121 }
Calin Juravled0d48522014-11-04 16:40:20 +00003122}
3123
Calin Juravle9aec02f2014-11-18 23:06:35 +00003124void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3125 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3126
3127 LocationSummary* locations =
3128 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3129
3130 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003131 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003132 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003133 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003134 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003135 // The shift count needs to be in CL or a constant.
3136 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003137 locations->SetOut(Location::SameAsFirstInput());
3138 break;
3139 }
3140 default:
3141 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3142 }
3143}
3144
3145void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3146 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3147
3148 LocationSummary* locations = op->GetLocations();
3149 Location first = locations->InAt(0);
3150 Location second = locations->InAt(1);
3151 DCHECK(first.Equals(locations->Out()));
3152
3153 switch (op->GetResultType()) {
3154 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003155 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003156 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003157 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003159 DCHECK_EQ(ECX, second_reg);
3160 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003161 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003162 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003163 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003164 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003165 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003166 }
3167 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003168 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3169 if (shift == 0) {
3170 return;
3171 }
3172 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003173 if (op->IsShl()) {
3174 __ shll(first_reg, imm);
3175 } else if (op->IsShr()) {
3176 __ sarl(first_reg, imm);
3177 } else {
3178 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003179 }
3180 }
3181 break;
3182 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003183 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003184 if (second.IsRegister()) {
3185 Register second_reg = second.AsRegister<Register>();
3186 DCHECK_EQ(ECX, second_reg);
3187 if (op->IsShl()) {
3188 GenerateShlLong(first, second_reg);
3189 } else if (op->IsShr()) {
3190 GenerateShrLong(first, second_reg);
3191 } else {
3192 GenerateUShrLong(first, second_reg);
3193 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003194 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003195 // Shift by a constant.
3196 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3197 // Nothing to do if the shift is 0, as the input is already the output.
3198 if (shift != 0) {
3199 if (op->IsShl()) {
3200 GenerateShlLong(first, shift);
3201 } else if (op->IsShr()) {
3202 GenerateShrLong(first, shift);
3203 } else {
3204 GenerateUShrLong(first, shift);
3205 }
3206 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003207 }
3208 break;
3209 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003210 default:
3211 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3212 }
3213}
3214
Mark P Mendell73945692015-04-29 14:56:17 +00003215void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3216 Register low = loc.AsRegisterPairLow<Register>();
3217 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003218 if (shift == 1) {
3219 // This is just an addition.
3220 __ addl(low, low);
3221 __ adcl(high, high);
3222 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003223 // Shift by 32 is easy. High gets low, and low gets 0.
3224 codegen_->EmitParallelMoves(
3225 loc.ToLow(),
3226 loc.ToHigh(),
3227 Primitive::kPrimInt,
3228 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3229 loc.ToLow(),
3230 Primitive::kPrimInt);
3231 } else if (shift > 32) {
3232 // Low part becomes 0. High part is low part << (shift-32).
3233 __ movl(high, low);
3234 __ shll(high, Immediate(shift - 32));
3235 __ xorl(low, low);
3236 } else {
3237 // Between 1 and 31.
3238 __ shld(high, low, Immediate(shift));
3239 __ shll(low, Immediate(shift));
3240 }
3241}
3242
Calin Juravle9aec02f2014-11-18 23:06:35 +00003243void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3244 Label done;
3245 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3246 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3247 __ testl(shifter, Immediate(32));
3248 __ j(kEqual, &done);
3249 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3250 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3251 __ Bind(&done);
3252}
3253
Mark P Mendell73945692015-04-29 14:56:17 +00003254void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3255 Register low = loc.AsRegisterPairLow<Register>();
3256 Register high = loc.AsRegisterPairHigh<Register>();
3257 if (shift == 32) {
3258 // Need to copy the sign.
3259 DCHECK_NE(low, high);
3260 __ movl(low, high);
3261 __ sarl(high, Immediate(31));
3262 } else if (shift > 32) {
3263 DCHECK_NE(low, high);
3264 // High part becomes sign. Low part is shifted by shift - 32.
3265 __ movl(low, high);
3266 __ sarl(high, Immediate(31));
3267 __ sarl(low, Immediate(shift - 32));
3268 } else {
3269 // Between 1 and 31.
3270 __ shrd(low, high, Immediate(shift));
3271 __ sarl(high, Immediate(shift));
3272 }
3273}
3274
Calin Juravle9aec02f2014-11-18 23:06:35 +00003275void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3276 Label done;
3277 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3278 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3279 __ testl(shifter, Immediate(32));
3280 __ j(kEqual, &done);
3281 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3282 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3283 __ Bind(&done);
3284}
3285
Mark P Mendell73945692015-04-29 14:56:17 +00003286void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3287 Register low = loc.AsRegisterPairLow<Register>();
3288 Register high = loc.AsRegisterPairHigh<Register>();
3289 if (shift == 32) {
3290 // Shift by 32 is easy. Low gets high, and high gets 0.
3291 codegen_->EmitParallelMoves(
3292 loc.ToHigh(),
3293 loc.ToLow(),
3294 Primitive::kPrimInt,
3295 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3296 loc.ToHigh(),
3297 Primitive::kPrimInt);
3298 } else if (shift > 32) {
3299 // Low part is high >> (shift - 32). High part becomes 0.
3300 __ movl(low, high);
3301 __ shrl(low, Immediate(shift - 32));
3302 __ xorl(high, high);
3303 } else {
3304 // Between 1 and 31.
3305 __ shrd(low, high, Immediate(shift));
3306 __ shrl(high, Immediate(shift));
3307 }
3308}
3309
Calin Juravle9aec02f2014-11-18 23:06:35 +00003310void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3311 Label done;
3312 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3313 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3314 __ testl(shifter, Immediate(32));
3315 __ j(kEqual, &done);
3316 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3317 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3318 __ Bind(&done);
3319}
3320
3321void LocationsBuilderX86::VisitShl(HShl* shl) {
3322 HandleShift(shl);
3323}
3324
3325void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3326 HandleShift(shl);
3327}
3328
3329void LocationsBuilderX86::VisitShr(HShr* shr) {
3330 HandleShift(shr);
3331}
3332
3333void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3334 HandleShift(shr);
3335}
3336
3337void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3338 HandleShift(ushr);
3339}
3340
3341void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3342 HandleShift(ushr);
3343}
3344
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003345void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003346 LocationSummary* locations =
3347 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003348 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003349 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003350 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003351 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003352}
3353
3354void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3355 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003356 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003357 // Note: if heap poisoning is enabled, the entry point takes cares
3358 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003359 codegen_->InvokeRuntime(
3360 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3361 instruction,
3362 instruction->GetDexPc(),
3363 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003364 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003365}
3366
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003367void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3370 locations->SetOut(Location::RegisterLocation(EAX));
3371 InvokeRuntimeCallingConvention calling_convention;
3372 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003373 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003374 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003375}
3376
3377void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3378 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003379 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3380
Roland Levillain4d027112015-07-01 15:41:14 +01003381 // Note: if heap poisoning is enabled, the entry point takes cares
3382 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003383 codegen_->InvokeRuntime(
3384 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3385 instruction,
3386 instruction->GetDexPc(),
3387 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003388 DCHECK(!codegen_->IsLeafMethod());
3389}
3390
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003391void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003394 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3395 if (location.IsStackSlot()) {
3396 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3397 } else if (location.IsDoubleStackSlot()) {
3398 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003399 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003400 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003401}
3402
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003403void InstructionCodeGeneratorX86::VisitParameterValue(
3404 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3405}
3406
3407void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3408 LocationSummary* locations =
3409 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3410 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3411}
3412
3413void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003414}
3415
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003416void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003417 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003418 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003419 locations->SetInAt(0, Location::RequiresRegister());
3420 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003421}
3422
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003423void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3424 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003425 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003426 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003427 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003428 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003429 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003430 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003431 break;
3432
3433 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003434 __ notl(out.AsRegisterPairLow<Register>());
3435 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003436 break;
3437
3438 default:
3439 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3440 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003441}
3442
David Brazdil66d126e2015-04-03 16:02:44 +01003443void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3444 LocationSummary* locations =
3445 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3446 locations->SetInAt(0, Location::RequiresRegister());
3447 locations->SetOut(Location::SameAsFirstInput());
3448}
3449
3450void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003451 LocationSummary* locations = bool_not->GetLocations();
3452 Location in = locations->InAt(0);
3453 Location out = locations->Out();
3454 DCHECK(in.Equals(out));
3455 __ xorl(out.AsRegister<Register>(), Immediate(1));
3456}
3457
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003458void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003461 switch (compare->InputAt(0)->GetType()) {
3462 case Primitive::kPrimLong: {
3463 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003464 locations->SetInAt(1, Location::Any());
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3466 break;
3467 }
3468 case Primitive::kPrimFloat:
3469 case Primitive::kPrimDouble: {
3470 locations->SetInAt(0, Location::RequiresFpuRegister());
3471 locations->SetInAt(1, Location::RequiresFpuRegister());
3472 locations->SetOut(Location::RequiresRegister());
3473 break;
3474 }
3475 default:
3476 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3477 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003478}
3479
3480void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003481 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003483 Location left = locations->InAt(0);
3484 Location right = locations->InAt(1);
3485
3486 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003487 switch (compare->InputAt(0)->GetType()) {
3488 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003489 Register left_low = left.AsRegisterPairLow<Register>();
3490 Register left_high = left.AsRegisterPairHigh<Register>();
3491 int32_t val_low = 0;
3492 int32_t val_high = 0;
3493 bool right_is_const = false;
3494
3495 if (right.IsConstant()) {
3496 DCHECK(right.GetConstant()->IsLongConstant());
3497 right_is_const = true;
3498 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3499 val_low = Low32Bits(val);
3500 val_high = High32Bits(val);
3501 }
3502
Calin Juravleddb7df22014-11-25 20:56:51 +00003503 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003504 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003505 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003506 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003507 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003508 DCHECK(right_is_const) << right;
3509 if (val_high == 0) {
3510 __ testl(left_high, left_high);
3511 } else {
3512 __ cmpl(left_high, Immediate(val_high));
3513 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003514 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003515 __ j(kLess, &less); // Signed compare.
3516 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003517 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003518 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003519 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003520 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003521 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003522 DCHECK(right_is_const) << right;
3523 if (val_low == 0) {
3524 __ testl(left_low, left_low);
3525 } else {
3526 __ cmpl(left_low, Immediate(val_low));
3527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003529 break;
3530 }
3531 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003532 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003533 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3534 break;
3535 }
3536 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003538 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003539 break;
3540 }
3541 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003542 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003543 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003544 __ movl(out, Immediate(0));
3545 __ j(kEqual, &done);
3546 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3547
3548 __ Bind(&greater);
3549 __ movl(out, Immediate(1));
3550 __ jmp(&done);
3551
3552 __ Bind(&less);
3553 __ movl(out, Immediate(-1));
3554
3555 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003556}
3557
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003558void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003561 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3562 locations->SetInAt(i, Location::Any());
3563 }
3564 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003565}
3566
3567void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003568 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003569 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003570}
3571
Calin Juravle52c48962014-12-16 17:02:57 +00003572void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3573 /*
3574 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3575 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3576 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3577 */
3578 switch (kind) {
3579 case MemBarrierKind::kAnyAny: {
3580 __ mfence();
3581 break;
3582 }
3583 case MemBarrierKind::kAnyStore:
3584 case MemBarrierKind::kLoadAny:
3585 case MemBarrierKind::kStoreStore: {
3586 // nop
3587 break;
3588 }
3589 default:
3590 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003591 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003592}
3593
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003594
Vladimir Marko58155012015-08-19 12:49:41 +00003595void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3596 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3597 switch (invoke->GetMethodLoadKind()) {
3598 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3599 // temp = thread->string_init_entrypoint
3600 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3601 break;
3602 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3603 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3604 break;
3605 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3606 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3607 break;
3608 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3609 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3610 method_patches_.emplace_back(invoke->GetTargetMethod());
3611 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3612 break;
3613 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3614 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3615 FALLTHROUGH_INTENDED;
3616 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3617 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3618 Register method_reg;
3619 Register reg = temp.AsRegister<Register>();
3620 if (current_method.IsRegister()) {
3621 method_reg = current_method.AsRegister<Register>();
3622 } else {
3623 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3624 DCHECK(!current_method.IsValid());
3625 method_reg = reg;
3626 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3627 }
3628 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003629 __ movl(reg, Address(method_reg,
3630 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003631 // temp = temp[index_in_cache]
3632 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3633 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3634 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003635 }
Vladimir Marko58155012015-08-19 12:49:41 +00003636 }
3637
3638 switch (invoke->GetCodePtrLocation()) {
3639 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3640 __ call(GetFrameEntryLabel());
3641 break;
3642 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3643 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3644 Label* label = &relative_call_patches_.back().label;
3645 __ call(label); // Bind to the patch label, override at link time.
3646 __ Bind(label); // Bind the label at the end of the "call" insn.
3647 break;
3648 }
3649 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3650 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3651 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3652 // (Though the direct CALL ptr16:32 is available for consideration).
3653 FALLTHROUGH_INTENDED;
3654 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3655 // (callee_method + offset_of_quick_compiled_code)()
3656 __ call(Address(callee_method.AsRegister<Register>(),
3657 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3658 kX86WordSize).Int32Value()));
3659 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003660 }
3661
3662 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003663}
3664
Vladimir Marko58155012015-08-19 12:49:41 +00003665void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3666 DCHECK(linker_patches->empty());
3667 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3668 for (const MethodPatchInfo<Label>& info : method_patches_) {
3669 // The label points to the end of the "movl" insn but the literal offset for method
3670 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3671 uint32_t literal_offset = info.label.Position() - 4;
3672 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3673 info.target_method.dex_file,
3674 info.target_method.dex_method_index));
3675 }
3676 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3677 // The label points to the end of the "call" insn but the literal offset for method
3678 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3679 uint32_t literal_offset = info.label.Position() - 4;
3680 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3681 info.target_method.dex_file,
3682 info.target_method.dex_method_index));
3683 }
3684}
3685
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003686void CodeGeneratorX86::MarkGCCard(Register temp,
3687 Register card,
3688 Register object,
3689 Register value,
3690 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003692 if (value_can_be_null) {
3693 __ testl(value, value);
3694 __ j(kEqual, &is_null);
3695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003696 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3697 __ movl(temp, object);
3698 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003699 __ movb(Address(temp, card, TIMES_1, 0),
3700 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003701 if (value_can_be_null) {
3702 __ Bind(&is_null);
3703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704}
3705
Calin Juravle52c48962014-12-16 17:02:57 +00003706void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3707 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003708 LocationSummary* locations =
3709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003710 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003711
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003712 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3713 locations->SetOut(Location::RequiresFpuRegister());
3714 } else {
3715 // The output overlaps in case of long: we don't want the low move to overwrite
3716 // the object's location.
3717 locations->SetOut(Location::RequiresRegister(),
3718 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3719 : Location::kNoOutputOverlap);
3720 }
Calin Juravle52c48962014-12-16 17:02:57 +00003721
3722 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3723 // Long values can be loaded atomically into an XMM using movsd.
3724 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3725 // and then copy the XMM into the output 32bits at a time).
3726 locations->AddTemp(Location::RequiresFpuRegister());
3727 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003728}
3729
Calin Juravle52c48962014-12-16 17:02:57 +00003730void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3731 const FieldInfo& field_info) {
3732 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733
Calin Juravle52c48962014-12-16 17:02:57 +00003734 LocationSummary* locations = instruction->GetLocations();
3735 Register base = locations->InAt(0).AsRegister<Register>();
3736 Location out = locations->Out();
3737 bool is_volatile = field_info.IsVolatile();
3738 Primitive::Type field_type = field_info.GetFieldType();
3739 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3740
3741 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003742 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003743 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003744 break;
3745 }
3746
3747 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003748 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003749 break;
3750 }
3751
3752 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003753 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003754 break;
3755 }
3756
3757 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003758 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003759 break;
3760 }
3761
3762 case Primitive::kPrimInt:
3763 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003764 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003765 break;
3766 }
3767
3768 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003769 if (is_volatile) {
3770 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3771 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003772 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003773 __ movd(out.AsRegisterPairLow<Register>(), temp);
3774 __ psrlq(temp, Immediate(32));
3775 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3776 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003777 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003778 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003780 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3781 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003782 break;
3783 }
3784
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003785 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003786 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003787 break;
3788 }
3789
3790 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003791 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003792 break;
3793 }
3794
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003795 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003796 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003797 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003798 }
Calin Juravle52c48962014-12-16 17:02:57 +00003799
Calin Juravle77520bc2015-01-12 18:45:46 +00003800 // Longs are handled in the switch.
3801 if (field_type != Primitive::kPrimLong) {
3802 codegen_->MaybeRecordImplicitNullCheck(instruction);
3803 }
3804
Calin Juravle52c48962014-12-16 17:02:57 +00003805 if (is_volatile) {
3806 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3807 }
Roland Levillain4d027112015-07-01 15:41:14 +01003808
3809 if (field_type == Primitive::kPrimNot) {
3810 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3811 }
Calin Juravle52c48962014-12-16 17:02:57 +00003812}
3813
3814void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3815 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3816
3817 LocationSummary* locations =
3818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3819 locations->SetInAt(0, Location::RequiresRegister());
3820 bool is_volatile = field_info.IsVolatile();
3821 Primitive::Type field_type = field_info.GetFieldType();
3822 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3823 || (field_type == Primitive::kPrimByte);
3824
3825 // The register allocator does not support multiple
3826 // inputs that die at entry with one in a specific register.
3827 if (is_byte_type) {
3828 // Ensure the value is in a byte register.
3829 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003830 } else if (Primitive::IsFloatingPointType(field_type)) {
3831 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003832 } else {
3833 locations->SetInAt(1, Location::RequiresRegister());
3834 }
Calin Juravle52c48962014-12-16 17:02:57 +00003835 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003836 // Temporary registers for the write barrier.
3837 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003838 // Ensure the card is in a byte register.
3839 locations->AddTemp(Location::RegisterLocation(ECX));
3840 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3841 // 64bits value can be atomically written to an address with movsd and an XMM register.
3842 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3843 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3844 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3845 // isolated cases when we need this it isn't worth adding the extra complexity.
3846 locations->AddTemp(Location::RequiresFpuRegister());
3847 locations->AddTemp(Location::RequiresFpuRegister());
3848 }
3849}
3850
3851void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003852 const FieldInfo& field_info,
3853 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003854 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3855
3856 LocationSummary* locations = instruction->GetLocations();
3857 Register base = locations->InAt(0).AsRegister<Register>();
3858 Location value = locations->InAt(1);
3859 bool is_volatile = field_info.IsVolatile();
3860 Primitive::Type field_type = field_info.GetFieldType();
3861 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003862 bool needs_write_barrier =
3863 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003864
3865 if (is_volatile) {
3866 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3867 }
3868
3869 switch (field_type) {
3870 case Primitive::kPrimBoolean:
3871 case Primitive::kPrimByte: {
3872 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3873 break;
3874 }
3875
3876 case Primitive::kPrimShort:
3877 case Primitive::kPrimChar: {
3878 __ movw(Address(base, offset), value.AsRegister<Register>());
3879 break;
3880 }
3881
3882 case Primitive::kPrimInt:
3883 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003884 if (kPoisonHeapReferences && needs_write_barrier) {
3885 // Note that in the case where `value` is a null reference,
3886 // we do not enter this block, as the reference does not
3887 // need poisoning.
3888 DCHECK_EQ(field_type, Primitive::kPrimNot);
3889 Register temp = locations->GetTemp(0).AsRegister<Register>();
3890 __ movl(temp, value.AsRegister<Register>());
3891 __ PoisonHeapReference(temp);
3892 __ movl(Address(base, offset), temp);
3893 } else {
3894 __ movl(Address(base, offset), value.AsRegister<Register>());
3895 }
Calin Juravle52c48962014-12-16 17:02:57 +00003896 break;
3897 }
3898
3899 case Primitive::kPrimLong: {
3900 if (is_volatile) {
3901 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3902 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3903 __ movd(temp1, value.AsRegisterPairLow<Register>());
3904 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3905 __ punpckldq(temp1, temp2);
3906 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003907 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003908 } else {
3909 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003910 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003911 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3912 }
3913 break;
3914 }
3915
3916 case Primitive::kPrimFloat: {
3917 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3918 break;
3919 }
3920
3921 case Primitive::kPrimDouble: {
3922 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3923 break;
3924 }
3925
3926 case Primitive::kPrimVoid:
3927 LOG(FATAL) << "Unreachable type " << field_type;
3928 UNREACHABLE();
3929 }
3930
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 // Longs are handled in the switch.
3932 if (field_type != Primitive::kPrimLong) {
3933 codegen_->MaybeRecordImplicitNullCheck(instruction);
3934 }
3935
Roland Levillain4d027112015-07-01 15:41:14 +01003936 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 Register temp = locations->GetTemp(0).AsRegister<Register>();
3938 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003939 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003940 }
3941
Calin Juravle52c48962014-12-16 17:02:57 +00003942 if (is_volatile) {
3943 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3944 }
3945}
3946
3947void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3948 HandleFieldGet(instruction, instruction->GetFieldInfo());
3949}
3950
3951void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3952 HandleFieldGet(instruction, instruction->GetFieldInfo());
3953}
3954
3955void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3956 HandleFieldSet(instruction, instruction->GetFieldInfo());
3957}
3958
3959void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003960 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003961}
3962
3963void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3964 HandleFieldSet(instruction, instruction->GetFieldInfo());
3965}
3966
3967void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003968 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003969}
3970
3971void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3972 HandleFieldGet(instruction, instruction->GetFieldInfo());
3973}
3974
3975void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3976 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003977}
3978
3979void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations =
3981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003982 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3983 ? Location::RequiresRegister()
3984 : Location::Any();
3985 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003986 if (instruction->HasUses()) {
3987 locations->SetOut(Location::SameAsFirstInput());
3988 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989}
3990
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003991void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003992 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3993 return;
3994 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003995 LocationSummary* locations = instruction->GetLocations();
3996 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003997
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003998 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3999 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4000}
4001
4002void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004003 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004004 codegen_->AddSlowPath(slow_path);
4005
4006 LocationSummary* locations = instruction->GetLocations();
4007 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008
4009 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004010 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004011 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004012 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004013 } else {
4014 DCHECK(obj.IsConstant()) << obj;
4015 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
4016 __ jmp(slow_path->GetEntryLabel());
4017 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004018 }
4019 __ j(kEqual, slow_path->GetEntryLabel());
4020}
4021
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004022void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
4023 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
4024 GenerateImplicitNullCheck(instruction);
4025 } else {
4026 GenerateExplicitNullCheck(instruction);
4027 }
4028}
4029
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004030void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 LocationSummary* locations =
4032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004033 locations->SetInAt(0, Location::RequiresRegister());
4034 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004035 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4036 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4037 } else {
4038 // The output overlaps in case of long: we don't want the low move to overwrite
4039 // the array's location.
4040 locations->SetOut(Location::RequiresRegister(),
4041 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4042 : Location::kNoOutputOverlap);
4043 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004044}
4045
4046void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4047 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004048 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004049 Location index = locations->InAt(1);
4050
Calin Juravle77520bc2015-01-12 18:45:46 +00004051 Primitive::Type type = instruction->GetType();
4052 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004053 case Primitive::kPrimBoolean: {
4054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 if (index.IsConstant()) {
4057 __ movzxb(out, Address(obj,
4058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4059 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004060 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004061 }
4062 break;
4063 }
4064
4065 case Primitive::kPrimByte: {
4066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 if (index.IsConstant()) {
4069 __ movsxb(out, Address(obj,
4070 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4071 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004073 }
4074 break;
4075 }
4076
4077 case Primitive::kPrimShort: {
4078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004080 if (index.IsConstant()) {
4081 __ movsxw(out, Address(obj,
4082 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4083 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085 }
4086 break;
4087 }
4088
4089 case Primitive::kPrimChar: {
4090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004092 if (index.IsConstant()) {
4093 __ movzxw(out, Address(obj,
4094 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4095 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004096 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004097 }
4098 break;
4099 }
4100
4101 case Primitive::kPrimInt:
4102 case Primitive::kPrimNot: {
4103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004104 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 if (index.IsConstant()) {
4106 __ movl(out, Address(obj,
4107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004110 }
4111 break;
4112 }
4113
4114 case Primitive::kPrimLong: {
4115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004116 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004117 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 if (index.IsConstant()) {
4119 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004120 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004121 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004122 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004123 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004124 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004125 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004127 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 }
4130 break;
4131 }
4132
Mark Mendell7c8d0092015-01-26 11:21:33 -05004133 case Primitive::kPrimFloat: {
4134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4135 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4136 if (index.IsConstant()) {
4137 __ movss(out, Address(obj,
4138 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4139 } else {
4140 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4141 }
4142 break;
4143 }
4144
4145 case Primitive::kPrimDouble: {
4146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4147 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4148 if (index.IsConstant()) {
4149 __ movsd(out, Address(obj,
4150 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4151 } else {
4152 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4153 }
4154 break;
4155 }
4156
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004157 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004158 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004159 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004160 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004161
4162 if (type != Primitive::kPrimLong) {
4163 codegen_->MaybeRecordImplicitNullCheck(instruction);
4164 }
Roland Levillain4d027112015-07-01 15:41:14 +01004165
4166 if (type == Primitive::kPrimNot) {
4167 Register out = locations->Out().AsRegister<Register>();
4168 __ MaybeUnpoisonHeapReference(out);
4169 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004170}
4171
4172void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004173 // This location builder might end up asking to up to four registers, which is
4174 // not currently possible for baseline. The situation in which we need four
4175 // registers cannot be met by baseline though, because it has not run any
4176 // optimization.
4177
Nicolas Geoffray39468442014-09-02 15:17:15 +01004178 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004179 bool needs_write_barrier =
4180 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4181
Mark Mendell5f874182015-03-04 15:42:45 -05004182 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004183
Nicolas Geoffray39468442014-09-02 15:17:15 +01004184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4185 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004186 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004187
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004188 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004189 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004190 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4191 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4192 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004193 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004194 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4195 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004196 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004197 // In case of a byte operation, the register allocator does not support multiple
4198 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004199 locations->SetInAt(0, Location::RequiresRegister());
4200 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004201 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004202 // Ensure the value is in a byte register.
4203 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004204 } else if (Primitive::IsFloatingPointType(value_type)) {
4205 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004207 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004208 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004210 // Temporary registers for the write barrier.
4211 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004212 // Ensure the card is in a byte register.
4213 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004214 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216}
4217
4218void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4219 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004220 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004222 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004223 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004224 bool needs_runtime_call = locations->WillCall();
4225 bool needs_write_barrier =
4226 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227
4228 switch (value_type) {
4229 case Primitive::kPrimBoolean:
4230 case Primitive::kPrimByte: {
4231 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 if (index.IsConstant()) {
4233 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004234 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004235 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004236 } else {
4237 __ movb(Address(obj, offset),
4238 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004241 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004242 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004243 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004244 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004246 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004248 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004249 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250 break;
4251 }
4252
4253 case Primitive::kPrimShort:
4254 case Primitive::kPrimChar: {
4255 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004256 if (index.IsConstant()) {
4257 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004258 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004259 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004260 } else {
4261 __ movw(Address(obj, offset),
4262 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004265 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004266 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4267 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004268 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004269 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004270 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4271 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004272 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274 break;
4275 }
4276
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004277 case Primitive::kPrimInt:
4278 case Primitive::kPrimNot: {
4279 if (!needs_runtime_call) {
4280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4281 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004282 size_t offset =
4283 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004284 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004285 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4286 Register temp = locations->GetTemp(0).AsRegister<Register>();
4287 __ movl(temp, value.AsRegister<Register>());
4288 __ PoisonHeapReference(temp);
4289 __ movl(Address(obj, offset), temp);
4290 } else {
4291 __ movl(Address(obj, offset), value.AsRegister<Register>());
4292 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004293 } else {
4294 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004295 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4296 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4297 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4298 // Note: if heap poisoning is enabled, no need to poison
4299 // (negate) `v` if it is a reference, as it would be null.
4300 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004301 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004302 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004303 DCHECK(index.IsRegister()) << index;
4304 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004305 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4306 Register temp = locations->GetTemp(0).AsRegister<Register>();
4307 __ movl(temp, value.AsRegister<Register>());
4308 __ PoisonHeapReference(temp);
4309 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4310 } else {
4311 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4312 value.AsRegister<Register>());
4313 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004314 } else {
4315 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004316 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4317 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4318 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4319 // Note: if heap poisoning is enabled, no need to poison
4320 // (negate) `v` if it is a reference, as it would be null.
4321 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004322 }
4323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004325
4326 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004327 Register temp = locations->GetTemp(0).AsRegister<Register>();
4328 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004329 codegen_->MarkGCCard(
4330 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004331 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004332 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004333 DCHECK_EQ(value_type, Primitive::kPrimNot);
4334 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004335 // Note: if heap poisoning is enabled, pAputObject takes cares
4336 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004337 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4338 instruction,
4339 instruction->GetDexPc(),
4340 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 }
4342 break;
4343 }
4344
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004345 case Primitive::kPrimLong: {
4346 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004347 if (index.IsConstant()) {
4348 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004349 if (value.IsRegisterPair()) {
4350 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004352 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004353 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004354 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004355 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4356 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004358 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4359 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004360 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004361 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004363 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004364 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004365 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004366 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004367 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004368 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004369 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004371 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004372 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004373 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004374 Immediate(High32Bits(val)));
4375 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004376 }
4377 break;
4378 }
4379
Mark Mendell7c8d0092015-01-26 11:21:33 -05004380 case Primitive::kPrimFloat: {
4381 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4382 DCHECK(value.IsFpuRegister());
4383 if (index.IsConstant()) {
4384 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4385 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4386 } else {
4387 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4388 value.AsFpuRegister<XmmRegister>());
4389 }
4390 break;
4391 }
4392
4393 case Primitive::kPrimDouble: {
4394 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4395 DCHECK(value.IsFpuRegister());
4396 if (index.IsConstant()) {
4397 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4398 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4399 } else {
4400 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4401 value.AsFpuRegister<XmmRegister>());
4402 }
4403 break;
4404 }
4405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004406 case Primitive::kPrimVoid:
4407 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004408 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004409 }
4410}
4411
4412void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004414 locations->SetInAt(0, Location::RequiresRegister());
4415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416}
4417
4418void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4419 LocationSummary* locations = instruction->GetLocations();
4420 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 Register obj = locations->InAt(0).AsRegister<Register>();
4422 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004423 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004424 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425}
4426
4427void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004428 LocationSummary* locations =
4429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004430 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004431 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004432 if (instruction->HasUses()) {
4433 locations->SetOut(Location::SameAsFirstInput());
4434 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004435}
4436
4437void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4438 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004439 Location index_loc = locations->InAt(0);
4440 Location length_loc = locations->InAt(1);
4441 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004442 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004443
Mark Mendell99dbd682015-04-22 16:18:52 -04004444 if (length_loc.IsConstant()) {
4445 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4446 if (index_loc.IsConstant()) {
4447 // BCE will remove the bounds check if we are guarenteed to pass.
4448 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4449 if (index < 0 || index >= length) {
4450 codegen_->AddSlowPath(slow_path);
4451 __ jmp(slow_path->GetEntryLabel());
4452 } else {
4453 // Some optimization after BCE may have generated this, and we should not
4454 // generate a bounds check if it is a valid range.
4455 }
4456 return;
4457 }
4458
4459 // We have to reverse the jump condition because the length is the constant.
4460 Register index_reg = index_loc.AsRegister<Register>();
4461 __ cmpl(index_reg, Immediate(length));
4462 codegen_->AddSlowPath(slow_path);
4463 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004464 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004465 Register length = length_loc.AsRegister<Register>();
4466 if (index_loc.IsConstant()) {
4467 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4468 __ cmpl(length, Immediate(value));
4469 } else {
4470 __ cmpl(length, index_loc.AsRegister<Register>());
4471 }
4472 codegen_->AddSlowPath(slow_path);
4473 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004474 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475}
4476
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4478 temp->SetLocations(nullptr);
4479}
4480
4481void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4482 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004483 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484}
4485
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004486void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004487 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004488 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004489}
4490
4491void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004492 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4493}
4494
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004495void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4496 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4497}
4498
4499void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004500 HBasicBlock* block = instruction->GetBlock();
4501 if (block->GetLoopInformation() != nullptr) {
4502 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4503 // The back edge will generate the suspend check.
4504 return;
4505 }
4506 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4507 // The goto will generate the suspend check.
4508 return;
4509 }
4510 GenerateSuspendCheck(instruction, nullptr);
4511}
4512
4513void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4514 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004515 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004516 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4517 if (slow_path == nullptr) {
4518 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4519 instruction->SetSlowPath(slow_path);
4520 codegen_->AddSlowPath(slow_path);
4521 if (successor != nullptr) {
4522 DCHECK(successor->IsLoopHeader());
4523 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4524 }
4525 } else {
4526 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4527 }
4528
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004529 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004530 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004531 if (successor == nullptr) {
4532 __ j(kNotEqual, slow_path->GetEntryLabel());
4533 __ Bind(slow_path->GetReturnLabel());
4534 } else {
4535 __ j(kEqual, codegen_->GetLabelOf(successor));
4536 __ jmp(slow_path->GetEntryLabel());
4537 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004538}
4539
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004540X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4541 return codegen_->GetAssembler();
4542}
4543
Mark Mendell7c8d0092015-01-26 11:21:33 -05004544void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004545 ScratchRegisterScope ensure_scratch(
4546 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4547 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4548 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4549 __ movl(temp_reg, Address(ESP, src + stack_offset));
4550 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004551}
4552
4553void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004554 ScratchRegisterScope ensure_scratch(
4555 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4556 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4557 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4558 __ movl(temp_reg, Address(ESP, src + stack_offset));
4559 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4560 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4561 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004562}
4563
4564void ParallelMoveResolverX86::EmitMove(size_t index) {
4565 MoveOperands* move = moves_.Get(index);
4566 Location source = move->GetSource();
4567 Location destination = move->GetDestination();
4568
4569 if (source.IsRegister()) {
4570 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004571 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004572 } else {
4573 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004574 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004575 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004576 } else if (source.IsFpuRegister()) {
4577 if (destination.IsFpuRegister()) {
4578 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4579 } else if (destination.IsStackSlot()) {
4580 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4581 } else {
4582 DCHECK(destination.IsDoubleStackSlot());
4583 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4584 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004585 } else if (source.IsStackSlot()) {
4586 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004587 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004588 } else if (destination.IsFpuRegister()) {
4589 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004590 } else {
4591 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004592 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4593 }
4594 } else if (source.IsDoubleStackSlot()) {
4595 if (destination.IsFpuRegister()) {
4596 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4597 } else {
4598 DCHECK(destination.IsDoubleStackSlot()) << destination;
4599 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004600 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004601 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004602 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004603 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004604 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004605 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004606 if (value == 0) {
4607 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4608 } else {
4609 __ movl(destination.AsRegister<Register>(), Immediate(value));
4610 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004611 } else {
4612 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004613 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004614 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004615 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004616 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004617 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004618 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004619 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004620 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4621 if (value == 0) {
4622 // Easy handling of 0.0.
4623 __ xorps(dest, dest);
4624 } else {
4625 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004626 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4627 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4628 __ movl(temp, Immediate(value));
4629 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004630 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004631 } else {
4632 DCHECK(destination.IsStackSlot()) << destination;
4633 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4634 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004635 } else if (constant->IsLongConstant()) {
4636 int64_t value = constant->AsLongConstant()->GetValue();
4637 int32_t low_value = Low32Bits(value);
4638 int32_t high_value = High32Bits(value);
4639 Immediate low(low_value);
4640 Immediate high(high_value);
4641 if (destination.IsDoubleStackSlot()) {
4642 __ movl(Address(ESP, destination.GetStackIndex()), low);
4643 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4644 } else {
4645 __ movl(destination.AsRegisterPairLow<Register>(), low);
4646 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4647 }
4648 } else {
4649 DCHECK(constant->IsDoubleConstant());
4650 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004651 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004652 int32_t low_value = Low32Bits(value);
4653 int32_t high_value = High32Bits(value);
4654 Immediate low(low_value);
4655 Immediate high(high_value);
4656 if (destination.IsFpuRegister()) {
4657 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4658 if (value == 0) {
4659 // Easy handling of 0.0.
4660 __ xorpd(dest, dest);
4661 } else {
4662 __ pushl(high);
4663 __ pushl(low);
4664 __ movsd(dest, Address(ESP, 0));
4665 __ addl(ESP, Immediate(8));
4666 }
4667 } else {
4668 DCHECK(destination.IsDoubleStackSlot()) << destination;
4669 __ movl(Address(ESP, destination.GetStackIndex()), low);
4670 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4671 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004672 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004673 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004674 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004675 }
4676}
4677
Mark Mendella5c19ce2015-04-01 12:51:05 -04004678void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004679 Register suggested_scratch = reg == EAX ? EBX : EAX;
4680 ScratchRegisterScope ensure_scratch(
4681 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4682
4683 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4684 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4685 __ movl(Address(ESP, mem + stack_offset), reg);
4686 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004687}
4688
Mark Mendell7c8d0092015-01-26 11:21:33 -05004689void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004690 ScratchRegisterScope ensure_scratch(
4691 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4692
4693 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4694 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4695 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4696 __ movss(Address(ESP, mem + stack_offset), reg);
4697 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004698}
4699
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004700void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004701 ScratchRegisterScope ensure_scratch1(
4702 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004703
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004704 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4705 ScratchRegisterScope ensure_scratch2(
4706 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004707
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004708 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4709 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4710 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4711 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4712 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4713 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004714}
4715
4716void ParallelMoveResolverX86::EmitSwap(size_t index) {
4717 MoveOperands* move = moves_.Get(index);
4718 Location source = move->GetSource();
4719 Location destination = move->GetDestination();
4720
4721 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004722 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4723 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4724 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4725 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4726 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004727 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004728 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004729 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004731 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4732 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004733 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4734 // Use XOR Swap algorithm to avoid a temporary.
4735 DCHECK_NE(source.reg(), destination.reg());
4736 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4737 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4738 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4739 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4740 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4741 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4742 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004743 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4744 // Take advantage of the 16 bytes in the XMM register.
4745 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4746 Address stack(ESP, destination.GetStackIndex());
4747 // Load the double into the high doubleword.
4748 __ movhpd(reg, stack);
4749
4750 // Store the low double into the destination.
4751 __ movsd(stack, reg);
4752
4753 // Move the high double to the low double.
4754 __ psrldq(reg, Immediate(8));
4755 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4756 // Take advantage of the 16 bytes in the XMM register.
4757 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4758 Address stack(ESP, source.GetStackIndex());
4759 // Load the double into the high doubleword.
4760 __ movhpd(reg, stack);
4761
4762 // Store the low double into the destination.
4763 __ movsd(stack, reg);
4764
4765 // Move the high double to the low double.
4766 __ psrldq(reg, Immediate(8));
4767 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4768 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4769 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004770 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004771 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004772 }
4773}
4774
4775void ParallelMoveResolverX86::SpillScratch(int reg) {
4776 __ pushl(static_cast<Register>(reg));
4777}
4778
4779void ParallelMoveResolverX86::RestoreScratch(int reg) {
4780 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004781}
4782
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004783void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004784 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4785 ? LocationSummary::kCallOnSlowPath
4786 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004787 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004788 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004789 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004790 locations->SetOut(Location::RequiresRegister());
4791}
4792
4793void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004794 LocationSummary* locations = cls->GetLocations();
4795 Register out = locations->Out().AsRegister<Register>();
4796 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004797 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004798 DCHECK(!cls->CanCallRuntime());
4799 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004800 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004801 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004802 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004803 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004804 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004805 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004806 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004807
4808 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4809 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4810 codegen_->AddSlowPath(slow_path);
4811 __ testl(out, out);
4812 __ j(kEqual, slow_path->GetEntryLabel());
4813 if (cls->MustGenerateClinitCheck()) {
4814 GenerateClassInitializationCheck(slow_path, out);
4815 } else {
4816 __ Bind(slow_path->GetExitLabel());
4817 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004818 }
4819}
4820
4821void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4822 LocationSummary* locations =
4823 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4824 locations->SetInAt(0, Location::RequiresRegister());
4825 if (check->HasUses()) {
4826 locations->SetOut(Location::SameAsFirstInput());
4827 }
4828}
4829
4830void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004831 // We assume the class to not be null.
4832 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4833 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004835 GenerateClassInitializationCheck(slow_path,
4836 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004837}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004838
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004839void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4840 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4842 Immediate(mirror::Class::kStatusInitialized));
4843 __ j(kLess, slow_path->GetEntryLabel());
4844 __ Bind(slow_path->GetExitLabel());
4845 // No need for memory fence, thanks to the X86 memory model.
4846}
4847
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004848void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4849 LocationSummary* locations =
4850 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004851 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004852 locations->SetOut(Location::RequiresRegister());
4853}
4854
4855void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4856 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4857 codegen_->AddSlowPath(slow_path);
4858
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004859 LocationSummary* locations = load->GetLocations();
4860 Register out = locations->Out().AsRegister<Register>();
4861 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004862 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004863 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004864 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004865 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004866 __ testl(out, out);
4867 __ j(kEqual, slow_path->GetEntryLabel());
4868 __ Bind(slow_path->GetExitLabel());
4869}
4870
David Brazdilcb1c0552015-08-04 16:22:25 +01004871static Address GetExceptionTlsAddress() {
4872 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4873}
4874
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004875void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4878 locations->SetOut(Location::RequiresRegister());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004882 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4883}
4884
4885void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4886 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4887}
4888
4889void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4890 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004891}
4892
4893void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4894 LocationSummary* locations =
4895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4896 InvokeRuntimeCallingConvention calling_convention;
4897 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4898}
4899
4900void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004901 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4902 instruction,
4903 instruction->GetDexPc(),
4904 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004905}
4906
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004907void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004908 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4909 ? LocationSummary::kNoCall
4910 : LocationSummary::kCallOnSlowPath;
4911 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4912 locations->SetInAt(0, Location::RequiresRegister());
4913 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004914 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004915 locations->SetOut(Location::RequiresRegister());
4916}
4917
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004918void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004919 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004920 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004921 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004922 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004923 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4924 Label done, zero;
4925 SlowPathCodeX86* slow_path = nullptr;
4926
4927 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004928 // Avoid null check if we know obj is not null.
4929 if (instruction->MustDoNullCheck()) {
4930 __ testl(obj, obj);
4931 __ j(kEqual, &zero);
4932 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004933 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004934 __ movl(out, Address(obj, class_offset));
4935 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004936 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004937 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004938 } else {
4939 DCHECK(cls.IsStackSlot()) << cls;
4940 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4941 }
4942
4943 if (instruction->IsClassFinal()) {
4944 // Classes must be equal for the instanceof to succeed.
4945 __ j(kNotEqual, &zero);
4946 __ movl(out, Immediate(1));
4947 __ jmp(&done);
4948 } else {
4949 // If the classes are not equal, we go into a slow path.
4950 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004951 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004952 codegen_->AddSlowPath(slow_path);
4953 __ j(kNotEqual, slow_path->GetEntryLabel());
4954 __ movl(out, Immediate(1));
4955 __ jmp(&done);
4956 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004957
4958 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4959 __ Bind(&zero);
4960 __ movl(out, Immediate(0));
4961 }
4962
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004963 if (slow_path != nullptr) {
4964 __ Bind(slow_path->GetExitLabel());
4965 }
4966 __ Bind(&done);
4967}
4968
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004969void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4971 instruction, LocationSummary::kCallOnSlowPath);
4972 locations->SetInAt(0, Location::RequiresRegister());
4973 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004974 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004975 locations->AddTemp(Location::RequiresRegister());
4976}
4977
4978void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4979 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004980 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004981 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004982 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004983 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004984 SlowPathCodeX86* slow_path =
4985 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004986 codegen_->AddSlowPath(slow_path);
4987
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004988 // Avoid null check if we know obj is not null.
4989 if (instruction->MustDoNullCheck()) {
4990 __ testl(obj, obj);
4991 __ j(kEqual, slow_path->GetExitLabel());
4992 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004993 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004994 __ movl(temp, Address(obj, class_offset));
4995 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004996 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004997 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004998 } else {
4999 DCHECK(cls.IsStackSlot()) << cls;
5000 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5001 }
Roland Levillain4d027112015-07-01 15:41:14 +01005002 // The checkcast succeeds if the classes are equal (fast path).
5003 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005004 __ j(kNotEqual, slow_path->GetEntryLabel());
5005 __ Bind(slow_path->GetExitLabel());
5006}
5007
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005008void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5009 LocationSummary* locations =
5010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5011 InvokeRuntimeCallingConvention calling_convention;
5012 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5013}
5014
5015void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005016 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5017 : QUICK_ENTRY_POINT(pUnlockObject),
5018 instruction,
5019 instruction->GetDexPc(),
5020 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005021}
5022
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005023void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5024void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5025void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5026
5027void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5028 LocationSummary* locations =
5029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5030 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5031 || instruction->GetResultType() == Primitive::kPrimLong);
5032 locations->SetInAt(0, Location::RequiresRegister());
5033 locations->SetInAt(1, Location::Any());
5034 locations->SetOut(Location::SameAsFirstInput());
5035}
5036
5037void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5038 HandleBitwiseOperation(instruction);
5039}
5040
5041void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5042 HandleBitwiseOperation(instruction);
5043}
5044
5045void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5046 HandleBitwiseOperation(instruction);
5047}
5048
5049void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5050 LocationSummary* locations = instruction->GetLocations();
5051 Location first = locations->InAt(0);
5052 Location second = locations->InAt(1);
5053 DCHECK(first.Equals(locations->Out()));
5054
5055 if (instruction->GetResultType() == Primitive::kPrimInt) {
5056 if (second.IsRegister()) {
5057 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005058 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005059 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005060 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005061 } else {
5062 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005063 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005064 }
5065 } else if (second.IsConstant()) {
5066 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005067 __ andl(first.AsRegister<Register>(),
5068 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005069 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005070 __ orl(first.AsRegister<Register>(),
5071 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005072 } else {
5073 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005074 __ xorl(first.AsRegister<Register>(),
5075 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005076 }
5077 } else {
5078 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005079 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005080 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005081 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005082 } else {
5083 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005085 }
5086 }
5087 } else {
5088 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5089 if (second.IsRegisterPair()) {
5090 if (instruction->IsAnd()) {
5091 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5092 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5093 } else if (instruction->IsOr()) {
5094 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5095 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5096 } else {
5097 DCHECK(instruction->IsXor());
5098 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5099 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5100 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005101 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005102 if (instruction->IsAnd()) {
5103 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5104 __ andl(first.AsRegisterPairHigh<Register>(),
5105 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5106 } else if (instruction->IsOr()) {
5107 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5108 __ orl(first.AsRegisterPairHigh<Register>(),
5109 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5110 } else {
5111 DCHECK(instruction->IsXor());
5112 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5113 __ xorl(first.AsRegisterPairHigh<Register>(),
5114 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5115 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005116 } else {
5117 DCHECK(second.IsConstant()) << second;
5118 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005119 int32_t low_value = Low32Bits(value);
5120 int32_t high_value = High32Bits(value);
5121 Immediate low(low_value);
5122 Immediate high(high_value);
5123 Register first_low = first.AsRegisterPairLow<Register>();
5124 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005125 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005126 if (low_value == 0) {
5127 __ xorl(first_low, first_low);
5128 } else if (low_value != -1) {
5129 __ andl(first_low, low);
5130 }
5131 if (high_value == 0) {
5132 __ xorl(first_high, first_high);
5133 } else if (high_value != -1) {
5134 __ andl(first_high, high);
5135 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005136 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005137 if (low_value != 0) {
5138 __ orl(first_low, low);
5139 }
5140 if (high_value != 0) {
5141 __ orl(first_high, high);
5142 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005143 } else {
5144 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005145 if (low_value != 0) {
5146 __ xorl(first_low, low);
5147 }
5148 if (high_value != 0) {
5149 __ xorl(first_high, high);
5150 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005151 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005152 }
5153 }
5154}
5155
Calin Juravleb1498f62015-02-16 13:13:29 +00005156void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5157 // Nothing to do, this should be removed during prepare for register allocator.
5158 UNUSED(instruction);
5159 LOG(FATAL) << "Unreachable";
5160}
5161
5162void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5163 // Nothing to do, this should be removed during prepare for register allocator.
5164 UNUSED(instruction);
5165 LOG(FATAL) << "Unreachable";
5166}
5167
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005168void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5169 DCHECK(codegen_->IsBaseline());
5170 LocationSummary* locations =
5171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5172 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5173}
5174
5175void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5176 DCHECK(codegen_->IsBaseline());
5177 // Will be generated at use site.
5178}
5179
Mark Mendell0616ae02015-04-17 12:49:27 -04005180void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5181 HX86ComputeBaseMethodAddress* insn) {
5182 LocationSummary* locations =
5183 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5184 locations->SetOut(Location::RequiresRegister());
5185}
5186
5187void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5188 HX86ComputeBaseMethodAddress* insn) {
5189 LocationSummary* locations = insn->GetLocations();
5190 Register reg = locations->Out().AsRegister<Register>();
5191
5192 // Generate call to next instruction.
5193 Label next_instruction;
5194 __ call(&next_instruction);
5195 __ Bind(&next_instruction);
5196
5197 // Remember this offset for later use with constant area.
5198 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5199
5200 // Grab the return address off the stack.
5201 __ popl(reg);
5202}
5203
5204void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5205 HX86LoadFromConstantTable* insn) {
5206 LocationSummary* locations =
5207 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5208
5209 locations->SetInAt(0, Location::RequiresRegister());
5210 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5211
5212 // If we don't need to be materialized, we only need the inputs to be set.
5213 if (!insn->NeedsMaterialization()) {
5214 return;
5215 }
5216
5217 switch (insn->GetType()) {
5218 case Primitive::kPrimFloat:
5219 case Primitive::kPrimDouble:
5220 locations->SetOut(Location::RequiresFpuRegister());
5221 break;
5222
5223 case Primitive::kPrimInt:
5224 locations->SetOut(Location::RequiresRegister());
5225 break;
5226
5227 default:
5228 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5229 }
5230}
5231
5232void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5233 if (!insn->NeedsMaterialization()) {
5234 return;
5235 }
5236
5237 LocationSummary* locations = insn->GetLocations();
5238 Location out = locations->Out();
5239 Register const_area = locations->InAt(0).AsRegister<Register>();
5240 HConstant *value = insn->GetConstant();
5241
5242 switch (insn->GetType()) {
5243 case Primitive::kPrimFloat:
5244 __ movss(out.AsFpuRegister<XmmRegister>(),
5245 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5246 break;
5247
5248 case Primitive::kPrimDouble:
5249 __ movsd(out.AsFpuRegister<XmmRegister>(),
5250 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5251 break;
5252
5253 case Primitive::kPrimInt:
5254 __ movl(out.AsRegister<Register>(),
5255 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5256 break;
5257
5258 default:
5259 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5260 }
5261}
5262
5263void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5264 // Generate the constant area if needed.
5265 X86Assembler* assembler = GetAssembler();
5266 if (!assembler->IsConstantAreaEmpty()) {
5267 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5268 // byte values.
5269 assembler->Align(4, 0);
5270 constant_area_start_ = assembler->CodeSize();
5271 assembler->AddConstantArea();
5272 }
5273
5274 // And finish up.
5275 CodeGenerator::Finalize(allocator);
5276}
5277
5278/**
5279 * Class to handle late fixup of offsets into constant area.
5280 */
5281class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5282 public:
5283 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5284 : codegen_(codegen), offset_into_constant_area_(offset) {}
5285
5286 private:
5287 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5288 // Patch the correct offset for the instruction. The place to patch is the
5289 // last 4 bytes of the instruction.
5290 // The value to patch is the distance from the offset in the constant area
5291 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5292 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5293 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5294
5295 // Patch in the right value.
5296 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5297 }
5298
5299 const CodeGeneratorX86& codegen_;
5300
5301 // Location in constant area that the fixup refers to.
5302 int offset_into_constant_area_;
5303};
5304
5305Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5306 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5307 return Address(reg, kDummy32BitOffset, fixup);
5308}
5309
5310Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5311 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5312 return Address(reg, kDummy32BitOffset, fixup);
5313}
5314
5315Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5316 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5317 return Address(reg, kDummy32BitOffset, fixup);
5318}
5319
5320Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5321 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5322 return Address(reg, kDummy32BitOffset, fixup);
5323}
5324
5325/**
5326 * Finds instructions that need the constant area base as an input.
5327 */
5328class ConstantHandlerVisitor : public HGraphVisitor {
5329 public:
5330 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5331
5332 private:
5333 void VisitAdd(HAdd* add) OVERRIDE {
5334 BinaryFP(add);
5335 }
5336
5337 void VisitSub(HSub* sub) OVERRIDE {
5338 BinaryFP(sub);
5339 }
5340
5341 void VisitMul(HMul* mul) OVERRIDE {
5342 BinaryFP(mul);
5343 }
5344
5345 void VisitDiv(HDiv* div) OVERRIDE {
5346 BinaryFP(div);
5347 }
5348
5349 void VisitReturn(HReturn* ret) OVERRIDE {
5350 HConstant* value = ret->InputAt(0)->AsConstant();
5351 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5352 ReplaceInput(ret, value, 0, true);
5353 }
5354 }
5355
5356 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5357 HandleInvoke(invoke);
5358 }
5359
5360 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5361 HandleInvoke(invoke);
5362 }
5363
5364 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5365 HandleInvoke(invoke);
5366 }
5367
5368 void BinaryFP(HBinaryOperation* bin) {
5369 HConstant* rhs = bin->InputAt(1)->AsConstant();
5370 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5371 ReplaceInput(bin, rhs, 1, false);
5372 }
5373 }
5374
5375 void InitializeConstantAreaPointer(HInstruction* user) {
5376 // Ensure we only initialize the pointer once.
5377 if (base_ != nullptr) {
5378 return;
5379 }
5380
5381 HGraph* graph = GetGraph();
5382 HBasicBlock* entry = graph->GetEntryBlock();
5383 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5384 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5385 entry->InsertInstructionBefore(base_, insert_pos);
5386 DCHECK(base_ != nullptr);
5387 }
5388
5389 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5390 InitializeConstantAreaPointer(insn);
5391 HGraph* graph = GetGraph();
5392 HBasicBlock* block = insn->GetBlock();
5393 HX86LoadFromConstantTable* load_constant =
5394 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5395 block->InsertInstructionBefore(load_constant, insn);
5396 insn->ReplaceInput(load_constant, input_index);
5397 }
5398
5399 void HandleInvoke(HInvoke* invoke) {
5400 // Ensure that we can load FP arguments from the constant area.
5401 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5402 HConstant* input = invoke->InputAt(i)->AsConstant();
5403 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5404 ReplaceInput(invoke, input, i, true);
5405 }
5406 }
5407 }
5408
5409 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5410 // input to the HX86LoadFromConstantTable instructions.
5411 HX86ComputeBaseMethodAddress* base_;
5412};
5413
5414void ConstantAreaFixups::Run() {
5415 ConstantHandlerVisitor visitor(graph_);
5416 visitor.VisitInsertionOrder();
5417}
5418
Roland Levillain4d027112015-07-01 15:41:14 +01005419#undef __
5420
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005421} // namespace x86
5422} // namespace art