blob: a5ad226e0b3c8818bcda38917624a6a69c40bff9 [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) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001552 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1553 return;
1554 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001555
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001556 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001557 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001558 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001559}
1560
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001561void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1562 HandleInvoke(invoke);
1563 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001564 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001565}
1566
1567void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1568 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001569 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001570 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1571 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001572 LocationSummary* locations = invoke->GetLocations();
1573 Location receiver = locations->InAt(0);
1574 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1575
1576 // Set the hidden argument.
1577 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001578 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001579
1580 // temp = object->GetClass();
1581 if (receiver.IsStackSlot()) {
1582 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1583 __ movl(temp, Address(temp, class_offset));
1584 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001586 }
Roland Levillain4d027112015-07-01 15:41:14 +01001587 codegen_->MaybeRecordImplicitNullCheck(invoke);
1588 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001589 // temp = temp->GetImtEntryAt(method_offset);
1590 __ movl(temp, Address(temp, method_offset));
1591 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001592 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001593 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001594
1595 DCHECK(!codegen_->IsLeafMethod());
1596 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1597}
1598
Roland Levillain88cb1752014-10-20 16:36:47 +01001599void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1600 LocationSummary* locations =
1601 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1602 switch (neg->GetResultType()) {
1603 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001604 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001605 locations->SetInAt(0, Location::RequiresRegister());
1606 locations->SetOut(Location::SameAsFirstInput());
1607 break;
1608
Roland Levillain88cb1752014-10-20 16:36:47 +01001609 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001610 locations->SetInAt(0, Location::RequiresFpuRegister());
1611 locations->SetOut(Location::SameAsFirstInput());
1612 locations->AddTemp(Location::RequiresRegister());
1613 locations->AddTemp(Location::RequiresFpuRegister());
1614 break;
1615
Roland Levillain88cb1752014-10-20 16:36:47 +01001616 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001617 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001618 locations->SetOut(Location::SameAsFirstInput());
1619 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001620 break;
1621
1622 default:
1623 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1624 }
1625}
1626
1627void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1628 LocationSummary* locations = neg->GetLocations();
1629 Location out = locations->Out();
1630 Location in = locations->InAt(0);
1631 switch (neg->GetResultType()) {
1632 case Primitive::kPrimInt:
1633 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001634 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001635 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001636 break;
1637
1638 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001639 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001640 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001641 __ negl(out.AsRegisterPairLow<Register>());
1642 // Negation is similar to subtraction from zero. The least
1643 // significant byte triggers a borrow when it is different from
1644 // zero; to take it into account, add 1 to the most significant
1645 // byte if the carry flag (CF) is set to 1 after the first NEGL
1646 // operation.
1647 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1648 __ negl(out.AsRegisterPairHigh<Register>());
1649 break;
1650
Roland Levillain5368c212014-11-27 15:03:41 +00001651 case Primitive::kPrimFloat: {
1652 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 Register constant = locations->GetTemp(0).AsRegister<Register>();
1654 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001655 // Implement float negation with an exclusive or with value
1656 // 0x80000000 (mask for bit 31, representing the sign of a
1657 // single-precision floating-point number).
1658 __ movl(constant, Immediate(INT32_C(0x80000000)));
1659 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001660 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001661 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001662 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001663
Roland Levillain5368c212014-11-27 15:03:41 +00001664 case Primitive::kPrimDouble: {
1665 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001667 // Implement double negation with an exclusive or with value
1668 // 0x8000000000000000 (mask for bit 63, representing the sign of
1669 // a double-precision floating-point number).
1670 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001671 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001672 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001673 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001674
1675 default:
1676 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1677 }
1678}
1679
Roland Levillaindff1f282014-11-05 14:15:05 +00001680void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001681 Primitive::Type result_type = conversion->GetResultType();
1682 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001683 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001684
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001685 // The float-to-long and double-to-long type conversions rely on a
1686 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001687 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001688 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1689 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001690 ? LocationSummary::kCall
1691 : LocationSummary::kNoCall;
1692 LocationSummary* locations =
1693 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1694
David Brazdilb2bd1c52015-03-25 11:17:37 +00001695 // The Java language does not allow treating boolean as an integral type but
1696 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001697
Roland Levillaindff1f282014-11-05 14:15:05 +00001698 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001699 case Primitive::kPrimByte:
1700 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001701 case Primitive::kPrimBoolean:
1702 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001703 case Primitive::kPrimShort:
1704 case Primitive::kPrimInt:
1705 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001706 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001707 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1708 // Make the output overlap to please the register allocator. This greatly simplifies
1709 // the validation of the linear scan implementation
1710 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001711 break;
1712
1713 default:
1714 LOG(FATAL) << "Unexpected type conversion from " << input_type
1715 << " to " << result_type;
1716 }
1717 break;
1718
Roland Levillain01a8d712014-11-14 16:27:39 +00001719 case Primitive::kPrimShort:
1720 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001721 case Primitive::kPrimBoolean:
1722 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001723 case Primitive::kPrimByte:
1724 case Primitive::kPrimInt:
1725 case Primitive::kPrimChar:
1726 // Processing a Dex `int-to-short' instruction.
1727 locations->SetInAt(0, Location::Any());
1728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1729 break;
1730
1731 default:
1732 LOG(FATAL) << "Unexpected type conversion from " << input_type
1733 << " to " << result_type;
1734 }
1735 break;
1736
Roland Levillain946e1432014-11-11 17:35:19 +00001737 case Primitive::kPrimInt:
1738 switch (input_type) {
1739 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001740 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001741 locations->SetInAt(0, Location::Any());
1742 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1743 break;
1744
1745 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001746 // Processing a Dex `float-to-int' instruction.
1747 locations->SetInAt(0, Location::RequiresFpuRegister());
1748 locations->SetOut(Location::RequiresRegister());
1749 locations->AddTemp(Location::RequiresFpuRegister());
1750 break;
1751
Roland Levillain946e1432014-11-11 17:35:19 +00001752 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001753 // Processing a Dex `double-to-int' instruction.
1754 locations->SetInAt(0, Location::RequiresFpuRegister());
1755 locations->SetOut(Location::RequiresRegister());
1756 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001757 break;
1758
1759 default:
1760 LOG(FATAL) << "Unexpected type conversion from " << input_type
1761 << " to " << result_type;
1762 }
1763 break;
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 case Primitive::kPrimLong:
1766 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001767 case Primitive::kPrimBoolean:
1768 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001769 case Primitive::kPrimByte:
1770 case Primitive::kPrimShort:
1771 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001772 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001773 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001774 locations->SetInAt(0, Location::RegisterLocation(EAX));
1775 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1776 break;
1777
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001778 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001779 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001780 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001781 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001782 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1783 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1784
Vladimir Marko949c91f2015-01-27 10:48:44 +00001785 // The runtime helper puts the result in EAX, EDX.
1786 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001787 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001788 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001789
1790 default:
1791 LOG(FATAL) << "Unexpected type conversion from " << input_type
1792 << " to " << result_type;
1793 }
1794 break;
1795
Roland Levillain981e4542014-11-14 11:47:14 +00001796 case Primitive::kPrimChar:
1797 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001798 case Primitive::kPrimBoolean:
1799 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001800 case Primitive::kPrimByte:
1801 case Primitive::kPrimShort:
1802 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001803 // Processing a Dex `int-to-char' instruction.
1804 locations->SetInAt(0, Location::Any());
1805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1806 break;
1807
1808 default:
1809 LOG(FATAL) << "Unexpected type conversion from " << input_type
1810 << " to " << result_type;
1811 }
1812 break;
1813
Roland Levillaindff1f282014-11-05 14:15:05 +00001814 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001815 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001816 case Primitive::kPrimBoolean:
1817 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001818 case Primitive::kPrimByte:
1819 case Primitive::kPrimShort:
1820 case Primitive::kPrimInt:
1821 case Primitive::kPrimChar:
1822 // Processing a Dex `int-to-float' instruction.
1823 locations->SetInAt(0, Location::RequiresRegister());
1824 locations->SetOut(Location::RequiresFpuRegister());
1825 break;
1826
1827 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001828 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001829 locations->SetInAt(0, Location::Any());
1830 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001831 break;
1832
Roland Levillaincff13742014-11-17 14:32:17 +00001833 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001834 // Processing a Dex `double-to-float' instruction.
1835 locations->SetInAt(0, Location::RequiresFpuRegister());
1836 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unexpected type conversion from " << input_type
1841 << " to " << result_type;
1842 };
1843 break;
1844
Roland Levillaindff1f282014-11-05 14:15:05 +00001845 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001846 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001847 case Primitive::kPrimBoolean:
1848 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001849 case Primitive::kPrimByte:
1850 case Primitive::kPrimShort:
1851 case Primitive::kPrimInt:
1852 case Primitive::kPrimChar:
1853 // Processing a Dex `int-to-double' instruction.
1854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetOut(Location::RequiresFpuRegister());
1856 break;
1857
1858 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001859 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001860 locations->SetInAt(0, Location::Any());
1861 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001862 break;
1863
Roland Levillaincff13742014-11-17 14:32:17 +00001864 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001865 // Processing a Dex `float-to-double' instruction.
1866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected type conversion from " << input_type
1872 << " to " << result_type;
1873 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001874 break;
1875
1876 default:
1877 LOG(FATAL) << "Unexpected type conversion from " << input_type
1878 << " to " << result_type;
1879 }
1880}
1881
1882void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1883 LocationSummary* locations = conversion->GetLocations();
1884 Location out = locations->Out();
1885 Location in = locations->InAt(0);
1886 Primitive::Type result_type = conversion->GetResultType();
1887 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001888 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001889 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001890 case Primitive::kPrimByte:
1891 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001892 case Primitive::kPrimBoolean:
1893 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001894 case Primitive::kPrimShort:
1895 case Primitive::kPrimInt:
1896 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001897 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001898 if (in.IsRegister()) {
1899 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001900 } else {
1901 DCHECK(in.GetConstant()->IsIntConstant());
1902 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1903 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1904 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001905 break;
1906
1907 default:
1908 LOG(FATAL) << "Unexpected type conversion from " << input_type
1909 << " to " << result_type;
1910 }
1911 break;
1912
Roland Levillain01a8d712014-11-14 16:27:39 +00001913 case Primitive::kPrimShort:
1914 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001915 case Primitive::kPrimBoolean:
1916 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001917 case Primitive::kPrimByte:
1918 case Primitive::kPrimInt:
1919 case Primitive::kPrimChar:
1920 // Processing a Dex `int-to-short' instruction.
1921 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001923 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001925 } else {
1926 DCHECK(in.GetConstant()->IsIntConstant());
1927 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001928 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001929 }
1930 break;
1931
1932 default:
1933 LOG(FATAL) << "Unexpected type conversion from " << input_type
1934 << " to " << result_type;
1935 }
1936 break;
1937
Roland Levillain946e1432014-11-11 17:35:19 +00001938 case Primitive::kPrimInt:
1939 switch (input_type) {
1940 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001941 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001942 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001943 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001944 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001945 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001946 } else {
1947 DCHECK(in.IsConstant());
1948 DCHECK(in.GetConstant()->IsLongConstant());
1949 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001951 }
1952 break;
1953
Roland Levillain3f8f9362014-12-02 17:45:01 +00001954 case Primitive::kPrimFloat: {
1955 // Processing a Dex `float-to-int' instruction.
1956 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1957 Register output = out.AsRegister<Register>();
1958 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1959 Label done, nan;
1960
1961 __ movl(output, Immediate(kPrimIntMax));
1962 // temp = int-to-float(output)
1963 __ cvtsi2ss(temp, output);
1964 // if input >= temp goto done
1965 __ comiss(input, temp);
1966 __ j(kAboveEqual, &done);
1967 // if input == NaN goto nan
1968 __ j(kUnordered, &nan);
1969 // output = float-to-int-truncate(input)
1970 __ cvttss2si(output, input);
1971 __ jmp(&done);
1972 __ Bind(&nan);
1973 // output = 0
1974 __ xorl(output, output);
1975 __ Bind(&done);
1976 break;
1977 }
1978
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001979 case Primitive::kPrimDouble: {
1980 // Processing a Dex `double-to-int' instruction.
1981 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1982 Register output = out.AsRegister<Register>();
1983 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1984 Label done, nan;
1985
1986 __ movl(output, Immediate(kPrimIntMax));
1987 // temp = int-to-double(output)
1988 __ cvtsi2sd(temp, output);
1989 // if input >= temp goto done
1990 __ comisd(input, temp);
1991 __ j(kAboveEqual, &done);
1992 // if input == NaN goto nan
1993 __ j(kUnordered, &nan);
1994 // output = double-to-int-truncate(input)
1995 __ cvttsd2si(output, input);
1996 __ jmp(&done);
1997 __ Bind(&nan);
1998 // output = 0
1999 __ xorl(output, output);
2000 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002001 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002002 }
Roland Levillain946e1432014-11-11 17:35:19 +00002003
2004 default:
2005 LOG(FATAL) << "Unexpected type conversion from " << input_type
2006 << " to " << result_type;
2007 }
2008 break;
2009
Roland Levillaindff1f282014-11-05 14:15:05 +00002010 case Primitive::kPrimLong:
2011 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002012 case Primitive::kPrimBoolean:
2013 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002014 case Primitive::kPrimByte:
2015 case Primitive::kPrimShort:
2016 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002017 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002018 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002019 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2020 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002021 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 __ cdq();
2023 break;
2024
2025 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002026 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002027 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2028 conversion,
2029 conversion->GetDexPc(),
2030 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002031 break;
2032
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002034 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002035 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2036 conversion,
2037 conversion->GetDexPc(),
2038 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002039 break;
2040
2041 default:
2042 LOG(FATAL) << "Unexpected type conversion from " << input_type
2043 << " to " << result_type;
2044 }
2045 break;
2046
Roland Levillain981e4542014-11-14 11:47:14 +00002047 case Primitive::kPrimChar:
2048 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002049 case Primitive::kPrimBoolean:
2050 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002051 case Primitive::kPrimByte:
2052 case Primitive::kPrimShort:
2053 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002054 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2055 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002057 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002059 } else {
2060 DCHECK(in.GetConstant()->IsIntConstant());
2061 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002062 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002063 }
2064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 }
2070 break;
2071
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002073 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002074 case Primitive::kPrimBoolean:
2075 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002076 case Primitive::kPrimByte:
2077 case Primitive::kPrimShort:
2078 case Primitive::kPrimInt:
2079 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002080 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002082 break;
2083
Roland Levillain6d0e4832014-11-27 18:31:21 +00002084 case Primitive::kPrimLong: {
2085 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002086 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002087
Roland Levillain232ade02015-04-20 15:14:36 +01002088 // Create stack space for the call to
2089 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2090 // TODO: enhance register allocator to ask for stack temporaries.
2091 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2092 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2093 __ subl(ESP, Immediate(adjustment));
2094 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002095
Roland Levillain232ade02015-04-20 15:14:36 +01002096 // Load the value to the FP stack, using temporaries if needed.
2097 PushOntoFPStack(in, 0, adjustment, false, true);
2098
2099 if (out.IsStackSlot()) {
2100 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2101 } else {
2102 __ fstps(Address(ESP, 0));
2103 Location stack_temp = Location::StackSlot(0);
2104 codegen_->Move32(out, stack_temp);
2105 }
2106
2107 // Remove the temporary stack space we allocated.
2108 if (adjustment != 0) {
2109 __ addl(ESP, Immediate(adjustment));
2110 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002111 break;
2112 }
2113
Roland Levillaincff13742014-11-17 14:32:17 +00002114 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002115 // Processing a Dex `double-to-float' instruction.
2116 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002117 break;
2118
2119 default:
2120 LOG(FATAL) << "Unexpected type conversion from " << input_type
2121 << " to " << result_type;
2122 };
2123 break;
2124
Roland Levillaindff1f282014-11-05 14:15:05 +00002125 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002126 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002127 case Primitive::kPrimBoolean:
2128 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002129 case Primitive::kPrimByte:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimInt:
2132 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002133 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002135 break;
2136
Roland Levillain647b9ed2014-11-27 12:06:00 +00002137 case Primitive::kPrimLong: {
2138 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002139 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002140
Roland Levillain232ade02015-04-20 15:14:36 +01002141 // Create stack space for the call to
2142 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2143 // TODO: enhance register allocator to ask for stack temporaries.
2144 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2145 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2146 __ subl(ESP, Immediate(adjustment));
2147 }
2148
2149 // Load the value to the FP stack, using temporaries if needed.
2150 PushOntoFPStack(in, 0, adjustment, false, true);
2151
2152 if (out.IsDoubleStackSlot()) {
2153 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2154 } else {
2155 __ fstpl(Address(ESP, 0));
2156 Location stack_temp = Location::DoubleStackSlot(0);
2157 codegen_->Move64(out, stack_temp);
2158 }
2159
2160 // Remove the temporary stack space we allocated.
2161 if (adjustment != 0) {
2162 __ addl(ESP, Immediate(adjustment));
2163 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002164 break;
2165 }
2166
Roland Levillaincff13742014-11-17 14:32:17 +00002167 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002168 // Processing a Dex `float-to-double' instruction.
2169 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002170 break;
2171
2172 default:
2173 LOG(FATAL) << "Unexpected type conversion from " << input_type
2174 << " to " << result_type;
2175 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type conversion from " << input_type
2180 << " to " << result_type;
2181 }
2182}
2183
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002184void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002187 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002188 case Primitive::kPrimInt: {
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2192 break;
2193 }
2194
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002195 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002196 locations->SetInAt(0, Location::RequiresRegister());
2197 locations->SetInAt(1, Location::Any());
2198 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199 break;
2200 }
2201
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002202 case Primitive::kPrimFloat:
2203 case Primitive::kPrimDouble: {
2204 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002205 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002206 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002208 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002210 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2212 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002213 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002214}
2215
2216void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2217 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002218 Location first = locations->InAt(0);
2219 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002220 Location out = locations->Out();
2221
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002222 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002223 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002224 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002225 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2226 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002227 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2228 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002229 } else {
2230 __ leal(out.AsRegister<Register>(), Address(
2231 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2232 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002234 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2235 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2236 __ addl(out.AsRegister<Register>(), Immediate(value));
2237 } else {
2238 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2239 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002240 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002241 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002243 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002244 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002245 }
2246
2247 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002248 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002249 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2250 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002251 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2253 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002254 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002255 } else {
2256 DCHECK(second.IsConstant()) << second;
2257 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2258 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2259 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002260 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002261 break;
2262 }
2263
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002264 case Primitive::kPrimFloat: {
2265 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002266 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002267 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2268 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2269 DCHECK(!const_area->NeedsMaterialization());
2270 __ addss(first.AsFpuRegister<XmmRegister>(),
2271 codegen_->LiteralFloatAddress(
2272 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2273 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2274 } else {
2275 DCHECK(second.IsStackSlot());
2276 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002277 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002278 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002279 }
2280
2281 case Primitive::kPrimDouble: {
2282 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002284 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2285 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2286 DCHECK(!const_area->NeedsMaterialization());
2287 __ addsd(first.AsFpuRegister<XmmRegister>(),
2288 codegen_->LiteralDoubleAddress(
2289 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2290 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2291 } else {
2292 DCHECK(second.IsDoubleStackSlot());
2293 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 }
2295 break;
2296 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002298 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002299 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002300 }
2301}
2302
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002303void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002304 LocationSummary* locations =
2305 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002306 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002307 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002308 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetInAt(1, Location::Any());
2311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312 break;
2313 }
Calin Juravle11351682014-10-23 15:38:15 +01002314 case Primitive::kPrimFloat:
2315 case Primitive::kPrimDouble: {
2316 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002317 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002318 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 break;
Calin Juravle11351682014-10-23 15:38:15 +01002320 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002321
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002322 default:
Calin Juravle11351682014-10-23 15:38:15 +01002323 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002324 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002325}
2326
2327void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2328 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002329 Location first = locations->InAt(0);
2330 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002331 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002332 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002334 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002336 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002337 __ subl(first.AsRegister<Register>(),
2338 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002339 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002340 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002341 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002342 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343 }
2344
2345 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002346 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002347 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2348 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002349 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002350 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002351 __ sbbl(first.AsRegisterPairHigh<Register>(),
2352 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002353 } else {
2354 DCHECK(second.IsConstant()) << second;
2355 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2356 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2357 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002358 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002359 break;
2360 }
2361
Calin Juravle11351682014-10-23 15:38:15 +01002362 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002363 if (second.IsFpuRegister()) {
2364 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2365 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2366 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2367 DCHECK(!const_area->NeedsMaterialization());
2368 __ subss(first.AsFpuRegister<XmmRegister>(),
2369 codegen_->LiteralFloatAddress(
2370 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2371 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2372 } else {
2373 DCHECK(second.IsStackSlot());
2374 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2375 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002376 break;
Calin Juravle11351682014-10-23 15:38:15 +01002377 }
2378
2379 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002380 if (second.IsFpuRegister()) {
2381 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2382 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2383 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2384 DCHECK(!const_area->NeedsMaterialization());
2385 __ subsd(first.AsFpuRegister<XmmRegister>(),
2386 codegen_->LiteralDoubleAddress(
2387 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2388 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2389 } else {
2390 DCHECK(second.IsDoubleStackSlot());
2391 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2392 }
Calin Juravle11351682014-10-23 15:38:15 +01002393 break;
2394 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002396 default:
Calin Juravle11351682014-10-23 15:38:15 +01002397 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002398 }
2399}
2400
Calin Juravle34bacdf2014-10-07 20:23:36 +01002401void LocationsBuilderX86::VisitMul(HMul* mul) {
2402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2404 switch (mul->GetResultType()) {
2405 case Primitive::kPrimInt:
2406 locations->SetInAt(0, Location::RequiresRegister());
2407 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002408 if (mul->InputAt(1)->IsIntConstant()) {
2409 // Can use 3 operand multiply.
2410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2411 } else {
2412 locations->SetOut(Location::SameAsFirstInput());
2413 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002414 break;
2415 case Primitive::kPrimLong: {
2416 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002417 locations->SetInAt(1, Location::Any());
2418 locations->SetOut(Location::SameAsFirstInput());
2419 // Needed for imul on 32bits with 64bits output.
2420 locations->AddTemp(Location::RegisterLocation(EAX));
2421 locations->AddTemp(Location::RegisterLocation(EDX));
2422 break;
2423 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002424 case Primitive::kPrimFloat:
2425 case Primitive::kPrimDouble: {
2426 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002427 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002428 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002429 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002430 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431
2432 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002433 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002434 }
2435}
2436
2437void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2438 LocationSummary* locations = mul->GetLocations();
2439 Location first = locations->InAt(0);
2440 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002441 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002442
2443 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002444 case Primitive::kPrimInt:
2445 // The constant may have ended up in a register, so test explicitly to avoid
2446 // problems where the output may not be the same as the first operand.
2447 if (mul->InputAt(1)->IsIntConstant()) {
2448 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2449 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2450 } else if (second.IsRegister()) {
2451 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002452 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002453 } else {
2454 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002455 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002457 }
2458 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002459
2460 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002461 Register in1_hi = first.AsRegisterPairHigh<Register>();
2462 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002463 Register eax = locations->GetTemp(0).AsRegister<Register>();
2464 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002465
2466 DCHECK_EQ(EAX, eax);
2467 DCHECK_EQ(EDX, edx);
2468
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002469 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002470 // output: in1
2471 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2472 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2473 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002474 if (second.IsConstant()) {
2475 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002476
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002477 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2478 int32_t low_value = Low32Bits(value);
2479 int32_t high_value = High32Bits(value);
2480 Immediate low(low_value);
2481 Immediate high(high_value);
2482
2483 __ movl(eax, high);
2484 // eax <- in1.lo * in2.hi
2485 __ imull(eax, in1_lo);
2486 // in1.hi <- in1.hi * in2.lo
2487 __ imull(in1_hi, low);
2488 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2489 __ addl(in1_hi, eax);
2490 // move in2_lo to eax to prepare for double precision
2491 __ movl(eax, low);
2492 // edx:eax <- in1.lo * in2.lo
2493 __ mull(in1_lo);
2494 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2495 __ addl(in1_hi, edx);
2496 // in1.lo <- (in1.lo * in2.lo)[31:0];
2497 __ movl(in1_lo, eax);
2498 } else if (second.IsRegisterPair()) {
2499 Register in2_hi = second.AsRegisterPairHigh<Register>();
2500 Register in2_lo = second.AsRegisterPairLow<Register>();
2501
2502 __ movl(eax, in2_hi);
2503 // eax <- in1.lo * in2.hi
2504 __ imull(eax, in1_lo);
2505 // in1.hi <- in1.hi * in2.lo
2506 __ imull(in1_hi, in2_lo);
2507 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2508 __ addl(in1_hi, eax);
2509 // move in1_lo to eax to prepare for double precision
2510 __ movl(eax, in1_lo);
2511 // edx:eax <- in1.lo * in2.lo
2512 __ mull(in2_lo);
2513 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2514 __ addl(in1_hi, edx);
2515 // in1.lo <- (in1.lo * in2.lo)[31:0];
2516 __ movl(in1_lo, eax);
2517 } else {
2518 DCHECK(second.IsDoubleStackSlot()) << second;
2519 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2520 Address in2_lo(ESP, second.GetStackIndex());
2521
2522 __ movl(eax, in2_hi);
2523 // eax <- in1.lo * in2.hi
2524 __ imull(eax, in1_lo);
2525 // in1.hi <- in1.hi * in2.lo
2526 __ imull(in1_hi, in2_lo);
2527 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2528 __ addl(in1_hi, eax);
2529 // move in1_lo to eax to prepare for double precision
2530 __ movl(eax, in1_lo);
2531 // edx:eax <- in1.lo * in2.lo
2532 __ mull(in2_lo);
2533 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2534 __ addl(in1_hi, edx);
2535 // in1.lo <- (in1.lo * in2.lo)[31:0];
2536 __ movl(in1_lo, eax);
2537 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002538
2539 break;
2540 }
2541
Calin Juravleb5bfa962014-10-21 18:02:24 +01002542 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002543 DCHECK(first.Equals(locations->Out()));
2544 if (second.IsFpuRegister()) {
2545 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2546 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2547 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2548 DCHECK(!const_area->NeedsMaterialization());
2549 __ mulss(first.AsFpuRegister<XmmRegister>(),
2550 codegen_->LiteralFloatAddress(
2551 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2552 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2553 } else {
2554 DCHECK(second.IsStackSlot());
2555 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2556 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002557 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002558 }
2559
2560 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002561 DCHECK(first.Equals(locations->Out()));
2562 if (second.IsFpuRegister()) {
2563 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2564 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2565 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2566 DCHECK(!const_area->NeedsMaterialization());
2567 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2568 codegen_->LiteralDoubleAddress(
2569 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2570 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2571 } else {
2572 DCHECK(second.IsDoubleStackSlot());
2573 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2574 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002575 break;
2576 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002577
2578 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002579 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002580 }
2581}
2582
Roland Levillain232ade02015-04-20 15:14:36 +01002583void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2584 uint32_t temp_offset,
2585 uint32_t stack_adjustment,
2586 bool is_fp,
2587 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002588 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002589 DCHECK(!is_wide);
2590 if (is_fp) {
2591 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2592 } else {
2593 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2594 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002595 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002596 DCHECK(is_wide);
2597 if (is_fp) {
2598 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2599 } else {
2600 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2601 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002602 } else {
2603 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002604 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002605 Location stack_temp = Location::StackSlot(temp_offset);
2606 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002607 if (is_fp) {
2608 __ flds(Address(ESP, temp_offset));
2609 } else {
2610 __ filds(Address(ESP, temp_offset));
2611 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002612 } else {
2613 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2614 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002615 if (is_fp) {
2616 __ fldl(Address(ESP, temp_offset));
2617 } else {
2618 __ fildl(Address(ESP, temp_offset));
2619 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002620 }
2621 }
2622}
2623
2624void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2625 Primitive::Type type = rem->GetResultType();
2626 bool is_float = type == Primitive::kPrimFloat;
2627 size_t elem_size = Primitive::ComponentSize(type);
2628 LocationSummary* locations = rem->GetLocations();
2629 Location first = locations->InAt(0);
2630 Location second = locations->InAt(1);
2631 Location out = locations->Out();
2632
2633 // Create stack space for 2 elements.
2634 // TODO: enhance register allocator to ask for stack temporaries.
2635 __ subl(ESP, Immediate(2 * elem_size));
2636
2637 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002638 const bool is_wide = !is_float;
2639 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2640 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002641
2642 // Loop doing FPREM until we stabilize.
2643 Label retry;
2644 __ Bind(&retry);
2645 __ fprem();
2646
2647 // Move FP status to AX.
2648 __ fstsw();
2649
2650 // And see if the argument reduction is complete. This is signaled by the
2651 // C2 FPU flag bit set to 0.
2652 __ andl(EAX, Immediate(kC2ConditionMask));
2653 __ j(kNotEqual, &retry);
2654
2655 // We have settled on the final value. Retrieve it into an XMM register.
2656 // Store FP top of stack to real stack.
2657 if (is_float) {
2658 __ fsts(Address(ESP, 0));
2659 } else {
2660 __ fstl(Address(ESP, 0));
2661 }
2662
2663 // Pop the 2 items from the FP stack.
2664 __ fucompp();
2665
2666 // Load the value from the stack into an XMM register.
2667 DCHECK(out.IsFpuRegister()) << out;
2668 if (is_float) {
2669 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2670 } else {
2671 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2672 }
2673
2674 // And remove the temporary stack space we allocated.
2675 __ addl(ESP, Immediate(2 * elem_size));
2676}
2677
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002678
2679void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2680 DCHECK(instruction->IsDiv() || instruction->IsRem());
2681
2682 LocationSummary* locations = instruction->GetLocations();
2683 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002684 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002685
2686 Register out_register = locations->Out().AsRegister<Register>();
2687 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002688 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002689
2690 DCHECK(imm == 1 || imm == -1);
2691
2692 if (instruction->IsRem()) {
2693 __ xorl(out_register, out_register);
2694 } else {
2695 __ movl(out_register, input_register);
2696 if (imm == -1) {
2697 __ negl(out_register);
2698 }
2699 }
2700}
2701
2702
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002703void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002704 LocationSummary* locations = instruction->GetLocations();
2705
2706 Register out_register = locations->Out().AsRegister<Register>();
2707 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002708 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002709
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002710 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002711 Register num = locations->GetTemp(0).AsRegister<Register>();
2712
2713 __ leal(num, Address(input_register, std::abs(imm) - 1));
2714 __ testl(input_register, input_register);
2715 __ cmovl(kGreaterEqual, num, input_register);
2716 int shift = CTZ(imm);
2717 __ sarl(num, Immediate(shift));
2718
2719 if (imm < 0) {
2720 __ negl(num);
2721 }
2722
2723 __ movl(out_register, num);
2724}
2725
2726void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2727 DCHECK(instruction->IsDiv() || instruction->IsRem());
2728
2729 LocationSummary* locations = instruction->GetLocations();
2730 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2731
2732 Register eax = locations->InAt(0).AsRegister<Register>();
2733 Register out = locations->Out().AsRegister<Register>();
2734 Register num;
2735 Register edx;
2736
2737 if (instruction->IsDiv()) {
2738 edx = locations->GetTemp(0).AsRegister<Register>();
2739 num = locations->GetTemp(1).AsRegister<Register>();
2740 } else {
2741 edx = locations->Out().AsRegister<Register>();
2742 num = locations->GetTemp(0).AsRegister<Register>();
2743 }
2744
2745 DCHECK_EQ(EAX, eax);
2746 DCHECK_EQ(EDX, edx);
2747 if (instruction->IsDiv()) {
2748 DCHECK_EQ(EAX, out);
2749 } else {
2750 DCHECK_EQ(EDX, out);
2751 }
2752
2753 int64_t magic;
2754 int shift;
2755 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2756
2757 Label ndiv;
2758 Label end;
2759 // If numerator is 0, the result is 0, no computation needed.
2760 __ testl(eax, eax);
2761 __ j(kNotEqual, &ndiv);
2762
2763 __ xorl(out, out);
2764 __ jmp(&end);
2765
2766 __ Bind(&ndiv);
2767
2768 // Save the numerator.
2769 __ movl(num, eax);
2770
2771 // EAX = magic
2772 __ movl(eax, Immediate(magic));
2773
2774 // EDX:EAX = magic * numerator
2775 __ imull(num);
2776
2777 if (imm > 0 && magic < 0) {
2778 // EDX += num
2779 __ addl(edx, num);
2780 } else if (imm < 0 && magic > 0) {
2781 __ subl(edx, num);
2782 }
2783
2784 // Shift if needed.
2785 if (shift != 0) {
2786 __ sarl(edx, Immediate(shift));
2787 }
2788
2789 // EDX += 1 if EDX < 0
2790 __ movl(eax, edx);
2791 __ shrl(edx, Immediate(31));
2792 __ addl(edx, eax);
2793
2794 if (instruction->IsRem()) {
2795 __ movl(eax, num);
2796 __ imull(edx, Immediate(imm));
2797 __ subl(eax, edx);
2798 __ movl(edx, eax);
2799 } else {
2800 __ movl(eax, edx);
2801 }
2802 __ Bind(&end);
2803}
2804
Calin Juravlebacfec32014-11-14 15:54:36 +00002805void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2806 DCHECK(instruction->IsDiv() || instruction->IsRem());
2807
2808 LocationSummary* locations = instruction->GetLocations();
2809 Location out = locations->Out();
2810 Location first = locations->InAt(0);
2811 Location second = locations->InAt(1);
2812 bool is_div = instruction->IsDiv();
2813
2814 switch (instruction->GetResultType()) {
2815 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 DCHECK_EQ(EAX, first.AsRegister<Register>());
2817 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002818
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002819 if (instruction->InputAt(1)->IsIntConstant()) {
2820 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002821
2822 if (imm == 0) {
2823 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2824 } else if (imm == 1 || imm == -1) {
2825 DivRemOneOrMinusOne(instruction);
2826 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002827 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002828 } else {
2829 DCHECK(imm <= -2 || imm >= 2);
2830 GenerateDivRemWithAnyConstant(instruction);
2831 }
2832 } else {
2833 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002834 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002835 is_div);
2836 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002837
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002838 Register second_reg = second.AsRegister<Register>();
2839 // 0x80000000/-1 triggers an arithmetic exception!
2840 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2841 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002842
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002843 __ cmpl(second_reg, Immediate(-1));
2844 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002845
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002846 // edx:eax <- sign-extended of eax
2847 __ cdq();
2848 // eax = quotient, edx = remainder
2849 __ idivl(second_reg);
2850 __ Bind(slow_path->GetExitLabel());
2851 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002852 break;
2853 }
2854
2855 case Primitive::kPrimLong: {
2856 InvokeRuntimeCallingConvention calling_convention;
2857 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2858 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2859 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2860 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2861 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2862 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2863
2864 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002865 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2866 instruction,
2867 instruction->GetDexPc(),
2868 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002869 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002870 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2871 instruction,
2872 instruction->GetDexPc(),
2873 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002874 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002875 break;
2876 }
2877
2878 default:
2879 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2880 }
2881}
2882
Calin Juravle7c4954d2014-10-28 16:57:40 +00002883void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002884 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002885 ? LocationSummary::kCall
2886 : LocationSummary::kNoCall;
2887 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2888
Calin Juravle7c4954d2014-10-28 16:57:40 +00002889 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002890 case Primitive::kPrimInt: {
2891 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002892 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002893 locations->SetOut(Location::SameAsFirstInput());
2894 // Intel uses edx:eax as the dividend.
2895 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002896 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2897 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2898 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002899 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002900 locations->AddTemp(Location::RequiresRegister());
2901 }
Calin Juravled0d48522014-11-04 16:40:20 +00002902 break;
2903 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002904 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002905 InvokeRuntimeCallingConvention calling_convention;
2906 locations->SetInAt(0, Location::RegisterPairLocation(
2907 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2908 locations->SetInAt(1, Location::RegisterPairLocation(
2909 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2910 // Runtime helper puts the result in EAX, EDX.
2911 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002912 break;
2913 }
2914 case Primitive::kPrimFloat:
2915 case Primitive::kPrimDouble: {
2916 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002917 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002918 locations->SetOut(Location::SameAsFirstInput());
2919 break;
2920 }
2921
2922 default:
2923 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2924 }
2925}
2926
2927void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2928 LocationSummary* locations = div->GetLocations();
2929 Location first = locations->InAt(0);
2930 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002931
2932 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002933 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002934 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002935 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002936 break;
2937 }
2938
2939 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002940 if (second.IsFpuRegister()) {
2941 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2942 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2943 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2944 DCHECK(!const_area->NeedsMaterialization());
2945 __ divss(first.AsFpuRegister<XmmRegister>(),
2946 codegen_->LiteralFloatAddress(
2947 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2948 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2949 } else {
2950 DCHECK(second.IsStackSlot());
2951 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2952 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002953 break;
2954 }
2955
2956 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002957 if (second.IsFpuRegister()) {
2958 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2959 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2960 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2961 DCHECK(!const_area->NeedsMaterialization());
2962 __ divsd(first.AsFpuRegister<XmmRegister>(),
2963 codegen_->LiteralDoubleAddress(
2964 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2965 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2966 } else {
2967 DCHECK(second.IsDoubleStackSlot());
2968 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2969 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002970 break;
2971 }
2972
2973 default:
2974 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2975 }
2976}
2977
Calin Juravlebacfec32014-11-14 15:54:36 +00002978void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002979 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002980
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002981 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2982 ? LocationSummary::kCall
2983 : LocationSummary::kNoCall;
2984 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002985
Calin Juravled2ec87d2014-12-08 14:24:46 +00002986 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002987 case Primitive::kPrimInt: {
2988 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002989 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002990 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002991 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2992 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2993 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002994 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002995 locations->AddTemp(Location::RequiresRegister());
2996 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002997 break;
2998 }
2999 case Primitive::kPrimLong: {
3000 InvokeRuntimeCallingConvention calling_convention;
3001 locations->SetInAt(0, Location::RegisterPairLocation(
3002 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3003 locations->SetInAt(1, Location::RegisterPairLocation(
3004 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3005 // Runtime helper puts the result in EAX, EDX.
3006 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3007 break;
3008 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003009 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003010 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003011 locations->SetInAt(0, Location::Any());
3012 locations->SetInAt(1, Location::Any());
3013 locations->SetOut(Location::RequiresFpuRegister());
3014 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003015 break;
3016 }
3017
3018 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003019 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003020 }
3021}
3022
3023void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3024 Primitive::Type type = rem->GetResultType();
3025 switch (type) {
3026 case Primitive::kPrimInt:
3027 case Primitive::kPrimLong: {
3028 GenerateDivRemIntegral(rem);
3029 break;
3030 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003031 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003032 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003033 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003034 break;
3035 }
3036 default:
3037 LOG(FATAL) << "Unexpected rem type " << type;
3038 }
3039}
3040
Calin Juravled0d48522014-11-04 16:40:20 +00003041void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil659562a2015-09-14 21:26:33 +00003042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003044 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003045 case Primitive::kPrimByte:
3046 case Primitive::kPrimChar:
3047 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003048 case Primitive::kPrimInt: {
3049 locations->SetInAt(0, Location::Any());
3050 break;
3051 }
3052 case Primitive::kPrimLong: {
3053 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3054 if (!instruction->IsConstant()) {
3055 locations->AddTemp(Location::RequiresRegister());
3056 }
3057 break;
3058 }
3059 default:
3060 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3061 }
Calin Juravled0d48522014-11-04 16:40:20 +00003062 if (instruction->HasUses()) {
3063 locations->SetOut(Location::SameAsFirstInput());
3064 }
3065}
3066
3067void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3068 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3069 codegen_->AddSlowPath(slow_path);
3070
3071 LocationSummary* locations = instruction->GetLocations();
3072 Location value = locations->InAt(0);
3073
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003074 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003075 case Primitive::kPrimByte:
3076 case Primitive::kPrimChar:
3077 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003078 case Primitive::kPrimInt: {
3079 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003080 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003081 __ j(kEqual, slow_path->GetEntryLabel());
3082 } else if (value.IsStackSlot()) {
3083 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3084 __ j(kEqual, slow_path->GetEntryLabel());
3085 } else {
3086 DCHECK(value.IsConstant()) << value;
3087 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3088 __ jmp(slow_path->GetEntryLabel());
3089 }
3090 }
3091 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003092 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003093 case Primitive::kPrimLong: {
3094 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003096 __ movl(temp, value.AsRegisterPairLow<Register>());
3097 __ orl(temp, value.AsRegisterPairHigh<Register>());
3098 __ j(kEqual, slow_path->GetEntryLabel());
3099 } else {
3100 DCHECK(value.IsConstant()) << value;
3101 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3102 __ jmp(slow_path->GetEntryLabel());
3103 }
3104 }
3105 break;
3106 }
3107 default:
3108 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003109 }
Calin Juravled0d48522014-11-04 16:40:20 +00003110}
3111
Calin Juravle9aec02f2014-11-18 23:06:35 +00003112void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3113 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3114
3115 LocationSummary* locations =
3116 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3117
3118 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003119 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003120 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003121 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003122 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003123 // The shift count needs to be in CL or a constant.
3124 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003125 locations->SetOut(Location::SameAsFirstInput());
3126 break;
3127 }
3128 default:
3129 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3130 }
3131}
3132
3133void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3134 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3135
3136 LocationSummary* locations = op->GetLocations();
3137 Location first = locations->InAt(0);
3138 Location second = locations->InAt(1);
3139 DCHECK(first.Equals(locations->Out()));
3140
3141 switch (op->GetResultType()) {
3142 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003143 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003144 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003145 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003146 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003147 DCHECK_EQ(ECX, second_reg);
3148 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003149 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003150 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003151 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003152 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003153 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003154 }
3155 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003156 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3157 if (shift == 0) {
3158 return;
3159 }
3160 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003161 if (op->IsShl()) {
3162 __ shll(first_reg, imm);
3163 } else if (op->IsShr()) {
3164 __ sarl(first_reg, imm);
3165 } else {
3166 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003167 }
3168 }
3169 break;
3170 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003171 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003172 if (second.IsRegister()) {
3173 Register second_reg = second.AsRegister<Register>();
3174 DCHECK_EQ(ECX, second_reg);
3175 if (op->IsShl()) {
3176 GenerateShlLong(first, second_reg);
3177 } else if (op->IsShr()) {
3178 GenerateShrLong(first, second_reg);
3179 } else {
3180 GenerateUShrLong(first, second_reg);
3181 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003182 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003183 // Shift by a constant.
3184 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3185 // Nothing to do if the shift is 0, as the input is already the output.
3186 if (shift != 0) {
3187 if (op->IsShl()) {
3188 GenerateShlLong(first, shift);
3189 } else if (op->IsShr()) {
3190 GenerateShrLong(first, shift);
3191 } else {
3192 GenerateUShrLong(first, shift);
3193 }
3194 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003195 }
3196 break;
3197 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003198 default:
3199 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3200 }
3201}
3202
Mark P Mendell73945692015-04-29 14:56:17 +00003203void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3204 Register low = loc.AsRegisterPairLow<Register>();
3205 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003206 if (shift == 1) {
3207 // This is just an addition.
3208 __ addl(low, low);
3209 __ adcl(high, high);
3210 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003211 // Shift by 32 is easy. High gets low, and low gets 0.
3212 codegen_->EmitParallelMoves(
3213 loc.ToLow(),
3214 loc.ToHigh(),
3215 Primitive::kPrimInt,
3216 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3217 loc.ToLow(),
3218 Primitive::kPrimInt);
3219 } else if (shift > 32) {
3220 // Low part becomes 0. High part is low part << (shift-32).
3221 __ movl(high, low);
3222 __ shll(high, Immediate(shift - 32));
3223 __ xorl(low, low);
3224 } else {
3225 // Between 1 and 31.
3226 __ shld(high, low, Immediate(shift));
3227 __ shll(low, Immediate(shift));
3228 }
3229}
3230
Calin Juravle9aec02f2014-11-18 23:06:35 +00003231void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3232 Label done;
3233 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3234 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3235 __ testl(shifter, Immediate(32));
3236 __ j(kEqual, &done);
3237 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3238 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3239 __ Bind(&done);
3240}
3241
Mark P Mendell73945692015-04-29 14:56:17 +00003242void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3243 Register low = loc.AsRegisterPairLow<Register>();
3244 Register high = loc.AsRegisterPairHigh<Register>();
3245 if (shift == 32) {
3246 // Need to copy the sign.
3247 DCHECK_NE(low, high);
3248 __ movl(low, high);
3249 __ sarl(high, Immediate(31));
3250 } else if (shift > 32) {
3251 DCHECK_NE(low, high);
3252 // High part becomes sign. Low part is shifted by shift - 32.
3253 __ movl(low, high);
3254 __ sarl(high, Immediate(31));
3255 __ sarl(low, Immediate(shift - 32));
3256 } else {
3257 // Between 1 and 31.
3258 __ shrd(low, high, Immediate(shift));
3259 __ sarl(high, Immediate(shift));
3260 }
3261}
3262
Calin Juravle9aec02f2014-11-18 23:06:35 +00003263void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3264 Label done;
3265 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3266 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3267 __ testl(shifter, Immediate(32));
3268 __ j(kEqual, &done);
3269 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3270 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3271 __ Bind(&done);
3272}
3273
Mark P Mendell73945692015-04-29 14:56:17 +00003274void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3275 Register low = loc.AsRegisterPairLow<Register>();
3276 Register high = loc.AsRegisterPairHigh<Register>();
3277 if (shift == 32) {
3278 // Shift by 32 is easy. Low gets high, and high gets 0.
3279 codegen_->EmitParallelMoves(
3280 loc.ToHigh(),
3281 loc.ToLow(),
3282 Primitive::kPrimInt,
3283 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3284 loc.ToHigh(),
3285 Primitive::kPrimInt);
3286 } else if (shift > 32) {
3287 // Low part is high >> (shift - 32). High part becomes 0.
3288 __ movl(low, high);
3289 __ shrl(low, Immediate(shift - 32));
3290 __ xorl(high, high);
3291 } else {
3292 // Between 1 and 31.
3293 __ shrd(low, high, Immediate(shift));
3294 __ shrl(high, Immediate(shift));
3295 }
3296}
3297
Calin Juravle9aec02f2014-11-18 23:06:35 +00003298void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3299 Label done;
3300 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3301 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3302 __ testl(shifter, Immediate(32));
3303 __ j(kEqual, &done);
3304 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3305 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3306 __ Bind(&done);
3307}
3308
3309void LocationsBuilderX86::VisitShl(HShl* shl) {
3310 HandleShift(shl);
3311}
3312
3313void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3314 HandleShift(shl);
3315}
3316
3317void LocationsBuilderX86::VisitShr(HShr* shr) {
3318 HandleShift(shr);
3319}
3320
3321void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3322 HandleShift(shr);
3323}
3324
3325void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3326 HandleShift(ushr);
3327}
3328
3329void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3330 HandleShift(ushr);
3331}
3332
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003333void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003334 LocationSummary* locations =
3335 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003336 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003337 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003338 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003340}
3341
3342void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3343 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003344 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003345 // Note: if heap poisoning is enabled, the entry point takes cares
3346 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003347 codegen_->InvokeRuntime(
3348 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3349 instruction,
3350 instruction->GetDexPc(),
3351 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003352 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003353}
3354
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003355void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3356 LocationSummary* locations =
3357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3358 locations->SetOut(Location::RegisterLocation(EAX));
3359 InvokeRuntimeCallingConvention calling_convention;
3360 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003361 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003362 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003363}
3364
3365void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3366 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003367 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3368
Roland Levillain4d027112015-07-01 15:41:14 +01003369 // Note: if heap poisoning is enabled, the entry point takes cares
3370 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003371 codegen_->InvokeRuntime(
3372 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3373 instruction,
3374 instruction->GetDexPc(),
3375 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003376 DCHECK(!codegen_->IsLeafMethod());
3377}
3378
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003379void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003380 LocationSummary* locations =
3381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003382 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3383 if (location.IsStackSlot()) {
3384 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3385 } else if (location.IsDoubleStackSlot()) {
3386 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003387 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003388 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003389}
3390
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003391void InstructionCodeGeneratorX86::VisitParameterValue(
3392 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3393}
3394
3395void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3396 LocationSummary* locations =
3397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3398 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3399}
3400
3401void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003402}
3403
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003404void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003405 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003406 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003407 locations->SetInAt(0, Location::RequiresRegister());
3408 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003409}
3410
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003411void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3412 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003413 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003414 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003415 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003416 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003417 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003418 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003419 break;
3420
3421 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003422 __ notl(out.AsRegisterPairLow<Register>());
3423 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003424 break;
3425
3426 default:
3427 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3428 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003429}
3430
David Brazdil66d126e2015-04-03 16:02:44 +01003431void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3434 locations->SetInAt(0, Location::RequiresRegister());
3435 locations->SetOut(Location::SameAsFirstInput());
3436}
3437
3438void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003439 LocationSummary* locations = bool_not->GetLocations();
3440 Location in = locations->InAt(0);
3441 Location out = locations->Out();
3442 DCHECK(in.Equals(out));
3443 __ xorl(out.AsRegister<Register>(), Immediate(1));
3444}
3445
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003446void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003447 LocationSummary* locations =
3448 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003449 switch (compare->InputAt(0)->GetType()) {
3450 case Primitive::kPrimLong: {
3451 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003452 locations->SetInAt(1, Location::Any());
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454 break;
3455 }
3456 case Primitive::kPrimFloat:
3457 case Primitive::kPrimDouble: {
3458 locations->SetInAt(0, Location::RequiresFpuRegister());
3459 locations->SetInAt(1, Location::RequiresFpuRegister());
3460 locations->SetOut(Location::RequiresRegister());
3461 break;
3462 }
3463 default:
3464 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3465 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003466}
3467
3468void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003469 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003470 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003471 Location left = locations->InAt(0);
3472 Location right = locations->InAt(1);
3473
3474 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003475 switch (compare->InputAt(0)->GetType()) {
3476 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003477 Register left_low = left.AsRegisterPairLow<Register>();
3478 Register left_high = left.AsRegisterPairHigh<Register>();
3479 int32_t val_low = 0;
3480 int32_t val_high = 0;
3481 bool right_is_const = false;
3482
3483 if (right.IsConstant()) {
3484 DCHECK(right.GetConstant()->IsLongConstant());
3485 right_is_const = true;
3486 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3487 val_low = Low32Bits(val);
3488 val_high = High32Bits(val);
3489 }
3490
Calin Juravleddb7df22014-11-25 20:56:51 +00003491 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003492 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003493 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003494 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003495 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003496 DCHECK(right_is_const) << right;
3497 if (val_high == 0) {
3498 __ testl(left_high, left_high);
3499 } else {
3500 __ cmpl(left_high, Immediate(val_high));
3501 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003502 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003503 __ j(kLess, &less); // Signed compare.
3504 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003505 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003506 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003507 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003508 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003509 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003510 DCHECK(right_is_const) << right;
3511 if (val_low == 0) {
3512 __ testl(left_low, left_low);
3513 } else {
3514 __ cmpl(left_low, Immediate(val_low));
3515 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003516 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003517 break;
3518 }
3519 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003521 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3522 break;
3523 }
3524 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003525 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003526 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003527 break;
3528 }
3529 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003530 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003531 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003532 __ movl(out, Immediate(0));
3533 __ j(kEqual, &done);
3534 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3535
3536 __ Bind(&greater);
3537 __ movl(out, Immediate(1));
3538 __ jmp(&done);
3539
3540 __ Bind(&less);
3541 __ movl(out, Immediate(-1));
3542
3543 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003544}
3545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003546void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003547 LocationSummary* locations =
3548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003549 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3550 locations->SetInAt(i, Location::Any());
3551 }
3552 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003553}
3554
3555void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003556 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003557 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003558}
3559
Calin Juravle52c48962014-12-16 17:02:57 +00003560void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3561 /*
3562 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3563 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3564 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3565 */
3566 switch (kind) {
3567 case MemBarrierKind::kAnyAny: {
3568 __ mfence();
3569 break;
3570 }
3571 case MemBarrierKind::kAnyStore:
3572 case MemBarrierKind::kLoadAny:
3573 case MemBarrierKind::kStoreStore: {
3574 // nop
3575 break;
3576 }
3577 default:
3578 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003579 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003580}
3581
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003582
Vladimir Marko58155012015-08-19 12:49:41 +00003583void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3584 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3585 switch (invoke->GetMethodLoadKind()) {
3586 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3587 // temp = thread->string_init_entrypoint
3588 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3589 break;
3590 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3591 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3592 break;
3593 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3594 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3595 break;
3596 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3597 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3598 method_patches_.emplace_back(invoke->GetTargetMethod());
3599 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3600 break;
3601 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3602 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3603 FALLTHROUGH_INTENDED;
3604 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3605 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3606 Register method_reg;
3607 Register reg = temp.AsRegister<Register>();
3608 if (current_method.IsRegister()) {
3609 method_reg = current_method.AsRegister<Register>();
3610 } else {
3611 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3612 DCHECK(!current_method.IsValid());
3613 method_reg = reg;
3614 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3615 }
3616 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003617 __ movl(reg, Address(method_reg,
3618 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003619 // temp = temp[index_in_cache]
3620 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3621 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3622 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003623 }
Vladimir Marko58155012015-08-19 12:49:41 +00003624 }
3625
3626 switch (invoke->GetCodePtrLocation()) {
3627 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3628 __ call(GetFrameEntryLabel());
3629 break;
3630 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3631 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3632 Label* label = &relative_call_patches_.back().label;
3633 __ call(label); // Bind to the patch label, override at link time.
3634 __ Bind(label); // Bind the label at the end of the "call" insn.
3635 break;
3636 }
3637 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3638 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3639 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3640 // (Though the direct CALL ptr16:32 is available for consideration).
3641 FALLTHROUGH_INTENDED;
3642 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3643 // (callee_method + offset_of_quick_compiled_code)()
3644 __ call(Address(callee_method.AsRegister<Register>(),
3645 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3646 kX86WordSize).Int32Value()));
3647 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003648 }
3649
3650 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003651}
3652
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003653void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3654 Register temp = temp_in.AsRegister<Register>();
3655 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3656 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3657 LocationSummary* locations = invoke->GetLocations();
3658 Location receiver = locations->InAt(0);
3659 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3660 // temp = object->GetClass();
3661 DCHECK(receiver.IsRegister());
3662 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3663 MaybeRecordImplicitNullCheck(invoke);
3664 __ MaybeUnpoisonHeapReference(temp);
3665 // temp = temp->GetMethodAt(method_offset);
3666 __ movl(temp, Address(temp, method_offset));
3667 // call temp->GetEntryPoint();
3668 __ call(Address(
3669 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3670}
3671
Vladimir Marko58155012015-08-19 12:49:41 +00003672void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3673 DCHECK(linker_patches->empty());
3674 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3675 for (const MethodPatchInfo<Label>& info : method_patches_) {
3676 // The label points to the end of the "movl" insn but the literal offset for method
3677 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3678 uint32_t literal_offset = info.label.Position() - 4;
3679 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3680 info.target_method.dex_file,
3681 info.target_method.dex_method_index));
3682 }
3683 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3684 // The label points to the end of the "call" insn but the literal offset for method
3685 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3686 uint32_t literal_offset = info.label.Position() - 4;
3687 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3688 info.target_method.dex_file,
3689 info.target_method.dex_method_index));
3690 }
3691}
3692
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003693void CodeGeneratorX86::MarkGCCard(Register temp,
3694 Register card,
3695 Register object,
3696 Register value,
3697 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003699 if (value_can_be_null) {
3700 __ testl(value, value);
3701 __ j(kEqual, &is_null);
3702 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003703 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3704 __ movl(temp, object);
3705 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003706 __ movb(Address(temp, card, TIMES_1, 0),
3707 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003708 if (value_can_be_null) {
3709 __ Bind(&is_null);
3710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003711}
3712
Calin Juravle52c48962014-12-16 17:02:57 +00003713void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3714 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003715 LocationSummary* locations =
3716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003717 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003718
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003719 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3720 locations->SetOut(Location::RequiresFpuRegister());
3721 } else {
3722 // The output overlaps in case of long: we don't want the low move to overwrite
3723 // the object's location.
3724 locations->SetOut(Location::RequiresRegister(),
3725 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3726 : Location::kNoOutputOverlap);
3727 }
Calin Juravle52c48962014-12-16 17:02:57 +00003728
3729 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3730 // Long values can be loaded atomically into an XMM using movsd.
3731 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3732 // and then copy the XMM into the output 32bits at a time).
3733 locations->AddTemp(Location::RequiresFpuRegister());
3734 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003735}
3736
Calin Juravle52c48962014-12-16 17:02:57 +00003737void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3738 const FieldInfo& field_info) {
3739 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003740
Calin Juravle52c48962014-12-16 17:02:57 +00003741 LocationSummary* locations = instruction->GetLocations();
3742 Register base = locations->InAt(0).AsRegister<Register>();
3743 Location out = locations->Out();
3744 bool is_volatile = field_info.IsVolatile();
3745 Primitive::Type field_type = field_info.GetFieldType();
3746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3747
3748 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003749 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003750 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751 break;
3752 }
3753
3754 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003755 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003756 break;
3757 }
3758
3759 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003760 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003761 break;
3762 }
3763
3764 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003765 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003766 break;
3767 }
3768
3769 case Primitive::kPrimInt:
3770 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003771 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003772 break;
3773 }
3774
3775 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003776 if (is_volatile) {
3777 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3778 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003780 __ movd(out.AsRegisterPairLow<Register>(), temp);
3781 __ psrlq(temp, Immediate(32));
3782 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3783 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003784 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003785 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003786 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003787 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3788 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003789 break;
3790 }
3791
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003792 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003793 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003794 break;
3795 }
3796
3797 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003798 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003799 break;
3800 }
3801
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003802 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003803 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003804 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805 }
Calin Juravle52c48962014-12-16 17:02:57 +00003806
Calin Juravle77520bc2015-01-12 18:45:46 +00003807 // Longs are handled in the switch.
3808 if (field_type != Primitive::kPrimLong) {
3809 codegen_->MaybeRecordImplicitNullCheck(instruction);
3810 }
3811
Calin Juravle52c48962014-12-16 17:02:57 +00003812 if (is_volatile) {
3813 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3814 }
Roland Levillain4d027112015-07-01 15:41:14 +01003815
3816 if (field_type == Primitive::kPrimNot) {
3817 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3818 }
Calin Juravle52c48962014-12-16 17:02:57 +00003819}
3820
3821void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3822 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3823
3824 LocationSummary* locations =
3825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3826 locations->SetInAt(0, Location::RequiresRegister());
3827 bool is_volatile = field_info.IsVolatile();
3828 Primitive::Type field_type = field_info.GetFieldType();
3829 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3830 || (field_type == Primitive::kPrimByte);
3831
3832 // The register allocator does not support multiple
3833 // inputs that die at entry with one in a specific register.
3834 if (is_byte_type) {
3835 // Ensure the value is in a byte register.
3836 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003837 } else if (Primitive::IsFloatingPointType(field_type)) {
3838 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003839 } else {
3840 locations->SetInAt(1, Location::RequiresRegister());
3841 }
Calin Juravle52c48962014-12-16 17:02:57 +00003842 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003843 // Temporary registers for the write barrier.
3844 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003845 // Ensure the card is in a byte register.
3846 locations->AddTemp(Location::RegisterLocation(ECX));
3847 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3848 // 64bits value can be atomically written to an address with movsd and an XMM register.
3849 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3850 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3851 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3852 // isolated cases when we need this it isn't worth adding the extra complexity.
3853 locations->AddTemp(Location::RequiresFpuRegister());
3854 locations->AddTemp(Location::RequiresFpuRegister());
3855 }
3856}
3857
3858void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003859 const FieldInfo& field_info,
3860 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003861 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3862
3863 LocationSummary* locations = instruction->GetLocations();
3864 Register base = locations->InAt(0).AsRegister<Register>();
3865 Location value = locations->InAt(1);
3866 bool is_volatile = field_info.IsVolatile();
3867 Primitive::Type field_type = field_info.GetFieldType();
3868 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003869 bool needs_write_barrier =
3870 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003871
3872 if (is_volatile) {
3873 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3874 }
3875
3876 switch (field_type) {
3877 case Primitive::kPrimBoolean:
3878 case Primitive::kPrimByte: {
3879 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3880 break;
3881 }
3882
3883 case Primitive::kPrimShort:
3884 case Primitive::kPrimChar: {
3885 __ movw(Address(base, offset), value.AsRegister<Register>());
3886 break;
3887 }
3888
3889 case Primitive::kPrimInt:
3890 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003891 if (kPoisonHeapReferences && needs_write_barrier) {
3892 // Note that in the case where `value` is a null reference,
3893 // we do not enter this block, as the reference does not
3894 // need poisoning.
3895 DCHECK_EQ(field_type, Primitive::kPrimNot);
3896 Register temp = locations->GetTemp(0).AsRegister<Register>();
3897 __ movl(temp, value.AsRegister<Register>());
3898 __ PoisonHeapReference(temp);
3899 __ movl(Address(base, offset), temp);
3900 } else {
3901 __ movl(Address(base, offset), value.AsRegister<Register>());
3902 }
Calin Juravle52c48962014-12-16 17:02:57 +00003903 break;
3904 }
3905
3906 case Primitive::kPrimLong: {
3907 if (is_volatile) {
3908 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3909 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3910 __ movd(temp1, value.AsRegisterPairLow<Register>());
3911 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3912 __ punpckldq(temp1, temp2);
3913 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003914 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003915 } else {
3916 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003917 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003918 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3919 }
3920 break;
3921 }
3922
3923 case Primitive::kPrimFloat: {
3924 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3925 break;
3926 }
3927
3928 case Primitive::kPrimDouble: {
3929 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3930 break;
3931 }
3932
3933 case Primitive::kPrimVoid:
3934 LOG(FATAL) << "Unreachable type " << field_type;
3935 UNREACHABLE();
3936 }
3937
Calin Juravle77520bc2015-01-12 18:45:46 +00003938 // Longs are handled in the switch.
3939 if (field_type != Primitive::kPrimLong) {
3940 codegen_->MaybeRecordImplicitNullCheck(instruction);
3941 }
3942
Roland Levillain4d027112015-07-01 15:41:14 +01003943 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 Register temp = locations->GetTemp(0).AsRegister<Register>();
3945 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003946 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 }
3948
Calin Juravle52c48962014-12-16 17:02:57 +00003949 if (is_volatile) {
3950 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3951 }
3952}
3953
3954void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3955 HandleFieldGet(instruction, instruction->GetFieldInfo());
3956}
3957
3958void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3959 HandleFieldGet(instruction, instruction->GetFieldInfo());
3960}
3961
3962void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3963 HandleFieldSet(instruction, instruction->GetFieldInfo());
3964}
3965
3966void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003967 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003968}
3969
3970void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3971 HandleFieldSet(instruction, instruction->GetFieldInfo());
3972}
3973
3974void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003975 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003976}
3977
3978void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3979 HandleFieldGet(instruction, instruction->GetFieldInfo());
3980}
3981
3982void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3983 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984}
3985
3986void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil659562a2015-09-14 21:26:33 +00003987 LocationSummary* locations =
3988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3989 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003990 ? Location::RequiresRegister()
3991 : Location::Any();
3992 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003993 if (instruction->HasUses()) {
3994 locations->SetOut(Location::SameAsFirstInput());
3995 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003996}
3997
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003998void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003999 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4000 return;
4001 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004002 LocationSummary* locations = instruction->GetLocations();
4003 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004004
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004005 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4006 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4007}
4008
4009void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004010 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004011 codegen_->AddSlowPath(slow_path);
4012
4013 LocationSummary* locations = instruction->GetLocations();
4014 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004015
4016 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004017 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004018 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004019 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004020 } else {
4021 DCHECK(obj.IsConstant()) << obj;
David Brazdil659562a2015-09-14 21:26:33 +00004022 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004023 __ jmp(slow_path->GetEntryLabel());
4024 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004025 }
4026 __ j(kEqual, slow_path->GetEntryLabel());
4027}
4028
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004029void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil659562a2015-09-14 21:26:33 +00004030 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004031 GenerateImplicitNullCheck(instruction);
4032 } else {
4033 GenerateExplicitNullCheck(instruction);
4034 }
4035}
4036
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004037void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004038 LocationSummary* locations =
4039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004040 locations->SetInAt(0, Location::RequiresRegister());
4041 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004042 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4043 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4044 } else {
4045 // The output overlaps in case of long: we don't want the low move to overwrite
4046 // the array's location.
4047 locations->SetOut(Location::RequiresRegister(),
4048 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4049 : Location::kNoOutputOverlap);
4050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004051}
4052
4053void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4054 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004055 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 Location index = locations->InAt(1);
4057
Calin Juravle77520bc2015-01-12 18:45:46 +00004058 Primitive::Type type = instruction->GetType();
4059 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004060 case Primitive::kPrimBoolean: {
4061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004062 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004063 if (index.IsConstant()) {
4064 __ movzxb(out, Address(obj,
4065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4066 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004067 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004068 }
4069 break;
4070 }
4071
4072 case Primitive::kPrimByte: {
4073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004075 if (index.IsConstant()) {
4076 __ movsxb(out, Address(obj,
4077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4078 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004079 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004080 }
4081 break;
4082 }
4083
4084 case Primitive::kPrimShort: {
4085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004087 if (index.IsConstant()) {
4088 __ movsxw(out, Address(obj,
4089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4090 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004092 }
4093 break;
4094 }
4095
4096 case Primitive::kPrimChar: {
4097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004099 if (index.IsConstant()) {
4100 __ movzxw(out, Address(obj,
4101 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4102 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004104 }
4105 break;
4106 }
4107
4108 case Primitive::kPrimInt:
4109 case Primitive::kPrimNot: {
4110 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004111 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004112 if (index.IsConstant()) {
4113 __ movl(out, Address(obj,
4114 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004116 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004117 }
4118 break;
4119 }
4120
4121 case Primitive::kPrimLong: {
4122 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004123 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004124 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004125 if (index.IsConstant()) {
4126 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004127 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004128 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004129 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004130 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004131 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004132 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004133 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004134 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004136 }
4137 break;
4138 }
4139
Mark Mendell7c8d0092015-01-26 11:21:33 -05004140 case Primitive::kPrimFloat: {
4141 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4142 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4143 if (index.IsConstant()) {
4144 __ movss(out, Address(obj,
4145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4146 } else {
4147 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4148 }
4149 break;
4150 }
4151
4152 case Primitive::kPrimDouble: {
4153 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4154 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4155 if (index.IsConstant()) {
4156 __ movsd(out, Address(obj,
4157 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4158 } else {
4159 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4160 }
4161 break;
4162 }
4163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004165 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004166 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004168
4169 if (type != Primitive::kPrimLong) {
4170 codegen_->MaybeRecordImplicitNullCheck(instruction);
4171 }
Roland Levillain4d027112015-07-01 15:41:14 +01004172
4173 if (type == Primitive::kPrimNot) {
4174 Register out = locations->Out().AsRegister<Register>();
4175 __ MaybeUnpoisonHeapReference(out);
4176 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004177}
4178
4179void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004180 // This location builder might end up asking to up to four registers, which is
4181 // not currently possible for baseline. The situation in which we need four
4182 // registers cannot be met by baseline though, because it has not run any
4183 // optimization.
4184
Nicolas Geoffray39468442014-09-02 15:17:15 +01004185 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004186 bool needs_write_barrier =
4187 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4188
Mark Mendell5f874182015-03-04 15:42:45 -05004189 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004190
Nicolas Geoffray39468442014-09-02 15:17:15 +01004191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4192 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004193 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004194
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004195 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004196 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004197 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4198 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4199 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004200 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004201 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4202 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004203 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004204 // In case of a byte operation, the register allocator does not support multiple
4205 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004206 locations->SetInAt(0, Location::RequiresRegister());
4207 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004208 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004209 // Ensure the value is in a byte register.
4210 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004211 } else if (Primitive::IsFloatingPointType(value_type)) {
4212 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004213 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004214 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004215 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004216 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004217 // Temporary registers for the write barrier.
4218 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004219 // Ensure the card is in a byte register.
4220 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004221 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223}
4224
4225void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4226 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004227 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004228 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004229 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004230 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004231 bool needs_runtime_call = locations->WillCall();
4232 bool needs_write_barrier =
4233 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234
4235 switch (value_type) {
4236 case Primitive::kPrimBoolean:
4237 case Primitive::kPrimByte: {
4238 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004239 if (index.IsConstant()) {
4240 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004241 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004242 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004243 } else {
4244 __ movb(Address(obj, offset),
4245 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004247 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004248 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004249 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004250 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004251 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004252 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004253 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004256 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 break;
4258 }
4259
4260 case Primitive::kPrimShort:
4261 case Primitive::kPrimChar: {
4262 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 if (index.IsConstant()) {
4264 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004265 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004266 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004267 } else {
4268 __ movw(Address(obj, offset),
4269 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004272 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4274 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004275 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004276 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004277 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4278 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 break;
4282 }
4283
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004284 case Primitive::kPrimInt:
4285 case Primitive::kPrimNot: {
4286 if (!needs_runtime_call) {
4287 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4288 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004289 size_t offset =
4290 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004291 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004292 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4293 Register temp = locations->GetTemp(0).AsRegister<Register>();
4294 __ movl(temp, value.AsRegister<Register>());
4295 __ PoisonHeapReference(temp);
4296 __ movl(Address(obj, offset), temp);
4297 } else {
4298 __ movl(Address(obj, offset), value.AsRegister<Register>());
4299 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004300 } else {
4301 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004302 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4303 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4304 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4305 // Note: if heap poisoning is enabled, no need to poison
4306 // (negate) `v` if it is a reference, as it would be null.
4307 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004308 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004309 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004310 DCHECK(index.IsRegister()) << index;
4311 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004312 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4313 Register temp = locations->GetTemp(0).AsRegister<Register>();
4314 __ movl(temp, value.AsRegister<Register>());
4315 __ PoisonHeapReference(temp);
4316 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4317 } else {
4318 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4319 value.AsRegister<Register>());
4320 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004321 } else {
4322 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004323 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4324 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4325 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4326 // Note: if heap poisoning is enabled, no need to poison
4327 // (negate) `v` if it is a reference, as it would be null.
4328 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004329 }
4330 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004332
4333 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 Register temp = locations->GetTemp(0).AsRegister<Register>();
4335 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004336 codegen_->MarkGCCard(
4337 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004338 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004339 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004340 DCHECK_EQ(value_type, Primitive::kPrimNot);
4341 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004342 // Note: if heap poisoning is enabled, pAputObject takes cares
4343 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004344 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4345 instruction,
4346 instruction->GetDexPc(),
4347 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348 }
4349 break;
4350 }
4351
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 case Primitive::kPrimLong: {
4353 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354 if (index.IsConstant()) {
4355 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004356 if (value.IsRegisterPair()) {
4357 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004358 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004359 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004360 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004361 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004362 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4363 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004364 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004365 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4366 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004368 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004369 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004370 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004372 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004373 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004374 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004375 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004376 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004378 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004380 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004381 Immediate(High32Bits(val)));
4382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 }
4384 break;
4385 }
4386
Mark Mendell7c8d0092015-01-26 11:21:33 -05004387 case Primitive::kPrimFloat: {
4388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4389 DCHECK(value.IsFpuRegister());
4390 if (index.IsConstant()) {
4391 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4392 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4393 } else {
4394 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4395 value.AsFpuRegister<XmmRegister>());
4396 }
4397 break;
4398 }
4399
4400 case Primitive::kPrimDouble: {
4401 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4402 DCHECK(value.IsFpuRegister());
4403 if (index.IsConstant()) {
4404 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4405 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4406 } else {
4407 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4408 value.AsFpuRegister<XmmRegister>());
4409 }
4410 break;
4411 }
4412
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 case Primitive::kPrimVoid:
4414 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004415 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 }
4417}
4418
4419void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004421 locations->SetInAt(0, Location::RequiresRegister());
4422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004423}
4424
4425void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4426 LocationSummary* locations = instruction->GetLocations();
4427 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 Register obj = locations->InAt(0).AsRegister<Register>();
4429 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432}
4433
4434void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil659562a2015-09-14 21:26:33 +00004435 LocationSummary* locations =
4436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004437 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004438 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004439 if (instruction->HasUses()) {
4440 locations->SetOut(Location::SameAsFirstInput());
4441 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442}
4443
4444void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4445 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004446 Location index_loc = locations->InAt(0);
4447 Location length_loc = locations->InAt(1);
4448 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004449 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450
Mark Mendell99dbd682015-04-22 16:18:52 -04004451 if (length_loc.IsConstant()) {
4452 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4453 if (index_loc.IsConstant()) {
4454 // BCE will remove the bounds check if we are guarenteed to pass.
4455 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4456 if (index < 0 || index >= length) {
4457 codegen_->AddSlowPath(slow_path);
4458 __ jmp(slow_path->GetEntryLabel());
4459 } else {
4460 // Some optimization after BCE may have generated this, and we should not
4461 // generate a bounds check if it is a valid range.
4462 }
4463 return;
4464 }
4465
4466 // We have to reverse the jump condition because the length is the constant.
4467 Register index_reg = index_loc.AsRegister<Register>();
4468 __ cmpl(index_reg, Immediate(length));
4469 codegen_->AddSlowPath(slow_path);
4470 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004471 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004472 Register length = length_loc.AsRegister<Register>();
4473 if (index_loc.IsConstant()) {
4474 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4475 __ cmpl(length, Immediate(value));
4476 } else {
4477 __ cmpl(length, index_loc.AsRegister<Register>());
4478 }
4479 codegen_->AddSlowPath(slow_path);
4480 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004481 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004482}
4483
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4485 temp->SetLocations(nullptr);
4486}
4487
4488void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4489 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004490 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491}
4492
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004493void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004494 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004495 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004496}
4497
4498void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4500}
4501
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004502void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4504}
4505
4506void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004507 HBasicBlock* block = instruction->GetBlock();
4508 if (block->GetLoopInformation() != nullptr) {
4509 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4510 // The back edge will generate the suspend check.
4511 return;
4512 }
4513 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4514 // The goto will generate the suspend check.
4515 return;
4516 }
4517 GenerateSuspendCheck(instruction, nullptr);
4518}
4519
4520void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4521 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004522 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004523 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4524 if (slow_path == nullptr) {
4525 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4526 instruction->SetSlowPath(slow_path);
4527 codegen_->AddSlowPath(slow_path);
4528 if (successor != nullptr) {
4529 DCHECK(successor->IsLoopHeader());
4530 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4531 }
4532 } else {
4533 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4534 }
4535
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004536 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004537 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004538 if (successor == nullptr) {
4539 __ j(kNotEqual, slow_path->GetEntryLabel());
4540 __ Bind(slow_path->GetReturnLabel());
4541 } else {
4542 __ j(kEqual, codegen_->GetLabelOf(successor));
4543 __ jmp(slow_path->GetEntryLabel());
4544 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004545}
4546
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004547X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4548 return codegen_->GetAssembler();
4549}
4550
Mark Mendell7c8d0092015-01-26 11:21:33 -05004551void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004552 ScratchRegisterScope ensure_scratch(
4553 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4554 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4555 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4556 __ movl(temp_reg, Address(ESP, src + stack_offset));
4557 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004558}
4559
4560void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004561 ScratchRegisterScope ensure_scratch(
4562 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4563 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4564 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4565 __ movl(temp_reg, Address(ESP, src + stack_offset));
4566 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4567 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4568 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004569}
4570
4571void ParallelMoveResolverX86::EmitMove(size_t index) {
4572 MoveOperands* move = moves_.Get(index);
4573 Location source = move->GetSource();
4574 Location destination = move->GetDestination();
4575
4576 if (source.IsRegister()) {
4577 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004579 } else {
4580 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004581 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004582 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004583 } else if (source.IsFpuRegister()) {
4584 if (destination.IsFpuRegister()) {
4585 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4586 } else if (destination.IsStackSlot()) {
4587 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4588 } else {
4589 DCHECK(destination.IsDoubleStackSlot());
4590 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4591 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004592 } else if (source.IsStackSlot()) {
4593 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004594 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004595 } else if (destination.IsFpuRegister()) {
4596 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004597 } else {
4598 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004599 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4600 }
4601 } else if (source.IsDoubleStackSlot()) {
4602 if (destination.IsFpuRegister()) {
4603 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4604 } else {
4605 DCHECK(destination.IsDoubleStackSlot()) << destination;
4606 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004607 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004608 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004609 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004610 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004611 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004612 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004613 if (value == 0) {
4614 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4615 } else {
4616 __ movl(destination.AsRegister<Register>(), Immediate(value));
4617 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004618 } else {
4619 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004620 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004621 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004622 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004623 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004624 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004625 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004626 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004627 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4628 if (value == 0) {
4629 // Easy handling of 0.0.
4630 __ xorps(dest, dest);
4631 } else {
4632 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004633 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4634 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4635 __ movl(temp, Immediate(value));
4636 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004637 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004638 } else {
4639 DCHECK(destination.IsStackSlot()) << destination;
4640 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4641 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004642 } else if (constant->IsLongConstant()) {
4643 int64_t value = constant->AsLongConstant()->GetValue();
4644 int32_t low_value = Low32Bits(value);
4645 int32_t high_value = High32Bits(value);
4646 Immediate low(low_value);
4647 Immediate high(high_value);
4648 if (destination.IsDoubleStackSlot()) {
4649 __ movl(Address(ESP, destination.GetStackIndex()), low);
4650 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4651 } else {
4652 __ movl(destination.AsRegisterPairLow<Register>(), low);
4653 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4654 }
4655 } else {
4656 DCHECK(constant->IsDoubleConstant());
4657 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004658 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004659 int32_t low_value = Low32Bits(value);
4660 int32_t high_value = High32Bits(value);
4661 Immediate low(low_value);
4662 Immediate high(high_value);
4663 if (destination.IsFpuRegister()) {
4664 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4665 if (value == 0) {
4666 // Easy handling of 0.0.
4667 __ xorpd(dest, dest);
4668 } else {
4669 __ pushl(high);
4670 __ pushl(low);
4671 __ movsd(dest, Address(ESP, 0));
4672 __ addl(ESP, Immediate(8));
4673 }
4674 } else {
4675 DCHECK(destination.IsDoubleStackSlot()) << destination;
4676 __ movl(Address(ESP, destination.GetStackIndex()), low);
4677 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4678 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004679 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004680 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004681 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004682 }
4683}
4684
Mark Mendella5c19ce2015-04-01 12:51:05 -04004685void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004686 Register suggested_scratch = reg == EAX ? EBX : EAX;
4687 ScratchRegisterScope ensure_scratch(
4688 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4689
4690 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4691 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4692 __ movl(Address(ESP, mem + stack_offset), reg);
4693 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004694}
4695
Mark Mendell7c8d0092015-01-26 11:21:33 -05004696void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004697 ScratchRegisterScope ensure_scratch(
4698 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4699
4700 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4701 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4702 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4703 __ movss(Address(ESP, mem + stack_offset), reg);
4704 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004705}
4706
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004707void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004708 ScratchRegisterScope ensure_scratch1(
4709 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004710
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004711 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4712 ScratchRegisterScope ensure_scratch2(
4713 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004714
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004715 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4716 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4717 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4718 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4719 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4720 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004721}
4722
4723void ParallelMoveResolverX86::EmitSwap(size_t index) {
4724 MoveOperands* move = moves_.Get(index);
4725 Location source = move->GetSource();
4726 Location destination = move->GetDestination();
4727
4728 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004729 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4730 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4731 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4732 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4733 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004734 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004735 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004736 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004737 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004738 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4739 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004740 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4741 // Use XOR Swap algorithm to avoid a temporary.
4742 DCHECK_NE(source.reg(), destination.reg());
4743 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4744 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4745 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4746 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4747 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4748 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4749 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004750 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4751 // Take advantage of the 16 bytes in the XMM register.
4752 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4753 Address stack(ESP, destination.GetStackIndex());
4754 // Load the double into the high doubleword.
4755 __ movhpd(reg, stack);
4756
4757 // Store the low double into the destination.
4758 __ movsd(stack, reg);
4759
4760 // Move the high double to the low double.
4761 __ psrldq(reg, Immediate(8));
4762 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4763 // Take advantage of the 16 bytes in the XMM register.
4764 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4765 Address stack(ESP, source.GetStackIndex());
4766 // Load the double into the high doubleword.
4767 __ movhpd(reg, stack);
4768
4769 // Store the low double into the destination.
4770 __ movsd(stack, reg);
4771
4772 // Move the high double to the low double.
4773 __ psrldq(reg, Immediate(8));
4774 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4775 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4776 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004777 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004778 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004779 }
4780}
4781
4782void ParallelMoveResolverX86::SpillScratch(int reg) {
4783 __ pushl(static_cast<Register>(reg));
4784}
4785
4786void ParallelMoveResolverX86::RestoreScratch(int reg) {
4787 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004788}
4789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004790void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004791 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4792 ? LocationSummary::kCallOnSlowPath
4793 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004794 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004795 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004796 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004797 locations->SetOut(Location::RequiresRegister());
4798}
4799
4800void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004801 LocationSummary* locations = cls->GetLocations();
4802 Register out = locations->Out().AsRegister<Register>();
4803 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004804 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004805 DCHECK(!cls->CanCallRuntime());
4806 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004807 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004808 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004809 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004810 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004811 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004812 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004813 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004814
4815 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4816 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4817 codegen_->AddSlowPath(slow_path);
4818 __ testl(out, out);
4819 __ j(kEqual, slow_path->GetEntryLabel());
4820 if (cls->MustGenerateClinitCheck()) {
4821 GenerateClassInitializationCheck(slow_path, out);
4822 } else {
4823 __ Bind(slow_path->GetExitLabel());
4824 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004825 }
4826}
4827
4828void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4829 LocationSummary* locations =
4830 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4831 locations->SetInAt(0, Location::RequiresRegister());
4832 if (check->HasUses()) {
4833 locations->SetOut(Location::SameAsFirstInput());
4834 }
4835}
4836
4837void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004838 // We assume the class to not be null.
4839 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4840 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004842 GenerateClassInitializationCheck(slow_path,
4843 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004844}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004845
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004846void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4847 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004848 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4849 Immediate(mirror::Class::kStatusInitialized));
4850 __ j(kLess, slow_path->GetEntryLabel());
4851 __ Bind(slow_path->GetExitLabel());
4852 // No need for memory fence, thanks to the X86 memory model.
4853}
4854
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004855void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4856 LocationSummary* locations =
4857 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004858 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004859 locations->SetOut(Location::RequiresRegister());
4860}
4861
4862void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4863 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4864 codegen_->AddSlowPath(slow_path);
4865
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004866 LocationSummary* locations = load->GetLocations();
4867 Register out = locations->Out().AsRegister<Register>();
4868 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004869 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004870 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004871 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004872 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004873 __ testl(out, out);
4874 __ j(kEqual, slow_path->GetEntryLabel());
4875 __ Bind(slow_path->GetExitLabel());
4876}
4877
David Brazdilcb1c0552015-08-04 16:22:25 +01004878static Address GetExceptionTlsAddress() {
4879 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4880}
4881
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004882void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4883 LocationSummary* locations =
4884 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4885 locations->SetOut(Location::RequiresRegister());
4886}
4887
4888void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004889 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4890}
4891
4892void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4893 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4894}
4895
4896void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4897 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004898}
4899
4900void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4901 LocationSummary* locations =
4902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4903 InvokeRuntimeCallingConvention calling_convention;
4904 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4905}
4906
4907void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004908 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4909 instruction,
4910 instruction->GetDexPc(),
4911 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004912}
4913
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004914void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004915 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4916 ? LocationSummary::kNoCall
4917 : LocationSummary::kCallOnSlowPath;
4918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4919 locations->SetInAt(0, Location::RequiresRegister());
4920 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004921 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004922 locations->SetOut(Location::RequiresRegister());
4923}
4924
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004925void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004926 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004927 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004928 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004929 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004930 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4931 Label done, zero;
4932 SlowPathCodeX86* slow_path = nullptr;
4933
4934 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004935 // Avoid null check if we know obj is not null.
4936 if (instruction->MustDoNullCheck()) {
4937 __ testl(obj, obj);
4938 __ j(kEqual, &zero);
4939 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004940 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004941 __ movl(out, Address(obj, class_offset));
4942 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004943 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004944 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004945 } else {
4946 DCHECK(cls.IsStackSlot()) << cls;
4947 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4948 }
4949
4950 if (instruction->IsClassFinal()) {
4951 // Classes must be equal for the instanceof to succeed.
4952 __ j(kNotEqual, &zero);
4953 __ movl(out, Immediate(1));
4954 __ jmp(&done);
4955 } else {
4956 // If the classes are not equal, we go into a slow path.
4957 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004958 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004959 codegen_->AddSlowPath(slow_path);
4960 __ j(kNotEqual, slow_path->GetEntryLabel());
4961 __ movl(out, Immediate(1));
4962 __ jmp(&done);
4963 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004964
4965 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4966 __ Bind(&zero);
4967 __ movl(out, Immediate(0));
4968 }
4969
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004970 if (slow_path != nullptr) {
4971 __ Bind(slow_path->GetExitLabel());
4972 }
4973 __ Bind(&done);
4974}
4975
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004976void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4978 instruction, LocationSummary::kCallOnSlowPath);
4979 locations->SetInAt(0, Location::RequiresRegister());
4980 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004981 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004982 locations->AddTemp(Location::RequiresRegister());
4983}
4984
4985void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4986 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004987 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004988 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004990 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004991 SlowPathCodeX86* slow_path =
4992 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004993 codegen_->AddSlowPath(slow_path);
4994
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004995 // Avoid null check if we know obj is not null.
4996 if (instruction->MustDoNullCheck()) {
4997 __ testl(obj, obj);
4998 __ j(kEqual, slow_path->GetExitLabel());
4999 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005000 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01005001 __ movl(temp, Address(obj, class_offset));
5002 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005003 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005004 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005005 } else {
5006 DCHECK(cls.IsStackSlot()) << cls;
5007 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5008 }
Roland Levillain4d027112015-07-01 15:41:14 +01005009 // The checkcast succeeds if the classes are equal (fast path).
5010 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005011 __ j(kNotEqual, slow_path->GetEntryLabel());
5012 __ Bind(slow_path->GetExitLabel());
5013}
5014
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005015void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5018 InvokeRuntimeCallingConvention calling_convention;
5019 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5020}
5021
5022void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005023 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5024 : QUICK_ENTRY_POINT(pUnlockObject),
5025 instruction,
5026 instruction->GetDexPc(),
5027 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005028}
5029
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005030void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5031void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5032void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5033
5034void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5035 LocationSummary* locations =
5036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5037 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5038 || instruction->GetResultType() == Primitive::kPrimLong);
5039 locations->SetInAt(0, Location::RequiresRegister());
5040 locations->SetInAt(1, Location::Any());
5041 locations->SetOut(Location::SameAsFirstInput());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5045 HandleBitwiseOperation(instruction);
5046}
5047
5048void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5049 HandleBitwiseOperation(instruction);
5050}
5051
5052void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5053 HandleBitwiseOperation(instruction);
5054}
5055
5056void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5057 LocationSummary* locations = instruction->GetLocations();
5058 Location first = locations->InAt(0);
5059 Location second = locations->InAt(1);
5060 DCHECK(first.Equals(locations->Out()));
5061
5062 if (instruction->GetResultType() == Primitive::kPrimInt) {
5063 if (second.IsRegister()) {
5064 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005065 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005066 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005068 } else {
5069 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005070 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005071 }
5072 } else if (second.IsConstant()) {
5073 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005074 __ andl(first.AsRegister<Register>(),
5075 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005076 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005077 __ orl(first.AsRegister<Register>(),
5078 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005079 } else {
5080 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005081 __ xorl(first.AsRegister<Register>(),
5082 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005083 }
5084 } else {
5085 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005086 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005087 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005088 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005089 } else {
5090 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005092 }
5093 }
5094 } else {
5095 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5096 if (second.IsRegisterPair()) {
5097 if (instruction->IsAnd()) {
5098 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5099 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5100 } else if (instruction->IsOr()) {
5101 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5102 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5103 } else {
5104 DCHECK(instruction->IsXor());
5105 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5106 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5107 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005108 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005109 if (instruction->IsAnd()) {
5110 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5111 __ andl(first.AsRegisterPairHigh<Register>(),
5112 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5113 } else if (instruction->IsOr()) {
5114 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5115 __ orl(first.AsRegisterPairHigh<Register>(),
5116 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5117 } else {
5118 DCHECK(instruction->IsXor());
5119 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5120 __ xorl(first.AsRegisterPairHigh<Register>(),
5121 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5122 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005123 } else {
5124 DCHECK(second.IsConstant()) << second;
5125 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005126 int32_t low_value = Low32Bits(value);
5127 int32_t high_value = High32Bits(value);
5128 Immediate low(low_value);
5129 Immediate high(high_value);
5130 Register first_low = first.AsRegisterPairLow<Register>();
5131 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005132 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005133 if (low_value == 0) {
5134 __ xorl(first_low, first_low);
5135 } else if (low_value != -1) {
5136 __ andl(first_low, low);
5137 }
5138 if (high_value == 0) {
5139 __ xorl(first_high, first_high);
5140 } else if (high_value != -1) {
5141 __ andl(first_high, high);
5142 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005143 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005144 if (low_value != 0) {
5145 __ orl(first_low, low);
5146 }
5147 if (high_value != 0) {
5148 __ orl(first_high, high);
5149 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005150 } else {
5151 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005152 if (low_value != 0) {
5153 __ xorl(first_low, low);
5154 }
5155 if (high_value != 0) {
5156 __ xorl(first_high, high);
5157 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005158 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005159 }
5160 }
5161}
5162
Calin Juravleb1498f62015-02-16 13:13:29 +00005163void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5164 // Nothing to do, this should be removed during prepare for register allocator.
5165 UNUSED(instruction);
5166 LOG(FATAL) << "Unreachable";
5167}
5168
5169void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5170 // Nothing to do, this should be removed during prepare for register allocator.
5171 UNUSED(instruction);
5172 LOG(FATAL) << "Unreachable";
5173}
5174
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005175void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5176 DCHECK(codegen_->IsBaseline());
5177 LocationSummary* locations =
5178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5179 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5180}
5181
5182void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5183 DCHECK(codegen_->IsBaseline());
5184 // Will be generated at use site.
5185}
5186
Mark Mendell0616ae02015-04-17 12:49:27 -04005187void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5188 HX86ComputeBaseMethodAddress* insn) {
5189 LocationSummary* locations =
5190 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5191 locations->SetOut(Location::RequiresRegister());
5192}
5193
5194void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5195 HX86ComputeBaseMethodAddress* insn) {
5196 LocationSummary* locations = insn->GetLocations();
5197 Register reg = locations->Out().AsRegister<Register>();
5198
5199 // Generate call to next instruction.
5200 Label next_instruction;
5201 __ call(&next_instruction);
5202 __ Bind(&next_instruction);
5203
5204 // Remember this offset for later use with constant area.
5205 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5206
5207 // Grab the return address off the stack.
5208 __ popl(reg);
5209}
5210
5211void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5212 HX86LoadFromConstantTable* insn) {
5213 LocationSummary* locations =
5214 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5215
5216 locations->SetInAt(0, Location::RequiresRegister());
5217 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5218
5219 // If we don't need to be materialized, we only need the inputs to be set.
5220 if (!insn->NeedsMaterialization()) {
5221 return;
5222 }
5223
5224 switch (insn->GetType()) {
5225 case Primitive::kPrimFloat:
5226 case Primitive::kPrimDouble:
5227 locations->SetOut(Location::RequiresFpuRegister());
5228 break;
5229
5230 case Primitive::kPrimInt:
5231 locations->SetOut(Location::RequiresRegister());
5232 break;
5233
5234 default:
5235 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5236 }
5237}
5238
5239void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5240 if (!insn->NeedsMaterialization()) {
5241 return;
5242 }
5243
5244 LocationSummary* locations = insn->GetLocations();
5245 Location out = locations->Out();
5246 Register const_area = locations->InAt(0).AsRegister<Register>();
5247 HConstant *value = insn->GetConstant();
5248
5249 switch (insn->GetType()) {
5250 case Primitive::kPrimFloat:
5251 __ movss(out.AsFpuRegister<XmmRegister>(),
5252 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5253 break;
5254
5255 case Primitive::kPrimDouble:
5256 __ movsd(out.AsFpuRegister<XmmRegister>(),
5257 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5258 break;
5259
5260 case Primitive::kPrimInt:
5261 __ movl(out.AsRegister<Register>(),
5262 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5263 break;
5264
5265 default:
5266 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5267 }
5268}
5269
5270void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5271 // Generate the constant area if needed.
5272 X86Assembler* assembler = GetAssembler();
5273 if (!assembler->IsConstantAreaEmpty()) {
5274 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5275 // byte values.
5276 assembler->Align(4, 0);
5277 constant_area_start_ = assembler->CodeSize();
5278 assembler->AddConstantArea();
5279 }
5280
5281 // And finish up.
5282 CodeGenerator::Finalize(allocator);
5283}
5284
5285/**
5286 * Class to handle late fixup of offsets into constant area.
5287 */
5288class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5289 public:
5290 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5291 : codegen_(codegen), offset_into_constant_area_(offset) {}
5292
5293 private:
5294 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5295 // Patch the correct offset for the instruction. The place to patch is the
5296 // last 4 bytes of the instruction.
5297 // The value to patch is the distance from the offset in the constant area
5298 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5299 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5300 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5301
5302 // Patch in the right value.
5303 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5304 }
5305
5306 const CodeGeneratorX86& codegen_;
5307
5308 // Location in constant area that the fixup refers to.
5309 int offset_into_constant_area_;
5310};
5311
5312Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5313 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5314 return Address(reg, kDummy32BitOffset, fixup);
5315}
5316
5317Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5318 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5319 return Address(reg, kDummy32BitOffset, fixup);
5320}
5321
5322Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5323 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5324 return Address(reg, kDummy32BitOffset, fixup);
5325}
5326
5327Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5328 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5329 return Address(reg, kDummy32BitOffset, fixup);
5330}
5331
5332/**
5333 * Finds instructions that need the constant area base as an input.
5334 */
5335class ConstantHandlerVisitor : public HGraphVisitor {
5336 public:
5337 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5338
5339 private:
5340 void VisitAdd(HAdd* add) OVERRIDE {
5341 BinaryFP(add);
5342 }
5343
5344 void VisitSub(HSub* sub) OVERRIDE {
5345 BinaryFP(sub);
5346 }
5347
5348 void VisitMul(HMul* mul) OVERRIDE {
5349 BinaryFP(mul);
5350 }
5351
5352 void VisitDiv(HDiv* div) OVERRIDE {
5353 BinaryFP(div);
5354 }
5355
5356 void VisitReturn(HReturn* ret) OVERRIDE {
5357 HConstant* value = ret->InputAt(0)->AsConstant();
5358 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5359 ReplaceInput(ret, value, 0, true);
5360 }
5361 }
5362
5363 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5364 HandleInvoke(invoke);
5365 }
5366
5367 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5368 HandleInvoke(invoke);
5369 }
5370
5371 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5372 HandleInvoke(invoke);
5373 }
5374
5375 void BinaryFP(HBinaryOperation* bin) {
5376 HConstant* rhs = bin->InputAt(1)->AsConstant();
5377 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5378 ReplaceInput(bin, rhs, 1, false);
5379 }
5380 }
5381
5382 void InitializeConstantAreaPointer(HInstruction* user) {
5383 // Ensure we only initialize the pointer once.
5384 if (base_ != nullptr) {
5385 return;
5386 }
5387
5388 HGraph* graph = GetGraph();
5389 HBasicBlock* entry = graph->GetEntryBlock();
5390 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5391 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5392 entry->InsertInstructionBefore(base_, insert_pos);
5393 DCHECK(base_ != nullptr);
5394 }
5395
5396 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5397 InitializeConstantAreaPointer(insn);
5398 HGraph* graph = GetGraph();
5399 HBasicBlock* block = insn->GetBlock();
5400 HX86LoadFromConstantTable* load_constant =
5401 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5402 block->InsertInstructionBefore(load_constant, insn);
5403 insn->ReplaceInput(load_constant, input_index);
5404 }
5405
5406 void HandleInvoke(HInvoke* invoke) {
5407 // Ensure that we can load FP arguments from the constant area.
5408 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5409 HConstant* input = invoke->InputAt(i)->AsConstant();
5410 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5411 ReplaceInput(invoke, input, i, true);
5412 }
5413 }
5414 }
5415
5416 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5417 // input to the HX86LoadFromConstantTable instructions.
5418 HX86ComputeBaseMethodAddress* base_;
5419};
5420
5421void ConstantAreaFixups::Run() {
5422 ConstantHandlerVisitor visitor(graph_);
5423 visitor.VisitInsertionOrder();
5424}
5425
Roland Levillain4d027112015-07-01 15:41:14 +01005426#undef __
5427
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005428} // namespace x86
5429} // namespace art