blob: dc5c86efc63bab5cd41fee1fdf4343908da6589c [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,
434 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500435 : CodeGenerator(graph,
436 kNumberOfCpuRegisters,
437 kNumberOfXmmRegisters,
438 kNumberOfRegisterPairs,
439 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
440 arraysize(kCoreCalleeSaves))
441 | (1 << kFakeReturnRegister),
442 0,
443 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100444 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100445 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100446 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400447 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000448 isa_features_(isa_features),
449 method_patches_(graph->GetArena()->Adapter()),
450 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000451 // Use a fake return address register to mimic Quick.
452 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100453}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100455Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100456 switch (type) {
457 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459 X86ManagedRegister pair =
460 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100461 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
462 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
464 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100465 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100467 }
468
469 case Primitive::kPrimByte:
470 case Primitive::kPrimBoolean:
471 case Primitive::kPrimChar:
472 case Primitive::kPrimShort:
473 case Primitive::kPrimInt:
474 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100475 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100476 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
479 X86ManagedRegister current =
480 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
481 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100482 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 }
484 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100485 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 }
487
488 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100489 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100490 return Location::FpuRegisterLocation(
491 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100493
494 case Primitive::kPrimVoid:
495 LOG(FATAL) << "Unreachable type " << type;
496 }
497
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100498 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499}
500
Mark Mendell5f874182015-03-04 15:42:45 -0500501void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100502 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100503 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504
505 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100506 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100507
Mark Mendell5f874182015-03-04 15:42:45 -0500508 if (is_baseline) {
509 blocked_core_registers_[EBP] = true;
510 blocked_core_registers_[ESI] = true;
511 blocked_core_registers_[EDI] = true;
512 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100513
514 UpdateBlockedPairRegisters();
515}
516
517void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
518 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
519 X86ManagedRegister current =
520 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
521 if (blocked_core_registers_[current.AsRegisterPairLow()]
522 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
523 blocked_register_pairs_[i] = true;
524 }
525 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100526}
527
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100528InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
529 : HGraphVisitor(graph),
530 assembler_(codegen->GetAssembler()),
531 codegen_(codegen) {}
532
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100533static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100534 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100535}
536
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000537void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100538 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000539 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000540 bool skip_overflow_check =
541 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000542 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000543
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000544 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100545 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100546 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100547 }
548
Mark Mendell5f874182015-03-04 15:42:45 -0500549 if (HasEmptyFrame()) {
550 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000551 }
Mark Mendell5f874182015-03-04 15:42:45 -0500552
553 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
554 Register reg = kCoreCalleeSaves[i];
555 if (allocated_registers_.ContainsCoreRegister(reg)) {
556 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100557 __ cfi().AdjustCFAOffset(kX86WordSize);
558 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500559 }
560 }
561
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100562 int adjust = GetFrameSize() - FrameEntrySpillSize();
563 __ subl(ESP, Immediate(adjust));
564 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100565 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566}
567
568void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100569 __ cfi().RememberState();
570 if (!HasEmptyFrame()) {
571 int adjust = GetFrameSize() - FrameEntrySpillSize();
572 __ addl(ESP, Immediate(adjust));
573 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500574
David Srbeckyc34dc932015-04-12 09:27:43 +0100575 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
576 Register reg = kCoreCalleeSaves[i];
577 if (allocated_registers_.ContainsCoreRegister(reg)) {
578 __ popl(reg);
579 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
580 __ cfi().Restore(DWARFReg(reg));
581 }
Mark Mendell5f874182015-03-04 15:42:45 -0500582 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000583 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100584 __ ret();
585 __ cfi().RestoreState();
586 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000587}
588
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100589void CodeGeneratorX86::Bind(HBasicBlock* block) {
590 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000591}
592
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
594 switch (load->GetType()) {
595 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100597 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598
599 case Primitive::kPrimInt:
600 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100602 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603
604 case Primitive::kPrimBoolean:
605 case Primitive::kPrimByte:
606 case Primitive::kPrimChar:
607 case Primitive::kPrimShort:
608 case Primitive::kPrimVoid:
609 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700610 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100611 }
612
613 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700614 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100615}
616
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100617Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
618 switch (type) {
619 case Primitive::kPrimBoolean:
620 case Primitive::kPrimByte:
621 case Primitive::kPrimChar:
622 case Primitive::kPrimShort:
623 case Primitive::kPrimInt:
624 case Primitive::kPrimNot:
625 return Location::RegisterLocation(EAX);
626
627 case Primitive::kPrimLong:
628 return Location::RegisterPairLocation(EAX, EDX);
629
630 case Primitive::kPrimVoid:
631 return Location::NoLocation();
632
633 case Primitive::kPrimDouble:
634 case Primitive::kPrimFloat:
635 return Location::FpuRegisterLocation(XMM0);
636 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100637
638 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100639}
640
641Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
642 return Location::RegisterLocation(kMethodRegisterArgument);
643}
644
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100645Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100646 switch (type) {
647 case Primitive::kPrimBoolean:
648 case Primitive::kPrimByte:
649 case Primitive::kPrimChar:
650 case Primitive::kPrimShort:
651 case Primitive::kPrimInt:
652 case Primitive::kPrimNot: {
653 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000654 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100655 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100656 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100657 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000658 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100659 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100660 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100661
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000662 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100663 uint32_t index = gp_index_;
664 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000665 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100666 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100667 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
668 calling_convention.GetRegisterPairAt(index));
669 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100670 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000671 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
672 }
673 }
674
675 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100676 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000677 stack_index_++;
678 if (index < calling_convention.GetNumberOfFpuRegisters()) {
679 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
680 } else {
681 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
682 }
683 }
684
685 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100686 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000687 stack_index_ += 2;
688 if (index < calling_convention.GetNumberOfFpuRegisters()) {
689 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
690 } else {
691 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100692 }
693 }
694
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100695 case Primitive::kPrimVoid:
696 LOG(FATAL) << "Unexpected parameter type " << type;
697 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100698 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100699 return Location();
700}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100701
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702void CodeGeneratorX86::Move32(Location destination, Location source) {
703 if (source.Equals(destination)) {
704 return;
705 }
706 if (destination.IsRegister()) {
707 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000708 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000710 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 } else {
712 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000713 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 } else if (destination.IsFpuRegister()) {
716 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000719 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 } else {
721 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000725 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000727 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100728 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500730 } else if (source.IsConstant()) {
731 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000732 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500733 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 } else {
735 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100736 __ pushl(Address(ESP, source.GetStackIndex()));
737 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 }
739 }
740}
741
742void CodeGeneratorX86::Move64(Location destination, Location source) {
743 if (source.Equals(destination)) {
744 return;
745 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100746 if (destination.IsRegisterPair()) {
747 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000748 EmitParallelMoves(
749 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
750 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100751 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000752 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100753 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
754 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 } else if (source.IsFpuRegister()) {
756 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000758 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100760 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
761 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
763 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500765 if (source.IsFpuRegister()) {
766 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
767 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000768 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 } else {
770 LOG(FATAL) << "Unimplemented";
771 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100772 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000773 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100774 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000775 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100777 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100778 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100779 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000781 } else if (source.IsConstant()) {
782 HConstant* constant = source.GetConstant();
783 int64_t value;
784 if (constant->IsLongConstant()) {
785 value = constant->AsLongConstant()->GetValue();
786 } else {
787 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000788 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000789 }
790 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
791 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100792 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000793 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000794 EmitParallelMoves(
795 Location::StackSlot(source.GetStackIndex()),
796 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100797 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000798 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100799 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
800 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 }
802 }
803}
804
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100805void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000806 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100807 if (instruction->IsCurrentMethod()) {
808 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
809 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000810 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100811 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000812 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000813 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
814 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000815 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000817 } else if (location.IsStackSlot()) {
818 __ movl(Address(ESP, location.GetStackIndex()), imm);
819 } else {
820 DCHECK(location.IsConstant());
821 DCHECK_EQ(location.GetConstant(), const_to_move);
822 }
823 } else if (const_to_move->IsLongConstant()) {
824 int64_t value = const_to_move->AsLongConstant()->GetValue();
825 if (location.IsRegisterPair()) {
826 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
827 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
828 } else if (location.IsDoubleStackSlot()) {
829 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000830 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
831 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000832 } else {
833 DCHECK(location.IsConstant());
834 DCHECK_EQ(location.GetConstant(), instruction);
835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100836 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000837 } else if (instruction->IsTemporary()) {
838 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000839 if (temp_location.IsStackSlot()) {
840 Move32(location, temp_location);
841 } else {
842 DCHECK(temp_location.IsDoubleStackSlot());
843 Move64(location, temp_location);
844 }
Roland Levillain476df552014-10-09 17:51:36 +0100845 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100846 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100847 switch (instruction->GetType()) {
848 case Primitive::kPrimBoolean:
849 case Primitive::kPrimByte:
850 case Primitive::kPrimChar:
851 case Primitive::kPrimShort:
852 case Primitive::kPrimInt:
853 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 case Primitive::kPrimFloat:
855 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100856 break;
857
858 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100859 case Primitive::kPrimDouble:
860 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861 break;
862
863 default:
864 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
865 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100867 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 switch (instruction->GetType()) {
869 case Primitive::kPrimBoolean:
870 case Primitive::kPrimByte:
871 case Primitive::kPrimChar:
872 case Primitive::kPrimShort:
873 case Primitive::kPrimInt:
874 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000876 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877 break;
878
879 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100880 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000881 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 break;
883
884 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100885 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000887 }
888}
889
David Brazdilfc6a86a2015-06-26 10:33:45 +0000890void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 DCHECK(!successor->IsExitBlock());
892
893 HBasicBlock* block = got->GetBlock();
894 HInstruction* previous = got->GetPrevious();
895
896 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000897 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100898 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
899 return;
900 }
901
902 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
903 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
904 }
905 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000906 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000907 }
908}
909
David Brazdilfc6a86a2015-06-26 10:33:45 +0000910void LocationsBuilderX86::VisitGoto(HGoto* got) {
911 got->SetLocations(nullptr);
912}
913
914void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
915 HandleGoto(got, got->GetSuccessor());
916}
917
918void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
919 try_boundary->SetLocations(nullptr);
920}
921
922void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
923 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
924 if (!successor->IsExitBlock()) {
925 HandleGoto(try_boundary, successor);
926 }
927}
928
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000930 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000931}
932
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700934 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000935}
936
Mark Mendellc4701932015-04-10 13:18:51 -0400937void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
938 Label* true_label,
939 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 if (cond->IsFPConditionTrueIfNaN()) {
941 __ j(kUnordered, true_label);
942 } else if (cond->IsFPConditionFalseIfNaN()) {
943 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400944 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400946}
947
948void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
949 Label* true_label,
950 Label* false_label) {
951 LocationSummary* locations = cond->GetLocations();
952 Location left = locations->InAt(0);
953 Location right = locations->InAt(1);
954 IfCondition if_cond = cond->GetCondition();
955
Mark Mendellc4701932015-04-10 13:18:51 -0400956 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100957 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400958 IfCondition true_high_cond = if_cond;
959 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400961
962 // Set the conditions for the test, remembering that == needs to be
963 // decided using the low words.
964 switch (if_cond) {
965 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400966 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100967 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400968 break;
969 case kCondLT:
970 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400971 break;
972 case kCondLE:
973 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400974 break;
975 case kCondGT:
976 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400977 break;
978 case kCondGE:
979 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400980 break;
981 }
982
983 if (right.IsConstant()) {
984 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400985 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100986 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400987
988 if (val_high == 0) {
989 __ testl(left_high, left_high);
990 } else {
991 __ cmpl(left_high, Immediate(val_high));
992 }
993 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100994 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400995 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100996 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400997 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100998 __ j(X86SignedCondition(true_high_cond), true_label);
999 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001000 }
1001 // Must be equal high, so compare the lows.
1002 if (val_low == 0) {
1003 __ testl(left_low, left_low);
1004 } else {
1005 __ cmpl(left_low, Immediate(val_low));
1006 }
1007 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001008 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001009 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001010
1011 __ cmpl(left_high, right_high);
1012 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001014 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001015 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001016 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001017 __ j(X86SignedCondition(true_high_cond), true_label);
1018 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001019 }
1020 // Must be equal high, so compare the lows.
1021 __ cmpl(left_low, right_low);
1022 }
1023 // The last comparison might be unsigned.
1024 __ j(final_condition, true_label);
1025}
1026
1027void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1028 HCondition* condition,
1029 Label* true_target,
1030 Label* false_target,
1031 Label* always_true_target) {
1032 LocationSummary* locations = condition->GetLocations();
1033 Location left = locations->InAt(0);
1034 Location right = locations->InAt(1);
1035
1036 // We don't want true_target as a nullptr.
1037 if (true_target == nullptr) {
1038 true_target = always_true_target;
1039 }
1040 bool falls_through = (false_target == nullptr);
1041
1042 // FP compares don't like null false_targets.
1043 if (false_target == nullptr) {
1044 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1045 }
1046
1047 Primitive::Type type = condition->InputAt(0)->GetType();
1048 switch (type) {
1049 case Primitive::kPrimLong:
1050 GenerateLongComparesAndJumps(condition, true_target, false_target);
1051 break;
1052 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001053 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1054 GenerateFPJumps(condition, true_target, false_target);
1055 break;
1056 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001057 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1058 GenerateFPJumps(condition, true_target, false_target);
1059 break;
1060 default:
1061 LOG(FATAL) << "Unexpected compare type " << type;
1062 }
1063
1064 if (!falls_through) {
1065 __ jmp(false_target);
1066 }
1067}
1068
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001069void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1070 Label* true_target,
1071 Label* false_target,
1072 Label* always_true_target) {
1073 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001074 if (cond->IsIntConstant()) {
1075 // Constant condition, statically compared against 1.
1076 int32_t cond_value = cond->AsIntConstant()->GetValue();
1077 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001078 if (always_true_target != nullptr) {
1079 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001080 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001081 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001083 DCHECK_EQ(cond_value, 0);
1084 }
1085 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001086 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001087 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1088 // Moves do not affect the eflags register, so if the condition is
1089 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001090 // again. We can't use the eflags on long/FP conditions if they are
1091 // materialized due to the complex branching.
1092 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001093 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001094 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001095 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1096 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001097 if (!eflags_set) {
1098 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001099 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001101 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001102 } else {
1103 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1104 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001105 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001106 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001107 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001108 }
1109 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 // Condition has not been materialized, use its inputs as the
1111 // comparison and its condition as the branch condition.
1112
Mark Mendellc4701932015-04-10 13:18:51 -04001113 // Is this a long or FP comparison that has been folded into the HCondition?
1114 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1115 // Generate the comparison directly.
1116 GenerateCompareTestAndBranch(instruction->AsIf(),
1117 cond->AsCondition(),
1118 true_target,
1119 false_target,
1120 always_true_target);
1121 return;
1122 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001123
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001124 Location lhs = cond->GetLocations()->InAt(0);
1125 Location rhs = cond->GetLocations()->InAt(1);
1126 // LHS is guaranteed to be in a register (see
1127 // LocationsBuilderX86::VisitCondition).
1128 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001129 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001131 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001132 if (constant == 0) {
1133 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1134 } else {
1135 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1136 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001137 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001139 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001140 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001141 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001142 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001143 if (false_target != nullptr) {
1144 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001145 }
1146}
1147
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001148void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1149 LocationSummary* locations =
1150 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1151 HInstruction* cond = if_instr->InputAt(0);
1152 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1153 locations->SetInAt(0, Location::Any());
1154 }
1155}
1156
1157void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1158 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1159 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1160 Label* always_true_target = true_target;
1161 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1162 if_instr->IfTrueSuccessor())) {
1163 always_true_target = nullptr;
1164 }
1165 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1166 if_instr->IfFalseSuccessor())) {
1167 false_target = nullptr;
1168 }
1169 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1170}
1171
1172void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1173 LocationSummary* locations = new (GetGraph()->GetArena())
1174 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1175 HInstruction* cond = deoptimize->InputAt(0);
1176 DCHECK(cond->IsCondition());
1177 if (cond->AsCondition()->NeedsMaterialization()) {
1178 locations->SetInAt(0, Location::Any());
1179 }
1180}
1181
1182void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1183 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1184 DeoptimizationSlowPathX86(deoptimize);
1185 codegen_->AddSlowPath(slow_path);
1186 Label* slow_path_entry = slow_path->GetEntryLabel();
1187 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1188}
1189
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001190void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001191 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001192}
1193
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1195 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001196}
1197
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001199 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200}
1201
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001202void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001203 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001204 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205}
1206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 LocationSummary* locations =
1209 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 switch (store->InputAt(1)->GetType()) {
1211 case Primitive::kPrimBoolean:
1212 case Primitive::kPrimByte:
1213 case Primitive::kPrimChar:
1214 case Primitive::kPrimShort:
1215 case Primitive::kPrimInt:
1216 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1219 break;
1220
1221 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1224 break;
1225
1226 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001229}
1230
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001231void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001232 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001233}
1234
Roland Levillain0d37cd02015-05-27 16:39:19 +01001235void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001236 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001237 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001238 // Handle the long/FP comparisons made in instruction simplification.
1239 switch (cond->InputAt(0)->GetType()) {
1240 case Primitive::kPrimLong: {
1241 locations->SetInAt(0, Location::RequiresRegister());
1242 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1243 if (cond->NeedsMaterialization()) {
1244 locations->SetOut(Location::RequiresRegister());
1245 }
1246 break;
1247 }
1248 case Primitive::kPrimFloat:
1249 case Primitive::kPrimDouble: {
1250 locations->SetInAt(0, Location::RequiresFpuRegister());
1251 locations->SetInAt(1, Location::RequiresFpuRegister());
1252 if (cond->NeedsMaterialization()) {
1253 locations->SetOut(Location::RequiresRegister());
1254 }
1255 break;
1256 }
1257 default:
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 locations->SetInAt(1, Location::Any());
1260 if (cond->NeedsMaterialization()) {
1261 // We need a byte register.
1262 locations->SetOut(Location::RegisterLocation(ECX));
1263 }
1264 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001265 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001266}
1267
Roland Levillain0d37cd02015-05-27 16:39:19 +01001268void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001269 if (!cond->NeedsMaterialization()) {
1270 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001271 }
Mark Mendellc4701932015-04-10 13:18:51 -04001272
1273 LocationSummary* locations = cond->GetLocations();
1274 Location lhs = locations->InAt(0);
1275 Location rhs = locations->InAt(1);
1276 Register reg = locations->Out().AsRegister<Register>();
1277 Label true_label, false_label;
1278
1279 switch (cond->InputAt(0)->GetType()) {
1280 default: {
1281 // Integer case.
1282
1283 // Clear output register: setcc only sets the low byte.
1284 __ xorl(reg, reg);
1285
1286 if (rhs.IsRegister()) {
1287 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1288 } else if (rhs.IsConstant()) {
1289 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1290 if (constant == 0) {
1291 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1292 } else {
1293 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1294 }
1295 } else {
1296 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1297 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001298 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001299 return;
1300 }
1301 case Primitive::kPrimLong:
1302 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1303 break;
1304 case Primitive::kPrimFloat:
1305 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1306 GenerateFPJumps(cond, &true_label, &false_label);
1307 break;
1308 case Primitive::kPrimDouble:
1309 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1310 GenerateFPJumps(cond, &true_label, &false_label);
1311 break;
1312 }
1313
1314 // Convert the jumps into the result.
1315 Label done_label;
1316
Roland Levillain4fa13f62015-07-06 18:11:54 +01001317 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001318 __ Bind(&false_label);
1319 __ xorl(reg, reg);
1320 __ jmp(&done_label);
1321
Roland Levillain4fa13f62015-07-06 18:11:54 +01001322 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001323 __ Bind(&true_label);
1324 __ movl(reg, Immediate(1));
1325 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001326}
1327
1328void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1329 VisitCondition(comp);
1330}
1331
1332void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1333 VisitCondition(comp);
1334}
1335
1336void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1337 VisitCondition(comp);
1338}
1339
1340void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1341 VisitCondition(comp);
1342}
1343
1344void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1345 VisitCondition(comp);
1346}
1347
1348void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1349 VisitCondition(comp);
1350}
1351
1352void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1353 VisitCondition(comp);
1354}
1355
1356void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1357 VisitCondition(comp);
1358}
1359
1360void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1361 VisitCondition(comp);
1362}
1363
1364void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1365 VisitCondition(comp);
1366}
1367
1368void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1369 VisitCondition(comp);
1370}
1371
1372void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1373 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001374}
1375
1376void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001377 LocationSummary* locations =
1378 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001379 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380}
1381
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001382void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001383 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001384 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001385}
1386
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001387void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1388 LocationSummary* locations =
1389 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1390 locations->SetOut(Location::ConstantLocation(constant));
1391}
1392
1393void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1394 // Will be generated at use site.
1395 UNUSED(constant);
1396}
1397
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001398void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001399 LocationSummary* locations =
1400 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001401 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001402}
1403
1404void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1405 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001406 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001407}
1408
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001409void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1410 LocationSummary* locations =
1411 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1412 locations->SetOut(Location::ConstantLocation(constant));
1413}
1414
1415void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1416 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001417 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001418}
1419
1420void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1421 LocationSummary* locations =
1422 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1423 locations->SetOut(Location::ConstantLocation(constant));
1424}
1425
1426void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1427 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001428 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001429}
1430
Calin Juravle27df7582015-04-17 19:12:31 +01001431void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1432 memory_barrier->SetLocations(nullptr);
1433}
1434
1435void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1436 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1437}
1438
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001439void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001440 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001441}
1442
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001443void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001444 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001445 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001446}
1447
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001448void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001449 LocationSummary* locations =
1450 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001451 switch (ret->InputAt(0)->GetType()) {
1452 case Primitive::kPrimBoolean:
1453 case Primitive::kPrimByte:
1454 case Primitive::kPrimChar:
1455 case Primitive::kPrimShort:
1456 case Primitive::kPrimInt:
1457 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001458 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 break;
1460
1461 case Primitive::kPrimLong:
1462 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001463 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001464 break;
1465
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 case Primitive::kPrimFloat:
1467 case Primitive::kPrimDouble:
1468 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001469 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001470 break;
1471
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001472 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001475}
1476
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001477void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478 if (kIsDebugBuild) {
1479 switch (ret->InputAt(0)->GetType()) {
1480 case Primitive::kPrimBoolean:
1481 case Primitive::kPrimByte:
1482 case Primitive::kPrimChar:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
1485 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 break;
1488
1489 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001490 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1491 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492 break;
1493
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001494 case Primitive::kPrimFloat:
1495 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001496 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 break;
1498
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501 }
1502 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001503 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504}
1505
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001506void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001507 // When we do not run baseline, explicit clinit checks triggered by static
1508 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1509 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001510
Mark Mendellfb8d2792015-03-31 22:16:59 -04001511 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001512 if (intrinsic.TryDispatch(invoke)) {
1513 return;
1514 }
1515
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001516 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001517
1518 if (codegen_->IsBaseline()) {
1519 // Baseline does not have enough registers if the current method also
1520 // needs a register. We therefore do not require a register for it, and let
1521 // the code generation of the invoke handle it.
1522 LocationSummary* locations = invoke->GetLocations();
1523 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1524 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1525 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1526 }
1527 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001528}
1529
Mark Mendell09ed1a32015-03-25 08:30:06 -04001530static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1531 if (invoke->GetLocations()->Intrinsified()) {
1532 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1533 intrinsic.Dispatch(invoke);
1534 return true;
1535 }
1536 return false;
1537}
1538
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001539void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001540 // When we do not run baseline, explicit clinit checks triggered by static
1541 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1542 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001543
Mark Mendell09ed1a32015-03-25 08:30:06 -04001544 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1545 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001546 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001547
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001548 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001549 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001550 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001551 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001552}
1553
1554void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1555 HandleInvoke(invoke);
1556}
1557
1558void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001559 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001560 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001561}
1562
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001563void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001564 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1565 return;
1566 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001567
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001568 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001569 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001570 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001571}
1572
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001573void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1574 HandleInvoke(invoke);
1575 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001576 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001577}
1578
1579void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1580 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001582 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1583 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001584 LocationSummary* locations = invoke->GetLocations();
1585 Location receiver = locations->InAt(0);
1586 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1587
1588 // Set the hidden argument.
1589 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001590 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001591
1592 // temp = object->GetClass();
1593 if (receiver.IsStackSlot()) {
1594 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1595 __ movl(temp, Address(temp, class_offset));
1596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001598 }
Roland Levillain4d027112015-07-01 15:41:14 +01001599 codegen_->MaybeRecordImplicitNullCheck(invoke);
1600 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001601 // temp = temp->GetImtEntryAt(method_offset);
1602 __ movl(temp, Address(temp, method_offset));
1603 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001605 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606
1607 DCHECK(!codegen_->IsLeafMethod());
1608 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1609}
1610
Roland Levillain88cb1752014-10-20 16:36:47 +01001611void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1612 LocationSummary* locations =
1613 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1614 switch (neg->GetResultType()) {
1615 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001616 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetOut(Location::SameAsFirstInput());
1619 break;
1620
Roland Levillain88cb1752014-10-20 16:36:47 +01001621 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001622 locations->SetInAt(0, Location::RequiresFpuRegister());
1623 locations->SetOut(Location::SameAsFirstInput());
1624 locations->AddTemp(Location::RequiresRegister());
1625 locations->AddTemp(Location::RequiresFpuRegister());
1626 break;
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001629 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001630 locations->SetOut(Location::SameAsFirstInput());
1631 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001632 break;
1633
1634 default:
1635 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1636 }
1637}
1638
1639void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1640 LocationSummary* locations = neg->GetLocations();
1641 Location out = locations->Out();
1642 Location in = locations->InAt(0);
1643 switch (neg->GetResultType()) {
1644 case Primitive::kPrimInt:
1645 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001646 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001647 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001648 break;
1649
1650 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001651 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001652 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001653 __ negl(out.AsRegisterPairLow<Register>());
1654 // Negation is similar to subtraction from zero. The least
1655 // significant byte triggers a borrow when it is different from
1656 // zero; to take it into account, add 1 to the most significant
1657 // byte if the carry flag (CF) is set to 1 after the first NEGL
1658 // operation.
1659 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1660 __ negl(out.AsRegisterPairHigh<Register>());
1661 break;
1662
Roland Levillain5368c212014-11-27 15:03:41 +00001663 case Primitive::kPrimFloat: {
1664 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 Register constant = locations->GetTemp(0).AsRegister<Register>();
1666 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001667 // Implement float negation with an exclusive or with value
1668 // 0x80000000 (mask for bit 31, representing the sign of a
1669 // single-precision floating-point number).
1670 __ movl(constant, Immediate(INT32_C(0x80000000)));
1671 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001673 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001674 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001675
Roland Levillain5368c212014-11-27 15:03:41 +00001676 case Primitive::kPrimDouble: {
1677 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001678 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001679 // Implement double negation with an exclusive or with value
1680 // 0x8000000000000000 (mask for bit 63, representing the sign of
1681 // a double-precision floating-point number).
1682 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001684 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001685 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001686
1687 default:
1688 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1689 }
1690}
1691
Roland Levillaindff1f282014-11-05 14:15:05 +00001692void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001693 Primitive::Type result_type = conversion->GetResultType();
1694 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001695 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001696
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001697 // The float-to-long and double-to-long type conversions rely on a
1698 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001699 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001700 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1701 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001702 ? LocationSummary::kCall
1703 : LocationSummary::kNoCall;
1704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1706
David Brazdilb2bd1c52015-03-25 11:17:37 +00001707 // The Java language does not allow treating boolean as an integral type but
1708 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001709
Roland Levillaindff1f282014-11-05 14:15:05 +00001710 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001711 case Primitive::kPrimByte:
1712 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001713 case Primitive::kPrimBoolean:
1714 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001715 case Primitive::kPrimShort:
1716 case Primitive::kPrimInt:
1717 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001718 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001719 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1720 // Make the output overlap to please the register allocator. This greatly simplifies
1721 // the validation of the linear scan implementation
1722 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001723 break;
1724
1725 default:
1726 LOG(FATAL) << "Unexpected type conversion from " << input_type
1727 << " to " << result_type;
1728 }
1729 break;
1730
Roland Levillain01a8d712014-11-14 16:27:39 +00001731 case Primitive::kPrimShort:
1732 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001733 case Primitive::kPrimBoolean:
1734 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 case Primitive::kPrimByte:
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimChar:
1738 // Processing a Dex `int-to-short' instruction.
1739 locations->SetInAt(0, Location::Any());
1740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1741 break;
1742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillain946e1432014-11-11 17:35:19 +00001749 case Primitive::kPrimInt:
1750 switch (input_type) {
1751 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001752 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001753 locations->SetInAt(0, Location::Any());
1754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1755 break;
1756
1757 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001758 // Processing a Dex `float-to-int' instruction.
1759 locations->SetInAt(0, Location::RequiresFpuRegister());
1760 locations->SetOut(Location::RequiresRegister());
1761 locations->AddTemp(Location::RequiresFpuRegister());
1762 break;
1763
Roland Levillain946e1432014-11-11 17:35:19 +00001764 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001765 // Processing a Dex `double-to-int' instruction.
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetOut(Location::RequiresRegister());
1768 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimLong:
1778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001784 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001785 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 locations->SetInAt(0, Location::RegisterLocation(EAX));
1787 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1788 break;
1789
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001790 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001791 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001792 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001793 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001794 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1795 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1796
Vladimir Marko949c91f2015-01-27 10:48:44 +00001797 // The runtime helper puts the result in EAX, EDX.
1798 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001799 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001800 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001801
1802 default:
1803 LOG(FATAL) << "Unexpected type conversion from " << input_type
1804 << " to " << result_type;
1805 }
1806 break;
1807
Roland Levillain981e4542014-11-14 11:47:14 +00001808 case Primitive::kPrimChar:
1809 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001810 case Primitive::kPrimBoolean:
1811 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001812 case Primitive::kPrimByte:
1813 case Primitive::kPrimShort:
1814 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001815 // Processing a Dex `int-to-char' instruction.
1816 locations->SetInAt(0, Location::Any());
1817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1818 break;
1819
1820 default:
1821 LOG(FATAL) << "Unexpected type conversion from " << input_type
1822 << " to " << result_type;
1823 }
1824 break;
1825
Roland Levillaindff1f282014-11-05 14:15:05 +00001826 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001827 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001828 case Primitive::kPrimBoolean:
1829 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001830 case Primitive::kPrimByte:
1831 case Primitive::kPrimShort:
1832 case Primitive::kPrimInt:
1833 case Primitive::kPrimChar:
1834 // Processing a Dex `int-to-float' instruction.
1835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetOut(Location::RequiresFpuRegister());
1837 break;
1838
1839 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001840 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001841 locations->SetInAt(0, Location::Any());
1842 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001843 break;
1844
Roland Levillaincff13742014-11-17 14:32:17 +00001845 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001846 // Processing a Dex `double-to-float' instruction.
1847 locations->SetInAt(0, Location::RequiresFpuRegister());
1848 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001849 break;
1850
1851 default:
1852 LOG(FATAL) << "Unexpected type conversion from " << input_type
1853 << " to " << result_type;
1854 };
1855 break;
1856
Roland Levillaindff1f282014-11-05 14:15:05 +00001857 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001859 case Primitive::kPrimBoolean:
1860 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001861 case Primitive::kPrimByte:
1862 case Primitive::kPrimShort:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
1865 // Processing a Dex `int-to-double' instruction.
1866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetOut(Location::RequiresFpuRegister());
1868 break;
1869
1870 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001871 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001872 locations->SetInAt(0, Location::Any());
1873 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001874 break;
1875
Roland Levillaincff13742014-11-17 14:32:17 +00001876 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001877 // Processing a Dex `float-to-double' instruction.
1878 locations->SetInAt(0, Location::RequiresFpuRegister());
1879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001880 break;
1881
1882 default:
1883 LOG(FATAL) << "Unexpected type conversion from " << input_type
1884 << " to " << result_type;
1885 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001886 break;
1887
1888 default:
1889 LOG(FATAL) << "Unexpected type conversion from " << input_type
1890 << " to " << result_type;
1891 }
1892}
1893
1894void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1895 LocationSummary* locations = conversion->GetLocations();
1896 Location out = locations->Out();
1897 Location in = locations->InAt(0);
1898 Primitive::Type result_type = conversion->GetResultType();
1899 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001900 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001902 case Primitive::kPrimByte:
1903 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001904 case Primitive::kPrimBoolean:
1905 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001906 case Primitive::kPrimShort:
1907 case Primitive::kPrimInt:
1908 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001909 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001910 if (in.IsRegister()) {
1911 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001912 } else {
1913 DCHECK(in.GetConstant()->IsIntConstant());
1914 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1915 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1916 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001917 break;
1918
1919 default:
1920 LOG(FATAL) << "Unexpected type conversion from " << input_type
1921 << " to " << result_type;
1922 }
1923 break;
1924
Roland Levillain01a8d712014-11-14 16:27:39 +00001925 case Primitive::kPrimShort:
1926 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001927 case Primitive::kPrimBoolean:
1928 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001929 case Primitive::kPrimByte:
1930 case Primitive::kPrimInt:
1931 case Primitive::kPrimChar:
1932 // Processing a Dex `int-to-short' instruction.
1933 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001935 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001937 } else {
1938 DCHECK(in.GetConstant()->IsIntConstant());
1939 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001941 }
1942 break;
1943
1944 default:
1945 LOG(FATAL) << "Unexpected type conversion from " << input_type
1946 << " to " << result_type;
1947 }
1948 break;
1949
Roland Levillain946e1432014-11-11 17:35:19 +00001950 case Primitive::kPrimInt:
1951 switch (input_type) {
1952 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001953 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001954 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001956 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001958 } else {
1959 DCHECK(in.IsConstant());
1960 DCHECK(in.GetConstant()->IsLongConstant());
1961 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001963 }
1964 break;
1965
Roland Levillain3f8f9362014-12-02 17:45:01 +00001966 case Primitive::kPrimFloat: {
1967 // Processing a Dex `float-to-int' instruction.
1968 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1969 Register output = out.AsRegister<Register>();
1970 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1971 Label done, nan;
1972
1973 __ movl(output, Immediate(kPrimIntMax));
1974 // temp = int-to-float(output)
1975 __ cvtsi2ss(temp, output);
1976 // if input >= temp goto done
1977 __ comiss(input, temp);
1978 __ j(kAboveEqual, &done);
1979 // if input == NaN goto nan
1980 __ j(kUnordered, &nan);
1981 // output = float-to-int-truncate(input)
1982 __ cvttss2si(output, input);
1983 __ jmp(&done);
1984 __ Bind(&nan);
1985 // output = 0
1986 __ xorl(output, output);
1987 __ Bind(&done);
1988 break;
1989 }
1990
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001991 case Primitive::kPrimDouble: {
1992 // Processing a Dex `double-to-int' instruction.
1993 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1994 Register output = out.AsRegister<Register>();
1995 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1996 Label done, nan;
1997
1998 __ movl(output, Immediate(kPrimIntMax));
1999 // temp = int-to-double(output)
2000 __ cvtsi2sd(temp, output);
2001 // if input >= temp goto done
2002 __ comisd(input, temp);
2003 __ j(kAboveEqual, &done);
2004 // if input == NaN goto nan
2005 __ j(kUnordered, &nan);
2006 // output = double-to-int-truncate(input)
2007 __ cvttsd2si(output, input);
2008 __ jmp(&done);
2009 __ Bind(&nan);
2010 // output = 0
2011 __ xorl(output, output);
2012 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002013 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002014 }
Roland Levillain946e1432014-11-11 17:35:19 +00002015
2016 default:
2017 LOG(FATAL) << "Unexpected type conversion from " << input_type
2018 << " to " << result_type;
2019 }
2020 break;
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 case Primitive::kPrimLong:
2023 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002024 case Primitive::kPrimBoolean:
2025 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002026 case Primitive::kPrimByte:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002029 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002030 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002031 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2032 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002033 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002034 __ cdq();
2035 break;
2036
2037 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002038 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002039 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2040 conversion,
2041 conversion->GetDexPc(),
2042 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002043 break;
2044
Roland Levillaindff1f282014-11-05 14:15:05 +00002045 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002046 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002047 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2048 conversion,
2049 conversion->GetDexPc(),
2050 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002051 break;
2052
2053 default:
2054 LOG(FATAL) << "Unexpected type conversion from " << input_type
2055 << " to " << result_type;
2056 }
2057 break;
2058
Roland Levillain981e4542014-11-14 11:47:14 +00002059 case Primitive::kPrimChar:
2060 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002061 case Primitive::kPrimBoolean:
2062 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002063 case Primitive::kPrimByte:
2064 case Primitive::kPrimShort:
2065 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002066 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2067 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002069 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002070 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002071 } else {
2072 DCHECK(in.GetConstant()->IsIntConstant());
2073 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002075 }
2076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 }
2082 break;
2083
Roland Levillaindff1f282014-11-05 14:15:05 +00002084 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002085 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002086 case Primitive::kPrimBoolean:
2087 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002088 case Primitive::kPrimByte:
2089 case Primitive::kPrimShort:
2090 case Primitive::kPrimInt:
2091 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002092 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002094 break;
2095
Roland Levillain6d0e4832014-11-27 18:31:21 +00002096 case Primitive::kPrimLong: {
2097 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002098 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002099
Roland Levillain232ade02015-04-20 15:14:36 +01002100 // Create stack space for the call to
2101 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2102 // TODO: enhance register allocator to ask for stack temporaries.
2103 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2104 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2105 __ subl(ESP, Immediate(adjustment));
2106 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002107
Roland Levillain232ade02015-04-20 15:14:36 +01002108 // Load the value to the FP stack, using temporaries if needed.
2109 PushOntoFPStack(in, 0, adjustment, false, true);
2110
2111 if (out.IsStackSlot()) {
2112 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2113 } else {
2114 __ fstps(Address(ESP, 0));
2115 Location stack_temp = Location::StackSlot(0);
2116 codegen_->Move32(out, stack_temp);
2117 }
2118
2119 // Remove the temporary stack space we allocated.
2120 if (adjustment != 0) {
2121 __ addl(ESP, Immediate(adjustment));
2122 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002123 break;
2124 }
2125
Roland Levillaincff13742014-11-17 14:32:17 +00002126 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002127 // Processing a Dex `double-to-float' instruction.
2128 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002129 break;
2130
2131 default:
2132 LOG(FATAL) << "Unexpected type conversion from " << input_type
2133 << " to " << result_type;
2134 };
2135 break;
2136
Roland Levillaindff1f282014-11-05 14:15:05 +00002137 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002138 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002139 case Primitive::kPrimBoolean:
2140 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002141 case Primitive::kPrimByte:
2142 case Primitive::kPrimShort:
2143 case Primitive::kPrimInt:
2144 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002145 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002147 break;
2148
Roland Levillain647b9ed2014-11-27 12:06:00 +00002149 case Primitive::kPrimLong: {
2150 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002151 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002152
Roland Levillain232ade02015-04-20 15:14:36 +01002153 // Create stack space for the call to
2154 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2155 // TODO: enhance register allocator to ask for stack temporaries.
2156 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2157 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2158 __ subl(ESP, Immediate(adjustment));
2159 }
2160
2161 // Load the value to the FP stack, using temporaries if needed.
2162 PushOntoFPStack(in, 0, adjustment, false, true);
2163
2164 if (out.IsDoubleStackSlot()) {
2165 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2166 } else {
2167 __ fstpl(Address(ESP, 0));
2168 Location stack_temp = Location::DoubleStackSlot(0);
2169 codegen_->Move64(out, stack_temp);
2170 }
2171
2172 // Remove the temporary stack space we allocated.
2173 if (adjustment != 0) {
2174 __ addl(ESP, Immediate(adjustment));
2175 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002176 break;
2177 }
2178
Roland Levillaincff13742014-11-17 14:32:17 +00002179 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002180 // Processing a Dex `float-to-double' instruction.
2181 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002182 break;
2183
2184 default:
2185 LOG(FATAL) << "Unexpected type conversion from " << input_type
2186 << " to " << result_type;
2187 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 break;
2189
2190 default:
2191 LOG(FATAL) << "Unexpected type conversion from " << input_type
2192 << " to " << result_type;
2193 }
2194}
2195
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002196void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002197 LocationSummary* locations =
2198 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002199 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002200 case Primitive::kPrimInt: {
2201 locations->SetInAt(0, Location::RequiresRegister());
2202 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2204 break;
2205 }
2206
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetInAt(1, Location::Any());
2210 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002211 break;
2212 }
2213
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble: {
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002217 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002218 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002219 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002220 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002221
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002222 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002223 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2224 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002225 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002226}
2227
2228void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2229 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002230 Location first = locations->InAt(0);
2231 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002232 Location out = locations->Out();
2233
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002234 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002235 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002236 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002237 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2238 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002239 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2240 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002241 } else {
2242 __ leal(out.AsRegister<Register>(), Address(
2243 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2244 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002246 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2247 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2248 __ addl(out.AsRegister<Register>(), Immediate(value));
2249 } else {
2250 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2251 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002252 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002253 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002254 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002255 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002256 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002257 }
2258
2259 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002260 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002261 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2262 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002263 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2265 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002266 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002267 } else {
2268 DCHECK(second.IsConstant()) << second;
2269 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2270 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2271 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002273 break;
2274 }
2275
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002276 case Primitive::kPrimFloat: {
2277 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002278 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002279 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2280 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2281 DCHECK(!const_area->NeedsMaterialization());
2282 __ addss(first.AsFpuRegister<XmmRegister>(),
2283 codegen_->LiteralFloatAddress(
2284 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2285 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2286 } else {
2287 DCHECK(second.IsStackSlot());
2288 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002291 }
2292
2293 case Primitive::kPrimDouble: {
2294 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002296 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2297 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2298 DCHECK(!const_area->NeedsMaterialization());
2299 __ addsd(first.AsFpuRegister<XmmRegister>(),
2300 codegen_->LiteralDoubleAddress(
2301 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2302 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2303 } else {
2304 DCHECK(second.IsDoubleStackSlot());
2305 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 }
2307 break;
2308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002309
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002312 }
2313}
2314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002316 LocationSummary* locations =
2317 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002318 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002319 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002321 locations->SetInAt(0, Location::RequiresRegister());
2322 locations->SetInAt(1, Location::Any());
2323 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324 break;
2325 }
Calin Juravle11351682014-10-23 15:38:15 +01002326 case Primitive::kPrimFloat:
2327 case Primitive::kPrimDouble: {
2328 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002329 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002330 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 break;
Calin Juravle11351682014-10-23 15:38:15 +01002332 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002334 default:
Calin Juravle11351682014-10-23 15:38:15 +01002335 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002336 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002337}
2338
2339void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2340 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 Location first = locations->InAt(0);
2342 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002343 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002344 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002345 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002349 __ subl(first.AsRegister<Register>(),
2350 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002351 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002352 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002353 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002354 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 }
2356
2357 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002358 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002359 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2360 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002361 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002362 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002363 __ sbbl(first.AsRegisterPairHigh<Register>(),
2364 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002365 } else {
2366 DCHECK(second.IsConstant()) << second;
2367 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2368 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2369 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002370 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371 break;
2372 }
2373
Calin Juravle11351682014-10-23 15:38:15 +01002374 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002375 if (second.IsFpuRegister()) {
2376 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2377 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2378 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2379 DCHECK(!const_area->NeedsMaterialization());
2380 __ subss(first.AsFpuRegister<XmmRegister>(),
2381 codegen_->LiteralFloatAddress(
2382 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2383 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2384 } else {
2385 DCHECK(second.IsStackSlot());
2386 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388 break;
Calin Juravle11351682014-10-23 15:38:15 +01002389 }
2390
2391 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002392 if (second.IsFpuRegister()) {
2393 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2394 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2395 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2396 DCHECK(!const_area->NeedsMaterialization());
2397 __ subsd(first.AsFpuRegister<XmmRegister>(),
2398 codegen_->LiteralDoubleAddress(
2399 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2400 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2401 } else {
2402 DCHECK(second.IsDoubleStackSlot());
2403 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2404 }
Calin Juravle11351682014-10-23 15:38:15 +01002405 break;
2406 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002408 default:
Calin Juravle11351682014-10-23 15:38:15 +01002409 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002410 }
2411}
2412
Calin Juravle34bacdf2014-10-07 20:23:36 +01002413void LocationsBuilderX86::VisitMul(HMul* mul) {
2414 LocationSummary* locations =
2415 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2416 switch (mul->GetResultType()) {
2417 case Primitive::kPrimInt:
2418 locations->SetInAt(0, Location::RequiresRegister());
2419 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002420 if (mul->InputAt(1)->IsIntConstant()) {
2421 // Can use 3 operand multiply.
2422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423 } else {
2424 locations->SetOut(Location::SameAsFirstInput());
2425 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002426 break;
2427 case Primitive::kPrimLong: {
2428 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002429 locations->SetInAt(1, Location::Any());
2430 locations->SetOut(Location::SameAsFirstInput());
2431 // Needed for imul on 32bits with 64bits output.
2432 locations->AddTemp(Location::RegisterLocation(EAX));
2433 locations->AddTemp(Location::RegisterLocation(EDX));
2434 break;
2435 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002436 case Primitive::kPrimFloat:
2437 case Primitive::kPrimDouble: {
2438 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002439 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002440 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002442 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443
2444 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002445 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002446 }
2447}
2448
2449void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2450 LocationSummary* locations = mul->GetLocations();
2451 Location first = locations->InAt(0);
2452 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002453 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454
2455 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002456 case Primitive::kPrimInt:
2457 // The constant may have ended up in a register, so test explicitly to avoid
2458 // problems where the output may not be the same as the first operand.
2459 if (mul->InputAt(1)->IsIntConstant()) {
2460 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2461 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2462 } else if (second.IsRegister()) {
2463 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002465 } else {
2466 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002467 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002469 }
2470 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471
2472 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002473 Register in1_hi = first.AsRegisterPairHigh<Register>();
2474 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 Register eax = locations->GetTemp(0).AsRegister<Register>();
2476 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002477
2478 DCHECK_EQ(EAX, eax);
2479 DCHECK_EQ(EDX, edx);
2480
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002481 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002482 // output: in1
2483 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2484 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2485 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002486 if (second.IsConstant()) {
2487 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002488
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002489 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2490 int32_t low_value = Low32Bits(value);
2491 int32_t high_value = High32Bits(value);
2492 Immediate low(low_value);
2493 Immediate high(high_value);
2494
2495 __ movl(eax, high);
2496 // eax <- in1.lo * in2.hi
2497 __ imull(eax, in1_lo);
2498 // in1.hi <- in1.hi * in2.lo
2499 __ imull(in1_hi, low);
2500 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2501 __ addl(in1_hi, eax);
2502 // move in2_lo to eax to prepare for double precision
2503 __ movl(eax, low);
2504 // edx:eax <- in1.lo * in2.lo
2505 __ mull(in1_lo);
2506 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2507 __ addl(in1_hi, edx);
2508 // in1.lo <- (in1.lo * in2.lo)[31:0];
2509 __ movl(in1_lo, eax);
2510 } else if (second.IsRegisterPair()) {
2511 Register in2_hi = second.AsRegisterPairHigh<Register>();
2512 Register in2_lo = second.AsRegisterPairLow<Register>();
2513
2514 __ movl(eax, in2_hi);
2515 // eax <- in1.lo * in2.hi
2516 __ imull(eax, in1_lo);
2517 // in1.hi <- in1.hi * in2.lo
2518 __ imull(in1_hi, in2_lo);
2519 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2520 __ addl(in1_hi, eax);
2521 // move in1_lo to eax to prepare for double precision
2522 __ movl(eax, in1_lo);
2523 // edx:eax <- in1.lo * in2.lo
2524 __ mull(in2_lo);
2525 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2526 __ addl(in1_hi, edx);
2527 // in1.lo <- (in1.lo * in2.lo)[31:0];
2528 __ movl(in1_lo, eax);
2529 } else {
2530 DCHECK(second.IsDoubleStackSlot()) << second;
2531 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2532 Address in2_lo(ESP, second.GetStackIndex());
2533
2534 __ movl(eax, in2_hi);
2535 // eax <- in1.lo * in2.hi
2536 __ imull(eax, in1_lo);
2537 // in1.hi <- in1.hi * in2.lo
2538 __ imull(in1_hi, in2_lo);
2539 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2540 __ addl(in1_hi, eax);
2541 // move in1_lo to eax to prepare for double precision
2542 __ movl(eax, in1_lo);
2543 // edx:eax <- in1.lo * in2.lo
2544 __ mull(in2_lo);
2545 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2546 __ addl(in1_hi, edx);
2547 // in1.lo <- (in1.lo * in2.lo)[31:0];
2548 __ movl(in1_lo, eax);
2549 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002550
2551 break;
2552 }
2553
Calin Juravleb5bfa962014-10-21 18:02:24 +01002554 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002555 DCHECK(first.Equals(locations->Out()));
2556 if (second.IsFpuRegister()) {
2557 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2558 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2559 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2560 DCHECK(!const_area->NeedsMaterialization());
2561 __ mulss(first.AsFpuRegister<XmmRegister>(),
2562 codegen_->LiteralFloatAddress(
2563 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2564 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2565 } else {
2566 DCHECK(second.IsStackSlot());
2567 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2568 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002569 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002570 }
2571
2572 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002573 DCHECK(first.Equals(locations->Out()));
2574 if (second.IsFpuRegister()) {
2575 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2576 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2577 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2578 DCHECK(!const_area->NeedsMaterialization());
2579 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2580 codegen_->LiteralDoubleAddress(
2581 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2582 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2583 } else {
2584 DCHECK(second.IsDoubleStackSlot());
2585 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2586 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002587 break;
2588 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002589
2590 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002591 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002592 }
2593}
2594
Roland Levillain232ade02015-04-20 15:14:36 +01002595void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2596 uint32_t temp_offset,
2597 uint32_t stack_adjustment,
2598 bool is_fp,
2599 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002600 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002601 DCHECK(!is_wide);
2602 if (is_fp) {
2603 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2604 } else {
2605 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2606 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002607 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002608 DCHECK(is_wide);
2609 if (is_fp) {
2610 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2611 } else {
2612 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2613 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002614 } else {
2615 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002616 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002617 Location stack_temp = Location::StackSlot(temp_offset);
2618 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002619 if (is_fp) {
2620 __ flds(Address(ESP, temp_offset));
2621 } else {
2622 __ filds(Address(ESP, temp_offset));
2623 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002624 } else {
2625 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2626 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002627 if (is_fp) {
2628 __ fldl(Address(ESP, temp_offset));
2629 } else {
2630 __ fildl(Address(ESP, temp_offset));
2631 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002632 }
2633 }
2634}
2635
2636void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2637 Primitive::Type type = rem->GetResultType();
2638 bool is_float = type == Primitive::kPrimFloat;
2639 size_t elem_size = Primitive::ComponentSize(type);
2640 LocationSummary* locations = rem->GetLocations();
2641 Location first = locations->InAt(0);
2642 Location second = locations->InAt(1);
2643 Location out = locations->Out();
2644
2645 // Create stack space for 2 elements.
2646 // TODO: enhance register allocator to ask for stack temporaries.
2647 __ subl(ESP, Immediate(2 * elem_size));
2648
2649 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002650 const bool is_wide = !is_float;
2651 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2652 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002653
2654 // Loop doing FPREM until we stabilize.
2655 Label retry;
2656 __ Bind(&retry);
2657 __ fprem();
2658
2659 // Move FP status to AX.
2660 __ fstsw();
2661
2662 // And see if the argument reduction is complete. This is signaled by the
2663 // C2 FPU flag bit set to 0.
2664 __ andl(EAX, Immediate(kC2ConditionMask));
2665 __ j(kNotEqual, &retry);
2666
2667 // We have settled on the final value. Retrieve it into an XMM register.
2668 // Store FP top of stack to real stack.
2669 if (is_float) {
2670 __ fsts(Address(ESP, 0));
2671 } else {
2672 __ fstl(Address(ESP, 0));
2673 }
2674
2675 // Pop the 2 items from the FP stack.
2676 __ fucompp();
2677
2678 // Load the value from the stack into an XMM register.
2679 DCHECK(out.IsFpuRegister()) << out;
2680 if (is_float) {
2681 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2682 } else {
2683 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2684 }
2685
2686 // And remove the temporary stack space we allocated.
2687 __ addl(ESP, Immediate(2 * elem_size));
2688}
2689
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002690
2691void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2692 DCHECK(instruction->IsDiv() || instruction->IsRem());
2693
2694 LocationSummary* locations = instruction->GetLocations();
2695 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002696 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002697
2698 Register out_register = locations->Out().AsRegister<Register>();
2699 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002700 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002701
2702 DCHECK(imm == 1 || imm == -1);
2703
2704 if (instruction->IsRem()) {
2705 __ xorl(out_register, out_register);
2706 } else {
2707 __ movl(out_register, input_register);
2708 if (imm == -1) {
2709 __ negl(out_register);
2710 }
2711 }
2712}
2713
2714
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002715void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002716 LocationSummary* locations = instruction->GetLocations();
2717
2718 Register out_register = locations->Out().AsRegister<Register>();
2719 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002720 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002721
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002722 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002723 Register num = locations->GetTemp(0).AsRegister<Register>();
2724
2725 __ leal(num, Address(input_register, std::abs(imm) - 1));
2726 __ testl(input_register, input_register);
2727 __ cmovl(kGreaterEqual, num, input_register);
2728 int shift = CTZ(imm);
2729 __ sarl(num, Immediate(shift));
2730
2731 if (imm < 0) {
2732 __ negl(num);
2733 }
2734
2735 __ movl(out_register, num);
2736}
2737
2738void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2739 DCHECK(instruction->IsDiv() || instruction->IsRem());
2740
2741 LocationSummary* locations = instruction->GetLocations();
2742 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2743
2744 Register eax = locations->InAt(0).AsRegister<Register>();
2745 Register out = locations->Out().AsRegister<Register>();
2746 Register num;
2747 Register edx;
2748
2749 if (instruction->IsDiv()) {
2750 edx = locations->GetTemp(0).AsRegister<Register>();
2751 num = locations->GetTemp(1).AsRegister<Register>();
2752 } else {
2753 edx = locations->Out().AsRegister<Register>();
2754 num = locations->GetTemp(0).AsRegister<Register>();
2755 }
2756
2757 DCHECK_EQ(EAX, eax);
2758 DCHECK_EQ(EDX, edx);
2759 if (instruction->IsDiv()) {
2760 DCHECK_EQ(EAX, out);
2761 } else {
2762 DCHECK_EQ(EDX, out);
2763 }
2764
2765 int64_t magic;
2766 int shift;
2767 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2768
2769 Label ndiv;
2770 Label end;
2771 // If numerator is 0, the result is 0, no computation needed.
2772 __ testl(eax, eax);
2773 __ j(kNotEqual, &ndiv);
2774
2775 __ xorl(out, out);
2776 __ jmp(&end);
2777
2778 __ Bind(&ndiv);
2779
2780 // Save the numerator.
2781 __ movl(num, eax);
2782
2783 // EAX = magic
2784 __ movl(eax, Immediate(magic));
2785
2786 // EDX:EAX = magic * numerator
2787 __ imull(num);
2788
2789 if (imm > 0 && magic < 0) {
2790 // EDX += num
2791 __ addl(edx, num);
2792 } else if (imm < 0 && magic > 0) {
2793 __ subl(edx, num);
2794 }
2795
2796 // Shift if needed.
2797 if (shift != 0) {
2798 __ sarl(edx, Immediate(shift));
2799 }
2800
2801 // EDX += 1 if EDX < 0
2802 __ movl(eax, edx);
2803 __ shrl(edx, Immediate(31));
2804 __ addl(edx, eax);
2805
2806 if (instruction->IsRem()) {
2807 __ movl(eax, num);
2808 __ imull(edx, Immediate(imm));
2809 __ subl(eax, edx);
2810 __ movl(edx, eax);
2811 } else {
2812 __ movl(eax, edx);
2813 }
2814 __ Bind(&end);
2815}
2816
Calin Juravlebacfec32014-11-14 15:54:36 +00002817void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2818 DCHECK(instruction->IsDiv() || instruction->IsRem());
2819
2820 LocationSummary* locations = instruction->GetLocations();
2821 Location out = locations->Out();
2822 Location first = locations->InAt(0);
2823 Location second = locations->InAt(1);
2824 bool is_div = instruction->IsDiv();
2825
2826 switch (instruction->GetResultType()) {
2827 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 DCHECK_EQ(EAX, first.AsRegister<Register>());
2829 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002830
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002831 if (instruction->InputAt(1)->IsIntConstant()) {
2832 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002833
2834 if (imm == 0) {
2835 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2836 } else if (imm == 1 || imm == -1) {
2837 DivRemOneOrMinusOne(instruction);
2838 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002839 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002840 } else {
2841 DCHECK(imm <= -2 || imm >= 2);
2842 GenerateDivRemWithAnyConstant(instruction);
2843 }
2844 } else {
2845 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002846 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002847 is_div);
2848 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002849
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002850 Register second_reg = second.AsRegister<Register>();
2851 // 0x80000000/-1 triggers an arithmetic exception!
2852 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2853 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002854
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002855 __ cmpl(second_reg, Immediate(-1));
2856 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002857
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002858 // edx:eax <- sign-extended of eax
2859 __ cdq();
2860 // eax = quotient, edx = remainder
2861 __ idivl(second_reg);
2862 __ Bind(slow_path->GetExitLabel());
2863 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002864 break;
2865 }
2866
2867 case Primitive::kPrimLong: {
2868 InvokeRuntimeCallingConvention calling_convention;
2869 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2870 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2871 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2872 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2873 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2874 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2875
2876 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002877 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2878 instruction,
2879 instruction->GetDexPc(),
2880 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002881 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002882 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2883 instruction,
2884 instruction->GetDexPc(),
2885 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002886 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002887 break;
2888 }
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2892 }
2893}
2894
Calin Juravle7c4954d2014-10-28 16:57:40 +00002895void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002896 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002897 ? LocationSummary::kCall
2898 : LocationSummary::kNoCall;
2899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2900
Calin Juravle7c4954d2014-10-28 16:57:40 +00002901 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002902 case Primitive::kPrimInt: {
2903 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002904 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002905 locations->SetOut(Location::SameAsFirstInput());
2906 // Intel uses edx:eax as the dividend.
2907 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002908 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2909 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2910 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002911 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002912 locations->AddTemp(Location::RequiresRegister());
2913 }
Calin Juravled0d48522014-11-04 16:40:20 +00002914 break;
2915 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002916 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002917 InvokeRuntimeCallingConvention calling_convention;
2918 locations->SetInAt(0, Location::RegisterPairLocation(
2919 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2920 locations->SetInAt(1, Location::RegisterPairLocation(
2921 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2922 // Runtime helper puts the result in EAX, EDX.
2923 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002924 break;
2925 }
2926 case Primitive::kPrimFloat:
2927 case Primitive::kPrimDouble: {
2928 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002929 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002930 locations->SetOut(Location::SameAsFirstInput());
2931 break;
2932 }
2933
2934 default:
2935 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2936 }
2937}
2938
2939void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2940 LocationSummary* locations = div->GetLocations();
2941 Location first = locations->InAt(0);
2942 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002943
2944 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002945 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002946 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002947 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002948 break;
2949 }
2950
2951 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002952 if (second.IsFpuRegister()) {
2953 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2954 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2955 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2956 DCHECK(!const_area->NeedsMaterialization());
2957 __ divss(first.AsFpuRegister<XmmRegister>(),
2958 codegen_->LiteralFloatAddress(
2959 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2960 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2961 } else {
2962 DCHECK(second.IsStackSlot());
2963 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2964 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002965 break;
2966 }
2967
2968 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002969 if (second.IsFpuRegister()) {
2970 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2971 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2972 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2973 DCHECK(!const_area->NeedsMaterialization());
2974 __ divsd(first.AsFpuRegister<XmmRegister>(),
2975 codegen_->LiteralDoubleAddress(
2976 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2977 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2978 } else {
2979 DCHECK(second.IsDoubleStackSlot());
2980 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2981 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002982 break;
2983 }
2984
2985 default:
2986 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2987 }
2988}
2989
Calin Juravlebacfec32014-11-14 15:54:36 +00002990void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002991 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002992
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002993 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2994 ? LocationSummary::kCall
2995 : LocationSummary::kNoCall;
2996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002997
Calin Juravled2ec87d2014-12-08 14:24:46 +00002998 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002999 case Primitive::kPrimInt: {
3000 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003001 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003002 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003003 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3004 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3005 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003006 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003007 locations->AddTemp(Location::RequiresRegister());
3008 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003009 break;
3010 }
3011 case Primitive::kPrimLong: {
3012 InvokeRuntimeCallingConvention calling_convention;
3013 locations->SetInAt(0, Location::RegisterPairLocation(
3014 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3015 locations->SetInAt(1, Location::RegisterPairLocation(
3016 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3017 // Runtime helper puts the result in EAX, EDX.
3018 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3019 break;
3020 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003021 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003022 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003023 locations->SetInAt(0, Location::Any());
3024 locations->SetInAt(1, Location::Any());
3025 locations->SetOut(Location::RequiresFpuRegister());
3026 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003027 break;
3028 }
3029
3030 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003031 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003032 }
3033}
3034
3035void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3036 Primitive::Type type = rem->GetResultType();
3037 switch (type) {
3038 case Primitive::kPrimInt:
3039 case Primitive::kPrimLong: {
3040 GenerateDivRemIntegral(rem);
3041 break;
3042 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003043 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003044 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003045 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003046 break;
3047 }
3048 default:
3049 LOG(FATAL) << "Unexpected rem type " << type;
3050 }
3051}
3052
Calin Juravled0d48522014-11-04 16:40:20 +00003053void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003054 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3055 ? LocationSummary::kCallOnSlowPath
3056 : LocationSummary::kNoCall;
3057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003058 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003059 case Primitive::kPrimByte:
3060 case Primitive::kPrimChar:
3061 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003062 case Primitive::kPrimInt: {
3063 locations->SetInAt(0, Location::Any());
3064 break;
3065 }
3066 case Primitive::kPrimLong: {
3067 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3068 if (!instruction->IsConstant()) {
3069 locations->AddTemp(Location::RequiresRegister());
3070 }
3071 break;
3072 }
3073 default:
3074 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3075 }
Calin Juravled0d48522014-11-04 16:40:20 +00003076 if (instruction->HasUses()) {
3077 locations->SetOut(Location::SameAsFirstInput());
3078 }
3079}
3080
3081void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3082 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3083 codegen_->AddSlowPath(slow_path);
3084
3085 LocationSummary* locations = instruction->GetLocations();
3086 Location value = locations->InAt(0);
3087
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003088 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003089 case Primitive::kPrimByte:
3090 case Primitive::kPrimChar:
3091 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003092 case Primitive::kPrimInt: {
3093 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003095 __ j(kEqual, slow_path->GetEntryLabel());
3096 } else if (value.IsStackSlot()) {
3097 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3098 __ j(kEqual, slow_path->GetEntryLabel());
3099 } else {
3100 DCHECK(value.IsConstant()) << value;
3101 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3102 __ jmp(slow_path->GetEntryLabel());
3103 }
3104 }
3105 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003106 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003107 case Primitive::kPrimLong: {
3108 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003109 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003110 __ movl(temp, value.AsRegisterPairLow<Register>());
3111 __ orl(temp, value.AsRegisterPairHigh<Register>());
3112 __ j(kEqual, slow_path->GetEntryLabel());
3113 } else {
3114 DCHECK(value.IsConstant()) << value;
3115 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3116 __ jmp(slow_path->GetEntryLabel());
3117 }
3118 }
3119 break;
3120 }
3121 default:
3122 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003123 }
Calin Juravled0d48522014-11-04 16:40:20 +00003124}
3125
Calin Juravle9aec02f2014-11-18 23:06:35 +00003126void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3127 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3128
3129 LocationSummary* locations =
3130 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3131
3132 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003133 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003134 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003135 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003136 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003137 // The shift count needs to be in CL or a constant.
3138 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003139 locations->SetOut(Location::SameAsFirstInput());
3140 break;
3141 }
3142 default:
3143 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3144 }
3145}
3146
3147void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3148 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3149
3150 LocationSummary* locations = op->GetLocations();
3151 Location first = locations->InAt(0);
3152 Location second = locations->InAt(1);
3153 DCHECK(first.Equals(locations->Out()));
3154
3155 switch (op->GetResultType()) {
3156 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003157 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003158 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003159 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003160 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003161 DCHECK_EQ(ECX, second_reg);
3162 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003163 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003164 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003165 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003166 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003167 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003168 }
3169 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003170 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3171 if (shift == 0) {
3172 return;
3173 }
3174 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003175 if (op->IsShl()) {
3176 __ shll(first_reg, imm);
3177 } else if (op->IsShr()) {
3178 __ sarl(first_reg, imm);
3179 } else {
3180 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003181 }
3182 }
3183 break;
3184 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003185 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003186 if (second.IsRegister()) {
3187 Register second_reg = second.AsRegister<Register>();
3188 DCHECK_EQ(ECX, second_reg);
3189 if (op->IsShl()) {
3190 GenerateShlLong(first, second_reg);
3191 } else if (op->IsShr()) {
3192 GenerateShrLong(first, second_reg);
3193 } else {
3194 GenerateUShrLong(first, second_reg);
3195 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003196 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003197 // Shift by a constant.
3198 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3199 // Nothing to do if the shift is 0, as the input is already the output.
3200 if (shift != 0) {
3201 if (op->IsShl()) {
3202 GenerateShlLong(first, shift);
3203 } else if (op->IsShr()) {
3204 GenerateShrLong(first, shift);
3205 } else {
3206 GenerateUShrLong(first, shift);
3207 }
3208 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003209 }
3210 break;
3211 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003212 default:
3213 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3214 }
3215}
3216
Mark P Mendell73945692015-04-29 14:56:17 +00003217void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3218 Register low = loc.AsRegisterPairLow<Register>();
3219 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003220 if (shift == 1) {
3221 // This is just an addition.
3222 __ addl(low, low);
3223 __ adcl(high, high);
3224 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003225 // Shift by 32 is easy. High gets low, and low gets 0.
3226 codegen_->EmitParallelMoves(
3227 loc.ToLow(),
3228 loc.ToHigh(),
3229 Primitive::kPrimInt,
3230 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3231 loc.ToLow(),
3232 Primitive::kPrimInt);
3233 } else if (shift > 32) {
3234 // Low part becomes 0. High part is low part << (shift-32).
3235 __ movl(high, low);
3236 __ shll(high, Immediate(shift - 32));
3237 __ xorl(low, low);
3238 } else {
3239 // Between 1 and 31.
3240 __ shld(high, low, Immediate(shift));
3241 __ shll(low, Immediate(shift));
3242 }
3243}
3244
Calin Juravle9aec02f2014-11-18 23:06:35 +00003245void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3246 Label done;
3247 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3248 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3249 __ testl(shifter, Immediate(32));
3250 __ j(kEqual, &done);
3251 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3252 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3253 __ Bind(&done);
3254}
3255
Mark P Mendell73945692015-04-29 14:56:17 +00003256void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3257 Register low = loc.AsRegisterPairLow<Register>();
3258 Register high = loc.AsRegisterPairHigh<Register>();
3259 if (shift == 32) {
3260 // Need to copy the sign.
3261 DCHECK_NE(low, high);
3262 __ movl(low, high);
3263 __ sarl(high, Immediate(31));
3264 } else if (shift > 32) {
3265 DCHECK_NE(low, high);
3266 // High part becomes sign. Low part is shifted by shift - 32.
3267 __ movl(low, high);
3268 __ sarl(high, Immediate(31));
3269 __ sarl(low, Immediate(shift - 32));
3270 } else {
3271 // Between 1 and 31.
3272 __ shrd(low, high, Immediate(shift));
3273 __ sarl(high, Immediate(shift));
3274 }
3275}
3276
Calin Juravle9aec02f2014-11-18 23:06:35 +00003277void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3278 Label done;
3279 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3280 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3281 __ testl(shifter, Immediate(32));
3282 __ j(kEqual, &done);
3283 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3284 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3285 __ Bind(&done);
3286}
3287
Mark P Mendell73945692015-04-29 14:56:17 +00003288void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3289 Register low = loc.AsRegisterPairLow<Register>();
3290 Register high = loc.AsRegisterPairHigh<Register>();
3291 if (shift == 32) {
3292 // Shift by 32 is easy. Low gets high, and high gets 0.
3293 codegen_->EmitParallelMoves(
3294 loc.ToHigh(),
3295 loc.ToLow(),
3296 Primitive::kPrimInt,
3297 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3298 loc.ToHigh(),
3299 Primitive::kPrimInt);
3300 } else if (shift > 32) {
3301 // Low part is high >> (shift - 32). High part becomes 0.
3302 __ movl(low, high);
3303 __ shrl(low, Immediate(shift - 32));
3304 __ xorl(high, high);
3305 } else {
3306 // Between 1 and 31.
3307 __ shrd(low, high, Immediate(shift));
3308 __ shrl(high, Immediate(shift));
3309 }
3310}
3311
Calin Juravle9aec02f2014-11-18 23:06:35 +00003312void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3313 Label done;
3314 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3315 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3316 __ testl(shifter, Immediate(32));
3317 __ j(kEqual, &done);
3318 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3319 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3320 __ Bind(&done);
3321}
3322
3323void LocationsBuilderX86::VisitShl(HShl* shl) {
3324 HandleShift(shl);
3325}
3326
3327void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3328 HandleShift(shl);
3329}
3330
3331void LocationsBuilderX86::VisitShr(HShr* shr) {
3332 HandleShift(shr);
3333}
3334
3335void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3336 HandleShift(shr);
3337}
3338
3339void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3340 HandleShift(ushr);
3341}
3342
3343void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3344 HandleShift(ushr);
3345}
3346
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003347void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003348 LocationSummary* locations =
3349 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003350 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003351 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003352 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003353 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003354}
3355
3356void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3357 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003358 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003359 // Note: if heap poisoning is enabled, the entry point takes cares
3360 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003361 codegen_->InvokeRuntime(
3362 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3363 instruction,
3364 instruction->GetDexPc(),
3365 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003366 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003367}
3368
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003369void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3372 locations->SetOut(Location::RegisterLocation(EAX));
3373 InvokeRuntimeCallingConvention calling_convention;
3374 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003375 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003376 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003377}
3378
3379void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3380 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003381 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3382
Roland Levillain4d027112015-07-01 15:41:14 +01003383 // Note: if heap poisoning is enabled, the entry point takes cares
3384 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003385 codegen_->InvokeRuntime(
3386 Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())),
3387 instruction,
3388 instruction->GetDexPc(),
3389 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003390 DCHECK(!codegen_->IsLeafMethod());
3391}
3392
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003393void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003394 LocationSummary* locations =
3395 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003396 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3397 if (location.IsStackSlot()) {
3398 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3399 } else if (location.IsDoubleStackSlot()) {
3400 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003401 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003402 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003403}
3404
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003405void InstructionCodeGeneratorX86::VisitParameterValue(
3406 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3407}
3408
3409void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3410 LocationSummary* locations =
3411 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3412 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3413}
3414
3415void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003416}
3417
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003418void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003419 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003420 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003421 locations->SetInAt(0, Location::RequiresRegister());
3422 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003423}
3424
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003425void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3426 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003427 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003428 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003429 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003430 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003431 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003432 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003433 break;
3434
3435 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003436 __ notl(out.AsRegisterPairLow<Register>());
3437 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003438 break;
3439
3440 default:
3441 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3442 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003443}
3444
David Brazdil66d126e2015-04-03 16:02:44 +01003445void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3446 LocationSummary* locations =
3447 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3448 locations->SetInAt(0, Location::RequiresRegister());
3449 locations->SetOut(Location::SameAsFirstInput());
3450}
3451
3452void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003453 LocationSummary* locations = bool_not->GetLocations();
3454 Location in = locations->InAt(0);
3455 Location out = locations->Out();
3456 DCHECK(in.Equals(out));
3457 __ xorl(out.AsRegister<Register>(), Immediate(1));
3458}
3459
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003460void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003461 LocationSummary* locations =
3462 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003463 switch (compare->InputAt(0)->GetType()) {
3464 case Primitive::kPrimLong: {
3465 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003466 locations->SetInAt(1, Location::Any());
3467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3468 break;
3469 }
3470 case Primitive::kPrimFloat:
3471 case Primitive::kPrimDouble: {
3472 locations->SetInAt(0, Location::RequiresFpuRegister());
3473 locations->SetInAt(1, Location::RequiresFpuRegister());
3474 locations->SetOut(Location::RequiresRegister());
3475 break;
3476 }
3477 default:
3478 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3479 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003480}
3481
3482void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003483 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003484 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003485 Location left = locations->InAt(0);
3486 Location right = locations->InAt(1);
3487
3488 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003489 switch (compare->InputAt(0)->GetType()) {
3490 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003491 Register left_low = left.AsRegisterPairLow<Register>();
3492 Register left_high = left.AsRegisterPairHigh<Register>();
3493 int32_t val_low = 0;
3494 int32_t val_high = 0;
3495 bool right_is_const = false;
3496
3497 if (right.IsConstant()) {
3498 DCHECK(right.GetConstant()->IsLongConstant());
3499 right_is_const = true;
3500 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3501 val_low = Low32Bits(val);
3502 val_high = High32Bits(val);
3503 }
3504
Calin Juravleddb7df22014-11-25 20:56:51 +00003505 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003506 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003507 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003508 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003509 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003510 DCHECK(right_is_const) << right;
3511 if (val_high == 0) {
3512 __ testl(left_high, left_high);
3513 } else {
3514 __ cmpl(left_high, Immediate(val_high));
3515 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003516 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003517 __ j(kLess, &less); // Signed compare.
3518 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003519 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003520 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003521 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003522 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003523 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003524 DCHECK(right_is_const) << right;
3525 if (val_low == 0) {
3526 __ testl(left_low, left_low);
3527 } else {
3528 __ cmpl(left_low, Immediate(val_low));
3529 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003530 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003531 break;
3532 }
3533 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003535 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3536 break;
3537 }
3538 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003539 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003540 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003541 break;
3542 }
3543 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003544 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003545 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003546 __ movl(out, Immediate(0));
3547 __ j(kEqual, &done);
3548 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3549
3550 __ Bind(&greater);
3551 __ movl(out, Immediate(1));
3552 __ jmp(&done);
3553
3554 __ Bind(&less);
3555 __ movl(out, Immediate(-1));
3556
3557 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003558}
3559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003560void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003561 LocationSummary* locations =
3562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003563 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3564 locations->SetInAt(i, Location::Any());
3565 }
3566 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003567}
3568
3569void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003570 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003571 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003572}
3573
Calin Juravle52c48962014-12-16 17:02:57 +00003574void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3575 /*
3576 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3577 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3578 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3579 */
3580 switch (kind) {
3581 case MemBarrierKind::kAnyAny: {
3582 __ mfence();
3583 break;
3584 }
3585 case MemBarrierKind::kAnyStore:
3586 case MemBarrierKind::kLoadAny:
3587 case MemBarrierKind::kStoreStore: {
3588 // nop
3589 break;
3590 }
3591 default:
3592 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003593 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003594}
3595
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003596
Vladimir Marko58155012015-08-19 12:49:41 +00003597void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3598 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3599 switch (invoke->GetMethodLoadKind()) {
3600 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3601 // temp = thread->string_init_entrypoint
3602 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3603 break;
3604 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3605 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3606 break;
3607 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3608 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3609 break;
3610 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3611 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3612 method_patches_.emplace_back(invoke->GetTargetMethod());
3613 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3614 break;
3615 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3616 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3617 FALLTHROUGH_INTENDED;
3618 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3619 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3620 Register method_reg;
3621 Register reg = temp.AsRegister<Register>();
3622 if (current_method.IsRegister()) {
3623 method_reg = current_method.AsRegister<Register>();
3624 } else {
3625 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3626 DCHECK(!current_method.IsValid());
3627 method_reg = reg;
3628 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3629 }
3630 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003631 __ movl(reg, Address(method_reg,
3632 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003633 // temp = temp[index_in_cache]
3634 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3635 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3636 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003637 }
Vladimir Marko58155012015-08-19 12:49:41 +00003638 }
3639
3640 switch (invoke->GetCodePtrLocation()) {
3641 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3642 __ call(GetFrameEntryLabel());
3643 break;
3644 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3645 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3646 Label* label = &relative_call_patches_.back().label;
3647 __ call(label); // Bind to the patch label, override at link time.
3648 __ Bind(label); // Bind the label at the end of the "call" insn.
3649 break;
3650 }
3651 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3652 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3653 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3654 // (Though the direct CALL ptr16:32 is available for consideration).
3655 FALLTHROUGH_INTENDED;
3656 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3657 // (callee_method + offset_of_quick_compiled_code)()
3658 __ call(Address(callee_method.AsRegister<Register>(),
3659 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3660 kX86WordSize).Int32Value()));
3661 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003662 }
3663
3664 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003665}
3666
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003667void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3668 Register temp = temp_in.AsRegister<Register>();
3669 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3670 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3671 LocationSummary* locations = invoke->GetLocations();
3672 Location receiver = locations->InAt(0);
3673 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3674 // temp = object->GetClass();
3675 DCHECK(receiver.IsRegister());
3676 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3677 MaybeRecordImplicitNullCheck(invoke);
3678 __ MaybeUnpoisonHeapReference(temp);
3679 // temp = temp->GetMethodAt(method_offset);
3680 __ movl(temp, Address(temp, method_offset));
3681 // call temp->GetEntryPoint();
3682 __ call(Address(
3683 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3684}
3685
Vladimir Marko58155012015-08-19 12:49:41 +00003686void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3687 DCHECK(linker_patches->empty());
3688 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3689 for (const MethodPatchInfo<Label>& info : method_patches_) {
3690 // The label points to the end of the "movl" insn but the literal offset for method
3691 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3692 uint32_t literal_offset = info.label.Position() - 4;
3693 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3694 info.target_method.dex_file,
3695 info.target_method.dex_method_index));
3696 }
3697 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3698 // The label points to the end of the "call" insn but the literal offset for method
3699 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3700 uint32_t literal_offset = info.label.Position() - 4;
3701 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3702 info.target_method.dex_file,
3703 info.target_method.dex_method_index));
3704 }
3705}
3706
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003707void CodeGeneratorX86::MarkGCCard(Register temp,
3708 Register card,
3709 Register object,
3710 Register value,
3711 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003712 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003713 if (value_can_be_null) {
3714 __ testl(value, value);
3715 __ j(kEqual, &is_null);
3716 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3718 __ movl(temp, object);
3719 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003720 __ movb(Address(temp, card, TIMES_1, 0),
3721 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003722 if (value_can_be_null) {
3723 __ Bind(&is_null);
3724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725}
3726
Calin Juravle52c48962014-12-16 17:02:57 +00003727void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3728 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003729 LocationSummary* locations =
3730 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003731 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003732
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003733 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3734 locations->SetOut(Location::RequiresFpuRegister());
3735 } else {
3736 // The output overlaps in case of long: we don't want the low move to overwrite
3737 // the object's location.
3738 locations->SetOut(Location::RequiresRegister(),
3739 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3740 : Location::kNoOutputOverlap);
3741 }
Calin Juravle52c48962014-12-16 17:02:57 +00003742
3743 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3744 // Long values can be loaded atomically into an XMM using movsd.
3745 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3746 // and then copy the XMM into the output 32bits at a time).
3747 locations->AddTemp(Location::RequiresFpuRegister());
3748 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003749}
3750
Calin Juravle52c48962014-12-16 17:02:57 +00003751void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3752 const FieldInfo& field_info) {
3753 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003754
Calin Juravle52c48962014-12-16 17:02:57 +00003755 LocationSummary* locations = instruction->GetLocations();
3756 Register base = locations->InAt(0).AsRegister<Register>();
3757 Location out = locations->Out();
3758 bool is_volatile = field_info.IsVolatile();
3759 Primitive::Type field_type = field_info.GetFieldType();
3760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3761
3762 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003763 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003764 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003765 break;
3766 }
3767
3768 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003769 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003770 break;
3771 }
3772
3773 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003774 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003775 break;
3776 }
3777
3778 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003779 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003780 break;
3781 }
3782
3783 case Primitive::kPrimInt:
3784 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003785 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003786 break;
3787 }
3788
3789 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003790 if (is_volatile) {
3791 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3792 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003794 __ movd(out.AsRegisterPairLow<Register>(), temp);
3795 __ psrlq(temp, Immediate(32));
3796 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3797 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003798 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003799 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003801 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3802 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003803 break;
3804 }
3805
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003806 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003807 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003808 break;
3809 }
3810
3811 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003812 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003813 break;
3814 }
3815
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003816 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003817 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003818 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003819 }
Calin Juravle52c48962014-12-16 17:02:57 +00003820
Calin Juravle77520bc2015-01-12 18:45:46 +00003821 // Longs are handled in the switch.
3822 if (field_type != Primitive::kPrimLong) {
3823 codegen_->MaybeRecordImplicitNullCheck(instruction);
3824 }
3825
Calin Juravle52c48962014-12-16 17:02:57 +00003826 if (is_volatile) {
3827 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3828 }
Roland Levillain4d027112015-07-01 15:41:14 +01003829
3830 if (field_type == Primitive::kPrimNot) {
3831 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3832 }
Calin Juravle52c48962014-12-16 17:02:57 +00003833}
3834
3835void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3836 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3837
3838 LocationSummary* locations =
3839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3840 locations->SetInAt(0, Location::RequiresRegister());
3841 bool is_volatile = field_info.IsVolatile();
3842 Primitive::Type field_type = field_info.GetFieldType();
3843 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3844 || (field_type == Primitive::kPrimByte);
3845
3846 // The register allocator does not support multiple
3847 // inputs that die at entry with one in a specific register.
3848 if (is_byte_type) {
3849 // Ensure the value is in a byte register.
3850 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003851 } else if (Primitive::IsFloatingPointType(field_type)) {
3852 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003853 } else {
3854 locations->SetInAt(1, Location::RequiresRegister());
3855 }
Calin Juravle52c48962014-12-16 17:02:57 +00003856 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003857 // Temporary registers for the write barrier.
3858 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003859 // Ensure the card is in a byte register.
3860 locations->AddTemp(Location::RegisterLocation(ECX));
3861 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3862 // 64bits value can be atomically written to an address with movsd and an XMM register.
3863 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3864 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3865 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3866 // isolated cases when we need this it isn't worth adding the extra complexity.
3867 locations->AddTemp(Location::RequiresFpuRegister());
3868 locations->AddTemp(Location::RequiresFpuRegister());
3869 }
3870}
3871
3872void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003873 const FieldInfo& field_info,
3874 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003875 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3876
3877 LocationSummary* locations = instruction->GetLocations();
3878 Register base = locations->InAt(0).AsRegister<Register>();
3879 Location value = locations->InAt(1);
3880 bool is_volatile = field_info.IsVolatile();
3881 Primitive::Type field_type = field_info.GetFieldType();
3882 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003883 bool needs_write_barrier =
3884 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003885
3886 if (is_volatile) {
3887 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3888 }
3889
3890 switch (field_type) {
3891 case Primitive::kPrimBoolean:
3892 case Primitive::kPrimByte: {
3893 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3894 break;
3895 }
3896
3897 case Primitive::kPrimShort:
3898 case Primitive::kPrimChar: {
3899 __ movw(Address(base, offset), value.AsRegister<Register>());
3900 break;
3901 }
3902
3903 case Primitive::kPrimInt:
3904 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003905 if (kPoisonHeapReferences && needs_write_barrier) {
3906 // Note that in the case where `value` is a null reference,
3907 // we do not enter this block, as the reference does not
3908 // need poisoning.
3909 DCHECK_EQ(field_type, Primitive::kPrimNot);
3910 Register temp = locations->GetTemp(0).AsRegister<Register>();
3911 __ movl(temp, value.AsRegister<Register>());
3912 __ PoisonHeapReference(temp);
3913 __ movl(Address(base, offset), temp);
3914 } else {
3915 __ movl(Address(base, offset), value.AsRegister<Register>());
3916 }
Calin Juravle52c48962014-12-16 17:02:57 +00003917 break;
3918 }
3919
3920 case Primitive::kPrimLong: {
3921 if (is_volatile) {
3922 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3923 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3924 __ movd(temp1, value.AsRegisterPairLow<Register>());
3925 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3926 __ punpckldq(temp1, temp2);
3927 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003928 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003929 } else {
3930 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003932 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3933 }
3934 break;
3935 }
3936
3937 case Primitive::kPrimFloat: {
3938 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3939 break;
3940 }
3941
3942 case Primitive::kPrimDouble: {
3943 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3944 break;
3945 }
3946
3947 case Primitive::kPrimVoid:
3948 LOG(FATAL) << "Unreachable type " << field_type;
3949 UNREACHABLE();
3950 }
3951
Calin Juravle77520bc2015-01-12 18:45:46 +00003952 // Longs are handled in the switch.
3953 if (field_type != Primitive::kPrimLong) {
3954 codegen_->MaybeRecordImplicitNullCheck(instruction);
3955 }
3956
Roland Levillain4d027112015-07-01 15:41:14 +01003957 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 Register temp = locations->GetTemp(0).AsRegister<Register>();
3959 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003960 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003961 }
3962
Calin Juravle52c48962014-12-16 17:02:57 +00003963 if (is_volatile) {
3964 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3965 }
3966}
3967
3968void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3969 HandleFieldGet(instruction, instruction->GetFieldInfo());
3970}
3971
3972void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3973 HandleFieldGet(instruction, instruction->GetFieldInfo());
3974}
3975
3976void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3977 HandleFieldSet(instruction, instruction->GetFieldInfo());
3978}
3979
3980void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003981 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003982}
3983
3984void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3985 HandleFieldSet(instruction, instruction->GetFieldInfo());
3986}
3987
3988void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003989 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003990}
3991
3992void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3993 HandleFieldGet(instruction, instruction->GetFieldInfo());
3994}
3995
3996void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3997 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003998}
3999
4000void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004001 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4002 ? LocationSummary::kCallOnSlowPath
4003 : LocationSummary::kNoCall;
4004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4005 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004006 ? Location::RequiresRegister()
4007 : Location::Any();
4008 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004009 if (instruction->HasUses()) {
4010 locations->SetOut(Location::SameAsFirstInput());
4011 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004012}
4013
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004014void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004015 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4016 return;
4017 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004018 LocationSummary* locations = instruction->GetLocations();
4019 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004020
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004021 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4022 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4023}
4024
4025void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004026 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004027 codegen_->AddSlowPath(slow_path);
4028
4029 LocationSummary* locations = instruction->GetLocations();
4030 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031
4032 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004033 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004034 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004035 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004036 } else {
4037 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004038 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004039 __ jmp(slow_path->GetEntryLabel());
4040 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004041 }
4042 __ j(kEqual, slow_path->GetEntryLabel());
4043}
4044
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004045void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004046 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004047 GenerateImplicitNullCheck(instruction);
4048 } else {
4049 GenerateExplicitNullCheck(instruction);
4050 }
4051}
4052
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004053void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004056 locations->SetInAt(0, Location::RequiresRegister());
4057 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004058 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4059 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4060 } else {
4061 // The output overlaps in case of long: we don't want the low move to overwrite
4062 // the array's location.
4063 locations->SetOut(Location::RequiresRegister(),
4064 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4065 : Location::kNoOutputOverlap);
4066 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004067}
4068
4069void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4070 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 Location index = locations->InAt(1);
4073
Calin Juravle77520bc2015-01-12 18:45:46 +00004074 Primitive::Type type = instruction->GetType();
4075 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 case Primitive::kPrimBoolean: {
4077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079 if (index.IsConstant()) {
4080 __ movzxb(out, Address(obj,
4081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4082 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004083 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004084 }
4085 break;
4086 }
4087
4088 case Primitive::kPrimByte: {
4089 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004090 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004091 if (index.IsConstant()) {
4092 __ movsxb(out, Address(obj,
4093 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4094 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 }
4097 break;
4098 }
4099
4100 case Primitive::kPrimShort: {
4101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 if (index.IsConstant()) {
4104 __ movsxw(out, Address(obj,
4105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4106 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004107 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 }
4109 break;
4110 }
4111
4112 case Primitive::kPrimChar: {
4113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004114 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004115 if (index.IsConstant()) {
4116 __ movzxw(out, Address(obj,
4117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004119 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 }
4121 break;
4122 }
4123
4124 case Primitive::kPrimInt:
4125 case Primitive::kPrimNot: {
4126 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004127 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004128 if (index.IsConstant()) {
4129 __ movl(out, Address(obj,
4130 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4131 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004132 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004133 }
4134 break;
4135 }
4136
4137 case Primitive::kPrimLong: {
4138 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004139 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004140 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 if (index.IsConstant()) {
4142 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004143 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004145 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004146 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004147 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004148 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004149 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004150 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004151 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004152 }
4153 break;
4154 }
4155
Mark Mendell7c8d0092015-01-26 11:21:33 -05004156 case Primitive::kPrimFloat: {
4157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4158 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4159 if (index.IsConstant()) {
4160 __ movss(out, Address(obj,
4161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4162 } else {
4163 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4164 }
4165 break;
4166 }
4167
4168 case Primitive::kPrimDouble: {
4169 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4170 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4171 if (index.IsConstant()) {
4172 __ movsd(out, Address(obj,
4173 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4174 } else {
4175 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4176 }
4177 break;
4178 }
4179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004181 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004182 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004184
4185 if (type != Primitive::kPrimLong) {
4186 codegen_->MaybeRecordImplicitNullCheck(instruction);
4187 }
Roland Levillain4d027112015-07-01 15:41:14 +01004188
4189 if (type == Primitive::kPrimNot) {
4190 Register out = locations->Out().AsRegister<Register>();
4191 __ MaybeUnpoisonHeapReference(out);
4192 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004193}
4194
4195void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004196 // This location builder might end up asking to up to four registers, which is
4197 // not currently possible for baseline. The situation in which we need four
4198 // registers cannot be met by baseline though, because it has not run any
4199 // optimization.
4200
Nicolas Geoffray39468442014-09-02 15:17:15 +01004201 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004202 bool needs_write_barrier =
4203 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4204
Mark Mendell5f874182015-03-04 15:42:45 -05004205 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004206
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4208 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004209 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004211 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4214 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4215 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004217 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4218 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004219 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004220 // In case of a byte operation, the register allocator does not support multiple
4221 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004222 locations->SetInAt(0, Location::RequiresRegister());
4223 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004224 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004225 // Ensure the value is in a byte register.
4226 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004227 } else if (Primitive::IsFloatingPointType(value_type)) {
4228 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004230 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004233 // Temporary registers for the write barrier.
4234 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004235 // Ensure the card is in a byte register.
4236 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004237 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004238 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004239}
4240
4241void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4242 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004243 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004244 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004245 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004246 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004247 bool needs_runtime_call = locations->WillCall();
4248 bool needs_write_barrier =
4249 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250
4251 switch (value_type) {
4252 case Primitive::kPrimBoolean:
4253 case Primitive::kPrimByte: {
4254 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 if (index.IsConstant()) {
4256 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004257 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004258 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004259 } else {
4260 __ movb(Address(obj, offset),
4261 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4262 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004264 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004265 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004266 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004267 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004268 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004269 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004273 break;
4274 }
4275
4276 case Primitive::kPrimShort:
4277 case Primitive::kPrimChar: {
4278 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 if (index.IsConstant()) {
4280 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004281 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004282 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004283 } else {
4284 __ movw(Address(obj, offset),
4285 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4286 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004287 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004288 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004289 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4290 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004291 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004292 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004293 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4294 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004295 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004296 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 break;
4298 }
4299
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004300 case Primitive::kPrimInt:
4301 case Primitive::kPrimNot: {
4302 if (!needs_runtime_call) {
4303 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4304 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004305 size_t offset =
4306 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004307 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004308 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4309 Register temp = locations->GetTemp(0).AsRegister<Register>();
4310 __ movl(temp, value.AsRegister<Register>());
4311 __ PoisonHeapReference(temp);
4312 __ movl(Address(obj, offset), temp);
4313 } else {
4314 __ movl(Address(obj, offset), value.AsRegister<Register>());
4315 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004316 } else {
4317 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004318 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4319 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4320 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4321 // Note: if heap poisoning is enabled, no need to poison
4322 // (negate) `v` if it is a reference, as it would be null.
4323 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004324 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004325 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004326 DCHECK(index.IsRegister()) << index;
4327 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004328 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4329 Register temp = locations->GetTemp(0).AsRegister<Register>();
4330 __ movl(temp, value.AsRegister<Register>());
4331 __ PoisonHeapReference(temp);
4332 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4333 } else {
4334 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4335 value.AsRegister<Register>());
4336 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004337 } else {
4338 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004339 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4340 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4341 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4342 // Note: if heap poisoning is enabled, no need to poison
4343 // (negate) `v` if it is a reference, as it would be null.
4344 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004345 }
4346 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004347 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004348
4349 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 Register temp = locations->GetTemp(0).AsRegister<Register>();
4351 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004352 codegen_->MarkGCCard(
4353 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004354 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004355 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004356 DCHECK_EQ(value_type, Primitive::kPrimNot);
4357 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004358 // Note: if heap poisoning is enabled, pAputObject takes cares
4359 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004360 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4361 instruction,
4362 instruction->GetDexPc(),
4363 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004364 }
4365 break;
4366 }
4367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368 case Primitive::kPrimLong: {
4369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 if (index.IsConstant()) {
4371 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004372 if (value.IsRegisterPair()) {
4373 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004374 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004375 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004376 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004377 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004378 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4379 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004381 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004384 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004386 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004387 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004388 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004389 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004390 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004391 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004392 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004394 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004395 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004397 Immediate(High32Bits(val)));
4398 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004399 }
4400 break;
4401 }
4402
Mark Mendell7c8d0092015-01-26 11:21:33 -05004403 case Primitive::kPrimFloat: {
4404 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4405 DCHECK(value.IsFpuRegister());
4406 if (index.IsConstant()) {
4407 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4408 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4409 } else {
4410 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4411 value.AsFpuRegister<XmmRegister>());
4412 }
4413 break;
4414 }
4415
4416 case Primitive::kPrimDouble: {
4417 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4418 DCHECK(value.IsFpuRegister());
4419 if (index.IsConstant()) {
4420 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4421 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4422 } else {
4423 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4424 value.AsFpuRegister<XmmRegister>());
4425 }
4426 break;
4427 }
4428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 case Primitive::kPrimVoid:
4430 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004431 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432 }
4433}
4434
4435void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004437 locations->SetInAt(0, Location::RequiresRegister());
4438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439}
4440
4441void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4442 LocationSummary* locations = instruction->GetLocations();
4443 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004444 Register obj = locations->InAt(0).AsRegister<Register>();
4445 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004447 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448}
4449
4450void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004451 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4452 ? LocationSummary::kCallOnSlowPath
4453 : LocationSummary::kNoCall;
4454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004455 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004456 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004457 if (instruction->HasUses()) {
4458 locations->SetOut(Location::SameAsFirstInput());
4459 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460}
4461
4462void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4463 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004464 Location index_loc = locations->InAt(0);
4465 Location length_loc = locations->InAt(1);
4466 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004467 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468
Mark Mendell99dbd682015-04-22 16:18:52 -04004469 if (length_loc.IsConstant()) {
4470 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4471 if (index_loc.IsConstant()) {
4472 // BCE will remove the bounds check if we are guarenteed to pass.
4473 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4474 if (index < 0 || index >= length) {
4475 codegen_->AddSlowPath(slow_path);
4476 __ jmp(slow_path->GetEntryLabel());
4477 } else {
4478 // Some optimization after BCE may have generated this, and we should not
4479 // generate a bounds check if it is a valid range.
4480 }
4481 return;
4482 }
4483
4484 // We have to reverse the jump condition because the length is the constant.
4485 Register index_reg = index_loc.AsRegister<Register>();
4486 __ cmpl(index_reg, Immediate(length));
4487 codegen_->AddSlowPath(slow_path);
4488 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004489 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004490 Register length = length_loc.AsRegister<Register>();
4491 if (index_loc.IsConstant()) {
4492 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4493 __ cmpl(length, Immediate(value));
4494 } else {
4495 __ cmpl(length, index_loc.AsRegister<Register>());
4496 }
4497 codegen_->AddSlowPath(slow_path);
4498 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004499 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500}
4501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4503 temp->SetLocations(nullptr);
4504}
4505
4506void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4507 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004508 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509}
4510
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004511void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004512 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004513 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004514}
4515
4516void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004517 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4518}
4519
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004520void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4522}
4523
4524void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004525 HBasicBlock* block = instruction->GetBlock();
4526 if (block->GetLoopInformation() != nullptr) {
4527 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4528 // The back edge will generate the suspend check.
4529 return;
4530 }
4531 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4532 // The goto will generate the suspend check.
4533 return;
4534 }
4535 GenerateSuspendCheck(instruction, nullptr);
4536}
4537
4538void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4539 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004540 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004541 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4542 if (slow_path == nullptr) {
4543 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4544 instruction->SetSlowPath(slow_path);
4545 codegen_->AddSlowPath(slow_path);
4546 if (successor != nullptr) {
4547 DCHECK(successor->IsLoopHeader());
4548 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4549 }
4550 } else {
4551 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4552 }
4553
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004554 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004555 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004556 if (successor == nullptr) {
4557 __ j(kNotEqual, slow_path->GetEntryLabel());
4558 __ Bind(slow_path->GetReturnLabel());
4559 } else {
4560 __ j(kEqual, codegen_->GetLabelOf(successor));
4561 __ jmp(slow_path->GetEntryLabel());
4562 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004563}
4564
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004565X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4566 return codegen_->GetAssembler();
4567}
4568
Mark Mendell7c8d0092015-01-26 11:21:33 -05004569void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004570 ScratchRegisterScope ensure_scratch(
4571 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4572 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4573 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4574 __ movl(temp_reg, Address(ESP, src + stack_offset));
4575 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004576}
4577
4578void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004579 ScratchRegisterScope ensure_scratch(
4580 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4581 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4582 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4583 __ movl(temp_reg, Address(ESP, src + stack_offset));
4584 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4585 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4586 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004587}
4588
4589void ParallelMoveResolverX86::EmitMove(size_t index) {
4590 MoveOperands* move = moves_.Get(index);
4591 Location source = move->GetSource();
4592 Location destination = move->GetDestination();
4593
4594 if (source.IsRegister()) {
4595 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004596 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004597 } else {
4598 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004599 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004600 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004601 } else if (source.IsFpuRegister()) {
4602 if (destination.IsFpuRegister()) {
4603 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4604 } else if (destination.IsStackSlot()) {
4605 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4606 } else {
4607 DCHECK(destination.IsDoubleStackSlot());
4608 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4609 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004610 } else if (source.IsStackSlot()) {
4611 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004612 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004613 } else if (destination.IsFpuRegister()) {
4614 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004615 } else {
4616 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004617 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4618 }
4619 } else if (source.IsDoubleStackSlot()) {
4620 if (destination.IsFpuRegister()) {
4621 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4622 } else {
4623 DCHECK(destination.IsDoubleStackSlot()) << destination;
4624 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004625 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004626 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004627 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004628 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004629 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004630 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004631 if (value == 0) {
4632 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4633 } else {
4634 __ movl(destination.AsRegister<Register>(), Immediate(value));
4635 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004636 } else {
4637 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004638 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004639 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004640 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004641 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004642 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004643 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004644 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004645 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4646 if (value == 0) {
4647 // Easy handling of 0.0.
4648 __ xorps(dest, dest);
4649 } else {
4650 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004651 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4652 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4653 __ movl(temp, Immediate(value));
4654 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004655 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004656 } else {
4657 DCHECK(destination.IsStackSlot()) << destination;
4658 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4659 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004660 } else if (constant->IsLongConstant()) {
4661 int64_t value = constant->AsLongConstant()->GetValue();
4662 int32_t low_value = Low32Bits(value);
4663 int32_t high_value = High32Bits(value);
4664 Immediate low(low_value);
4665 Immediate high(high_value);
4666 if (destination.IsDoubleStackSlot()) {
4667 __ movl(Address(ESP, destination.GetStackIndex()), low);
4668 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4669 } else {
4670 __ movl(destination.AsRegisterPairLow<Register>(), low);
4671 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4672 }
4673 } else {
4674 DCHECK(constant->IsDoubleConstant());
4675 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004676 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004677 int32_t low_value = Low32Bits(value);
4678 int32_t high_value = High32Bits(value);
4679 Immediate low(low_value);
4680 Immediate high(high_value);
4681 if (destination.IsFpuRegister()) {
4682 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4683 if (value == 0) {
4684 // Easy handling of 0.0.
4685 __ xorpd(dest, dest);
4686 } else {
4687 __ pushl(high);
4688 __ pushl(low);
4689 __ movsd(dest, Address(ESP, 0));
4690 __ addl(ESP, Immediate(8));
4691 }
4692 } else {
4693 DCHECK(destination.IsDoubleStackSlot()) << destination;
4694 __ movl(Address(ESP, destination.GetStackIndex()), low);
4695 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4696 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004697 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004698 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004699 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004700 }
4701}
4702
Mark Mendella5c19ce2015-04-01 12:51:05 -04004703void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004704 Register suggested_scratch = reg == EAX ? EBX : EAX;
4705 ScratchRegisterScope ensure_scratch(
4706 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4707
4708 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4709 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4710 __ movl(Address(ESP, mem + stack_offset), reg);
4711 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004712}
4713
Mark Mendell7c8d0092015-01-26 11:21:33 -05004714void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004715 ScratchRegisterScope ensure_scratch(
4716 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4717
4718 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4719 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4720 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4721 __ movss(Address(ESP, mem + stack_offset), reg);
4722 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004723}
4724
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004725void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004726 ScratchRegisterScope ensure_scratch1(
4727 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004728
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004729 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4730 ScratchRegisterScope ensure_scratch2(
4731 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004732
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004733 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4734 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4735 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4736 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4737 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4738 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004739}
4740
4741void ParallelMoveResolverX86::EmitSwap(size_t index) {
4742 MoveOperands* move = moves_.Get(index);
4743 Location source = move->GetSource();
4744 Location destination = move->GetDestination();
4745
4746 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004747 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4748 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4749 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4750 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4751 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004752 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004753 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004754 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004756 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4757 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004758 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4759 // Use XOR Swap algorithm to avoid a temporary.
4760 DCHECK_NE(source.reg(), destination.reg());
4761 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4762 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4763 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4764 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4765 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4766 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4767 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004768 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4769 // Take advantage of the 16 bytes in the XMM register.
4770 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4771 Address stack(ESP, destination.GetStackIndex());
4772 // Load the double into the high doubleword.
4773 __ movhpd(reg, stack);
4774
4775 // Store the low double into the destination.
4776 __ movsd(stack, reg);
4777
4778 // Move the high double to the low double.
4779 __ psrldq(reg, Immediate(8));
4780 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4781 // Take advantage of the 16 bytes in the XMM register.
4782 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4783 Address stack(ESP, source.GetStackIndex());
4784 // Load the double into the high doubleword.
4785 __ movhpd(reg, stack);
4786
4787 // Store the low double into the destination.
4788 __ movsd(stack, reg);
4789
4790 // Move the high double to the low double.
4791 __ psrldq(reg, Immediate(8));
4792 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4793 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4794 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004795 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004796 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004797 }
4798}
4799
4800void ParallelMoveResolverX86::SpillScratch(int reg) {
4801 __ pushl(static_cast<Register>(reg));
4802}
4803
4804void ParallelMoveResolverX86::RestoreScratch(int reg) {
4805 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004806}
4807
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004808void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004809 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4810 ? LocationSummary::kCallOnSlowPath
4811 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004812 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004813 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004814 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004815 locations->SetOut(Location::RequiresRegister());
4816}
4817
4818void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004819 LocationSummary* locations = cls->GetLocations();
4820 Register out = locations->Out().AsRegister<Register>();
4821 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004822 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004823 DCHECK(!cls->CanCallRuntime());
4824 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004825 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004826 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004827 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004828 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004829 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004830 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004831 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004832
4833 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4834 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4835 codegen_->AddSlowPath(slow_path);
4836 __ testl(out, out);
4837 __ j(kEqual, slow_path->GetEntryLabel());
4838 if (cls->MustGenerateClinitCheck()) {
4839 GenerateClassInitializationCheck(slow_path, out);
4840 } else {
4841 __ Bind(slow_path->GetExitLabel());
4842 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004843 }
4844}
4845
4846void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4847 LocationSummary* locations =
4848 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4849 locations->SetInAt(0, Location::RequiresRegister());
4850 if (check->HasUses()) {
4851 locations->SetOut(Location::SameAsFirstInput());
4852 }
4853}
4854
4855void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004856 // We assume the class to not be null.
4857 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4858 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004859 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004860 GenerateClassInitializationCheck(slow_path,
4861 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004862}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004863
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004864void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4865 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004866 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4867 Immediate(mirror::Class::kStatusInitialized));
4868 __ j(kLess, slow_path->GetEntryLabel());
4869 __ Bind(slow_path->GetExitLabel());
4870 // No need for memory fence, thanks to the X86 memory model.
4871}
4872
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004873void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4874 LocationSummary* locations =
4875 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004876 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004877 locations->SetOut(Location::RequiresRegister());
4878}
4879
4880void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4881 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4882 codegen_->AddSlowPath(slow_path);
4883
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004884 LocationSummary* locations = load->GetLocations();
4885 Register out = locations->Out().AsRegister<Register>();
4886 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004887 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004888 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004889 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004890 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004891 __ testl(out, out);
4892 __ j(kEqual, slow_path->GetEntryLabel());
4893 __ Bind(slow_path->GetExitLabel());
4894}
4895
David Brazdilcb1c0552015-08-04 16:22:25 +01004896static Address GetExceptionTlsAddress() {
4897 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4898}
4899
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004900void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4901 LocationSummary* locations =
4902 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4903 locations->SetOut(Location::RequiresRegister());
4904}
4905
4906void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004907 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4908}
4909
4910void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4911 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4912}
4913
4914void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4915 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004916}
4917
4918void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4919 LocationSummary* locations =
4920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4921 InvokeRuntimeCallingConvention calling_convention;
4922 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4923}
4924
4925void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004926 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4927 instruction,
4928 instruction->GetDexPc(),
4929 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004930}
4931
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004932void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004933 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4934 ? LocationSummary::kNoCall
4935 : LocationSummary::kCallOnSlowPath;
4936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4937 locations->SetInAt(0, Location::RequiresRegister());
4938 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004939 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004940 locations->SetOut(Location::RequiresRegister());
4941}
4942
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004943void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004944 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004946 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004947 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4949 Label done, zero;
4950 SlowPathCodeX86* slow_path = nullptr;
4951
4952 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004953 // Avoid null check if we know obj is not null.
4954 if (instruction->MustDoNullCheck()) {
4955 __ testl(obj, obj);
4956 __ j(kEqual, &zero);
4957 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004958 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004959 __ movl(out, Address(obj, class_offset));
4960 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004961 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004962 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004963 } else {
4964 DCHECK(cls.IsStackSlot()) << cls;
4965 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4966 }
4967
4968 if (instruction->IsClassFinal()) {
4969 // Classes must be equal for the instanceof to succeed.
4970 __ j(kNotEqual, &zero);
4971 __ movl(out, Immediate(1));
4972 __ jmp(&done);
4973 } else {
4974 // If the classes are not equal, we go into a slow path.
4975 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004976 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004977 codegen_->AddSlowPath(slow_path);
4978 __ j(kNotEqual, slow_path->GetEntryLabel());
4979 __ movl(out, Immediate(1));
4980 __ jmp(&done);
4981 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004982
4983 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4984 __ Bind(&zero);
4985 __ movl(out, Immediate(0));
4986 }
4987
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004988 if (slow_path != nullptr) {
4989 __ Bind(slow_path->GetExitLabel());
4990 }
4991 __ Bind(&done);
4992}
4993
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004994void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4996 instruction, LocationSummary::kCallOnSlowPath);
4997 locations->SetInAt(0, Location::RequiresRegister());
4998 locations->SetInAt(1, Location::Any());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004999 // Note that TypeCheckSlowPathX86 uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005000 locations->AddTemp(Location::RequiresRegister());
5001}
5002
5003void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5004 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005005 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005006 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005007 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005008 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005009 SlowPathCodeX86* slow_path =
5010 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005011 codegen_->AddSlowPath(slow_path);
5012
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005013 // Avoid null check if we know obj is not null.
5014 if (instruction->MustDoNullCheck()) {
5015 __ testl(obj, obj);
5016 __ j(kEqual, slow_path->GetExitLabel());
5017 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005018 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01005019 __ movl(temp, Address(obj, class_offset));
5020 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005021 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005022 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005023 } else {
5024 DCHECK(cls.IsStackSlot()) << cls;
5025 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5026 }
Roland Levillain4d027112015-07-01 15:41:14 +01005027 // The checkcast succeeds if the classes are equal (fast path).
5028 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005029 __ j(kNotEqual, slow_path->GetEntryLabel());
5030 __ Bind(slow_path->GetExitLabel());
5031}
5032
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005033void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5034 LocationSummary* locations =
5035 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5036 InvokeRuntimeCallingConvention calling_convention;
5037 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5038}
5039
5040void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005041 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5042 : QUICK_ENTRY_POINT(pUnlockObject),
5043 instruction,
5044 instruction->GetDexPc(),
5045 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005046}
5047
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005048void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5049void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5050void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5051
5052void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5053 LocationSummary* locations =
5054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5055 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5056 || instruction->GetResultType() == Primitive::kPrimLong);
5057 locations->SetInAt(0, Location::RequiresRegister());
5058 locations->SetInAt(1, Location::Any());
5059 locations->SetOut(Location::SameAsFirstInput());
5060}
5061
5062void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5063 HandleBitwiseOperation(instruction);
5064}
5065
5066void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5067 HandleBitwiseOperation(instruction);
5068}
5069
5070void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5071 HandleBitwiseOperation(instruction);
5072}
5073
5074void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5075 LocationSummary* locations = instruction->GetLocations();
5076 Location first = locations->InAt(0);
5077 Location second = locations->InAt(1);
5078 DCHECK(first.Equals(locations->Out()));
5079
5080 if (instruction->GetResultType() == Primitive::kPrimInt) {
5081 if (second.IsRegister()) {
5082 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005083 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005084 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005086 } else {
5087 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005088 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005089 }
5090 } else if (second.IsConstant()) {
5091 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005092 __ andl(first.AsRegister<Register>(),
5093 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005094 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005095 __ orl(first.AsRegister<Register>(),
5096 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005097 } else {
5098 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005099 __ xorl(first.AsRegister<Register>(),
5100 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005101 }
5102 } else {
5103 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005104 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005105 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005106 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005107 } else {
5108 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005110 }
5111 }
5112 } else {
5113 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5114 if (second.IsRegisterPair()) {
5115 if (instruction->IsAnd()) {
5116 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5117 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5118 } else if (instruction->IsOr()) {
5119 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5120 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5121 } else {
5122 DCHECK(instruction->IsXor());
5123 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5124 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5125 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005126 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005127 if (instruction->IsAnd()) {
5128 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5129 __ andl(first.AsRegisterPairHigh<Register>(),
5130 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5131 } else if (instruction->IsOr()) {
5132 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5133 __ orl(first.AsRegisterPairHigh<Register>(),
5134 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5135 } else {
5136 DCHECK(instruction->IsXor());
5137 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5138 __ xorl(first.AsRegisterPairHigh<Register>(),
5139 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5140 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005141 } else {
5142 DCHECK(second.IsConstant()) << second;
5143 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005144 int32_t low_value = Low32Bits(value);
5145 int32_t high_value = High32Bits(value);
5146 Immediate low(low_value);
5147 Immediate high(high_value);
5148 Register first_low = first.AsRegisterPairLow<Register>();
5149 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005150 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005151 if (low_value == 0) {
5152 __ xorl(first_low, first_low);
5153 } else if (low_value != -1) {
5154 __ andl(first_low, low);
5155 }
5156 if (high_value == 0) {
5157 __ xorl(first_high, first_high);
5158 } else if (high_value != -1) {
5159 __ andl(first_high, high);
5160 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005161 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005162 if (low_value != 0) {
5163 __ orl(first_low, low);
5164 }
5165 if (high_value != 0) {
5166 __ orl(first_high, high);
5167 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005168 } else {
5169 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005170 if (low_value != 0) {
5171 __ xorl(first_low, low);
5172 }
5173 if (high_value != 0) {
5174 __ xorl(first_high, high);
5175 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005176 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005177 }
5178 }
5179}
5180
Calin Juravleb1498f62015-02-16 13:13:29 +00005181void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5182 // Nothing to do, this should be removed during prepare for register allocator.
5183 UNUSED(instruction);
5184 LOG(FATAL) << "Unreachable";
5185}
5186
5187void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5188 // Nothing to do, this should be removed during prepare for register allocator.
5189 UNUSED(instruction);
5190 LOG(FATAL) << "Unreachable";
5191}
5192
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005193void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5194 DCHECK(codegen_->IsBaseline());
5195 LocationSummary* locations =
5196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5197 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5198}
5199
5200void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5201 DCHECK(codegen_->IsBaseline());
5202 // Will be generated at use site.
5203}
5204
Mark Mendell0616ae02015-04-17 12:49:27 -04005205void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5206 HX86ComputeBaseMethodAddress* insn) {
5207 LocationSummary* locations =
5208 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5209 locations->SetOut(Location::RequiresRegister());
5210}
5211
5212void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5213 HX86ComputeBaseMethodAddress* insn) {
5214 LocationSummary* locations = insn->GetLocations();
5215 Register reg = locations->Out().AsRegister<Register>();
5216
5217 // Generate call to next instruction.
5218 Label next_instruction;
5219 __ call(&next_instruction);
5220 __ Bind(&next_instruction);
5221
5222 // Remember this offset for later use with constant area.
5223 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5224
5225 // Grab the return address off the stack.
5226 __ popl(reg);
5227}
5228
5229void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5230 HX86LoadFromConstantTable* insn) {
5231 LocationSummary* locations =
5232 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5233
5234 locations->SetInAt(0, Location::RequiresRegister());
5235 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5236
5237 // If we don't need to be materialized, we only need the inputs to be set.
5238 if (!insn->NeedsMaterialization()) {
5239 return;
5240 }
5241
5242 switch (insn->GetType()) {
5243 case Primitive::kPrimFloat:
5244 case Primitive::kPrimDouble:
5245 locations->SetOut(Location::RequiresFpuRegister());
5246 break;
5247
5248 case Primitive::kPrimInt:
5249 locations->SetOut(Location::RequiresRegister());
5250 break;
5251
5252 default:
5253 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5254 }
5255}
5256
5257void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5258 if (!insn->NeedsMaterialization()) {
5259 return;
5260 }
5261
5262 LocationSummary* locations = insn->GetLocations();
5263 Location out = locations->Out();
5264 Register const_area = locations->InAt(0).AsRegister<Register>();
5265 HConstant *value = insn->GetConstant();
5266
5267 switch (insn->GetType()) {
5268 case Primitive::kPrimFloat:
5269 __ movss(out.AsFpuRegister<XmmRegister>(),
5270 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5271 break;
5272
5273 case Primitive::kPrimDouble:
5274 __ movsd(out.AsFpuRegister<XmmRegister>(),
5275 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5276 break;
5277
5278 case Primitive::kPrimInt:
5279 __ movl(out.AsRegister<Register>(),
5280 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5281 break;
5282
5283 default:
5284 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5285 }
5286}
5287
5288void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5289 // Generate the constant area if needed.
5290 X86Assembler* assembler = GetAssembler();
5291 if (!assembler->IsConstantAreaEmpty()) {
5292 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5293 // byte values.
5294 assembler->Align(4, 0);
5295 constant_area_start_ = assembler->CodeSize();
5296 assembler->AddConstantArea();
5297 }
5298
5299 // And finish up.
5300 CodeGenerator::Finalize(allocator);
5301}
5302
5303/**
5304 * Class to handle late fixup of offsets into constant area.
5305 */
5306class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5307 public:
5308 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5309 : codegen_(codegen), offset_into_constant_area_(offset) {}
5310
5311 private:
5312 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5313 // Patch the correct offset for the instruction. The place to patch is the
5314 // last 4 bytes of the instruction.
5315 // The value to patch is the distance from the offset in the constant area
5316 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5317 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5318 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5319
5320 // Patch in the right value.
5321 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5322 }
5323
5324 const CodeGeneratorX86& codegen_;
5325
5326 // Location in constant area that the fixup refers to.
5327 int offset_into_constant_area_;
5328};
5329
5330Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5331 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5332 return Address(reg, kDummy32BitOffset, fixup);
5333}
5334
5335Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5336 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5337 return Address(reg, kDummy32BitOffset, fixup);
5338}
5339
5340Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5341 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5342 return Address(reg, kDummy32BitOffset, fixup);
5343}
5344
5345Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5346 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5347 return Address(reg, kDummy32BitOffset, fixup);
5348}
5349
5350/**
5351 * Finds instructions that need the constant area base as an input.
5352 */
5353class ConstantHandlerVisitor : public HGraphVisitor {
5354 public:
5355 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5356
5357 private:
5358 void VisitAdd(HAdd* add) OVERRIDE {
5359 BinaryFP(add);
5360 }
5361
5362 void VisitSub(HSub* sub) OVERRIDE {
5363 BinaryFP(sub);
5364 }
5365
5366 void VisitMul(HMul* mul) OVERRIDE {
5367 BinaryFP(mul);
5368 }
5369
5370 void VisitDiv(HDiv* div) OVERRIDE {
5371 BinaryFP(div);
5372 }
5373
5374 void VisitReturn(HReturn* ret) OVERRIDE {
5375 HConstant* value = ret->InputAt(0)->AsConstant();
5376 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5377 ReplaceInput(ret, value, 0, true);
5378 }
5379 }
5380
5381 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5382 HandleInvoke(invoke);
5383 }
5384
5385 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5386 HandleInvoke(invoke);
5387 }
5388
5389 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5390 HandleInvoke(invoke);
5391 }
5392
5393 void BinaryFP(HBinaryOperation* bin) {
5394 HConstant* rhs = bin->InputAt(1)->AsConstant();
5395 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5396 ReplaceInput(bin, rhs, 1, false);
5397 }
5398 }
5399
5400 void InitializeConstantAreaPointer(HInstruction* user) {
5401 // Ensure we only initialize the pointer once.
5402 if (base_ != nullptr) {
5403 return;
5404 }
5405
5406 HGraph* graph = GetGraph();
5407 HBasicBlock* entry = graph->GetEntryBlock();
5408 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5409 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5410 entry->InsertInstructionBefore(base_, insert_pos);
5411 DCHECK(base_ != nullptr);
5412 }
5413
5414 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5415 InitializeConstantAreaPointer(insn);
5416 HGraph* graph = GetGraph();
5417 HBasicBlock* block = insn->GetBlock();
5418 HX86LoadFromConstantTable* load_constant =
5419 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5420 block->InsertInstructionBefore(load_constant, insn);
5421 insn->ReplaceInput(load_constant, input_index);
5422 }
5423
5424 void HandleInvoke(HInvoke* invoke) {
5425 // Ensure that we can load FP arguments from the constant area.
5426 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5427 HConstant* input = invoke->InputAt(i)->AsConstant();
5428 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5429 ReplaceInput(invoke, input, i, true);
5430 }
5431 }
5432 }
5433
5434 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5435 // input to the HX86LoadFromConstantTable instructions.
5436 HX86ComputeBaseMethodAddress* base_;
5437};
5438
5439void ConstantAreaFixups::Run() {
5440 ConstantHandlerVisitor visitor(graph_);
5441 visitor.VisitInsertionOrder();
5442}
5443
Roland Levillain4d027112015-07-01 15:41:14 +01005444#undef __
5445
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005446} // namespace x86
5447} // namespace art