blob: c7ddabb5fb3707f2f138b968406fff7b0fe27dd0 [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());
David Brazdil77a48ae2015-09-15 12:34:04 +000059 if (instruction_->CanThrowIntoCatchBlock()) {
60 // Live registers will be restored in the catch block if caught.
61 SaveLiveRegisters(codegen, instruction_->GetLocations());
62 }
Alexandre Rames8158f282015-08-07 10:26:17 +010063 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
64 instruction_,
65 instruction_->GetDexPc(),
66 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 }
68
Alexandre Rames8158f282015-08-07 10:26:17 +010069 bool IsFatal() const OVERRIDE { return true; }
70
Alexandre Rames9931f312015-06-19 14:47:01 +010071 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
72
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
76};
77
Calin Juravled0d48522014-11-04 16:40:20 +000078class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
79 public:
80 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
81
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010083 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000085 if (instruction_->CanThrowIntoCatchBlock()) {
86 // Live registers will be restored in the catch block if caught.
87 SaveLiveRegisters(codegen, instruction_->GetLocations());
88 }
Alexandre Rames8158f282015-08-07 10:26:17 +010089 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
90 instruction_,
91 instruction_->GetDexPc(),
92 this);
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
100 HDivZeroCheck* const instruction_;
101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
102};
103
Calin Juravlebacfec32014-11-14 15:54:36 +0000104class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100106 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
Alexandre Rames9931f312015-06-19 14:47:01 +0100118 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
119
Calin Juravled0d48522014-11-04 16:40:20 +0000120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000134 // We're moving two locations to locations that could overlap, so we need a parallel
135 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000136 if (instruction_->CanThrowIntoCatchBlock()) {
137 // Live registers will be restored in the catch block if caught.
138 SaveLiveRegisters(codegen, instruction_->GetLocations());
139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000141 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100142 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100144 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
147 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Alexandre Rames9931f312015-06-19 14:47:01 +0100156 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160
161 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
162};
163
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000166 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000169 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100170 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100173 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
174 instruction_,
175 instruction_->GetDexPc(),
176 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 if (successor_ == nullptr) {
179 __ jmp(GetReturnLabel());
180 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100181 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 }
184
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 Label* GetReturnLabel() {
186 DCHECK(successor_ == nullptr);
187 return &return_label_;
188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100190 HBasicBlock* GetSuccessor() const {
191 return successor_;
192 }
193
Alexandre Rames9931f312015-06-19 14:47:01 +0100194 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
195
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 private:
197 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 Label return_label_;
200
201 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
202};
203
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000204class LoadStringSlowPathX86 : public SlowPathCodeX86 {
205 public:
206 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
207
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209 LocationSummary* locations = instruction_->GetLocations();
210 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
211
212 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
213 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000214 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215
216 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800217 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
219 instruction_,
220 instruction_->GetDexPc(),
221 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000224
225 __ jmp(GetExitLabel());
226 }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 private:
231 HLoadString* const instruction_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
234};
235
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236class LoadClassSlowPathX86 : public SlowPathCodeX86 {
237 public:
238 LoadClassSlowPathX86(HLoadClass* cls,
239 HInstruction* at,
240 uint32_t dex_pc,
241 bool do_clinit)
242 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
243 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
244 }
245
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 LocationSummary* locations = at_->GetLocations();
248 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
249 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251
252 InvokeRuntimeCallingConvention calling_convention;
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100254 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
255 : QUICK_ENTRY_POINT(pInitializeType),
256 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 __ jmp(GetExitLabel());
267 }
268
Alexandre Rames9931f312015-06-19 14:47:01 +0100269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
270
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
275 // The instruction where this slow path is happening.
276 // (Might be the load class or an initialization check).
277 HInstruction* const at_;
278
279 // The dex PC of `at_`.
280 const uint32_t dex_pc_;
281
282 // Whether to initialize the class.
283 const bool do_clinit_;
284
285 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
286};
287
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
289 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100290 explicit TypeCheckSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
295 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 DCHECK(instruction_->IsCheckCast()
297 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
300 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000301 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
303 // We're moving two locations to locations that could overlap, so we need a parallel
304 // move resolver.
305 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000306 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000308 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100309 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100311 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
312 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100315 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
316 instruction_,
317 instruction_->GetDexPc(),
318 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 } else {
320 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100321 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
322 instruction_,
323 instruction_->GetDexPc(),
324 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 }
326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 if (instruction_->IsInstanceOf()) {
328 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
329 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000330 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 __ jmp(GetExitLabel());
333 }
334
Alexandre Rames9931f312015-06-19 14:47:01 +0100335 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
336
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
341};
342
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
344 public:
345 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
346 : instruction_(instruction) {}
347
348 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100349 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
360
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 private:
362 HInstruction* const instruction_;
363 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
364};
365
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100366#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100367#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100368
Roland Levillain4fa13f62015-07-06 18:11:54 +0100369inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700370 switch (cond) {
371 case kCondEQ: return kEqual;
372 case kCondNE: return kNotEqual;
373 case kCondLT: return kLess;
374 case kCondLE: return kLessEqual;
375 case kCondGT: return kGreater;
376 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700377 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100378 LOG(FATAL) << "Unreachable";
379 UNREACHABLE();
380}
381
382inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
383 switch (cond) {
384 case kCondEQ: return kEqual;
385 case kCondNE: return kNotEqual;
386 case kCondLT: return kBelow;
387 case kCondLE: return kBelowEqual;
388 case kCondGT: return kAbove;
389 case kCondGE: return kAboveEqual;
390 }
391 LOG(FATAL) << "Unreachable";
392 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700393}
394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100395void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100396 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397}
398
399void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100400 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401}
402
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100403size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
404 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
405 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100406}
407
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
409 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
410 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100411}
412
Mark Mendell7c8d0092015-01-26 11:21:33 -0500413size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
414 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
415 return GetFloatingPointSpillSlotSize();
416}
417
418size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
419 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
420 return GetFloatingPointSpillSlotSize();
421}
422
Alexandre Rames8158f282015-08-07 10:26:17 +0100423void CodeGeneratorX86::InvokeRuntime(Address entry_point,
424 HInstruction* instruction,
425 uint32_t dex_pc,
426 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100427 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100428 __ fs()->call(entry_point);
429 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100430}
431
Mark Mendellfb8d2792015-03-31 22:16:59 -0400432CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
433 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100434 const CompilerOptions& compiler_options,
435 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500436 : CodeGenerator(graph,
437 kNumberOfCpuRegisters,
438 kNumberOfXmmRegisters,
439 kNumberOfRegisterPairs,
440 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
441 arraysize(kCoreCalleeSaves))
442 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100443 0,
444 compiler_options,
445 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100446 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100447 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100448 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400449 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000450 isa_features_(isa_features),
451 method_patches_(graph->GetArena()->Adapter()),
452 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000453 // Use a fake return address register to mimic Quick.
454 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100455}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100456
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100457Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458 switch (type) {
459 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100460 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100461 X86ManagedRegister pair =
462 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100463 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
464 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100465 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
466 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100467 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100469 }
470
471 case Primitive::kPrimByte:
472 case Primitive::kPrimBoolean:
473 case Primitive::kPrimChar:
474 case Primitive::kPrimShort:
475 case Primitive::kPrimInt:
476 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
481 X86ManagedRegister current =
482 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
483 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100484 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100485 }
486 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100487 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100488 }
489
490 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100491 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100492 return Location::FpuRegisterLocation(
493 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100494 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100495
496 case Primitive::kPrimVoid:
497 LOG(FATAL) << "Unreachable type " << type;
498 }
499
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100500 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501}
502
Mark Mendell5f874182015-03-04 15:42:45 -0500503void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100505 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100506
507 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100508 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100509
Mark Mendell5f874182015-03-04 15:42:45 -0500510 if (is_baseline) {
511 blocked_core_registers_[EBP] = true;
512 blocked_core_registers_[ESI] = true;
513 blocked_core_registers_[EDI] = true;
514 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100515
516 UpdateBlockedPairRegisters();
517}
518
519void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
520 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
521 X86ManagedRegister current =
522 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
523 if (blocked_core_registers_[current.AsRegisterPairLow()]
524 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
525 blocked_register_pairs_[i] = true;
526 }
527 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100528}
529
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100530InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
531 : HGraphVisitor(graph),
532 assembler_(codegen->GetAssembler()),
533 codegen_(codegen) {}
534
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100535static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100536 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100537}
538
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000539void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100540 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000541 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000542 bool skip_overflow_check =
543 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000544 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000545
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000546 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100547 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100548 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100549 }
550
Mark Mendell5f874182015-03-04 15:42:45 -0500551 if (HasEmptyFrame()) {
552 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000553 }
Mark Mendell5f874182015-03-04 15:42:45 -0500554
555 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
556 Register reg = kCoreCalleeSaves[i];
557 if (allocated_registers_.ContainsCoreRegister(reg)) {
558 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100559 __ cfi().AdjustCFAOffset(kX86WordSize);
560 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500561 }
562 }
563
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100564 int adjust = GetFrameSize() - FrameEntrySpillSize();
565 __ subl(ESP, Immediate(adjust));
566 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100567 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000568}
569
570void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100571 __ cfi().RememberState();
572 if (!HasEmptyFrame()) {
573 int adjust = GetFrameSize() - FrameEntrySpillSize();
574 __ addl(ESP, Immediate(adjust));
575 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500576
David Srbeckyc34dc932015-04-12 09:27:43 +0100577 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
578 Register reg = kCoreCalleeSaves[i];
579 if (allocated_registers_.ContainsCoreRegister(reg)) {
580 __ popl(reg);
581 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
582 __ cfi().Restore(DWARFReg(reg));
583 }
Mark Mendell5f874182015-03-04 15:42:45 -0500584 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000585 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100586 __ ret();
587 __ cfi().RestoreState();
588 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000589}
590
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100591void CodeGeneratorX86::Bind(HBasicBlock* block) {
592 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000593}
594
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100595Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
596 switch (load->GetType()) {
597 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100598 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100599 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100600
601 case Primitive::kPrimInt:
602 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100605
606 case Primitive::kPrimBoolean:
607 case Primitive::kPrimByte:
608 case Primitive::kPrimChar:
609 case Primitive::kPrimShort:
610 case Primitive::kPrimVoid:
611 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700612 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613 }
614
615 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700616 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100617}
618
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100619Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
620 switch (type) {
621 case Primitive::kPrimBoolean:
622 case Primitive::kPrimByte:
623 case Primitive::kPrimChar:
624 case Primitive::kPrimShort:
625 case Primitive::kPrimInt:
626 case Primitive::kPrimNot:
627 return Location::RegisterLocation(EAX);
628
629 case Primitive::kPrimLong:
630 return Location::RegisterPairLocation(EAX, EDX);
631
632 case Primitive::kPrimVoid:
633 return Location::NoLocation();
634
635 case Primitive::kPrimDouble:
636 case Primitive::kPrimFloat:
637 return Location::FpuRegisterLocation(XMM0);
638 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100639
640 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100641}
642
643Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
644 return Location::RegisterLocation(kMethodRegisterArgument);
645}
646
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100647Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100648 switch (type) {
649 case Primitive::kPrimBoolean:
650 case Primitive::kPrimByte:
651 case Primitive::kPrimChar:
652 case Primitive::kPrimShort:
653 case Primitive::kPrimInt:
654 case Primitive::kPrimNot: {
655 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000656 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100657 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100659 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000660 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100661 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100662 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100663
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000664 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100665 uint32_t index = gp_index_;
666 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000667 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100668 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
670 calling_convention.GetRegisterPairAt(index));
671 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100672 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000673 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
674 }
675 }
676
677 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100678 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000679 stack_index_++;
680 if (index < calling_convention.GetNumberOfFpuRegisters()) {
681 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
682 } else {
683 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
684 }
685 }
686
687 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100688 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000689 stack_index_ += 2;
690 if (index < calling_convention.GetNumberOfFpuRegisters()) {
691 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
692 } else {
693 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100694 }
695 }
696
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100697 case Primitive::kPrimVoid:
698 LOG(FATAL) << "Unexpected parameter type " << type;
699 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100700 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100701 return Location();
702}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100703
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704void CodeGeneratorX86::Move32(Location destination, Location source) {
705 if (source.Equals(destination)) {
706 return;
707 }
708 if (destination.IsRegister()) {
709 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000712 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 } else {
714 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000715 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 } else if (destination.IsFpuRegister()) {
718 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000719 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000721 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 } else {
723 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000724 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000727 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100728 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000731 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500732 } else if (source.IsConstant()) {
733 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000734 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500735 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 } else {
737 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100738 __ pushl(Address(ESP, source.GetStackIndex()));
739 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 }
741 }
742}
743
744void CodeGeneratorX86::Move64(Location destination, Location source) {
745 if (source.Equals(destination)) {
746 return;
747 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100748 if (destination.IsRegisterPair()) {
749 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000750 EmitParallelMoves(
751 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
752 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100753 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000754 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100755 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
756 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 } else if (source.IsFpuRegister()) {
758 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000760 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100762 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
763 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
765 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100766 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500767 if (source.IsFpuRegister()) {
768 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
769 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000770 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100771 } else {
772 LOG(FATAL) << "Unimplemented";
773 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000775 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000777 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100778 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100780 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000782 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000783 } else if (source.IsConstant()) {
784 HConstant* constant = source.GetConstant();
785 int64_t value;
786 if (constant->IsLongConstant()) {
787 value = constant->AsLongConstant()->GetValue();
788 } else {
789 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000790 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000791 }
792 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
793 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100794 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000795 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000796 EmitParallelMoves(
797 Location::StackSlot(source.GetStackIndex()),
798 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100799 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000800 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100801 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
802 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 }
804 }
805}
806
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100807void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000808 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100809 if (instruction->IsCurrentMethod()) {
810 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
811 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000812 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100813 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000814 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000815 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
816 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000817 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000818 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000819 } else if (location.IsStackSlot()) {
820 __ movl(Address(ESP, location.GetStackIndex()), imm);
821 } else {
822 DCHECK(location.IsConstant());
823 DCHECK_EQ(location.GetConstant(), const_to_move);
824 }
825 } else if (const_to_move->IsLongConstant()) {
826 int64_t value = const_to_move->AsLongConstant()->GetValue();
827 if (location.IsRegisterPair()) {
828 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
829 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
830 } else if (location.IsDoubleStackSlot()) {
831 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000832 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
833 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000834 } else {
835 DCHECK(location.IsConstant());
836 DCHECK_EQ(location.GetConstant(), instruction);
837 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000839 } else if (instruction->IsTemporary()) {
840 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000841 if (temp_location.IsStackSlot()) {
842 Move32(location, temp_location);
843 } else {
844 DCHECK(temp_location.IsDoubleStackSlot());
845 Move64(location, temp_location);
846 }
Roland Levillain476df552014-10-09 17:51:36 +0100847 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100848 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 switch (instruction->GetType()) {
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimInt:
855 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 case Primitive::kPrimFloat:
857 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100858 break;
859
860 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100861 case Primitive::kPrimDouble:
862 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 break;
864
865 default:
866 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
867 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100869 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 switch (instruction->GetType()) {
871 case Primitive::kPrimBoolean:
872 case Primitive::kPrimByte:
873 case Primitive::kPrimChar:
874 case Primitive::kPrimShort:
875 case Primitive::kPrimInt:
876 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000878 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 break;
880
881 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000883 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 break;
885
886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000889 }
890}
891
David Brazdilfc6a86a2015-06-26 10:33:45 +0000892void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100893 DCHECK(!successor->IsExitBlock());
894
895 HBasicBlock* block = got->GetBlock();
896 HInstruction* previous = got->GetPrevious();
897
898 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000899 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100900 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
901 return;
902 }
903
904 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
905 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
906 }
907 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000908 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000909 }
910}
911
David Brazdilfc6a86a2015-06-26 10:33:45 +0000912void LocationsBuilderX86::VisitGoto(HGoto* got) {
913 got->SetLocations(nullptr);
914}
915
916void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
917 HandleGoto(got, got->GetSuccessor());
918}
919
920void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
921 try_boundary->SetLocations(nullptr);
922}
923
924void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
925 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
926 if (!successor->IsExitBlock()) {
927 HandleGoto(try_boundary, successor);
928 }
929}
930
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000931void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000933}
934
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000935void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700936 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000937}
938
Mark Mendellc4701932015-04-10 13:18:51 -0400939void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
940 Label* true_label,
941 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 if (cond->IsFPConditionTrueIfNaN()) {
943 __ j(kUnordered, true_label);
944 } else if (cond->IsFPConditionFalseIfNaN()) {
945 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400946 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100947 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400948}
949
950void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
951 Label* true_label,
952 Label* false_label) {
953 LocationSummary* locations = cond->GetLocations();
954 Location left = locations->InAt(0);
955 Location right = locations->InAt(1);
956 IfCondition if_cond = cond->GetCondition();
957
Mark Mendellc4701932015-04-10 13:18:51 -0400958 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400960 IfCondition true_high_cond = if_cond;
961 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400963
964 // Set the conditions for the test, remembering that == needs to be
965 // decided using the low words.
966 switch (if_cond) {
967 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400968 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100969 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400970 break;
971 case kCondLT:
972 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400973 break;
974 case kCondLE:
975 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400976 break;
977 case kCondGT:
978 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400979 break;
980 case kCondGE:
981 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400982 break;
983 }
984
985 if (right.IsConstant()) {
986 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400987 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100988 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400989
990 if (val_high == 0) {
991 __ testl(left_high, left_high);
992 } else {
993 __ cmpl(left_high, Immediate(val_high));
994 }
995 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100996 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400997 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100998 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400999 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001000 __ j(X86SignedCondition(true_high_cond), true_label);
1001 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001002 }
1003 // Must be equal high, so compare the lows.
1004 if (val_low == 0) {
1005 __ testl(left_low, left_low);
1006 } else {
1007 __ cmpl(left_low, Immediate(val_low));
1008 }
1009 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001010 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001011 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001012
1013 __ cmpl(left_high, right_high);
1014 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001015 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001016 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001017 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001018 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001019 __ j(X86SignedCondition(true_high_cond), true_label);
1020 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001021 }
1022 // Must be equal high, so compare the lows.
1023 __ cmpl(left_low, right_low);
1024 }
1025 // The last comparison might be unsigned.
1026 __ j(final_condition, true_label);
1027}
1028
1029void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1030 HCondition* condition,
1031 Label* true_target,
1032 Label* false_target,
1033 Label* always_true_target) {
1034 LocationSummary* locations = condition->GetLocations();
1035 Location left = locations->InAt(0);
1036 Location right = locations->InAt(1);
1037
1038 // We don't want true_target as a nullptr.
1039 if (true_target == nullptr) {
1040 true_target = always_true_target;
1041 }
1042 bool falls_through = (false_target == nullptr);
1043
1044 // FP compares don't like null false_targets.
1045 if (false_target == nullptr) {
1046 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1047 }
1048
1049 Primitive::Type type = condition->InputAt(0)->GetType();
1050 switch (type) {
1051 case Primitive::kPrimLong:
1052 GenerateLongComparesAndJumps(condition, true_target, false_target);
1053 break;
1054 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001055 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1056 GenerateFPJumps(condition, true_target, false_target);
1057 break;
1058 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001059 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1060 GenerateFPJumps(condition, true_target, false_target);
1061 break;
1062 default:
1063 LOG(FATAL) << "Unexpected compare type " << type;
1064 }
1065
1066 if (!falls_through) {
1067 __ jmp(false_target);
1068 }
1069}
1070
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001071void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1072 Label* true_target,
1073 Label* false_target,
1074 Label* always_true_target) {
1075 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001076 if (cond->IsIntConstant()) {
1077 // Constant condition, statically compared against 1.
1078 int32_t cond_value = cond->AsIntConstant()->GetValue();
1079 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001080 if (always_true_target != nullptr) {
1081 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001082 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001083 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001084 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001085 DCHECK_EQ(cond_value, 0);
1086 }
1087 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001089 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1090 // Moves do not affect the eflags register, so if the condition is
1091 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001092 // again. We can't use the eflags on long/FP conditions if they are
1093 // materialized due to the complex branching.
1094 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001095 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001096 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001097 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1098 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 if (!eflags_set) {
1100 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001101 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001102 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001103 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 } else {
1105 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1106 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001107 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001109 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001110 }
1111 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001112 // Condition has not been materialized, use its inputs as the
1113 // comparison and its condition as the branch condition.
1114
Mark Mendellc4701932015-04-10 13:18:51 -04001115 // Is this a long or FP comparison that has been folded into the HCondition?
1116 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1117 // Generate the comparison directly.
1118 GenerateCompareTestAndBranch(instruction->AsIf(),
1119 cond->AsCondition(),
1120 true_target,
1121 false_target,
1122 always_true_target);
1123 return;
1124 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001125
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001126 Location lhs = cond->GetLocations()->InAt(0);
1127 Location rhs = cond->GetLocations()->InAt(1);
1128 // LHS is guaranteed to be in a register (see
1129 // LocationsBuilderX86::VisitCondition).
1130 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001131 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001132 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001133 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001134 if (constant == 0) {
1135 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1136 } else {
1137 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1138 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001140 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001141 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001142 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001143 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001144 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001145 if (false_target != nullptr) {
1146 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001147 }
1148}
1149
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001150void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1151 LocationSummary* locations =
1152 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1153 HInstruction* cond = if_instr->InputAt(0);
1154 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1155 locations->SetInAt(0, Location::Any());
1156 }
1157}
1158
1159void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1160 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1161 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1162 Label* always_true_target = true_target;
1163 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1164 if_instr->IfTrueSuccessor())) {
1165 always_true_target = nullptr;
1166 }
1167 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1168 if_instr->IfFalseSuccessor())) {
1169 false_target = nullptr;
1170 }
1171 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1172}
1173
1174void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1175 LocationSummary* locations = new (GetGraph()->GetArena())
1176 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1177 HInstruction* cond = deoptimize->InputAt(0);
1178 DCHECK(cond->IsCondition());
1179 if (cond->AsCondition()->NeedsMaterialization()) {
1180 locations->SetInAt(0, Location::Any());
1181 }
1182}
1183
1184void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1185 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1186 DeoptimizationSlowPathX86(deoptimize);
1187 codegen_->AddSlowPath(slow_path);
1188 Label* slow_path_entry = slow_path->GetEntryLabel();
1189 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1190}
1191
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001192void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001194}
1195
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001196void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1197 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198}
1199
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001201 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001202}
1203
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001204void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001205 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001206 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001207}
1208
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001210 LocationSummary* locations =
1211 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 switch (store->InputAt(1)->GetType()) {
1213 case Primitive::kPrimBoolean:
1214 case Primitive::kPrimByte:
1215 case Primitive::kPrimChar:
1216 case Primitive::kPrimShort:
1217 case Primitive::kPrimInt:
1218 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001219 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1221 break;
1222
1223 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1226 break;
1227
1228 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001231}
1232
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001233void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001234 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001235}
1236
Roland Levillain0d37cd02015-05-27 16:39:19 +01001237void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001238 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001239 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001240 // Handle the long/FP comparisons made in instruction simplification.
1241 switch (cond->InputAt(0)->GetType()) {
1242 case Primitive::kPrimLong: {
1243 locations->SetInAt(0, Location::RequiresRegister());
1244 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1245 if (cond->NeedsMaterialization()) {
1246 locations->SetOut(Location::RequiresRegister());
1247 }
1248 break;
1249 }
1250 case Primitive::kPrimFloat:
1251 case Primitive::kPrimDouble: {
1252 locations->SetInAt(0, Location::RequiresFpuRegister());
1253 locations->SetInAt(1, Location::RequiresFpuRegister());
1254 if (cond->NeedsMaterialization()) {
1255 locations->SetOut(Location::RequiresRegister());
1256 }
1257 break;
1258 }
1259 default:
1260 locations->SetInAt(0, Location::RequiresRegister());
1261 locations->SetInAt(1, Location::Any());
1262 if (cond->NeedsMaterialization()) {
1263 // We need a byte register.
1264 locations->SetOut(Location::RegisterLocation(ECX));
1265 }
1266 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001267 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001268}
1269
Roland Levillain0d37cd02015-05-27 16:39:19 +01001270void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001271 if (!cond->NeedsMaterialization()) {
1272 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001273 }
Mark Mendellc4701932015-04-10 13:18:51 -04001274
1275 LocationSummary* locations = cond->GetLocations();
1276 Location lhs = locations->InAt(0);
1277 Location rhs = locations->InAt(1);
1278 Register reg = locations->Out().AsRegister<Register>();
1279 Label true_label, false_label;
1280
1281 switch (cond->InputAt(0)->GetType()) {
1282 default: {
1283 // Integer case.
1284
1285 // Clear output register: setcc only sets the low byte.
1286 __ xorl(reg, reg);
1287
1288 if (rhs.IsRegister()) {
1289 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1290 } else if (rhs.IsConstant()) {
1291 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1292 if (constant == 0) {
1293 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1294 } else {
1295 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1296 }
1297 } else {
1298 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1299 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001300 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001301 return;
1302 }
1303 case Primitive::kPrimLong:
1304 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1305 break;
1306 case Primitive::kPrimFloat:
1307 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1308 GenerateFPJumps(cond, &true_label, &false_label);
1309 break;
1310 case Primitive::kPrimDouble:
1311 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1312 GenerateFPJumps(cond, &true_label, &false_label);
1313 break;
1314 }
1315
1316 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001317 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001318
Roland Levillain4fa13f62015-07-06 18:11:54 +01001319 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001320 __ Bind(&false_label);
1321 __ xorl(reg, reg);
1322 __ jmp(&done_label);
1323
Roland Levillain4fa13f62015-07-06 18:11:54 +01001324 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001325 __ Bind(&true_label);
1326 __ movl(reg, Immediate(1));
1327 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001328}
1329
1330void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1331 VisitCondition(comp);
1332}
1333
1334void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1335 VisitCondition(comp);
1336}
1337
1338void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1339 VisitCondition(comp);
1340}
1341
1342void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1343 VisitCondition(comp);
1344}
1345
1346void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1347 VisitCondition(comp);
1348}
1349
1350void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1351 VisitCondition(comp);
1352}
1353
1354void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1355 VisitCondition(comp);
1356}
1357
1358void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1359 VisitCondition(comp);
1360}
1361
1362void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1363 VisitCondition(comp);
1364}
1365
1366void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1367 VisitCondition(comp);
1368}
1369
1370void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1371 VisitCondition(comp);
1372}
1373
1374void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1375 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001376}
1377
1378void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001379 LocationSummary* locations =
1380 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001381 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001382}
1383
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001384void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001385 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001386 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001387}
1388
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001389void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1390 LocationSummary* locations =
1391 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1392 locations->SetOut(Location::ConstantLocation(constant));
1393}
1394
1395void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1396 // Will be generated at use site.
1397 UNUSED(constant);
1398}
1399
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001400void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001401 LocationSummary* locations =
1402 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001403 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404}
1405
1406void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1407 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001408 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001409}
1410
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001411void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1412 LocationSummary* locations =
1413 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1414 locations->SetOut(Location::ConstantLocation(constant));
1415}
1416
1417void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1418 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001419 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001420}
1421
1422void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1423 LocationSummary* locations =
1424 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1425 locations->SetOut(Location::ConstantLocation(constant));
1426}
1427
1428void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1429 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001430 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001431}
1432
Calin Juravle27df7582015-04-17 19:12:31 +01001433void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1434 memory_barrier->SetLocations(nullptr);
1435}
1436
1437void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1438 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1439}
1440
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001441void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001442 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001443}
1444
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001445void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001446 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001447 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001448}
1449
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001450void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001451 LocationSummary* locations =
1452 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001453 switch (ret->InputAt(0)->GetType()) {
1454 case Primitive::kPrimBoolean:
1455 case Primitive::kPrimByte:
1456 case Primitive::kPrimChar:
1457 case Primitive::kPrimShort:
1458 case Primitive::kPrimInt:
1459 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001461 break;
1462
1463 case Primitive::kPrimLong:
1464 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001466 break;
1467
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001468 case Primitive::kPrimFloat:
1469 case Primitive::kPrimDouble:
1470 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001471 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001472 break;
1473
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001475 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001476 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001477}
1478
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001479void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480 if (kIsDebugBuild) {
1481 switch (ret->InputAt(0)->GetType()) {
1482 case Primitive::kPrimBoolean:
1483 case Primitive::kPrimByte:
1484 case Primitive::kPrimChar:
1485 case Primitive::kPrimShort:
1486 case Primitive::kPrimInt:
1487 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 break;
1490
1491 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001492 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1493 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 break;
1495
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001496 case Primitive::kPrimFloat:
1497 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001498 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001499 break;
1500
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001502 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001503 }
1504 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001505 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506}
1507
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001508void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001509 // When we do not run baseline, explicit clinit checks triggered by static
1510 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1511 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001512
Mark Mendellfb8d2792015-03-31 22:16:59 -04001513 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001514 if (intrinsic.TryDispatch(invoke)) {
1515 return;
1516 }
1517
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001518 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001519
1520 if (codegen_->IsBaseline()) {
1521 // Baseline does not have enough registers if the current method also
1522 // needs a register. We therefore do not require a register for it, and let
1523 // the code generation of the invoke handle it.
1524 LocationSummary* locations = invoke->GetLocations();
1525 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1526 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1527 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1528 }
1529 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001530}
1531
Mark Mendell09ed1a32015-03-25 08:30:06 -04001532static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1533 if (invoke->GetLocations()->Intrinsified()) {
1534 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1535 intrinsic.Dispatch(invoke);
1536 return true;
1537 }
1538 return false;
1539}
1540
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001541void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001542 // When we do not run baseline, explicit clinit checks triggered by static
1543 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1544 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001545
Mark Mendell09ed1a32015-03-25 08:30:06 -04001546 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1547 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001548 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001549
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001550 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001551 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001552 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001553 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001554}
1555
1556void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1557 HandleInvoke(invoke);
1558}
1559
1560void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001561 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001562 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001563}
1564
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001565void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001566 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1567 return;
1568 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001569
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001570 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001571 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001572 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001573}
1574
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001575void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1576 HandleInvoke(invoke);
1577 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001578 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001579}
1580
1581void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1582 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001584 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1585 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001586 LocationSummary* locations = invoke->GetLocations();
1587 Location receiver = locations->InAt(0);
1588 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1589
1590 // Set the hidden argument.
1591 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001592 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001593
1594 // temp = object->GetClass();
1595 if (receiver.IsStackSlot()) {
1596 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1597 __ movl(temp, Address(temp, class_offset));
1598 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001599 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001600 }
Roland Levillain4d027112015-07-01 15:41:14 +01001601 codegen_->MaybeRecordImplicitNullCheck(invoke);
1602 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001603 // temp = temp->GetImtEntryAt(method_offset);
1604 __ movl(temp, Address(temp, method_offset));
1605 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001606 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001607 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001608
1609 DCHECK(!codegen_->IsLeafMethod());
1610 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1611}
1612
Roland Levillain88cb1752014-10-20 16:36:47 +01001613void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1614 LocationSummary* locations =
1615 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1616 switch (neg->GetResultType()) {
1617 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001618 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001619 locations->SetInAt(0, Location::RequiresRegister());
1620 locations->SetOut(Location::SameAsFirstInput());
1621 break;
1622
Roland Levillain88cb1752014-10-20 16:36:47 +01001623 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001624 locations->SetInAt(0, Location::RequiresFpuRegister());
1625 locations->SetOut(Location::SameAsFirstInput());
1626 locations->AddTemp(Location::RequiresRegister());
1627 locations->AddTemp(Location::RequiresFpuRegister());
1628 break;
1629
Roland Levillain88cb1752014-10-20 16:36:47 +01001630 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001631 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001632 locations->SetOut(Location::SameAsFirstInput());
1633 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001634 break;
1635
1636 default:
1637 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1638 }
1639}
1640
1641void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1642 LocationSummary* locations = neg->GetLocations();
1643 Location out = locations->Out();
1644 Location in = locations->InAt(0);
1645 switch (neg->GetResultType()) {
1646 case Primitive::kPrimInt:
1647 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001648 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001649 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001650 break;
1651
1652 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001653 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001654 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001655 __ negl(out.AsRegisterPairLow<Register>());
1656 // Negation is similar to subtraction from zero. The least
1657 // significant byte triggers a borrow when it is different from
1658 // zero; to take it into account, add 1 to the most significant
1659 // byte if the carry flag (CF) is set to 1 after the first NEGL
1660 // operation.
1661 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1662 __ negl(out.AsRegisterPairHigh<Register>());
1663 break;
1664
Roland Levillain5368c212014-11-27 15:03:41 +00001665 case Primitive::kPrimFloat: {
1666 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 Register constant = locations->GetTemp(0).AsRegister<Register>();
1668 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001669 // Implement float negation with an exclusive or with value
1670 // 0x80000000 (mask for bit 31, representing the sign of a
1671 // single-precision floating-point number).
1672 __ movl(constant, Immediate(INT32_C(0x80000000)));
1673 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001674 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001675 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001676 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001677
Roland Levillain5368c212014-11-27 15:03:41 +00001678 case Primitive::kPrimDouble: {
1679 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001681 // Implement double negation with an exclusive or with value
1682 // 0x8000000000000000 (mask for bit 63, representing the sign of
1683 // a double-precision floating-point number).
1684 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001686 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001687 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001688
1689 default:
1690 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1691 }
1692}
1693
Roland Levillaindff1f282014-11-05 14:15:05 +00001694void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001695 Primitive::Type result_type = conversion->GetResultType();
1696 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001697 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001698
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001699 // The float-to-long and double-to-long type conversions rely on a
1700 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001701 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001702 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1703 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001704 ? LocationSummary::kCall
1705 : LocationSummary::kNoCall;
1706 LocationSummary* locations =
1707 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1708
David Brazdilb2bd1c52015-03-25 11:17:37 +00001709 // The Java language does not allow treating boolean as an integral type but
1710 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001711
Roland Levillaindff1f282014-11-05 14:15:05 +00001712 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001713 case Primitive::kPrimByte:
1714 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001715 case Primitive::kPrimBoolean:
1716 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001717 case Primitive::kPrimShort:
1718 case Primitive::kPrimInt:
1719 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001720 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001721 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1722 // Make the output overlap to please the register allocator. This greatly simplifies
1723 // the validation of the linear scan implementation
1724 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001725 break;
1726
1727 default:
1728 LOG(FATAL) << "Unexpected type conversion from " << input_type
1729 << " to " << result_type;
1730 }
1731 break;
1732
Roland Levillain01a8d712014-11-14 16:27:39 +00001733 case Primitive::kPrimShort:
1734 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001735 case Primitive::kPrimBoolean:
1736 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001737 case Primitive::kPrimByte:
1738 case Primitive::kPrimInt:
1739 case Primitive::kPrimChar:
1740 // Processing a Dex `int-to-short' instruction.
1741 locations->SetInAt(0, Location::Any());
1742 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1743 break;
1744
1745 default:
1746 LOG(FATAL) << "Unexpected type conversion from " << input_type
1747 << " to " << result_type;
1748 }
1749 break;
1750
Roland Levillain946e1432014-11-11 17:35:19 +00001751 case Primitive::kPrimInt:
1752 switch (input_type) {
1753 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001754 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001755 locations->SetInAt(0, Location::Any());
1756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1757 break;
1758
1759 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001760 // Processing a Dex `float-to-int' instruction.
1761 locations->SetInAt(0, Location::RequiresFpuRegister());
1762 locations->SetOut(Location::RequiresRegister());
1763 locations->AddTemp(Location::RequiresFpuRegister());
1764 break;
1765
Roland Levillain946e1432014-11-11 17:35:19 +00001766 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001767 // Processing a Dex `double-to-int' instruction.
1768 locations->SetInAt(0, Location::RequiresFpuRegister());
1769 locations->SetOut(Location::RequiresRegister());
1770 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001771 break;
1772
1773 default:
1774 LOG(FATAL) << "Unexpected type conversion from " << input_type
1775 << " to " << result_type;
1776 }
1777 break;
1778
Roland Levillaindff1f282014-11-05 14:15:05 +00001779 case Primitive::kPrimLong:
1780 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001781 case Primitive::kPrimBoolean:
1782 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001783 case Primitive::kPrimByte:
1784 case Primitive::kPrimShort:
1785 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001786 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001787 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001788 locations->SetInAt(0, Location::RegisterLocation(EAX));
1789 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1790 break;
1791
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001792 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001793 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001794 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001795 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001796 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1797 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1798
Vladimir Marko949c91f2015-01-27 10:48:44 +00001799 // The runtime helper puts the result in EAX, EDX.
1800 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001801 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001802 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001803
1804 default:
1805 LOG(FATAL) << "Unexpected type conversion from " << input_type
1806 << " to " << result_type;
1807 }
1808 break;
1809
Roland Levillain981e4542014-11-14 11:47:14 +00001810 case Primitive::kPrimChar:
1811 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001812 case Primitive::kPrimBoolean:
1813 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001814 case Primitive::kPrimByte:
1815 case Primitive::kPrimShort:
1816 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001817 // Processing a Dex `int-to-char' instruction.
1818 locations->SetInAt(0, Location::Any());
1819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1820 break;
1821
1822 default:
1823 LOG(FATAL) << "Unexpected type conversion from " << input_type
1824 << " to " << result_type;
1825 }
1826 break;
1827
Roland Levillaindff1f282014-11-05 14:15:05 +00001828 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001829 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001830 case Primitive::kPrimBoolean:
1831 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001832 case Primitive::kPrimByte:
1833 case Primitive::kPrimShort:
1834 case Primitive::kPrimInt:
1835 case Primitive::kPrimChar:
1836 // Processing a Dex `int-to-float' instruction.
1837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetOut(Location::RequiresFpuRegister());
1839 break;
1840
1841 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001842 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001843 locations->SetInAt(0, Location::Any());
1844 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001845 break;
1846
Roland Levillaincff13742014-11-17 14:32:17 +00001847 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001848 // Processing a Dex `double-to-float' instruction.
1849 locations->SetInAt(0, Location::RequiresFpuRegister());
1850 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001851 break;
1852
1853 default:
1854 LOG(FATAL) << "Unexpected type conversion from " << input_type
1855 << " to " << result_type;
1856 };
1857 break;
1858
Roland Levillaindff1f282014-11-05 14:15:05 +00001859 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001860 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001861 case Primitive::kPrimBoolean:
1862 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001863 case Primitive::kPrimByte:
1864 case Primitive::kPrimShort:
1865 case Primitive::kPrimInt:
1866 case Primitive::kPrimChar:
1867 // Processing a Dex `int-to-double' instruction.
1868 locations->SetInAt(0, Location::RequiresRegister());
1869 locations->SetOut(Location::RequiresFpuRegister());
1870 break;
1871
1872 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001873 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001874 locations->SetInAt(0, Location::Any());
1875 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876 break;
1877
Roland Levillaincff13742014-11-17 14:32:17 +00001878 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001879 // Processing a Dex `float-to-double' instruction.
1880 locations->SetInAt(0, Location::RequiresFpuRegister());
1881 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001882 break;
1883
1884 default:
1885 LOG(FATAL) << "Unexpected type conversion from " << input_type
1886 << " to " << result_type;
1887 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001888 break;
1889
1890 default:
1891 LOG(FATAL) << "Unexpected type conversion from " << input_type
1892 << " to " << result_type;
1893 }
1894}
1895
1896void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1897 LocationSummary* locations = conversion->GetLocations();
1898 Location out = locations->Out();
1899 Location in = locations->InAt(0);
1900 Primitive::Type result_type = conversion->GetResultType();
1901 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001902 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001903 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001904 case Primitive::kPrimByte:
1905 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001906 case Primitive::kPrimBoolean:
1907 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001908 case Primitive::kPrimShort:
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001911 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001912 if (in.IsRegister()) {
1913 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001914 } else {
1915 DCHECK(in.GetConstant()->IsIntConstant());
1916 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1917 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1918 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001919 break;
1920
1921 default:
1922 LOG(FATAL) << "Unexpected type conversion from " << input_type
1923 << " to " << result_type;
1924 }
1925 break;
1926
Roland Levillain01a8d712014-11-14 16:27:39 +00001927 case Primitive::kPrimShort:
1928 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001929 case Primitive::kPrimBoolean:
1930 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001931 case Primitive::kPrimByte:
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimChar:
1934 // Processing a Dex `int-to-short' instruction.
1935 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001937 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001938 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001939 } else {
1940 DCHECK(in.GetConstant()->IsIntConstant());
1941 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001942 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001943 }
1944 break;
1945
1946 default:
1947 LOG(FATAL) << "Unexpected type conversion from " << input_type
1948 << " to " << result_type;
1949 }
1950 break;
1951
Roland Levillain946e1432014-11-11 17:35:19 +00001952 case Primitive::kPrimInt:
1953 switch (input_type) {
1954 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001955 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001956 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001958 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001959 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001960 } else {
1961 DCHECK(in.IsConstant());
1962 DCHECK(in.GetConstant()->IsLongConstant());
1963 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001965 }
1966 break;
1967
Roland Levillain3f8f9362014-12-02 17:45:01 +00001968 case Primitive::kPrimFloat: {
1969 // Processing a Dex `float-to-int' instruction.
1970 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1971 Register output = out.AsRegister<Register>();
1972 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04001973 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00001974
1975 __ movl(output, Immediate(kPrimIntMax));
1976 // temp = int-to-float(output)
1977 __ cvtsi2ss(temp, output);
1978 // if input >= temp goto done
1979 __ comiss(input, temp);
1980 __ j(kAboveEqual, &done);
1981 // if input == NaN goto nan
1982 __ j(kUnordered, &nan);
1983 // output = float-to-int-truncate(input)
1984 __ cvttss2si(output, input);
1985 __ jmp(&done);
1986 __ Bind(&nan);
1987 // output = 0
1988 __ xorl(output, output);
1989 __ Bind(&done);
1990 break;
1991 }
1992
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001993 case Primitive::kPrimDouble: {
1994 // Processing a Dex `double-to-int' instruction.
1995 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1996 Register output = out.AsRegister<Register>();
1997 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04001998 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001999
2000 __ movl(output, Immediate(kPrimIntMax));
2001 // temp = int-to-double(output)
2002 __ cvtsi2sd(temp, output);
2003 // if input >= temp goto done
2004 __ comisd(input, temp);
2005 __ j(kAboveEqual, &done);
2006 // if input == NaN goto nan
2007 __ j(kUnordered, &nan);
2008 // output = double-to-int-truncate(input)
2009 __ cvttsd2si(output, input);
2010 __ jmp(&done);
2011 __ Bind(&nan);
2012 // output = 0
2013 __ xorl(output, output);
2014 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002015 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002016 }
Roland Levillain946e1432014-11-11 17:35:19 +00002017
2018 default:
2019 LOG(FATAL) << "Unexpected type conversion from " << input_type
2020 << " to " << result_type;
2021 }
2022 break;
2023
Roland Levillaindff1f282014-11-05 14:15:05 +00002024 case Primitive::kPrimLong:
2025 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002026 case Primitive::kPrimBoolean:
2027 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002028 case Primitive::kPrimByte:
2029 case Primitive::kPrimShort:
2030 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002031 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002032 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2034 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 __ cdq();
2037 break;
2038
2039 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002040 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002041 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2042 conversion,
2043 conversion->GetDexPc(),
2044 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002045 break;
2046
Roland Levillaindff1f282014-11-05 14:15:05 +00002047 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002048 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002049 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2050 conversion,
2051 conversion->GetDexPc(),
2052 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected type conversion from " << input_type
2057 << " to " << result_type;
2058 }
2059 break;
2060
Roland Levillain981e4542014-11-14 11:47:14 +00002061 case Primitive::kPrimChar:
2062 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002063 case Primitive::kPrimBoolean:
2064 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002065 case Primitive::kPrimByte:
2066 case Primitive::kPrimShort:
2067 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002068 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2069 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002070 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002071 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002072 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002073 } else {
2074 DCHECK(in.GetConstant()->IsIntConstant());
2075 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002076 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002077 }
2078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected type conversion from " << input_type
2082 << " to " << result_type;
2083 }
2084 break;
2085
Roland Levillaindff1f282014-11-05 14:15:05 +00002086 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002087 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002088 case Primitive::kPrimBoolean:
2089 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002090 case Primitive::kPrimByte:
2091 case Primitive::kPrimShort:
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002094 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002095 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002096 break;
2097
Roland Levillain6d0e4832014-11-27 18:31:21 +00002098 case Primitive::kPrimLong: {
2099 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002100 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002101
Roland Levillain232ade02015-04-20 15:14:36 +01002102 // Create stack space for the call to
2103 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2104 // TODO: enhance register allocator to ask for stack temporaries.
2105 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2106 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2107 __ subl(ESP, Immediate(adjustment));
2108 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002109
Roland Levillain232ade02015-04-20 15:14:36 +01002110 // Load the value to the FP stack, using temporaries if needed.
2111 PushOntoFPStack(in, 0, adjustment, false, true);
2112
2113 if (out.IsStackSlot()) {
2114 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2115 } else {
2116 __ fstps(Address(ESP, 0));
2117 Location stack_temp = Location::StackSlot(0);
2118 codegen_->Move32(out, stack_temp);
2119 }
2120
2121 // Remove the temporary stack space we allocated.
2122 if (adjustment != 0) {
2123 __ addl(ESP, Immediate(adjustment));
2124 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002125 break;
2126 }
2127
Roland Levillaincff13742014-11-17 14:32:17 +00002128 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002129 // Processing a Dex `double-to-float' instruction.
2130 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected type conversion from " << input_type
2135 << " to " << result_type;
2136 };
2137 break;
2138
Roland Levillaindff1f282014-11-05 14:15:05 +00002139 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002140 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002141 case Primitive::kPrimBoolean:
2142 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002143 case Primitive::kPrimByte:
2144 case Primitive::kPrimShort:
2145 case Primitive::kPrimInt:
2146 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002147 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002148 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002149 break;
2150
Roland Levillain647b9ed2014-11-27 12:06:00 +00002151 case Primitive::kPrimLong: {
2152 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002153 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002154
Roland Levillain232ade02015-04-20 15:14:36 +01002155 // Create stack space for the call to
2156 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2157 // TODO: enhance register allocator to ask for stack temporaries.
2158 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2159 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2160 __ subl(ESP, Immediate(adjustment));
2161 }
2162
2163 // Load the value to the FP stack, using temporaries if needed.
2164 PushOntoFPStack(in, 0, adjustment, false, true);
2165
2166 if (out.IsDoubleStackSlot()) {
2167 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2168 } else {
2169 __ fstpl(Address(ESP, 0));
2170 Location stack_temp = Location::DoubleStackSlot(0);
2171 codegen_->Move64(out, stack_temp);
2172 }
2173
2174 // Remove the temporary stack space we allocated.
2175 if (adjustment != 0) {
2176 __ addl(ESP, Immediate(adjustment));
2177 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002178 break;
2179 }
2180
Roland Levillaincff13742014-11-17 14:32:17 +00002181 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002182 // Processing a Dex `float-to-double' instruction.
2183 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002184 break;
2185
2186 default:
2187 LOG(FATAL) << "Unexpected type conversion from " << input_type
2188 << " to " << result_type;
2189 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002190 break;
2191
2192 default:
2193 LOG(FATAL) << "Unexpected type conversion from " << input_type
2194 << " to " << result_type;
2195 }
2196}
2197
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002198void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 LocationSummary* locations =
2200 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002201 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002202 case Primitive::kPrimInt: {
2203 locations->SetInAt(0, Location::RequiresRegister());
2204 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2206 break;
2207 }
2208
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002210 locations->SetInAt(0, Location::RequiresRegister());
2211 locations->SetInAt(1, Location::Any());
2212 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002213 break;
2214 }
2215
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002216 case Primitive::kPrimFloat:
2217 case Primitive::kPrimDouble: {
2218 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002219 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002220 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002221 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002222 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002223
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002224 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002225 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2226 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002227 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002228}
2229
2230void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2231 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002232 Location first = locations->InAt(0);
2233 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002234 Location out = locations->Out();
2235
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002236 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002239 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2240 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002241 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2242 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002243 } else {
2244 __ leal(out.AsRegister<Register>(), Address(
2245 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2246 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002247 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002248 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2249 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2250 __ addl(out.AsRegister<Register>(), Immediate(value));
2251 } else {
2252 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2253 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002254 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002255 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002256 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002257 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002258 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002259 }
2260
2261 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002262 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002263 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2264 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002265 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002266 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2267 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002268 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002269 } else {
2270 DCHECK(second.IsConstant()) << second;
2271 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2272 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2273 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002274 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002275 break;
2276 }
2277
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002278 case Primitive::kPrimFloat: {
2279 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002280 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002281 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2282 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2283 DCHECK(!const_area->NeedsMaterialization());
2284 __ addss(first.AsFpuRegister<XmmRegister>(),
2285 codegen_->LiteralFloatAddress(
2286 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2287 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2288 } else {
2289 DCHECK(second.IsStackSlot());
2290 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002291 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002292 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002293 }
2294
2295 case Primitive::kPrimDouble: {
2296 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002298 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2299 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2300 DCHECK(!const_area->NeedsMaterialization());
2301 __ addsd(first.AsFpuRegister<XmmRegister>(),
2302 codegen_->LiteralDoubleAddress(
2303 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2304 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2305 } else {
2306 DCHECK(second.IsDoubleStackSlot());
2307 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002308 }
2309 break;
2310 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002312 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002314 }
2315}
2316
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002317void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002318 LocationSummary* locations =
2319 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002320 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002321 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002323 locations->SetInAt(0, Location::RequiresRegister());
2324 locations->SetInAt(1, Location::Any());
2325 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 break;
2327 }
Calin Juravle11351682014-10-23 15:38:15 +01002328 case Primitive::kPrimFloat:
2329 case Primitive::kPrimDouble: {
2330 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002331 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002332 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333 break;
Calin Juravle11351682014-10-23 15:38:15 +01002334 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002336 default:
Calin Juravle11351682014-10-23 15:38:15 +01002337 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002338 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002339}
2340
2341void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2342 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002343 Location first = locations->InAt(0);
2344 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002345 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002346 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002350 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002351 __ subl(first.AsRegister<Register>(),
2352 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002353 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002355 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002356 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002357 }
2358
2359 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002360 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002361 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2362 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002363 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002364 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 __ sbbl(first.AsRegisterPairHigh<Register>(),
2366 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002367 } else {
2368 DCHECK(second.IsConstant()) << second;
2369 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2370 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2371 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002372 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373 break;
2374 }
2375
Calin Juravle11351682014-10-23 15:38:15 +01002376 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002377 if (second.IsFpuRegister()) {
2378 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2379 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2380 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2381 DCHECK(!const_area->NeedsMaterialization());
2382 __ subss(first.AsFpuRegister<XmmRegister>(),
2383 codegen_->LiteralFloatAddress(
2384 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2385 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2386 } else {
2387 DCHECK(second.IsStackSlot());
2388 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2389 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002390 break;
Calin Juravle11351682014-10-23 15:38:15 +01002391 }
2392
2393 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002394 if (second.IsFpuRegister()) {
2395 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2396 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2397 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2398 DCHECK(!const_area->NeedsMaterialization());
2399 __ subsd(first.AsFpuRegister<XmmRegister>(),
2400 codegen_->LiteralDoubleAddress(
2401 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2402 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2403 } else {
2404 DCHECK(second.IsDoubleStackSlot());
2405 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2406 }
Calin Juravle11351682014-10-23 15:38:15 +01002407 break;
2408 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002409
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002410 default:
Calin Juravle11351682014-10-23 15:38:15 +01002411 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002412 }
2413}
2414
Calin Juravle34bacdf2014-10-07 20:23:36 +01002415void LocationsBuilderX86::VisitMul(HMul* mul) {
2416 LocationSummary* locations =
2417 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2418 switch (mul->GetResultType()) {
2419 case Primitive::kPrimInt:
2420 locations->SetInAt(0, Location::RequiresRegister());
2421 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002422 if (mul->InputAt(1)->IsIntConstant()) {
2423 // Can use 3 operand multiply.
2424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2425 } else {
2426 locations->SetOut(Location::SameAsFirstInput());
2427 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002428 break;
2429 case Primitive::kPrimLong: {
2430 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431 locations->SetInAt(1, Location::Any());
2432 locations->SetOut(Location::SameAsFirstInput());
2433 // Needed for imul on 32bits with 64bits output.
2434 locations->AddTemp(Location::RegisterLocation(EAX));
2435 locations->AddTemp(Location::RegisterLocation(EDX));
2436 break;
2437 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002438 case Primitive::kPrimFloat:
2439 case Primitive::kPrimDouble: {
2440 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002441 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002442 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002444 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002445
2446 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002447 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002448 }
2449}
2450
2451void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2452 LocationSummary* locations = mul->GetLocations();
2453 Location first = locations->InAt(0);
2454 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002455 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002456
2457 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002458 case Primitive::kPrimInt:
2459 // The constant may have ended up in a register, so test explicitly to avoid
2460 // problems where the output may not be the same as the first operand.
2461 if (mul->InputAt(1)->IsIntConstant()) {
2462 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2463 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2464 } else if (second.IsRegister()) {
2465 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002466 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002467 } else {
2468 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002469 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002470 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471 }
2472 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002473
2474 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002475 Register in1_hi = first.AsRegisterPairHigh<Register>();
2476 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 Register eax = locations->GetTemp(0).AsRegister<Register>();
2478 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002479
2480 DCHECK_EQ(EAX, eax);
2481 DCHECK_EQ(EDX, edx);
2482
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002483 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002484 // output: in1
2485 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2486 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2487 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002488 if (second.IsConstant()) {
2489 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002490
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002491 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2492 int32_t low_value = Low32Bits(value);
2493 int32_t high_value = High32Bits(value);
2494 Immediate low(low_value);
2495 Immediate high(high_value);
2496
2497 __ movl(eax, high);
2498 // eax <- in1.lo * in2.hi
2499 __ imull(eax, in1_lo);
2500 // in1.hi <- in1.hi * in2.lo
2501 __ imull(in1_hi, low);
2502 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2503 __ addl(in1_hi, eax);
2504 // move in2_lo to eax to prepare for double precision
2505 __ movl(eax, low);
2506 // edx:eax <- in1.lo * in2.lo
2507 __ mull(in1_lo);
2508 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2509 __ addl(in1_hi, edx);
2510 // in1.lo <- (in1.lo * in2.lo)[31:0];
2511 __ movl(in1_lo, eax);
2512 } else if (second.IsRegisterPair()) {
2513 Register in2_hi = second.AsRegisterPairHigh<Register>();
2514 Register in2_lo = second.AsRegisterPairLow<Register>();
2515
2516 __ movl(eax, in2_hi);
2517 // eax <- in1.lo * in2.hi
2518 __ imull(eax, in1_lo);
2519 // in1.hi <- in1.hi * in2.lo
2520 __ imull(in1_hi, in2_lo);
2521 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2522 __ addl(in1_hi, eax);
2523 // move in1_lo to eax to prepare for double precision
2524 __ movl(eax, in1_lo);
2525 // edx:eax <- in1.lo * in2.lo
2526 __ mull(in2_lo);
2527 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2528 __ addl(in1_hi, edx);
2529 // in1.lo <- (in1.lo * in2.lo)[31:0];
2530 __ movl(in1_lo, eax);
2531 } else {
2532 DCHECK(second.IsDoubleStackSlot()) << second;
2533 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2534 Address in2_lo(ESP, second.GetStackIndex());
2535
2536 __ movl(eax, in2_hi);
2537 // eax <- in1.lo * in2.hi
2538 __ imull(eax, in1_lo);
2539 // in1.hi <- in1.hi * in2.lo
2540 __ imull(in1_hi, in2_lo);
2541 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2542 __ addl(in1_hi, eax);
2543 // move in1_lo to eax to prepare for double precision
2544 __ movl(eax, in1_lo);
2545 // edx:eax <- in1.lo * in2.lo
2546 __ mull(in2_lo);
2547 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2548 __ addl(in1_hi, edx);
2549 // in1.lo <- (in1.lo * in2.lo)[31:0];
2550 __ movl(in1_lo, eax);
2551 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002552
2553 break;
2554 }
2555
Calin Juravleb5bfa962014-10-21 18:02:24 +01002556 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002557 DCHECK(first.Equals(locations->Out()));
2558 if (second.IsFpuRegister()) {
2559 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2560 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2561 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2562 DCHECK(!const_area->NeedsMaterialization());
2563 __ mulss(first.AsFpuRegister<XmmRegister>(),
2564 codegen_->LiteralFloatAddress(
2565 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2566 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2567 } else {
2568 DCHECK(second.IsStackSlot());
2569 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2570 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002571 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002572 }
2573
2574 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002575 DCHECK(first.Equals(locations->Out()));
2576 if (second.IsFpuRegister()) {
2577 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2578 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2579 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2580 DCHECK(!const_area->NeedsMaterialization());
2581 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2582 codegen_->LiteralDoubleAddress(
2583 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2584 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2585 } else {
2586 DCHECK(second.IsDoubleStackSlot());
2587 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2588 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002589 break;
2590 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002591
2592 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002593 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002594 }
2595}
2596
Roland Levillain232ade02015-04-20 15:14:36 +01002597void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2598 uint32_t temp_offset,
2599 uint32_t stack_adjustment,
2600 bool is_fp,
2601 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002602 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002603 DCHECK(!is_wide);
2604 if (is_fp) {
2605 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2606 } else {
2607 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2608 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002609 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002610 DCHECK(is_wide);
2611 if (is_fp) {
2612 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2613 } else {
2614 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2615 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002616 } else {
2617 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002618 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002619 Location stack_temp = Location::StackSlot(temp_offset);
2620 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002621 if (is_fp) {
2622 __ flds(Address(ESP, temp_offset));
2623 } else {
2624 __ filds(Address(ESP, temp_offset));
2625 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002626 } else {
2627 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2628 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002629 if (is_fp) {
2630 __ fldl(Address(ESP, temp_offset));
2631 } else {
2632 __ fildl(Address(ESP, temp_offset));
2633 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002634 }
2635 }
2636}
2637
2638void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2639 Primitive::Type type = rem->GetResultType();
2640 bool is_float = type == Primitive::kPrimFloat;
2641 size_t elem_size = Primitive::ComponentSize(type);
2642 LocationSummary* locations = rem->GetLocations();
2643 Location first = locations->InAt(0);
2644 Location second = locations->InAt(1);
2645 Location out = locations->Out();
2646
2647 // Create stack space for 2 elements.
2648 // TODO: enhance register allocator to ask for stack temporaries.
2649 __ subl(ESP, Immediate(2 * elem_size));
2650
2651 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002652 const bool is_wide = !is_float;
2653 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2654 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002655
2656 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002657 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002658 __ Bind(&retry);
2659 __ fprem();
2660
2661 // Move FP status to AX.
2662 __ fstsw();
2663
2664 // And see if the argument reduction is complete. This is signaled by the
2665 // C2 FPU flag bit set to 0.
2666 __ andl(EAX, Immediate(kC2ConditionMask));
2667 __ j(kNotEqual, &retry);
2668
2669 // We have settled on the final value. Retrieve it into an XMM register.
2670 // Store FP top of stack to real stack.
2671 if (is_float) {
2672 __ fsts(Address(ESP, 0));
2673 } else {
2674 __ fstl(Address(ESP, 0));
2675 }
2676
2677 // Pop the 2 items from the FP stack.
2678 __ fucompp();
2679
2680 // Load the value from the stack into an XMM register.
2681 DCHECK(out.IsFpuRegister()) << out;
2682 if (is_float) {
2683 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2684 } else {
2685 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2686 }
2687
2688 // And remove the temporary stack space we allocated.
2689 __ addl(ESP, Immediate(2 * elem_size));
2690}
2691
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002692
2693void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2694 DCHECK(instruction->IsDiv() || instruction->IsRem());
2695
2696 LocationSummary* locations = instruction->GetLocations();
2697 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002698 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002699
2700 Register out_register = locations->Out().AsRegister<Register>();
2701 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002702 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002703
2704 DCHECK(imm == 1 || imm == -1);
2705
2706 if (instruction->IsRem()) {
2707 __ xorl(out_register, out_register);
2708 } else {
2709 __ movl(out_register, input_register);
2710 if (imm == -1) {
2711 __ negl(out_register);
2712 }
2713 }
2714}
2715
2716
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002717void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002718 LocationSummary* locations = instruction->GetLocations();
2719
2720 Register out_register = locations->Out().AsRegister<Register>();
2721 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002722 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002723
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002724 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002725 Register num = locations->GetTemp(0).AsRegister<Register>();
2726
2727 __ leal(num, Address(input_register, std::abs(imm) - 1));
2728 __ testl(input_register, input_register);
2729 __ cmovl(kGreaterEqual, num, input_register);
2730 int shift = CTZ(imm);
2731 __ sarl(num, Immediate(shift));
2732
2733 if (imm < 0) {
2734 __ negl(num);
2735 }
2736
2737 __ movl(out_register, num);
2738}
2739
2740void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2741 DCHECK(instruction->IsDiv() || instruction->IsRem());
2742
2743 LocationSummary* locations = instruction->GetLocations();
2744 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2745
2746 Register eax = locations->InAt(0).AsRegister<Register>();
2747 Register out = locations->Out().AsRegister<Register>();
2748 Register num;
2749 Register edx;
2750
2751 if (instruction->IsDiv()) {
2752 edx = locations->GetTemp(0).AsRegister<Register>();
2753 num = locations->GetTemp(1).AsRegister<Register>();
2754 } else {
2755 edx = locations->Out().AsRegister<Register>();
2756 num = locations->GetTemp(0).AsRegister<Register>();
2757 }
2758
2759 DCHECK_EQ(EAX, eax);
2760 DCHECK_EQ(EDX, edx);
2761 if (instruction->IsDiv()) {
2762 DCHECK_EQ(EAX, out);
2763 } else {
2764 DCHECK_EQ(EDX, out);
2765 }
2766
2767 int64_t magic;
2768 int shift;
2769 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2770
Mark Mendell0c9497d2015-08-21 09:30:05 -04002771 NearLabel ndiv;
2772 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002773 // If numerator is 0, the result is 0, no computation needed.
2774 __ testl(eax, eax);
2775 __ j(kNotEqual, &ndiv);
2776
2777 __ xorl(out, out);
2778 __ jmp(&end);
2779
2780 __ Bind(&ndiv);
2781
2782 // Save the numerator.
2783 __ movl(num, eax);
2784
2785 // EAX = magic
2786 __ movl(eax, Immediate(magic));
2787
2788 // EDX:EAX = magic * numerator
2789 __ imull(num);
2790
2791 if (imm > 0 && magic < 0) {
2792 // EDX += num
2793 __ addl(edx, num);
2794 } else if (imm < 0 && magic > 0) {
2795 __ subl(edx, num);
2796 }
2797
2798 // Shift if needed.
2799 if (shift != 0) {
2800 __ sarl(edx, Immediate(shift));
2801 }
2802
2803 // EDX += 1 if EDX < 0
2804 __ movl(eax, edx);
2805 __ shrl(edx, Immediate(31));
2806 __ addl(edx, eax);
2807
2808 if (instruction->IsRem()) {
2809 __ movl(eax, num);
2810 __ imull(edx, Immediate(imm));
2811 __ subl(eax, edx);
2812 __ movl(edx, eax);
2813 } else {
2814 __ movl(eax, edx);
2815 }
2816 __ Bind(&end);
2817}
2818
Calin Juravlebacfec32014-11-14 15:54:36 +00002819void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2820 DCHECK(instruction->IsDiv() || instruction->IsRem());
2821
2822 LocationSummary* locations = instruction->GetLocations();
2823 Location out = locations->Out();
2824 Location first = locations->InAt(0);
2825 Location second = locations->InAt(1);
2826 bool is_div = instruction->IsDiv();
2827
2828 switch (instruction->GetResultType()) {
2829 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 DCHECK_EQ(EAX, first.AsRegister<Register>());
2831 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002832
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002833 if (instruction->InputAt(1)->IsIntConstant()) {
2834 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002835
2836 if (imm == 0) {
2837 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2838 } else if (imm == 1 || imm == -1) {
2839 DivRemOneOrMinusOne(instruction);
2840 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002841 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002842 } else {
2843 DCHECK(imm <= -2 || imm >= 2);
2844 GenerateDivRemWithAnyConstant(instruction);
2845 }
2846 } else {
2847 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002848 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002849 is_div);
2850 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002851
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002852 Register second_reg = second.AsRegister<Register>();
2853 // 0x80000000/-1 triggers an arithmetic exception!
2854 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2855 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002856
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002857 __ cmpl(second_reg, Immediate(-1));
2858 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002859
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002860 // edx:eax <- sign-extended of eax
2861 __ cdq();
2862 // eax = quotient, edx = remainder
2863 __ idivl(second_reg);
2864 __ Bind(slow_path->GetExitLabel());
2865 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002866 break;
2867 }
2868
2869 case Primitive::kPrimLong: {
2870 InvokeRuntimeCallingConvention calling_convention;
2871 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2872 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2873 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2874 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2875 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2876 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2877
2878 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002879 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2880 instruction,
2881 instruction->GetDexPc(),
2882 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002883 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002884 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2885 instruction,
2886 instruction->GetDexPc(),
2887 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002888 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002889 break;
2890 }
2891
2892 default:
2893 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2894 }
2895}
2896
Calin Juravle7c4954d2014-10-28 16:57:40 +00002897void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002898 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002899 ? LocationSummary::kCall
2900 : LocationSummary::kNoCall;
2901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2902
Calin Juravle7c4954d2014-10-28 16:57:40 +00002903 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002904 case Primitive::kPrimInt: {
2905 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002906 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002907 locations->SetOut(Location::SameAsFirstInput());
2908 // Intel uses edx:eax as the dividend.
2909 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002910 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2911 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2912 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002913 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002914 locations->AddTemp(Location::RequiresRegister());
2915 }
Calin Juravled0d48522014-11-04 16:40:20 +00002916 break;
2917 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002918 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002919 InvokeRuntimeCallingConvention calling_convention;
2920 locations->SetInAt(0, Location::RegisterPairLocation(
2921 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2922 locations->SetInAt(1, Location::RegisterPairLocation(
2923 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2924 // Runtime helper puts the result in EAX, EDX.
2925 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002926 break;
2927 }
2928 case Primitive::kPrimFloat:
2929 case Primitive::kPrimDouble: {
2930 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002931 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002932 locations->SetOut(Location::SameAsFirstInput());
2933 break;
2934 }
2935
2936 default:
2937 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2938 }
2939}
2940
2941void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2942 LocationSummary* locations = div->GetLocations();
2943 Location first = locations->InAt(0);
2944 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002945
2946 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002947 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002948 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002949 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002950 break;
2951 }
2952
2953 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002954 if (second.IsFpuRegister()) {
2955 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2956 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2957 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2958 DCHECK(!const_area->NeedsMaterialization());
2959 __ divss(first.AsFpuRegister<XmmRegister>(),
2960 codegen_->LiteralFloatAddress(
2961 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2962 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2963 } else {
2964 DCHECK(second.IsStackSlot());
2965 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2966 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002967 break;
2968 }
2969
2970 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002971 if (second.IsFpuRegister()) {
2972 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2973 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2974 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2975 DCHECK(!const_area->NeedsMaterialization());
2976 __ divsd(first.AsFpuRegister<XmmRegister>(),
2977 codegen_->LiteralDoubleAddress(
2978 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2979 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2980 } else {
2981 DCHECK(second.IsDoubleStackSlot());
2982 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2983 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002984 break;
2985 }
2986
2987 default:
2988 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2989 }
2990}
2991
Calin Juravlebacfec32014-11-14 15:54:36 +00002992void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002993 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002994
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002995 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2996 ? LocationSummary::kCall
2997 : LocationSummary::kNoCall;
2998 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002999
Calin Juravled2ec87d2014-12-08 14:24:46 +00003000 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003001 case Primitive::kPrimInt: {
3002 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003004 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003005 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3006 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3007 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003008 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003009 locations->AddTemp(Location::RequiresRegister());
3010 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003011 break;
3012 }
3013 case Primitive::kPrimLong: {
3014 InvokeRuntimeCallingConvention calling_convention;
3015 locations->SetInAt(0, Location::RegisterPairLocation(
3016 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3017 locations->SetInAt(1, Location::RegisterPairLocation(
3018 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3019 // Runtime helper puts the result in EAX, EDX.
3020 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3021 break;
3022 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003023 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003024 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003025 locations->SetInAt(0, Location::Any());
3026 locations->SetInAt(1, Location::Any());
3027 locations->SetOut(Location::RequiresFpuRegister());
3028 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003029 break;
3030 }
3031
3032 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003033 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003034 }
3035}
3036
3037void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3038 Primitive::Type type = rem->GetResultType();
3039 switch (type) {
3040 case Primitive::kPrimInt:
3041 case Primitive::kPrimLong: {
3042 GenerateDivRemIntegral(rem);
3043 break;
3044 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003045 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003046 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003047 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003048 break;
3049 }
3050 default:
3051 LOG(FATAL) << "Unexpected rem type " << type;
3052 }
3053}
3054
Calin Juravled0d48522014-11-04 16:40:20 +00003055void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003056 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3057 ? LocationSummary::kCallOnSlowPath
3058 : LocationSummary::kNoCall;
3059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003060 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003061 case Primitive::kPrimByte:
3062 case Primitive::kPrimChar:
3063 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003064 case Primitive::kPrimInt: {
3065 locations->SetInAt(0, Location::Any());
3066 break;
3067 }
3068 case Primitive::kPrimLong: {
3069 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3070 if (!instruction->IsConstant()) {
3071 locations->AddTemp(Location::RequiresRegister());
3072 }
3073 break;
3074 }
3075 default:
3076 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3077 }
Calin Juravled0d48522014-11-04 16:40:20 +00003078 if (instruction->HasUses()) {
3079 locations->SetOut(Location::SameAsFirstInput());
3080 }
3081}
3082
3083void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3084 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3085 codegen_->AddSlowPath(slow_path);
3086
3087 LocationSummary* locations = instruction->GetLocations();
3088 Location value = locations->InAt(0);
3089
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003090 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003091 case Primitive::kPrimByte:
3092 case Primitive::kPrimChar:
3093 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003094 case Primitive::kPrimInt: {
3095 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003096 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003097 __ j(kEqual, slow_path->GetEntryLabel());
3098 } else if (value.IsStackSlot()) {
3099 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3100 __ j(kEqual, slow_path->GetEntryLabel());
3101 } else {
3102 DCHECK(value.IsConstant()) << value;
3103 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3104 __ jmp(slow_path->GetEntryLabel());
3105 }
3106 }
3107 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003108 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003109 case Primitive::kPrimLong: {
3110 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003112 __ movl(temp, value.AsRegisterPairLow<Register>());
3113 __ orl(temp, value.AsRegisterPairHigh<Register>());
3114 __ j(kEqual, slow_path->GetEntryLabel());
3115 } else {
3116 DCHECK(value.IsConstant()) << value;
3117 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3118 __ jmp(slow_path->GetEntryLabel());
3119 }
3120 }
3121 break;
3122 }
3123 default:
3124 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003125 }
Calin Juravled0d48522014-11-04 16:40:20 +00003126}
3127
Calin Juravle9aec02f2014-11-18 23:06:35 +00003128void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3129 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3130
3131 LocationSummary* locations =
3132 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3133
3134 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003135 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003136 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003137 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003138 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003139 // The shift count needs to be in CL or a constant.
3140 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003141 locations->SetOut(Location::SameAsFirstInput());
3142 break;
3143 }
3144 default:
3145 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3146 }
3147}
3148
3149void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3150 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3151
3152 LocationSummary* locations = op->GetLocations();
3153 Location first = locations->InAt(0);
3154 Location second = locations->InAt(1);
3155 DCHECK(first.Equals(locations->Out()));
3156
3157 switch (op->GetResultType()) {
3158 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003159 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003160 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003161 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003162 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003163 DCHECK_EQ(ECX, second_reg);
3164 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003165 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003166 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003167 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003168 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003169 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003170 }
3171 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003172 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3173 if (shift == 0) {
3174 return;
3175 }
3176 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003177 if (op->IsShl()) {
3178 __ shll(first_reg, imm);
3179 } else if (op->IsShr()) {
3180 __ sarl(first_reg, imm);
3181 } else {
3182 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003183 }
3184 }
3185 break;
3186 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003187 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003188 if (second.IsRegister()) {
3189 Register second_reg = second.AsRegister<Register>();
3190 DCHECK_EQ(ECX, second_reg);
3191 if (op->IsShl()) {
3192 GenerateShlLong(first, second_reg);
3193 } else if (op->IsShr()) {
3194 GenerateShrLong(first, second_reg);
3195 } else {
3196 GenerateUShrLong(first, second_reg);
3197 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003198 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003199 // Shift by a constant.
3200 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3201 // Nothing to do if the shift is 0, as the input is already the output.
3202 if (shift != 0) {
3203 if (op->IsShl()) {
3204 GenerateShlLong(first, shift);
3205 } else if (op->IsShr()) {
3206 GenerateShrLong(first, shift);
3207 } else {
3208 GenerateUShrLong(first, shift);
3209 }
3210 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003211 }
3212 break;
3213 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003214 default:
3215 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3216 }
3217}
3218
Mark P Mendell73945692015-04-29 14:56:17 +00003219void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3220 Register low = loc.AsRegisterPairLow<Register>();
3221 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003222 if (shift == 1) {
3223 // This is just an addition.
3224 __ addl(low, low);
3225 __ adcl(high, high);
3226 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003227 // Shift by 32 is easy. High gets low, and low gets 0.
3228 codegen_->EmitParallelMoves(
3229 loc.ToLow(),
3230 loc.ToHigh(),
3231 Primitive::kPrimInt,
3232 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3233 loc.ToLow(),
3234 Primitive::kPrimInt);
3235 } else if (shift > 32) {
3236 // Low part becomes 0. High part is low part << (shift-32).
3237 __ movl(high, low);
3238 __ shll(high, Immediate(shift - 32));
3239 __ xorl(low, low);
3240 } else {
3241 // Between 1 and 31.
3242 __ shld(high, low, Immediate(shift));
3243 __ shll(low, Immediate(shift));
3244 }
3245}
3246
Calin Juravle9aec02f2014-11-18 23:06:35 +00003247void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003248 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003249 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3250 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3251 __ testl(shifter, Immediate(32));
3252 __ j(kEqual, &done);
3253 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3254 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3255 __ Bind(&done);
3256}
3257
Mark P Mendell73945692015-04-29 14:56:17 +00003258void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3259 Register low = loc.AsRegisterPairLow<Register>();
3260 Register high = loc.AsRegisterPairHigh<Register>();
3261 if (shift == 32) {
3262 // Need to copy the sign.
3263 DCHECK_NE(low, high);
3264 __ movl(low, high);
3265 __ sarl(high, Immediate(31));
3266 } else if (shift > 32) {
3267 DCHECK_NE(low, high);
3268 // High part becomes sign. Low part is shifted by shift - 32.
3269 __ movl(low, high);
3270 __ sarl(high, Immediate(31));
3271 __ sarl(low, Immediate(shift - 32));
3272 } else {
3273 // Between 1 and 31.
3274 __ shrd(low, high, Immediate(shift));
3275 __ sarl(high, Immediate(shift));
3276 }
3277}
3278
Calin Juravle9aec02f2014-11-18 23:06:35 +00003279void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003280 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003281 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3282 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3283 __ testl(shifter, Immediate(32));
3284 __ j(kEqual, &done);
3285 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3286 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3287 __ Bind(&done);
3288}
3289
Mark P Mendell73945692015-04-29 14:56:17 +00003290void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3291 Register low = loc.AsRegisterPairLow<Register>();
3292 Register high = loc.AsRegisterPairHigh<Register>();
3293 if (shift == 32) {
3294 // Shift by 32 is easy. Low gets high, and high gets 0.
3295 codegen_->EmitParallelMoves(
3296 loc.ToHigh(),
3297 loc.ToLow(),
3298 Primitive::kPrimInt,
3299 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3300 loc.ToHigh(),
3301 Primitive::kPrimInt);
3302 } else if (shift > 32) {
3303 // Low part is high >> (shift - 32). High part becomes 0.
3304 __ movl(low, high);
3305 __ shrl(low, Immediate(shift - 32));
3306 __ xorl(high, high);
3307 } else {
3308 // Between 1 and 31.
3309 __ shrd(low, high, Immediate(shift));
3310 __ shrl(high, Immediate(shift));
3311 }
3312}
3313
Calin Juravle9aec02f2014-11-18 23:06:35 +00003314void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003315 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003316 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3317 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3318 __ testl(shifter, Immediate(32));
3319 __ j(kEqual, &done);
3320 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3321 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3322 __ Bind(&done);
3323}
3324
3325void LocationsBuilderX86::VisitShl(HShl* shl) {
3326 HandleShift(shl);
3327}
3328
3329void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3330 HandleShift(shl);
3331}
3332
3333void LocationsBuilderX86::VisitShr(HShr* shr) {
3334 HandleShift(shr);
3335}
3336
3337void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3338 HandleShift(shr);
3339}
3340
3341void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3342 HandleShift(ushr);
3343}
3344
3345void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3346 HandleShift(ushr);
3347}
3348
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003349void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003350 LocationSummary* locations =
3351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003352 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003353 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003354 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003355 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003356}
3357
3358void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3359 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003360 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003361 // Note: if heap poisoning is enabled, the entry point takes cares
3362 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003363 codegen_->InvokeRuntime(
3364 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3365 instruction,
3366 instruction->GetDexPc(),
3367 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003368 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003369}
3370
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003371void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3374 locations->SetOut(Location::RegisterLocation(EAX));
3375 InvokeRuntimeCallingConvention calling_convention;
3376 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003377 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003378 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003379}
3380
3381void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3382 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003383 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3384
Roland Levillain4d027112015-07-01 15:41:14 +01003385 // Note: if heap poisoning is enabled, the entry point takes cares
3386 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003387 codegen_->InvokeRuntime(
3388 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3389 instruction,
3390 instruction->GetDexPc(),
3391 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003392 DCHECK(!codegen_->IsLeafMethod());
3393}
3394
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003395void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003396 LocationSummary* locations =
3397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003398 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3399 if (location.IsStackSlot()) {
3400 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3401 } else if (location.IsDoubleStackSlot()) {
3402 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003403 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003404 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003405}
3406
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003407void InstructionCodeGeneratorX86::VisitParameterValue(
3408 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3409}
3410
3411void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3412 LocationSummary* locations =
3413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3414 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3415}
3416
3417void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003418}
3419
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003420void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003421 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003422 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003423 locations->SetInAt(0, Location::RequiresRegister());
3424 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003425}
3426
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003427void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3428 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003429 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003430 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003431 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003432 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003433 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003434 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003435 break;
3436
3437 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003438 __ notl(out.AsRegisterPairLow<Register>());
3439 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003440 break;
3441
3442 default:
3443 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3444 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003445}
3446
David Brazdil66d126e2015-04-03 16:02:44 +01003447void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3448 LocationSummary* locations =
3449 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3450 locations->SetInAt(0, Location::RequiresRegister());
3451 locations->SetOut(Location::SameAsFirstInput());
3452}
3453
3454void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003455 LocationSummary* locations = bool_not->GetLocations();
3456 Location in = locations->InAt(0);
3457 Location out = locations->Out();
3458 DCHECK(in.Equals(out));
3459 __ xorl(out.AsRegister<Register>(), Immediate(1));
3460}
3461
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003462void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003463 LocationSummary* locations =
3464 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003465 switch (compare->InputAt(0)->GetType()) {
3466 case Primitive::kPrimLong: {
3467 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003468 locations->SetInAt(1, Location::Any());
3469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3470 break;
3471 }
3472 case Primitive::kPrimFloat:
3473 case Primitive::kPrimDouble: {
3474 locations->SetInAt(0, Location::RequiresFpuRegister());
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
3476 locations->SetOut(Location::RequiresRegister());
3477 break;
3478 }
3479 default:
3480 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3481 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003482}
3483
3484void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003485 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003487 Location left = locations->InAt(0);
3488 Location right = locations->InAt(1);
3489
Mark Mendell0c9497d2015-08-21 09:30:05 -04003490 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003491 switch (compare->InputAt(0)->GetType()) {
3492 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003493 Register left_low = left.AsRegisterPairLow<Register>();
3494 Register left_high = left.AsRegisterPairHigh<Register>();
3495 int32_t val_low = 0;
3496 int32_t val_high = 0;
3497 bool right_is_const = false;
3498
3499 if (right.IsConstant()) {
3500 DCHECK(right.GetConstant()->IsLongConstant());
3501 right_is_const = true;
3502 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3503 val_low = Low32Bits(val);
3504 val_high = High32Bits(val);
3505 }
3506
Calin Juravleddb7df22014-11-25 20:56:51 +00003507 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003508 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003509 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003510 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003511 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003512 DCHECK(right_is_const) << right;
3513 if (val_high == 0) {
3514 __ testl(left_high, left_high);
3515 } else {
3516 __ cmpl(left_high, Immediate(val_high));
3517 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003518 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003519 __ j(kLess, &less); // Signed compare.
3520 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003521 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003522 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003523 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003524 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003525 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003526 DCHECK(right_is_const) << right;
3527 if (val_low == 0) {
3528 __ testl(left_low, left_low);
3529 } else {
3530 __ cmpl(left_low, Immediate(val_low));
3531 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003532 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003533 break;
3534 }
3535 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003537 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3538 break;
3539 }
3540 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003541 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003542 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003543 break;
3544 }
3545 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003546 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003547 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003548 __ movl(out, Immediate(0));
3549 __ j(kEqual, &done);
3550 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3551
3552 __ Bind(&greater);
3553 __ movl(out, Immediate(1));
3554 __ jmp(&done);
3555
3556 __ Bind(&less);
3557 __ movl(out, Immediate(-1));
3558
3559 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003560}
3561
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003562void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003563 LocationSummary* locations =
3564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003565 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3566 locations->SetInAt(i, Location::Any());
3567 }
3568 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003569}
3570
3571void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003572 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003573 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003574}
3575
Calin Juravle52c48962014-12-16 17:02:57 +00003576void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3577 /*
3578 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3579 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3580 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3581 */
3582 switch (kind) {
3583 case MemBarrierKind::kAnyAny: {
3584 __ mfence();
3585 break;
3586 }
3587 case MemBarrierKind::kAnyStore:
3588 case MemBarrierKind::kLoadAny:
3589 case MemBarrierKind::kStoreStore: {
3590 // nop
3591 break;
3592 }
3593 default:
3594 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003595 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003596}
3597
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598
Vladimir Marko58155012015-08-19 12:49:41 +00003599void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3600 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3601 switch (invoke->GetMethodLoadKind()) {
3602 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3603 // temp = thread->string_init_entrypoint
3604 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3605 break;
3606 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3607 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3608 break;
3609 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3610 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3611 break;
3612 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3613 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3614 method_patches_.emplace_back(invoke->GetTargetMethod());
3615 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3616 break;
3617 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3618 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3619 FALLTHROUGH_INTENDED;
3620 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3621 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3622 Register method_reg;
3623 Register reg = temp.AsRegister<Register>();
3624 if (current_method.IsRegister()) {
3625 method_reg = current_method.AsRegister<Register>();
3626 } else {
3627 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3628 DCHECK(!current_method.IsValid());
3629 method_reg = reg;
3630 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3631 }
3632 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003633 __ movl(reg, Address(method_reg,
3634 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003635 // temp = temp[index_in_cache]
3636 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3637 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3638 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003639 }
Vladimir Marko58155012015-08-19 12:49:41 +00003640 }
3641
3642 switch (invoke->GetCodePtrLocation()) {
3643 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3644 __ call(GetFrameEntryLabel());
3645 break;
3646 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3647 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3648 Label* label = &relative_call_patches_.back().label;
3649 __ call(label); // Bind to the patch label, override at link time.
3650 __ Bind(label); // Bind the label at the end of the "call" insn.
3651 break;
3652 }
3653 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3654 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3655 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3656 // (Though the direct CALL ptr16:32 is available for consideration).
3657 FALLTHROUGH_INTENDED;
3658 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3659 // (callee_method + offset_of_quick_compiled_code)()
3660 __ call(Address(callee_method.AsRegister<Register>(),
3661 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3662 kX86WordSize).Int32Value()));
3663 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003664 }
3665
3666 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003667}
3668
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003669void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3670 Register temp = temp_in.AsRegister<Register>();
3671 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3672 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3673 LocationSummary* locations = invoke->GetLocations();
3674 Location receiver = locations->InAt(0);
3675 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3676 // temp = object->GetClass();
3677 DCHECK(receiver.IsRegister());
3678 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3679 MaybeRecordImplicitNullCheck(invoke);
3680 __ MaybeUnpoisonHeapReference(temp);
3681 // temp = temp->GetMethodAt(method_offset);
3682 __ movl(temp, Address(temp, method_offset));
3683 // call temp->GetEntryPoint();
3684 __ call(Address(
3685 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3686}
3687
Vladimir Marko58155012015-08-19 12:49:41 +00003688void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3689 DCHECK(linker_patches->empty());
3690 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3691 for (const MethodPatchInfo<Label>& info : method_patches_) {
3692 // The label points to the end of the "movl" insn but the literal offset for method
3693 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3694 uint32_t literal_offset = info.label.Position() - 4;
3695 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3696 info.target_method.dex_file,
3697 info.target_method.dex_method_index));
3698 }
3699 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3700 // The label points to the end of the "call" insn but the literal offset for method
3701 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3702 uint32_t literal_offset = info.label.Position() - 4;
3703 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3704 info.target_method.dex_file,
3705 info.target_method.dex_method_index));
3706 }
3707}
3708
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003709void CodeGeneratorX86::MarkGCCard(Register temp,
3710 Register card,
3711 Register object,
3712 Register value,
3713 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003714 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003715 if (value_can_be_null) {
3716 __ testl(value, value);
3717 __ j(kEqual, &is_null);
3718 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003719 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3720 __ movl(temp, object);
3721 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003722 __ movb(Address(temp, card, TIMES_1, 0),
3723 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003724 if (value_can_be_null) {
3725 __ Bind(&is_null);
3726 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727}
3728
Calin Juravle52c48962014-12-16 17:02:57 +00003729void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3730 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003731 LocationSummary* locations =
3732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003733 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003734
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003735 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3736 locations->SetOut(Location::RequiresFpuRegister());
3737 } else {
3738 // The output overlaps in case of long: we don't want the low move to overwrite
3739 // the object's location.
3740 locations->SetOut(Location::RequiresRegister(),
3741 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3742 : Location::kNoOutputOverlap);
3743 }
Calin Juravle52c48962014-12-16 17:02:57 +00003744
3745 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3746 // Long values can be loaded atomically into an XMM using movsd.
3747 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3748 // and then copy the XMM into the output 32bits at a time).
3749 locations->AddTemp(Location::RequiresFpuRegister());
3750 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003751}
3752
Calin Juravle52c48962014-12-16 17:02:57 +00003753void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3754 const FieldInfo& field_info) {
3755 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003756
Calin Juravle52c48962014-12-16 17:02:57 +00003757 LocationSummary* locations = instruction->GetLocations();
3758 Register base = locations->InAt(0).AsRegister<Register>();
3759 Location out = locations->Out();
3760 bool is_volatile = field_info.IsVolatile();
3761 Primitive::Type field_type = field_info.GetFieldType();
3762 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3763
3764 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003765 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003766 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003767 break;
3768 }
3769
3770 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003771 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003772 break;
3773 }
3774
3775 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003776 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003777 break;
3778 }
3779
3780 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003781 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003782 break;
3783 }
3784
3785 case Primitive::kPrimInt:
3786 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003787 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788 break;
3789 }
3790
3791 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003792 if (is_volatile) {
3793 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3794 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003795 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003796 __ movd(out.AsRegisterPairLow<Register>(), temp);
3797 __ psrlq(temp, Immediate(32));
3798 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3799 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003800 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003801 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003802 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003803 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3804 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805 break;
3806 }
3807
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003808 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003809 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003810 break;
3811 }
3812
3813 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003814 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003815 break;
3816 }
3817
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003818 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003819 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003820 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821 }
Calin Juravle52c48962014-12-16 17:02:57 +00003822
Calin Juravle77520bc2015-01-12 18:45:46 +00003823 // Longs are handled in the switch.
3824 if (field_type != Primitive::kPrimLong) {
3825 codegen_->MaybeRecordImplicitNullCheck(instruction);
3826 }
3827
Calin Juravle52c48962014-12-16 17:02:57 +00003828 if (is_volatile) {
3829 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3830 }
Roland Levillain4d027112015-07-01 15:41:14 +01003831
3832 if (field_type == Primitive::kPrimNot) {
3833 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3834 }
Calin Juravle52c48962014-12-16 17:02:57 +00003835}
3836
3837void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3838 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3839
3840 LocationSummary* locations =
3841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3842 locations->SetInAt(0, Location::RequiresRegister());
3843 bool is_volatile = field_info.IsVolatile();
3844 Primitive::Type field_type = field_info.GetFieldType();
3845 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3846 || (field_type == Primitive::kPrimByte);
3847
3848 // The register allocator does not support multiple
3849 // inputs that die at entry with one in a specific register.
3850 if (is_byte_type) {
3851 // Ensure the value is in a byte register.
3852 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003853 } else if (Primitive::IsFloatingPointType(field_type)) {
3854 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003855 } else {
3856 locations->SetInAt(1, Location::RequiresRegister());
3857 }
Calin Juravle52c48962014-12-16 17:02:57 +00003858 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003859 // Temporary registers for the write barrier.
3860 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003861 // Ensure the card is in a byte register.
3862 locations->AddTemp(Location::RegisterLocation(ECX));
3863 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3864 // 64bits value can be atomically written to an address with movsd and an XMM register.
3865 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3866 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3867 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3868 // isolated cases when we need this it isn't worth adding the extra complexity.
3869 locations->AddTemp(Location::RequiresFpuRegister());
3870 locations->AddTemp(Location::RequiresFpuRegister());
3871 }
3872}
3873
3874void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003875 const FieldInfo& field_info,
3876 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003877 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3878
3879 LocationSummary* locations = instruction->GetLocations();
3880 Register base = locations->InAt(0).AsRegister<Register>();
3881 Location value = locations->InAt(1);
3882 bool is_volatile = field_info.IsVolatile();
3883 Primitive::Type field_type = field_info.GetFieldType();
3884 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003885 bool needs_write_barrier =
3886 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003887
3888 if (is_volatile) {
3889 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3890 }
3891
3892 switch (field_type) {
3893 case Primitive::kPrimBoolean:
3894 case Primitive::kPrimByte: {
3895 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3896 break;
3897 }
3898
3899 case Primitive::kPrimShort:
3900 case Primitive::kPrimChar: {
3901 __ movw(Address(base, offset), value.AsRegister<Register>());
3902 break;
3903 }
3904
3905 case Primitive::kPrimInt:
3906 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003907 if (kPoisonHeapReferences && needs_write_barrier) {
3908 // Note that in the case where `value` is a null reference,
3909 // we do not enter this block, as the reference does not
3910 // need poisoning.
3911 DCHECK_EQ(field_type, Primitive::kPrimNot);
3912 Register temp = locations->GetTemp(0).AsRegister<Register>();
3913 __ movl(temp, value.AsRegister<Register>());
3914 __ PoisonHeapReference(temp);
3915 __ movl(Address(base, offset), temp);
3916 } else {
3917 __ movl(Address(base, offset), value.AsRegister<Register>());
3918 }
Calin Juravle52c48962014-12-16 17:02:57 +00003919 break;
3920 }
3921
3922 case Primitive::kPrimLong: {
3923 if (is_volatile) {
3924 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3925 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3926 __ movd(temp1, value.AsRegisterPairLow<Register>());
3927 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3928 __ punpckldq(temp1, temp2);
3929 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003930 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003931 } else {
3932 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003933 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003934 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3935 }
3936 break;
3937 }
3938
3939 case Primitive::kPrimFloat: {
3940 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3941 break;
3942 }
3943
3944 case Primitive::kPrimDouble: {
3945 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3946 break;
3947 }
3948
3949 case Primitive::kPrimVoid:
3950 LOG(FATAL) << "Unreachable type " << field_type;
3951 UNREACHABLE();
3952 }
3953
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 // Longs are handled in the switch.
3955 if (field_type != Primitive::kPrimLong) {
3956 codegen_->MaybeRecordImplicitNullCheck(instruction);
3957 }
3958
Roland Levillain4d027112015-07-01 15:41:14 +01003959 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003960 Register temp = locations->GetTemp(0).AsRegister<Register>();
3961 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003962 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003963 }
3964
Calin Juravle52c48962014-12-16 17:02:57 +00003965 if (is_volatile) {
3966 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3967 }
3968}
3969
3970void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3971 HandleFieldGet(instruction, instruction->GetFieldInfo());
3972}
3973
3974void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3975 HandleFieldGet(instruction, instruction->GetFieldInfo());
3976}
3977
3978void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3979 HandleFieldSet(instruction, instruction->GetFieldInfo());
3980}
3981
3982void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003983 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003984}
3985
3986void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3987 HandleFieldSet(instruction, instruction->GetFieldInfo());
3988}
3989
3990void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003991 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003992}
3993
3994void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3995 HandleFieldGet(instruction, instruction->GetFieldInfo());
3996}
3997
3998void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3999 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004000}
4001
4002void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004003 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4004 ? LocationSummary::kCallOnSlowPath
4005 : LocationSummary::kNoCall;
4006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4007 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004008 ? Location::RequiresRegister()
4009 : Location::Any();
4010 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004011 if (instruction->HasUses()) {
4012 locations->SetOut(Location::SameAsFirstInput());
4013 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004014}
4015
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004016void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004017 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4018 return;
4019 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004020 LocationSummary* locations = instruction->GetLocations();
4021 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004022
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004023 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4024 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4025}
4026
4027void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004028 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004029 codegen_->AddSlowPath(slow_path);
4030
4031 LocationSummary* locations = instruction->GetLocations();
4032 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004033
4034 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004035 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004036 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004037 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004038 } else {
4039 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004040 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004041 __ jmp(slow_path->GetEntryLabel());
4042 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043 }
4044 __ j(kEqual, slow_path->GetEntryLabel());
4045}
4046
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004047void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004048 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004049 GenerateImplicitNullCheck(instruction);
4050 } else {
4051 GenerateExplicitNullCheck(instruction);
4052 }
4053}
4054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004056 LocationSummary* locations =
4057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004060 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4061 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4062 } else {
4063 // The output overlaps in case of long: we don't want the low move to overwrite
4064 // the array's location.
4065 locations->SetOut(Location::RequiresRegister(),
4066 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4067 : Location::kNoOutputOverlap);
4068 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004069}
4070
4071void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4072 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004073 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074 Location index = locations->InAt(1);
4075
Calin Juravle77520bc2015-01-12 18:45:46 +00004076 Primitive::Type type = instruction->GetType();
4077 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004078 case Primitive::kPrimBoolean: {
4079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004080 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004081 if (index.IsConstant()) {
4082 __ movzxb(out, Address(obj,
4083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4084 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004085 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004086 }
4087 break;
4088 }
4089
4090 case Primitive::kPrimByte: {
4091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004093 if (index.IsConstant()) {
4094 __ movsxb(out, Address(obj,
4095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098 }
4099 break;
4100 }
4101
4102 case Primitive::kPrimShort: {
4103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004104 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 if (index.IsConstant()) {
4106 __ movsxw(out, Address(obj,
4107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004110 }
4111 break;
4112 }
4113
4114 case Primitive::kPrimChar: {
4115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004116 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004117 if (index.IsConstant()) {
4118 __ movzxw(out, Address(obj,
4119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 }
4123 break;
4124 }
4125
4126 case Primitive::kPrimInt:
4127 case Primitive::kPrimNot: {
4128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004129 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004130 if (index.IsConstant()) {
4131 __ movl(out, Address(obj,
4132 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4133 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004134 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135 }
4136 break;
4137 }
4138
4139 case Primitive::kPrimLong: {
4140 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004141 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004142 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143 if (index.IsConstant()) {
4144 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004145 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004147 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004148 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004149 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004150 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004151 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004152 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004154 }
4155 break;
4156 }
4157
Mark Mendell7c8d0092015-01-26 11:21:33 -05004158 case Primitive::kPrimFloat: {
4159 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4160 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4161 if (index.IsConstant()) {
4162 __ movss(out, Address(obj,
4163 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4164 } else {
4165 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4166 }
4167 break;
4168 }
4169
4170 case Primitive::kPrimDouble: {
4171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4172 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4173 if (index.IsConstant()) {
4174 __ movsd(out, Address(obj,
4175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4176 } else {
4177 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4178 }
4179 break;
4180 }
4181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004182 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004183 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004184 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004186
4187 if (type != Primitive::kPrimLong) {
4188 codegen_->MaybeRecordImplicitNullCheck(instruction);
4189 }
Roland Levillain4d027112015-07-01 15:41:14 +01004190
4191 if (type == Primitive::kPrimNot) {
4192 Register out = locations->Out().AsRegister<Register>();
4193 __ MaybeUnpoisonHeapReference(out);
4194 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004195}
4196
4197void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004198 // This location builder might end up asking to up to four registers, which is
4199 // not currently possible for baseline. The situation in which we need four
4200 // registers cannot be met by baseline though, because it has not run any
4201 // optimization.
4202
Nicolas Geoffray39468442014-09-02 15:17:15 +01004203 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004204 bool needs_write_barrier =
4205 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4206
Mark Mendell5f874182015-03-04 15:42:45 -05004207 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004208
Nicolas Geoffray39468442014-09-02 15:17:15 +01004209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4210 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004211 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004212
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004213 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4216 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4217 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004218 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004219 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4220 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004221 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004222 // In case of a byte operation, the register allocator does not support multiple
4223 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004224 locations->SetInAt(0, Location::RequiresRegister());
4225 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004226 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004227 // Ensure the value is in a byte register.
4228 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004229 } else if (Primitive::IsFloatingPointType(value_type)) {
4230 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004232 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004234 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004235 // Temporary registers for the write barrier.
4236 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004237 // Ensure the card is in a byte register.
4238 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004241}
4242
4243void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4244 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004246 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004247 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004248 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004249 bool needs_runtime_call = locations->WillCall();
4250 bool needs_write_barrier =
4251 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004252
4253 switch (value_type) {
4254 case Primitive::kPrimBoolean:
4255 case Primitive::kPrimByte: {
4256 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 if (index.IsConstant()) {
4258 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004259 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004260 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004261 } else {
4262 __ movb(Address(obj, offset),
4263 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4264 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004266 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004267 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004268 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004269 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004271 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4272 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 break;
4276 }
4277
4278 case Primitive::kPrimShort:
4279 case Primitive::kPrimChar: {
4280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 if (index.IsConstant()) {
4282 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004283 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004284 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004285 } else {
4286 __ movw(Address(obj, offset),
4287 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4288 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004290 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4292 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004293 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004294 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004295 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4296 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004299 break;
4300 }
4301
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004302 case Primitive::kPrimInt:
4303 case Primitive::kPrimNot: {
4304 if (!needs_runtime_call) {
4305 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4306 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004307 size_t offset =
4308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004309 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004310 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4311 Register temp = locations->GetTemp(0).AsRegister<Register>();
4312 __ movl(temp, value.AsRegister<Register>());
4313 __ PoisonHeapReference(temp);
4314 __ movl(Address(obj, offset), temp);
4315 } else {
4316 __ movl(Address(obj, offset), value.AsRegister<Register>());
4317 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004318 } else {
4319 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004320 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4321 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4322 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4323 // Note: if heap poisoning is enabled, no need to poison
4324 // (negate) `v` if it is a reference, as it would be null.
4325 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004326 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004327 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004328 DCHECK(index.IsRegister()) << index;
4329 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004330 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4331 Register temp = locations->GetTemp(0).AsRegister<Register>();
4332 __ movl(temp, value.AsRegister<Register>());
4333 __ PoisonHeapReference(temp);
4334 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4335 } else {
4336 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4337 value.AsRegister<Register>());
4338 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004339 } else {
4340 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004341 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4342 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4343 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4344 // Note: if heap poisoning is enabled, no need to poison
4345 // (negate) `v` if it is a reference, as it would be null.
4346 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004347 }
4348 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004349 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004350
4351 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 Register temp = locations->GetTemp(0).AsRegister<Register>();
4353 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004354 codegen_->MarkGCCard(
4355 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004356 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004357 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004358 DCHECK_EQ(value_type, Primitive::kPrimNot);
4359 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004360 // Note: if heap poisoning is enabled, pAputObject takes cares
4361 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004362 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4363 instruction,
4364 instruction->GetDexPc(),
4365 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 }
4367 break;
4368 }
4369
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 case Primitive::kPrimLong: {
4371 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004372 if (index.IsConstant()) {
4373 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004374 if (value.IsRegisterPair()) {
4375 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004377 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004378 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004379 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004380 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4381 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004383 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4384 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004386 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004387 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004388 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004391 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004392 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004393 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004394 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004396 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004397 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004399 Immediate(High32Bits(val)));
4400 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004401 }
4402 break;
4403 }
4404
Mark Mendell7c8d0092015-01-26 11:21:33 -05004405 case Primitive::kPrimFloat: {
4406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4407 DCHECK(value.IsFpuRegister());
4408 if (index.IsConstant()) {
4409 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4410 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4411 } else {
4412 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4413 value.AsFpuRegister<XmmRegister>());
4414 }
4415 break;
4416 }
4417
4418 case Primitive::kPrimDouble: {
4419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4420 DCHECK(value.IsFpuRegister());
4421 if (index.IsConstant()) {
4422 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4423 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4424 } else {
4425 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4426 value.AsFpuRegister<XmmRegister>());
4427 }
4428 break;
4429 }
4430
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 case Primitive::kPrimVoid:
4432 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004433 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004434 }
4435}
4436
4437void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4438 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004439 locations->SetInAt(0, Location::RequiresRegister());
4440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004441}
4442
4443void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4444 LocationSummary* locations = instruction->GetLocations();
4445 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004446 Register obj = locations->InAt(0).AsRegister<Register>();
4447 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004449 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450}
4451
4452void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004453 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4454 ? LocationSummary::kCallOnSlowPath
4455 : LocationSummary::kNoCall;
4456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004457 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004458 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004459 if (instruction->HasUses()) {
4460 locations->SetOut(Location::SameAsFirstInput());
4461 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462}
4463
4464void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4465 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004466 Location index_loc = locations->InAt(0);
4467 Location length_loc = locations->InAt(1);
4468 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004469 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004470
Mark Mendell99dbd682015-04-22 16:18:52 -04004471 if (length_loc.IsConstant()) {
4472 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4473 if (index_loc.IsConstant()) {
4474 // BCE will remove the bounds check if we are guarenteed to pass.
4475 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4476 if (index < 0 || index >= length) {
4477 codegen_->AddSlowPath(slow_path);
4478 __ jmp(slow_path->GetEntryLabel());
4479 } else {
4480 // Some optimization after BCE may have generated this, and we should not
4481 // generate a bounds check if it is a valid range.
4482 }
4483 return;
4484 }
4485
4486 // We have to reverse the jump condition because the length is the constant.
4487 Register index_reg = index_loc.AsRegister<Register>();
4488 __ cmpl(index_reg, Immediate(length));
4489 codegen_->AddSlowPath(slow_path);
4490 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004491 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004492 Register length = length_loc.AsRegister<Register>();
4493 if (index_loc.IsConstant()) {
4494 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4495 __ cmpl(length, Immediate(value));
4496 } else {
4497 __ cmpl(length, index_loc.AsRegister<Register>());
4498 }
4499 codegen_->AddSlowPath(slow_path);
4500 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004501 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004502}
4503
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4505 temp->SetLocations(nullptr);
4506}
4507
4508void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4509 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004510 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004511}
4512
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004513void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004514 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004515 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004516}
4517
4518void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004519 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4520}
4521
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004522void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4524}
4525
4526void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004527 HBasicBlock* block = instruction->GetBlock();
4528 if (block->GetLoopInformation() != nullptr) {
4529 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4530 // The back edge will generate the suspend check.
4531 return;
4532 }
4533 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4534 // The goto will generate the suspend check.
4535 return;
4536 }
4537 GenerateSuspendCheck(instruction, nullptr);
4538}
4539
4540void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4541 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004542 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004543 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4544 if (slow_path == nullptr) {
4545 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4546 instruction->SetSlowPath(slow_path);
4547 codegen_->AddSlowPath(slow_path);
4548 if (successor != nullptr) {
4549 DCHECK(successor->IsLoopHeader());
4550 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4551 }
4552 } else {
4553 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4554 }
4555
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004556 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004557 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004558 if (successor == nullptr) {
4559 __ j(kNotEqual, slow_path->GetEntryLabel());
4560 __ Bind(slow_path->GetReturnLabel());
4561 } else {
4562 __ j(kEqual, codegen_->GetLabelOf(successor));
4563 __ jmp(slow_path->GetEntryLabel());
4564 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004565}
4566
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004567X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4568 return codegen_->GetAssembler();
4569}
4570
Mark Mendell7c8d0092015-01-26 11:21:33 -05004571void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004572 ScratchRegisterScope ensure_scratch(
4573 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4574 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4575 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4576 __ movl(temp_reg, Address(ESP, src + stack_offset));
4577 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004578}
4579
4580void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004581 ScratchRegisterScope ensure_scratch(
4582 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4583 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4584 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4585 __ movl(temp_reg, Address(ESP, src + stack_offset));
4586 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4587 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4588 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004589}
4590
4591void ParallelMoveResolverX86::EmitMove(size_t index) {
4592 MoveOperands* move = moves_.Get(index);
4593 Location source = move->GetSource();
4594 Location destination = move->GetDestination();
4595
4596 if (source.IsRegister()) {
4597 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004598 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004599 } else {
4600 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004601 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004602 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004603 } else if (source.IsFpuRegister()) {
4604 if (destination.IsFpuRegister()) {
4605 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4606 } else if (destination.IsStackSlot()) {
4607 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4608 } else {
4609 DCHECK(destination.IsDoubleStackSlot());
4610 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4611 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004612 } else if (source.IsStackSlot()) {
4613 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004614 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004615 } else if (destination.IsFpuRegister()) {
4616 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004617 } else {
4618 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004619 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4620 }
4621 } else if (source.IsDoubleStackSlot()) {
4622 if (destination.IsFpuRegister()) {
4623 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4624 } else {
4625 DCHECK(destination.IsDoubleStackSlot()) << destination;
4626 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004627 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004628 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004629 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004630 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004631 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004632 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004633 if (value == 0) {
4634 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4635 } else {
4636 __ movl(destination.AsRegister<Register>(), Immediate(value));
4637 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004638 } else {
4639 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004640 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004641 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004642 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004643 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004644 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004645 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004646 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004647 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4648 if (value == 0) {
4649 // Easy handling of 0.0.
4650 __ xorps(dest, dest);
4651 } else {
4652 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004653 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4654 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4655 __ movl(temp, Immediate(value));
4656 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004657 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004658 } else {
4659 DCHECK(destination.IsStackSlot()) << destination;
4660 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4661 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004662 } else if (constant->IsLongConstant()) {
4663 int64_t value = constant->AsLongConstant()->GetValue();
4664 int32_t low_value = Low32Bits(value);
4665 int32_t high_value = High32Bits(value);
4666 Immediate low(low_value);
4667 Immediate high(high_value);
4668 if (destination.IsDoubleStackSlot()) {
4669 __ movl(Address(ESP, destination.GetStackIndex()), low);
4670 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4671 } else {
4672 __ movl(destination.AsRegisterPairLow<Register>(), low);
4673 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4674 }
4675 } else {
4676 DCHECK(constant->IsDoubleConstant());
4677 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004678 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004679 int32_t low_value = Low32Bits(value);
4680 int32_t high_value = High32Bits(value);
4681 Immediate low(low_value);
4682 Immediate high(high_value);
4683 if (destination.IsFpuRegister()) {
4684 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4685 if (value == 0) {
4686 // Easy handling of 0.0.
4687 __ xorpd(dest, dest);
4688 } else {
4689 __ pushl(high);
4690 __ pushl(low);
4691 __ movsd(dest, Address(ESP, 0));
4692 __ addl(ESP, Immediate(8));
4693 }
4694 } else {
4695 DCHECK(destination.IsDoubleStackSlot()) << destination;
4696 __ movl(Address(ESP, destination.GetStackIndex()), low);
4697 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4698 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004699 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004700 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004701 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004702 }
4703}
4704
Mark Mendella5c19ce2015-04-01 12:51:05 -04004705void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004706 Register suggested_scratch = reg == EAX ? EBX : EAX;
4707 ScratchRegisterScope ensure_scratch(
4708 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4709
4710 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4711 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4712 __ movl(Address(ESP, mem + stack_offset), reg);
4713 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004714}
4715
Mark Mendell7c8d0092015-01-26 11:21:33 -05004716void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004717 ScratchRegisterScope ensure_scratch(
4718 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4719
4720 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4721 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4722 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4723 __ movss(Address(ESP, mem + stack_offset), reg);
4724 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004725}
4726
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004727void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004728 ScratchRegisterScope ensure_scratch1(
4729 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004730
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004731 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4732 ScratchRegisterScope ensure_scratch2(
4733 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004734
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004735 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4736 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4737 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4738 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4739 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4740 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004741}
4742
4743void ParallelMoveResolverX86::EmitSwap(size_t index) {
4744 MoveOperands* move = moves_.Get(index);
4745 Location source = move->GetSource();
4746 Location destination = move->GetDestination();
4747
4748 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004749 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4750 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4751 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4752 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4753 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004754 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004756 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004757 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004758 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4759 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004760 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4761 // Use XOR Swap algorithm to avoid a temporary.
4762 DCHECK_NE(source.reg(), destination.reg());
4763 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4764 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4765 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4766 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4767 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4768 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4769 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004770 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4771 // Take advantage of the 16 bytes in the XMM register.
4772 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4773 Address stack(ESP, destination.GetStackIndex());
4774 // Load the double into the high doubleword.
4775 __ movhpd(reg, stack);
4776
4777 // Store the low double into the destination.
4778 __ movsd(stack, reg);
4779
4780 // Move the high double to the low double.
4781 __ psrldq(reg, Immediate(8));
4782 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4783 // Take advantage of the 16 bytes in the XMM register.
4784 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4785 Address stack(ESP, source.GetStackIndex());
4786 // Load the double into the high doubleword.
4787 __ movhpd(reg, stack);
4788
4789 // Store the low double into the destination.
4790 __ movsd(stack, reg);
4791
4792 // Move the high double to the low double.
4793 __ psrldq(reg, Immediate(8));
4794 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4795 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4796 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004797 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004798 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004799 }
4800}
4801
4802void ParallelMoveResolverX86::SpillScratch(int reg) {
4803 __ pushl(static_cast<Register>(reg));
4804}
4805
4806void ParallelMoveResolverX86::RestoreScratch(int reg) {
4807 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004808}
4809
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004810void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004811 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4812 ? LocationSummary::kCallOnSlowPath
4813 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004814 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004815 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004816 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004817 locations->SetOut(Location::RequiresRegister());
4818}
4819
4820void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004821 LocationSummary* locations = cls->GetLocations();
4822 Register out = locations->Out().AsRegister<Register>();
4823 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004824 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004825 DCHECK(!cls->CanCallRuntime());
4826 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004827 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004828 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004829 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004830 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004831 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004832 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004833 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004834
4835 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4836 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4837 codegen_->AddSlowPath(slow_path);
4838 __ testl(out, out);
4839 __ j(kEqual, slow_path->GetEntryLabel());
4840 if (cls->MustGenerateClinitCheck()) {
4841 GenerateClassInitializationCheck(slow_path, out);
4842 } else {
4843 __ Bind(slow_path->GetExitLabel());
4844 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004845 }
4846}
4847
4848void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4849 LocationSummary* locations =
4850 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4851 locations->SetInAt(0, Location::RequiresRegister());
4852 if (check->HasUses()) {
4853 locations->SetOut(Location::SameAsFirstInput());
4854 }
4855}
4856
4857void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004858 // We assume the class to not be null.
4859 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4860 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004861 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004862 GenerateClassInitializationCheck(slow_path,
4863 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004864}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004865
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004866void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4867 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004868 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4869 Immediate(mirror::Class::kStatusInitialized));
4870 __ j(kLess, slow_path->GetEntryLabel());
4871 __ Bind(slow_path->GetExitLabel());
4872 // No need for memory fence, thanks to the X86 memory model.
4873}
4874
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004875void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004878 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004879 locations->SetOut(Location::RequiresRegister());
4880}
4881
4882void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4883 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4884 codegen_->AddSlowPath(slow_path);
4885
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004886 LocationSummary* locations = load->GetLocations();
4887 Register out = locations->Out().AsRegister<Register>();
4888 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004889 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004890 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004891 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004892 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004893 __ testl(out, out);
4894 __ j(kEqual, slow_path->GetEntryLabel());
4895 __ Bind(slow_path->GetExitLabel());
4896}
4897
David Brazdilcb1c0552015-08-04 16:22:25 +01004898static Address GetExceptionTlsAddress() {
4899 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4900}
4901
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004902void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4903 LocationSummary* locations =
4904 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4905 locations->SetOut(Location::RequiresRegister());
4906}
4907
4908void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004909 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4910}
4911
4912void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4913 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4914}
4915
4916void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4917 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004918}
4919
4920void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4921 LocationSummary* locations =
4922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4923 InvokeRuntimeCallingConvention calling_convention;
4924 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4925}
4926
4927void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004928 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4929 instruction,
4930 instruction->GetDexPc(),
4931 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004932}
4933
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004934void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004935 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4936 ? LocationSummary::kNoCall
4937 : LocationSummary::kCallOnSlowPath;
4938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4939 locations->SetInAt(0, Location::RequiresRegister());
4940 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004941 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004942 locations->SetOut(Location::RequiresRegister());
4943}
4944
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004945void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004946 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004947 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004948 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004949 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004950 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Mark Mendell0c9497d2015-08-21 09:30:05 -04004951 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004952 SlowPathCodeX86* slow_path = nullptr;
4953
4954 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004955 // Avoid null check if we know obj is not null.
4956 if (instruction->MustDoNullCheck()) {
4957 __ testl(obj, obj);
4958 __ j(kEqual, &zero);
4959 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004960 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004961 __ movl(out, Address(obj, class_offset));
4962 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004963 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004964 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004965 } else {
4966 DCHECK(cls.IsStackSlot()) << cls;
4967 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4968 }
4969
4970 if (instruction->IsClassFinal()) {
4971 // Classes must be equal for the instanceof to succeed.
4972 __ j(kNotEqual, &zero);
4973 __ movl(out, Immediate(1));
4974 __ jmp(&done);
4975 } else {
4976 // If the classes are not equal, we go into a slow path.
4977 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004978 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004979 codegen_->AddSlowPath(slow_path);
4980 __ j(kNotEqual, slow_path->GetEntryLabel());
4981 __ movl(out, Immediate(1));
4982 __ jmp(&done);
4983 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004984
4985 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4986 __ Bind(&zero);
4987 __ movl(out, Immediate(0));
4988 }
4989
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004990 if (slow_path != nullptr) {
4991 __ Bind(slow_path->GetExitLabel());
4992 }
4993 __ Bind(&done);
4994}
4995
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004996void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4998 instruction, LocationSummary::kCallOnSlowPath);
4999 locations->SetInAt(0, Location::RequiresRegister());
5000 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005001 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005002 locations->AddTemp(Location::RequiresRegister());
5003}
5004
5005void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5006 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005007 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005008 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005009 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005010 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005011 SlowPathCodeX86* slow_path =
5012 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005013 codegen_->AddSlowPath(slow_path);
5014
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005015 // Avoid null check if we know obj is not null.
5016 if (instruction->MustDoNullCheck()) {
5017 __ testl(obj, obj);
5018 __ j(kEqual, slow_path->GetExitLabel());
5019 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005020 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01005021 __ movl(temp, Address(obj, class_offset));
5022 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005023 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005024 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005025 } else {
5026 DCHECK(cls.IsStackSlot()) << cls;
5027 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5028 }
Roland Levillain4d027112015-07-01 15:41:14 +01005029 // The checkcast succeeds if the classes are equal (fast path).
5030 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005031 __ j(kNotEqual, slow_path->GetEntryLabel());
5032 __ Bind(slow_path->GetExitLabel());
5033}
5034
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005035void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5036 LocationSummary* locations =
5037 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5038 InvokeRuntimeCallingConvention calling_convention;
5039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5040}
5041
5042void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005043 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5044 : QUICK_ENTRY_POINT(pUnlockObject),
5045 instruction,
5046 instruction->GetDexPc(),
5047 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005048}
5049
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005050void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5051void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5052void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5053
5054void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5055 LocationSummary* locations =
5056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5057 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5058 || instruction->GetResultType() == Primitive::kPrimLong);
5059 locations->SetInAt(0, Location::RequiresRegister());
5060 locations->SetInAt(1, Location::Any());
5061 locations->SetOut(Location::SameAsFirstInput());
5062}
5063
5064void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5065 HandleBitwiseOperation(instruction);
5066}
5067
5068void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5069 HandleBitwiseOperation(instruction);
5070}
5071
5072void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5073 HandleBitwiseOperation(instruction);
5074}
5075
5076void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5077 LocationSummary* locations = instruction->GetLocations();
5078 Location first = locations->InAt(0);
5079 Location second = locations->InAt(1);
5080 DCHECK(first.Equals(locations->Out()));
5081
5082 if (instruction->GetResultType() == Primitive::kPrimInt) {
5083 if (second.IsRegister()) {
5084 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005086 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005088 } else {
5089 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005090 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005091 }
5092 } else if (second.IsConstant()) {
5093 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005094 __ andl(first.AsRegister<Register>(),
5095 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005096 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005097 __ orl(first.AsRegister<Register>(),
5098 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005099 } else {
5100 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005101 __ xorl(first.AsRegister<Register>(),
5102 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005103 }
5104 } else {
5105 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005106 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005107 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005108 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005109 } else {
5110 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005111 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005112 }
5113 }
5114 } else {
5115 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5116 if (second.IsRegisterPair()) {
5117 if (instruction->IsAnd()) {
5118 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5119 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5120 } else if (instruction->IsOr()) {
5121 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5122 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5123 } else {
5124 DCHECK(instruction->IsXor());
5125 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5126 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5127 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005128 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005129 if (instruction->IsAnd()) {
5130 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5131 __ andl(first.AsRegisterPairHigh<Register>(),
5132 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5133 } else if (instruction->IsOr()) {
5134 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5135 __ orl(first.AsRegisterPairHigh<Register>(),
5136 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5137 } else {
5138 DCHECK(instruction->IsXor());
5139 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5140 __ xorl(first.AsRegisterPairHigh<Register>(),
5141 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5142 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005143 } else {
5144 DCHECK(second.IsConstant()) << second;
5145 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005146 int32_t low_value = Low32Bits(value);
5147 int32_t high_value = High32Bits(value);
5148 Immediate low(low_value);
5149 Immediate high(high_value);
5150 Register first_low = first.AsRegisterPairLow<Register>();
5151 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005152 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005153 if (low_value == 0) {
5154 __ xorl(first_low, first_low);
5155 } else if (low_value != -1) {
5156 __ andl(first_low, low);
5157 }
5158 if (high_value == 0) {
5159 __ xorl(first_high, first_high);
5160 } else if (high_value != -1) {
5161 __ andl(first_high, high);
5162 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005163 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005164 if (low_value != 0) {
5165 __ orl(first_low, low);
5166 }
5167 if (high_value != 0) {
5168 __ orl(first_high, high);
5169 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005170 } else {
5171 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005172 if (low_value != 0) {
5173 __ xorl(first_low, low);
5174 }
5175 if (high_value != 0) {
5176 __ xorl(first_high, high);
5177 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005178 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005179 }
5180 }
5181}
5182
Calin Juravleb1498f62015-02-16 13:13:29 +00005183void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5184 // Nothing to do, this should be removed during prepare for register allocator.
5185 UNUSED(instruction);
5186 LOG(FATAL) << "Unreachable";
5187}
5188
5189void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5190 // Nothing to do, this should be removed during prepare for register allocator.
5191 UNUSED(instruction);
5192 LOG(FATAL) << "Unreachable";
5193}
5194
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005195void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5196 DCHECK(codegen_->IsBaseline());
5197 LocationSummary* locations =
5198 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5199 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5200}
5201
5202void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5203 DCHECK(codegen_->IsBaseline());
5204 // Will be generated at use site.
5205}
5206
Mark Mendell0616ae02015-04-17 12:49:27 -04005207void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5208 HX86ComputeBaseMethodAddress* insn) {
5209 LocationSummary* locations =
5210 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5211 locations->SetOut(Location::RequiresRegister());
5212}
5213
5214void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5215 HX86ComputeBaseMethodAddress* insn) {
5216 LocationSummary* locations = insn->GetLocations();
5217 Register reg = locations->Out().AsRegister<Register>();
5218
5219 // Generate call to next instruction.
5220 Label next_instruction;
5221 __ call(&next_instruction);
5222 __ Bind(&next_instruction);
5223
5224 // Remember this offset for later use with constant area.
5225 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5226
5227 // Grab the return address off the stack.
5228 __ popl(reg);
5229}
5230
5231void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5232 HX86LoadFromConstantTable* insn) {
5233 LocationSummary* locations =
5234 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5235
5236 locations->SetInAt(0, Location::RequiresRegister());
5237 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5238
5239 // If we don't need to be materialized, we only need the inputs to be set.
5240 if (!insn->NeedsMaterialization()) {
5241 return;
5242 }
5243
5244 switch (insn->GetType()) {
5245 case Primitive::kPrimFloat:
5246 case Primitive::kPrimDouble:
5247 locations->SetOut(Location::RequiresFpuRegister());
5248 break;
5249
5250 case Primitive::kPrimInt:
5251 locations->SetOut(Location::RequiresRegister());
5252 break;
5253
5254 default:
5255 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5256 }
5257}
5258
5259void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5260 if (!insn->NeedsMaterialization()) {
5261 return;
5262 }
5263
5264 LocationSummary* locations = insn->GetLocations();
5265 Location out = locations->Out();
5266 Register const_area = locations->InAt(0).AsRegister<Register>();
5267 HConstant *value = insn->GetConstant();
5268
5269 switch (insn->GetType()) {
5270 case Primitive::kPrimFloat:
5271 __ movss(out.AsFpuRegister<XmmRegister>(),
5272 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5273 break;
5274
5275 case Primitive::kPrimDouble:
5276 __ movsd(out.AsFpuRegister<XmmRegister>(),
5277 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5278 break;
5279
5280 case Primitive::kPrimInt:
5281 __ movl(out.AsRegister<Register>(),
5282 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5283 break;
5284
5285 default:
5286 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5287 }
5288}
5289
5290void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5291 // Generate the constant area if needed.
5292 X86Assembler* assembler = GetAssembler();
5293 if (!assembler->IsConstantAreaEmpty()) {
5294 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5295 // byte values.
5296 assembler->Align(4, 0);
5297 constant_area_start_ = assembler->CodeSize();
5298 assembler->AddConstantArea();
5299 }
5300
5301 // And finish up.
5302 CodeGenerator::Finalize(allocator);
5303}
5304
5305/**
5306 * Class to handle late fixup of offsets into constant area.
5307 */
5308class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5309 public:
5310 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5311 : codegen_(codegen), offset_into_constant_area_(offset) {}
5312
5313 private:
5314 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5315 // Patch the correct offset for the instruction. The place to patch is the
5316 // last 4 bytes of the instruction.
5317 // The value to patch is the distance from the offset in the constant area
5318 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5319 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5320 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5321
5322 // Patch in the right value.
5323 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5324 }
5325
5326 const CodeGeneratorX86& codegen_;
5327
5328 // Location in constant area that the fixup refers to.
5329 int offset_into_constant_area_;
5330};
5331
5332Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5333 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5334 return Address(reg, kDummy32BitOffset, fixup);
5335}
5336
5337Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5338 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5339 return Address(reg, kDummy32BitOffset, fixup);
5340}
5341
5342Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5343 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5344 return Address(reg, kDummy32BitOffset, fixup);
5345}
5346
5347Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5348 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5349 return Address(reg, kDummy32BitOffset, fixup);
5350}
5351
5352/**
5353 * Finds instructions that need the constant area base as an input.
5354 */
5355class ConstantHandlerVisitor : public HGraphVisitor {
5356 public:
5357 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5358
5359 private:
5360 void VisitAdd(HAdd* add) OVERRIDE {
5361 BinaryFP(add);
5362 }
5363
5364 void VisitSub(HSub* sub) OVERRIDE {
5365 BinaryFP(sub);
5366 }
5367
5368 void VisitMul(HMul* mul) OVERRIDE {
5369 BinaryFP(mul);
5370 }
5371
5372 void VisitDiv(HDiv* div) OVERRIDE {
5373 BinaryFP(div);
5374 }
5375
5376 void VisitReturn(HReturn* ret) OVERRIDE {
5377 HConstant* value = ret->InputAt(0)->AsConstant();
5378 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5379 ReplaceInput(ret, value, 0, true);
5380 }
5381 }
5382
5383 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5384 HandleInvoke(invoke);
5385 }
5386
5387 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5388 HandleInvoke(invoke);
5389 }
5390
5391 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5392 HandleInvoke(invoke);
5393 }
5394
5395 void BinaryFP(HBinaryOperation* bin) {
5396 HConstant* rhs = bin->InputAt(1)->AsConstant();
5397 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5398 ReplaceInput(bin, rhs, 1, false);
5399 }
5400 }
5401
5402 void InitializeConstantAreaPointer(HInstruction* user) {
5403 // Ensure we only initialize the pointer once.
5404 if (base_ != nullptr) {
5405 return;
5406 }
5407
5408 HGraph* graph = GetGraph();
5409 HBasicBlock* entry = graph->GetEntryBlock();
5410 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5411 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5412 entry->InsertInstructionBefore(base_, insert_pos);
5413 DCHECK(base_ != nullptr);
5414 }
5415
5416 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5417 InitializeConstantAreaPointer(insn);
5418 HGraph* graph = GetGraph();
5419 HBasicBlock* block = insn->GetBlock();
5420 HX86LoadFromConstantTable* load_constant =
5421 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5422 block->InsertInstructionBefore(load_constant, insn);
5423 insn->ReplaceInput(load_constant, input_index);
5424 }
5425
5426 void HandleInvoke(HInvoke* invoke) {
5427 // Ensure that we can load FP arguments from the constant area.
5428 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5429 HConstant* input = invoke->InputAt(i)->AsConstant();
5430 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5431 ReplaceInput(invoke, input, i, true);
5432 }
5433 }
5434 }
5435
5436 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5437 // input to the HX86LoadFromConstantTable instructions.
5438 HX86ComputeBaseMethodAddress* base_;
5439};
5440
5441void ConstantAreaFixups::Run() {
5442 ConstantHandlerVisitor visitor(graph_);
5443 visitor.VisitInsertionOrder();
5444}
5445
Roland Levillain4d027112015-07-01 15:41:14 +01005446#undef __
5447
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005448} // namespace x86
5449} // namespace art