blob: 09e939de47bd3017435b905e1600c77cd9aed28c [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())->
Calin Juravle175dc732015-08-25 15:42:32 +010050#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Andreas Gampe85b62f22015-09-09 13:15:38 -070052class NullCheckSlowPathX86 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -070078class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000079 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700104class DivRemMinusOneSlowPathX86 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700126class BoundsCheckSlowPathX86 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700164class SuspendCheckSlowPathX86 : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700204class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700236class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700288class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 public:
Nicolas Geoffray75374372015-09-17 17:12:19 +0000290 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 Geoffray75374372015-09-17 17:12:19 +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 Geoffray75374372015-09-17 17:12:19 +0000327 if (instruction_->IsInstanceOf()) {
328 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100329 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000330 RestoreLiveRegisters(codegen, locations);
331
332 __ jmp(GetExitLabel());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333 }
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700343class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700344 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
Calin Juravle175dc732015-08-25 15:42:32 +0100423void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
424 HInstruction* instruction,
425 uint32_t dex_pc,
426 SlowPathCode* slow_path) {
427 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
428 instruction,
429 dex_pc,
430 slow_path);
431}
432
433void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100434 HInstruction* instruction,
435 uint32_t dex_pc,
436 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100437 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100438 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100439 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100440}
441
Mark Mendellfb8d2792015-03-31 22:16:59 -0400442CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
443 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100444 const CompilerOptions& compiler_options,
445 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500446 : CodeGenerator(graph,
447 kNumberOfCpuRegisters,
448 kNumberOfXmmRegisters,
449 kNumberOfRegisterPairs,
450 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
451 arraysize(kCoreCalleeSaves))
452 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100453 0,
454 compiler_options,
455 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100456 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100458 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400459 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000460 isa_features_(isa_features),
461 method_patches_(graph->GetArena()->Adapter()),
462 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000463 // Use a fake return address register to mimic Quick.
464 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100465}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100466
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100468 switch (type) {
469 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100470 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100471 X86ManagedRegister pair =
472 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100473 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
474 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100475 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
476 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100477 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100478 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100479 }
480
481 case Primitive::kPrimByte:
482 case Primitive::kPrimBoolean:
483 case Primitive::kPrimChar:
484 case Primitive::kPrimShort:
485 case Primitive::kPrimInt:
486 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100487 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100488 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100489 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100490 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
491 X86ManagedRegister current =
492 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
493 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100495 }
496 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100497 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 }
499
500 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100502 return Location::FpuRegisterLocation(
503 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimVoid:
507 LOG(FATAL) << "Unreachable type " << type;
508 }
509
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100511}
512
Mark Mendell5f874182015-03-04 15:42:45 -0500513void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100515 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100516
517 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100518 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100519
Mark Mendell5f874182015-03-04 15:42:45 -0500520 if (is_baseline) {
521 blocked_core_registers_[EBP] = true;
522 blocked_core_registers_[ESI] = true;
523 blocked_core_registers_[EDI] = true;
524 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100525
526 UpdateBlockedPairRegisters();
527}
528
529void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
530 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
531 X86ManagedRegister current =
532 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
533 if (blocked_core_registers_[current.AsRegisterPairLow()]
534 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
535 blocked_register_pairs_[i] = true;
536 }
537 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538}
539
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100540InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
541 : HGraphVisitor(graph),
542 assembler_(codegen->GetAssembler()),
543 codegen_(codegen) {}
544
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100545static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100546 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100547}
548
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100550 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000551 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000552 bool skip_overflow_check =
553 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000554 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000555
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000556 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100557 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100558 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100559 }
560
Mark Mendell5f874182015-03-04 15:42:45 -0500561 if (HasEmptyFrame()) {
562 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000563 }
Mark Mendell5f874182015-03-04 15:42:45 -0500564
565 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
566 Register reg = kCoreCalleeSaves[i];
567 if (allocated_registers_.ContainsCoreRegister(reg)) {
568 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100569 __ cfi().AdjustCFAOffset(kX86WordSize);
570 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500571 }
572 }
573
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100574 int adjust = GetFrameSize() - FrameEntrySpillSize();
575 __ subl(ESP, Immediate(adjust));
576 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100577 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578}
579
580void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100581 __ cfi().RememberState();
582 if (!HasEmptyFrame()) {
583 int adjust = GetFrameSize() - FrameEntrySpillSize();
584 __ addl(ESP, Immediate(adjust));
585 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500586
David Srbeckyc34dc932015-04-12 09:27:43 +0100587 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
588 Register reg = kCoreCalleeSaves[i];
589 if (allocated_registers_.ContainsCoreRegister(reg)) {
590 __ popl(reg);
591 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
592 __ cfi().Restore(DWARFReg(reg));
593 }
Mark Mendell5f874182015-03-04 15:42:45 -0500594 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000595 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100596 __ ret();
597 __ cfi().RestoreState();
598 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000599}
600
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100601void CodeGeneratorX86::Bind(HBasicBlock* block) {
602 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000603}
604
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100605Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
606 switch (load->GetType()) {
607 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100610
611 case Primitive::kPrimInt:
612 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100615
616 case Primitive::kPrimBoolean:
617 case Primitive::kPrimByte:
618 case Primitive::kPrimChar:
619 case Primitive::kPrimShort:
620 case Primitive::kPrimVoid:
621 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700622 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100623 }
624
625 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700626 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100627}
628
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100629Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
630 switch (type) {
631 case Primitive::kPrimBoolean:
632 case Primitive::kPrimByte:
633 case Primitive::kPrimChar:
634 case Primitive::kPrimShort:
635 case Primitive::kPrimInt:
636 case Primitive::kPrimNot:
637 return Location::RegisterLocation(EAX);
638
639 case Primitive::kPrimLong:
640 return Location::RegisterPairLocation(EAX, EDX);
641
642 case Primitive::kPrimVoid:
643 return Location::NoLocation();
644
645 case Primitive::kPrimDouble:
646 case Primitive::kPrimFloat:
647 return Location::FpuRegisterLocation(XMM0);
648 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100649
650 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100651}
652
653Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
654 return Location::RegisterLocation(kMethodRegisterArgument);
655}
656
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100657Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100658 switch (type) {
659 case Primitive::kPrimBoolean:
660 case Primitive::kPrimByte:
661 case Primitive::kPrimChar:
662 case Primitive::kPrimShort:
663 case Primitive::kPrimInt:
664 case Primitive::kPrimNot: {
665 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000666 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100667 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100668 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100669 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000670 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100671 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100672 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100673
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000674 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100675 uint32_t index = gp_index_;
676 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000677 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100678 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100679 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
680 calling_convention.GetRegisterPairAt(index));
681 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100682 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000683 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
684 }
685 }
686
687 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100688 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000689 stack_index_++;
690 if (index < calling_convention.GetNumberOfFpuRegisters()) {
691 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
692 } else {
693 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
694 }
695 }
696
697 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100698 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000699 stack_index_ += 2;
700 if (index < calling_convention.GetNumberOfFpuRegisters()) {
701 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
702 } else {
703 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100704 }
705 }
706
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100707 case Primitive::kPrimVoid:
708 LOG(FATAL) << "Unexpected parameter type " << type;
709 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100710 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100711 return Location();
712}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100713
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714void CodeGeneratorX86::Move32(Location destination, Location source) {
715 if (source.Equals(destination)) {
716 return;
717 }
718 if (destination.IsRegister()) {
719 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000720 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 } else {
724 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 } else if (destination.IsFpuRegister()) {
728 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000731 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 } else {
733 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000734 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000737 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000739 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000741 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500742 } else if (source.IsConstant()) {
743 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000744 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500745 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 } else {
747 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100748 __ pushl(Address(ESP, source.GetStackIndex()));
749 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 }
751 }
752}
753
754void CodeGeneratorX86::Move64(Location destination, Location source) {
755 if (source.Equals(destination)) {
756 return;
757 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100758 if (destination.IsRegisterPair()) {
759 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000760 EmitParallelMoves(
761 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
762 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100763 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000764 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100765 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
766 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else if (source.IsFpuRegister()) {
768 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000770 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100772 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
773 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
775 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500777 if (source.IsFpuRegister()) {
778 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
779 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 } else {
782 LOG(FATAL) << "Unimplemented";
783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000785 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100786 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000787 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100788 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100790 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000792 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000793 } else if (source.IsConstant()) {
794 HConstant* constant = source.GetConstant();
795 int64_t value;
796 if (constant->IsLongConstant()) {
797 value = constant->AsLongConstant()->GetValue();
798 } else {
799 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000800 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000801 }
802 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
803 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000805 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000806 EmitParallelMoves(
807 Location::StackSlot(source.GetStackIndex()),
808 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100809 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000810 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100811 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
812 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 }
814 }
815}
816
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100817void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100819 if (instruction->IsCurrentMethod()) {
820 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
821 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000822 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100823 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000824 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000825 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
826 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000827 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000828 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000829 } else if (location.IsStackSlot()) {
830 __ movl(Address(ESP, location.GetStackIndex()), imm);
831 } else {
832 DCHECK(location.IsConstant());
833 DCHECK_EQ(location.GetConstant(), const_to_move);
834 }
835 } else if (const_to_move->IsLongConstant()) {
836 int64_t value = const_to_move->AsLongConstant()->GetValue();
837 if (location.IsRegisterPair()) {
838 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
839 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
840 } else if (location.IsDoubleStackSlot()) {
841 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000842 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
843 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000844 } else {
845 DCHECK(location.IsConstant());
846 DCHECK_EQ(location.GetConstant(), instruction);
847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000849 } else if (instruction->IsTemporary()) {
850 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000851 if (temp_location.IsStackSlot()) {
852 Move32(location, temp_location);
853 } else {
854 DCHECK(temp_location.IsDoubleStackSlot());
855 Move64(location, temp_location);
856 }
Roland Levillain476df552014-10-09 17:51:36 +0100857 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 switch (instruction->GetType()) {
860 case Primitive::kPrimBoolean:
861 case Primitive::kPrimByte:
862 case Primitive::kPrimChar:
863 case Primitive::kPrimShort:
864 case Primitive::kPrimInt:
865 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 case Primitive::kPrimFloat:
867 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 break;
869
870 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100871 case Primitive::kPrimDouble:
872 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873 break;
874
875 default:
876 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
877 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000878 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100879 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 switch (instruction->GetType()) {
881 case Primitive::kPrimBoolean:
882 case Primitive::kPrimByte:
883 case Primitive::kPrimChar:
884 case Primitive::kPrimShort:
885 case Primitive::kPrimInt:
886 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000888 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 break;
890
891 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 break;
895
896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899 }
900}
901
Calin Juravle175dc732015-08-25 15:42:32 +0100902void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
903 DCHECK(location.IsRegister());
904 __ movl(location.AsRegister<Register>(), Immediate(value));
905}
906
David Brazdilfc6a86a2015-06-26 10:33:45 +0000907void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100908 DCHECK(!successor->IsExitBlock());
909
910 HBasicBlock* block = got->GetBlock();
911 HInstruction* previous = got->GetPrevious();
912
913 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000914 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
916 return;
917 }
918
919 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
920 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
921 }
922 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000924 }
925}
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927void LocationsBuilderX86::VisitGoto(HGoto* got) {
928 got->SetLocations(nullptr);
929}
930
931void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
932 HandleGoto(got, got->GetSuccessor());
933}
934
935void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
936 try_boundary->SetLocations(nullptr);
937}
938
939void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
940 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
941 if (!successor->IsExitBlock()) {
942 HandleGoto(try_boundary, successor);
943 }
944}
945
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000946void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000947 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000948}
949
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700951 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000952}
953
Mark Mendellc4701932015-04-10 13:18:51 -0400954void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
955 Label* true_label,
956 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100957 if (cond->IsFPConditionTrueIfNaN()) {
958 __ j(kUnordered, true_label);
959 } else if (cond->IsFPConditionFalseIfNaN()) {
960 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400961 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400963}
964
965void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
966 Label* true_label,
967 Label* false_label) {
968 LocationSummary* locations = cond->GetLocations();
969 Location left = locations->InAt(0);
970 Location right = locations->InAt(1);
971 IfCondition if_cond = cond->GetCondition();
972
Mark Mendellc4701932015-04-10 13:18:51 -0400973 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100974 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400975 IfCondition true_high_cond = if_cond;
976 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100977 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400978
979 // Set the conditions for the test, remembering that == needs to be
980 // decided using the low words.
981 switch (if_cond) {
982 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400983 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100984 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400985 break;
986 case kCondLT:
987 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400988 break;
989 case kCondLE:
990 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400991 break;
992 case kCondGT:
993 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400994 break;
995 case kCondGE:
996 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400997 break;
998 }
999
1000 if (right.IsConstant()) {
1001 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001002 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001003 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001004
1005 if (val_high == 0) {
1006 __ testl(left_high, left_high);
1007 } else {
1008 __ cmpl(left_high, Immediate(val_high));
1009 }
1010 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001011 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001012 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001014 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001015 __ j(X86SignedCondition(true_high_cond), true_label);
1016 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001017 }
1018 // Must be equal high, so compare the lows.
1019 if (val_low == 0) {
1020 __ testl(left_low, left_low);
1021 } else {
1022 __ cmpl(left_low, Immediate(val_low));
1023 }
1024 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001025 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001026 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001027
1028 __ cmpl(left_high, right_high);
1029 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001031 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001032 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001033 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001034 __ j(X86SignedCondition(true_high_cond), true_label);
1035 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001036 }
1037 // Must be equal high, so compare the lows.
1038 __ cmpl(left_low, right_low);
1039 }
1040 // The last comparison might be unsigned.
1041 __ j(final_condition, true_label);
1042}
1043
1044void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1045 HCondition* condition,
1046 Label* true_target,
1047 Label* false_target,
1048 Label* always_true_target) {
1049 LocationSummary* locations = condition->GetLocations();
1050 Location left = locations->InAt(0);
1051 Location right = locations->InAt(1);
1052
1053 // We don't want true_target as a nullptr.
1054 if (true_target == nullptr) {
1055 true_target = always_true_target;
1056 }
1057 bool falls_through = (false_target == nullptr);
1058
1059 // FP compares don't like null false_targets.
1060 if (false_target == nullptr) {
1061 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1062 }
1063
1064 Primitive::Type type = condition->InputAt(0)->GetType();
1065 switch (type) {
1066 case Primitive::kPrimLong:
1067 GenerateLongComparesAndJumps(condition, true_target, false_target);
1068 break;
1069 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001070 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1071 GenerateFPJumps(condition, true_target, false_target);
1072 break;
1073 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001074 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1075 GenerateFPJumps(condition, true_target, false_target);
1076 break;
1077 default:
1078 LOG(FATAL) << "Unexpected compare type " << type;
1079 }
1080
1081 if (!falls_through) {
1082 __ jmp(false_target);
1083 }
1084}
1085
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001086void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1087 Label* true_target,
1088 Label* false_target,
1089 Label* always_true_target) {
1090 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001091 if (cond->IsIntConstant()) {
1092 // Constant condition, statically compared against 1.
1093 int32_t cond_value = cond->AsIntConstant()->GetValue();
1094 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001095 if (always_true_target != nullptr) {
1096 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001097 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001098 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001099 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001100 DCHECK_EQ(cond_value, 0);
1101 }
1102 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001103 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1105 // Moves do not affect the eflags register, so if the condition is
1106 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001107 // again. We can't use the eflags on long/FP conditions if they are
1108 // materialized due to the complex branching.
1109 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001110 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001111 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001112 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1113 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001114 if (!eflags_set) {
1115 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001116 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001118 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001119 } else {
1120 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1121 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001122 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001123 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001124 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001125 }
1126 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001127 // Condition has not been materialized, use its inputs as the
1128 // comparison and its condition as the branch condition.
1129
Mark Mendellc4701932015-04-10 13:18:51 -04001130 // Is this a long or FP comparison that has been folded into the HCondition?
1131 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1132 // Generate the comparison directly.
1133 GenerateCompareTestAndBranch(instruction->AsIf(),
1134 cond->AsCondition(),
1135 true_target,
1136 false_target,
1137 always_true_target);
1138 return;
1139 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001140
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001141 Location lhs = cond->GetLocations()->InAt(0);
1142 Location rhs = cond->GetLocations()->InAt(1);
1143 // LHS is guaranteed to be in a register (see
1144 // LocationsBuilderX86::VisitCondition).
1145 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001146 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001147 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001148 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001149 if (constant == 0) {
1150 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1151 } else {
1152 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1153 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001154 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001155 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001156 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001157 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001158 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001159 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001160 if (false_target != nullptr) {
1161 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001162 }
1163}
1164
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001165void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1166 LocationSummary* locations =
1167 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1168 HInstruction* cond = if_instr->InputAt(0);
1169 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1170 locations->SetInAt(0, Location::Any());
1171 }
1172}
1173
1174void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1175 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1176 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1177 Label* always_true_target = true_target;
1178 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1179 if_instr->IfTrueSuccessor())) {
1180 always_true_target = nullptr;
1181 }
1182 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1183 if_instr->IfFalseSuccessor())) {
1184 false_target = nullptr;
1185 }
1186 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1187}
1188
1189void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1190 LocationSummary* locations = new (GetGraph()->GetArena())
1191 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1192 HInstruction* cond = deoptimize->InputAt(0);
1193 DCHECK(cond->IsCondition());
1194 if (cond->AsCondition()->NeedsMaterialization()) {
1195 locations->SetInAt(0, Location::Any());
1196 }
1197}
1198
1199void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001200 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001201 DeoptimizationSlowPathX86(deoptimize);
1202 codegen_->AddSlowPath(slow_path);
1203 Label* slow_path_entry = slow_path->GetEntryLabel();
1204 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1205}
1206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001207void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001208 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001209}
1210
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001211void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1212 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001213}
1214
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001215void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001216 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217}
1218
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001219void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001220 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001221 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001222}
1223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227 switch (store->InputAt(1)->GetType()) {
1228 case Primitive::kPrimBoolean:
1229 case Primitive::kPrimByte:
1230 case Primitive::kPrimChar:
1231 case Primitive::kPrimShort:
1232 case Primitive::kPrimInt:
1233 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1236 break;
1237
1238 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1241 break;
1242
1243 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001244 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001246}
1247
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001248void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001249 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001250}
1251
Roland Levillain0d37cd02015-05-27 16:39:19 +01001252void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001253 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001254 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001255 // Handle the long/FP comparisons made in instruction simplification.
1256 switch (cond->InputAt(0)->GetType()) {
1257 case Primitive::kPrimLong: {
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1260 if (cond->NeedsMaterialization()) {
1261 locations->SetOut(Location::RequiresRegister());
1262 }
1263 break;
1264 }
1265 case Primitive::kPrimFloat:
1266 case Primitive::kPrimDouble: {
1267 locations->SetInAt(0, Location::RequiresFpuRegister());
1268 locations->SetInAt(1, Location::RequiresFpuRegister());
1269 if (cond->NeedsMaterialization()) {
1270 locations->SetOut(Location::RequiresRegister());
1271 }
1272 break;
1273 }
1274 default:
1275 locations->SetInAt(0, Location::RequiresRegister());
1276 locations->SetInAt(1, Location::Any());
1277 if (cond->NeedsMaterialization()) {
1278 // We need a byte register.
1279 locations->SetOut(Location::RegisterLocation(ECX));
1280 }
1281 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001282 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001283}
1284
Roland Levillain0d37cd02015-05-27 16:39:19 +01001285void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001286 if (!cond->NeedsMaterialization()) {
1287 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001288 }
Mark Mendellc4701932015-04-10 13:18:51 -04001289
1290 LocationSummary* locations = cond->GetLocations();
1291 Location lhs = locations->InAt(0);
1292 Location rhs = locations->InAt(1);
1293 Register reg = locations->Out().AsRegister<Register>();
1294 Label true_label, false_label;
1295
1296 switch (cond->InputAt(0)->GetType()) {
1297 default: {
1298 // Integer case.
1299
1300 // Clear output register: setcc only sets the low byte.
1301 __ xorl(reg, reg);
1302
1303 if (rhs.IsRegister()) {
1304 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1305 } else if (rhs.IsConstant()) {
1306 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1307 if (constant == 0) {
1308 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1309 } else {
1310 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1311 }
1312 } else {
1313 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1314 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001315 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001316 return;
1317 }
1318 case Primitive::kPrimLong:
1319 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1320 break;
1321 case Primitive::kPrimFloat:
1322 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1323 GenerateFPJumps(cond, &true_label, &false_label);
1324 break;
1325 case Primitive::kPrimDouble:
1326 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1327 GenerateFPJumps(cond, &true_label, &false_label);
1328 break;
1329 }
1330
1331 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001332 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001333
Roland Levillain4fa13f62015-07-06 18:11:54 +01001334 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001335 __ Bind(&false_label);
1336 __ xorl(reg, reg);
1337 __ jmp(&done_label);
1338
Roland Levillain4fa13f62015-07-06 18:11:54 +01001339 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001340 __ Bind(&true_label);
1341 __ movl(reg, Immediate(1));
1342 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001343}
1344
1345void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1346 VisitCondition(comp);
1347}
1348
1349void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1350 VisitCondition(comp);
1351}
1352
1353void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1354 VisitCondition(comp);
1355}
1356
1357void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1358 VisitCondition(comp);
1359}
1360
1361void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1362 VisitCondition(comp);
1363}
1364
1365void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1366 VisitCondition(comp);
1367}
1368
1369void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1370 VisitCondition(comp);
1371}
1372
1373void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1374 VisitCondition(comp);
1375}
1376
1377void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1378 VisitCondition(comp);
1379}
1380
1381void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1382 VisitCondition(comp);
1383}
1384
1385void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1386 VisitCondition(comp);
1387}
1388
1389void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1390 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391}
1392
1393void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 LocationSummary* locations =
1395 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001396 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397}
1398
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001399void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001400 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001401 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001402}
1403
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001404void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1407 locations->SetOut(Location::ConstantLocation(constant));
1408}
1409
1410void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1411 // Will be generated at use site.
1412 UNUSED(constant);
1413}
1414
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001415void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001416 LocationSummary* locations =
1417 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001418 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001419}
1420
1421void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1422 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001423 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001424}
1425
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001426void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1427 LocationSummary* locations =
1428 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1429 locations->SetOut(Location::ConstantLocation(constant));
1430}
1431
1432void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1433 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001434 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001435}
1436
1437void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1438 LocationSummary* locations =
1439 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1440 locations->SetOut(Location::ConstantLocation(constant));
1441}
1442
1443void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1444 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001445 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001446}
1447
Calin Juravle27df7582015-04-17 19:12:31 +01001448void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1449 memory_barrier->SetLocations(nullptr);
1450}
1451
1452void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1453 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1454}
1455
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001456void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001458}
1459
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001460void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001461 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001462 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001463}
1464
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001466 LocationSummary* locations =
1467 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001468 switch (ret->InputAt(0)->GetType()) {
1469 case Primitive::kPrimBoolean:
1470 case Primitive::kPrimByte:
1471 case Primitive::kPrimChar:
1472 case Primitive::kPrimShort:
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001475 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001476 break;
1477
1478 case Primitive::kPrimLong:
1479 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001480 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001481 break;
1482
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001483 case Primitive::kPrimFloat:
1484 case Primitive::kPrimDouble:
1485 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001486 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 break;
1488
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001491 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001492}
1493
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001494void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 if (kIsDebugBuild) {
1496 switch (ret->InputAt(0)->GetType()) {
1497 case Primitive::kPrimBoolean:
1498 case Primitive::kPrimByte:
1499 case Primitive::kPrimChar:
1500 case Primitive::kPrimShort:
1501 case Primitive::kPrimInt:
1502 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001503 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001504 break;
1505
1506 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001507 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1508 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509 break;
1510
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001511 case Primitive::kPrimFloat:
1512 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001513 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001514 break;
1515
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001516 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001517 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518 }
1519 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001520 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001521}
1522
Calin Juravle175dc732015-08-25 15:42:32 +01001523void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1524 // The trampoline uses the same calling convention as dex calling conventions,
1525 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1526 // the method_idx.
1527 HandleInvoke(invoke);
1528}
1529
1530void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1531 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1532}
1533
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001534void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001535 // When we do not run baseline, explicit clinit checks triggered by static
1536 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1537 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001538
Mark Mendellfb8d2792015-03-31 22:16:59 -04001539 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001540 if (intrinsic.TryDispatch(invoke)) {
1541 return;
1542 }
1543
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001544 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001545
1546 if (codegen_->IsBaseline()) {
1547 // Baseline does not have enough registers if the current method also
1548 // needs a register. We therefore do not require a register for it, and let
1549 // the code generation of the invoke handle it.
1550 LocationSummary* locations = invoke->GetLocations();
1551 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1552 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1553 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1554 }
1555 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001556}
1557
Mark Mendell09ed1a32015-03-25 08:30:06 -04001558static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1559 if (invoke->GetLocations()->Intrinsified()) {
1560 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1561 intrinsic.Dispatch(invoke);
1562 return true;
1563 }
1564 return false;
1565}
1566
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001567void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001568 // When we do not run baseline, explicit clinit checks triggered by static
1569 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1570 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001571
Mark Mendell09ed1a32015-03-25 08:30:06 -04001572 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1573 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001574 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001575
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001576 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001577 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001578 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001579 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001580}
1581
1582void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1583 HandleInvoke(invoke);
1584}
1585
1586void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001587 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001588 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001589}
1590
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001591void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001592 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1593 return;
1594 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001595
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001596 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001597 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001598 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001599}
1600
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001601void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1602 HandleInvoke(invoke);
1603 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001604 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001605}
1606
1607void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1608 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001609 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001610 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1611 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001612 LocationSummary* locations = invoke->GetLocations();
1613 Location receiver = locations->InAt(0);
1614 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1615
1616 // Set the hidden argument.
1617 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001618 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001619
1620 // temp = object->GetClass();
1621 if (receiver.IsStackSlot()) {
1622 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1623 __ movl(temp, Address(temp, class_offset));
1624 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001625 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001626 }
Roland Levillain4d027112015-07-01 15:41:14 +01001627 codegen_->MaybeRecordImplicitNullCheck(invoke);
1628 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001629 // temp = temp->GetImtEntryAt(method_offset);
1630 __ movl(temp, Address(temp, method_offset));
1631 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001632 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001633 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001634
1635 DCHECK(!codegen_->IsLeafMethod());
1636 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1637}
1638
Roland Levillain88cb1752014-10-20 16:36:47 +01001639void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1640 LocationSummary* locations =
1641 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1642 switch (neg->GetResultType()) {
1643 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001644 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001645 locations->SetInAt(0, Location::RequiresRegister());
1646 locations->SetOut(Location::SameAsFirstInput());
1647 break;
1648
Roland Levillain88cb1752014-10-20 16:36:47 +01001649 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001650 locations->SetInAt(0, Location::RequiresFpuRegister());
1651 locations->SetOut(Location::SameAsFirstInput());
1652 locations->AddTemp(Location::RequiresRegister());
1653 locations->AddTemp(Location::RequiresFpuRegister());
1654 break;
1655
Roland Levillain88cb1752014-10-20 16:36:47 +01001656 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001657 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001658 locations->SetOut(Location::SameAsFirstInput());
1659 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001660 break;
1661
1662 default:
1663 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1664 }
1665}
1666
1667void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1668 LocationSummary* locations = neg->GetLocations();
1669 Location out = locations->Out();
1670 Location in = locations->InAt(0);
1671 switch (neg->GetResultType()) {
1672 case Primitive::kPrimInt:
1673 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001674 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001676 break;
1677
1678 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001679 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001680 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001681 __ negl(out.AsRegisterPairLow<Register>());
1682 // Negation is similar to subtraction from zero. The least
1683 // significant byte triggers a borrow when it is different from
1684 // zero; to take it into account, add 1 to the most significant
1685 // byte if the carry flag (CF) is set to 1 after the first NEGL
1686 // operation.
1687 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1688 __ negl(out.AsRegisterPairHigh<Register>());
1689 break;
1690
Roland Levillain5368c212014-11-27 15:03:41 +00001691 case Primitive::kPrimFloat: {
1692 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001693 Register constant = locations->GetTemp(0).AsRegister<Register>();
1694 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001695 // Implement float negation with an exclusive or with value
1696 // 0x80000000 (mask for bit 31, representing the sign of a
1697 // single-precision floating-point number).
1698 __ movl(constant, Immediate(INT32_C(0x80000000)));
1699 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001701 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001702 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001703
Roland Levillain5368c212014-11-27 15:03:41 +00001704 case Primitive::kPrimDouble: {
1705 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001706 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001707 // Implement double negation with an exclusive or with value
1708 // 0x8000000000000000 (mask for bit 63, representing the sign of
1709 // a double-precision floating-point number).
1710 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001712 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001713 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001714
1715 default:
1716 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1717 }
1718}
1719
Roland Levillaindff1f282014-11-05 14:15:05 +00001720void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001721 Primitive::Type result_type = conversion->GetResultType();
1722 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001723 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001724
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001725 // The float-to-long and double-to-long type conversions rely on a
1726 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001727 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001728 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1729 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001730 ? LocationSummary::kCall
1731 : LocationSummary::kNoCall;
1732 LocationSummary* locations =
1733 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1734
David Brazdilb2bd1c52015-03-25 11:17:37 +00001735 // The Java language does not allow treating boolean as an integral type but
1736 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001737
Roland Levillaindff1f282014-11-05 14:15:05 +00001738 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001739 case Primitive::kPrimByte:
1740 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001741 case Primitive::kPrimBoolean:
1742 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001743 case Primitive::kPrimShort:
1744 case Primitive::kPrimInt:
1745 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001746 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001747 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1748 // Make the output overlap to please the register allocator. This greatly simplifies
1749 // the validation of the linear scan implementation
1750 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001751 break;
1752
1753 default:
1754 LOG(FATAL) << "Unexpected type conversion from " << input_type
1755 << " to " << result_type;
1756 }
1757 break;
1758
Roland Levillain01a8d712014-11-14 16:27:39 +00001759 case Primitive::kPrimShort:
1760 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001761 case Primitive::kPrimBoolean:
1762 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001763 case Primitive::kPrimByte:
1764 case Primitive::kPrimInt:
1765 case Primitive::kPrimChar:
1766 // Processing a Dex `int-to-short' instruction.
1767 locations->SetInAt(0, Location::Any());
1768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillain946e1432014-11-11 17:35:19 +00001777 case Primitive::kPrimInt:
1778 switch (input_type) {
1779 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001780 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001781 locations->SetInAt(0, Location::Any());
1782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1783 break;
1784
1785 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001786 // Processing a Dex `float-to-int' instruction.
1787 locations->SetInAt(0, Location::RequiresFpuRegister());
1788 locations->SetOut(Location::RequiresRegister());
1789 locations->AddTemp(Location::RequiresFpuRegister());
1790 break;
1791
Roland Levillain946e1432014-11-11 17:35:19 +00001792 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001793 // Processing a Dex `double-to-int' instruction.
1794 locations->SetInAt(0, Location::RequiresFpuRegister());
1795 locations->SetOut(Location::RequiresRegister());
1796 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001797 break;
1798
1799 default:
1800 LOG(FATAL) << "Unexpected type conversion from " << input_type
1801 << " to " << result_type;
1802 }
1803 break;
1804
Roland Levillaindff1f282014-11-05 14:15:05 +00001805 case Primitive::kPrimLong:
1806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001807 case Primitive::kPrimBoolean:
1808 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001809 case Primitive::kPrimByte:
1810 case Primitive::kPrimShort:
1811 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001812 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001813 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001814 locations->SetInAt(0, Location::RegisterLocation(EAX));
1815 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1816 break;
1817
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001818 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001819 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001820 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001821 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001822 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1823 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1824
Vladimir Marko949c91f2015-01-27 10:48:44 +00001825 // The runtime helper puts the result in EAX, EDX.
1826 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001827 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001828 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001829
1830 default:
1831 LOG(FATAL) << "Unexpected type conversion from " << input_type
1832 << " to " << result_type;
1833 }
1834 break;
1835
Roland Levillain981e4542014-11-14 11:47:14 +00001836 case Primitive::kPrimChar:
1837 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001838 case Primitive::kPrimBoolean:
1839 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001840 case Primitive::kPrimByte:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001843 // Processing a Dex `int-to-char' instruction.
1844 locations->SetInAt(0, Location::Any());
1845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1846 break;
1847
1848 default:
1849 LOG(FATAL) << "Unexpected type conversion from " << input_type
1850 << " to " << result_type;
1851 }
1852 break;
1853
Roland Levillaindff1f282014-11-05 14:15:05 +00001854 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001855 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001856 case Primitive::kPrimBoolean:
1857 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001858 case Primitive::kPrimByte:
1859 case Primitive::kPrimShort:
1860 case Primitive::kPrimInt:
1861 case Primitive::kPrimChar:
1862 // Processing a Dex `int-to-float' instruction.
1863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetOut(Location::RequiresFpuRegister());
1865 break;
1866
1867 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001868 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001869 locations->SetInAt(0, Location::Any());
1870 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001871 break;
1872
Roland Levillaincff13742014-11-17 14:32:17 +00001873 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001874 // Processing a Dex `double-to-float' instruction.
1875 locations->SetInAt(0, Location::RequiresFpuRegister());
1876 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001877 break;
1878
1879 default:
1880 LOG(FATAL) << "Unexpected type conversion from " << input_type
1881 << " to " << result_type;
1882 };
1883 break;
1884
Roland Levillaindff1f282014-11-05 14:15:05 +00001885 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001886 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001887 case Primitive::kPrimBoolean:
1888 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001889 case Primitive::kPrimByte:
1890 case Primitive::kPrimShort:
1891 case Primitive::kPrimInt:
1892 case Primitive::kPrimChar:
1893 // Processing a Dex `int-to-double' instruction.
1894 locations->SetInAt(0, Location::RequiresRegister());
1895 locations->SetOut(Location::RequiresFpuRegister());
1896 break;
1897
1898 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001899 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001900 locations->SetInAt(0, Location::Any());
1901 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001902 break;
1903
Roland Levillaincff13742014-11-17 14:32:17 +00001904 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001905 // Processing a Dex `float-to-double' instruction.
1906 locations->SetInAt(0, Location::RequiresFpuRegister());
1907 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001914 break;
1915
1916 default:
1917 LOG(FATAL) << "Unexpected type conversion from " << input_type
1918 << " to " << result_type;
1919 }
1920}
1921
1922void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1923 LocationSummary* locations = conversion->GetLocations();
1924 Location out = locations->Out();
1925 Location in = locations->InAt(0);
1926 Primitive::Type result_type = conversion->GetResultType();
1927 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001928 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001929 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001930 case Primitive::kPrimByte:
1931 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001932 case Primitive::kPrimBoolean:
1933 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001934 case Primitive::kPrimShort:
1935 case Primitive::kPrimInt:
1936 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001937 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001938 if (in.IsRegister()) {
1939 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001940 } else {
1941 DCHECK(in.GetConstant()->IsIntConstant());
1942 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1943 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1944 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 }
1951 break;
1952
Roland Levillain01a8d712014-11-14 16:27:39 +00001953 case Primitive::kPrimShort:
1954 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001955 case Primitive::kPrimBoolean:
1956 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001957 case Primitive::kPrimByte:
1958 case Primitive::kPrimInt:
1959 case Primitive::kPrimChar:
1960 // Processing a Dex `int-to-short' instruction.
1961 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001963 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001965 } else {
1966 DCHECK(in.GetConstant()->IsIntConstant());
1967 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001969 }
1970 break;
1971
1972 default:
1973 LOG(FATAL) << "Unexpected type conversion from " << input_type
1974 << " to " << result_type;
1975 }
1976 break;
1977
Roland Levillain946e1432014-11-11 17:35:19 +00001978 case Primitive::kPrimInt:
1979 switch (input_type) {
1980 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001981 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001982 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001983 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001984 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001985 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001986 } else {
1987 DCHECK(in.IsConstant());
1988 DCHECK(in.GetConstant()->IsLongConstant());
1989 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001991 }
1992 break;
1993
Roland Levillain3f8f9362014-12-02 17:45:01 +00001994 case Primitive::kPrimFloat: {
1995 // Processing a Dex `float-to-int' instruction.
1996 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1997 Register output = out.AsRegister<Register>();
1998 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04001999 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002000
2001 __ movl(output, Immediate(kPrimIntMax));
2002 // temp = int-to-float(output)
2003 __ cvtsi2ss(temp, output);
2004 // if input >= temp goto done
2005 __ comiss(input, temp);
2006 __ j(kAboveEqual, &done);
2007 // if input == NaN goto nan
2008 __ j(kUnordered, &nan);
2009 // output = float-to-int-truncate(input)
2010 __ cvttss2si(output, input);
2011 __ jmp(&done);
2012 __ Bind(&nan);
2013 // output = 0
2014 __ xorl(output, output);
2015 __ Bind(&done);
2016 break;
2017 }
2018
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002019 case Primitive::kPrimDouble: {
2020 // Processing a Dex `double-to-int' instruction.
2021 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2022 Register output = out.AsRegister<Register>();
2023 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002024 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002025
2026 __ movl(output, Immediate(kPrimIntMax));
2027 // temp = int-to-double(output)
2028 __ cvtsi2sd(temp, output);
2029 // if input >= temp goto done
2030 __ comisd(input, temp);
2031 __ j(kAboveEqual, &done);
2032 // if input == NaN goto nan
2033 __ j(kUnordered, &nan);
2034 // output = double-to-int-truncate(input)
2035 __ cvttsd2si(output, input);
2036 __ jmp(&done);
2037 __ Bind(&nan);
2038 // output = 0
2039 __ xorl(output, output);
2040 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002041 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002042 }
Roland Levillain946e1432014-11-11 17:35:19 +00002043
2044 default:
2045 LOG(FATAL) << "Unexpected type conversion from " << input_type
2046 << " to " << result_type;
2047 }
2048 break;
2049
Roland Levillaindff1f282014-11-05 14:15:05 +00002050 case Primitive::kPrimLong:
2051 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002052 case Primitive::kPrimBoolean:
2053 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002054 case Primitive::kPrimByte:
2055 case Primitive::kPrimShort:
2056 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002057 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002058 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002059 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2060 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002061 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002062 __ cdq();
2063 break;
2064
2065 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002066 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002067 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2068 conversion,
2069 conversion->GetDexPc(),
2070 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002071 break;
2072
Roland Levillaindff1f282014-11-05 14:15:05 +00002073 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002074 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002075 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2076 conversion,
2077 conversion->GetDexPc(),
2078 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002079 break;
2080
2081 default:
2082 LOG(FATAL) << "Unexpected type conversion from " << input_type
2083 << " to " << result_type;
2084 }
2085 break;
2086
Roland Levillain981e4542014-11-14 11:47:14 +00002087 case Primitive::kPrimChar:
2088 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002089 case Primitive::kPrimBoolean:
2090 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002091 case Primitive::kPrimByte:
2092 case Primitive::kPrimShort:
2093 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002094 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2095 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002096 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002097 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002098 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002099 } else {
2100 DCHECK(in.GetConstant()->IsIntConstant());
2101 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002103 }
2104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected type conversion from " << input_type
2108 << " to " << result_type;
2109 }
2110 break;
2111
Roland Levillaindff1f282014-11-05 14:15:05 +00002112 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002113 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002114 case Primitive::kPrimBoolean:
2115 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002116 case Primitive::kPrimByte:
2117 case Primitive::kPrimShort:
2118 case Primitive::kPrimInt:
2119 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002120 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002122 break;
2123
Roland Levillain6d0e4832014-11-27 18:31:21 +00002124 case Primitive::kPrimLong: {
2125 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002126 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002127
Roland Levillain232ade02015-04-20 15:14:36 +01002128 // Create stack space for the call to
2129 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2130 // TODO: enhance register allocator to ask for stack temporaries.
2131 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2132 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2133 __ subl(ESP, Immediate(adjustment));
2134 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002135
Roland Levillain232ade02015-04-20 15:14:36 +01002136 // Load the value to the FP stack, using temporaries if needed.
2137 PushOntoFPStack(in, 0, adjustment, false, true);
2138
2139 if (out.IsStackSlot()) {
2140 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2141 } else {
2142 __ fstps(Address(ESP, 0));
2143 Location stack_temp = Location::StackSlot(0);
2144 codegen_->Move32(out, stack_temp);
2145 }
2146
2147 // Remove the temporary stack space we allocated.
2148 if (adjustment != 0) {
2149 __ addl(ESP, Immediate(adjustment));
2150 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002151 break;
2152 }
2153
Roland Levillaincff13742014-11-17 14:32:17 +00002154 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002155 // Processing a Dex `double-to-float' instruction.
2156 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002157 break;
2158
2159 default:
2160 LOG(FATAL) << "Unexpected type conversion from " << input_type
2161 << " to " << result_type;
2162 };
2163 break;
2164
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002166 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002167 case Primitive::kPrimBoolean:
2168 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002169 case Primitive::kPrimByte:
2170 case Primitive::kPrimShort:
2171 case Primitive::kPrimInt:
2172 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002173 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002175 break;
2176
Roland Levillain647b9ed2014-11-27 12:06:00 +00002177 case Primitive::kPrimLong: {
2178 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002179 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002180
Roland Levillain232ade02015-04-20 15:14:36 +01002181 // Create stack space for the call to
2182 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2183 // TODO: enhance register allocator to ask for stack temporaries.
2184 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2185 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2186 __ subl(ESP, Immediate(adjustment));
2187 }
2188
2189 // Load the value to the FP stack, using temporaries if needed.
2190 PushOntoFPStack(in, 0, adjustment, false, true);
2191
2192 if (out.IsDoubleStackSlot()) {
2193 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2194 } else {
2195 __ fstpl(Address(ESP, 0));
2196 Location stack_temp = Location::DoubleStackSlot(0);
2197 codegen_->Move64(out, stack_temp);
2198 }
2199
2200 // Remove the temporary stack space we allocated.
2201 if (adjustment != 0) {
2202 __ addl(ESP, Immediate(adjustment));
2203 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002204 break;
2205 }
2206
Roland Levillaincff13742014-11-17 14:32:17 +00002207 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002208 // Processing a Dex `float-to-double' instruction.
2209 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002210 break;
2211
2212 default:
2213 LOG(FATAL) << "Unexpected type conversion from " << input_type
2214 << " to " << result_type;
2215 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002216 break;
2217
2218 default:
2219 LOG(FATAL) << "Unexpected type conversion from " << input_type
2220 << " to " << result_type;
2221 }
2222}
2223
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002224void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002225 LocationSummary* locations =
2226 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002227 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002228 case Primitive::kPrimInt: {
2229 locations->SetInAt(0, Location::RequiresRegister());
2230 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233 }
2234
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002235 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002236 locations->SetInAt(0, Location::RequiresRegister());
2237 locations->SetInAt(1, Location::Any());
2238 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239 break;
2240 }
2241
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002242 case Primitive::kPrimFloat:
2243 case Primitive::kPrimDouble: {
2244 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002245 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002247 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002248 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002250 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002251 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2252 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002253 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002254}
2255
2256void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2257 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002258 Location first = locations->InAt(0);
2259 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002260 Location out = locations->Out();
2261
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002262 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002263 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002264 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002265 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2266 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002267 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2268 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002269 } else {
2270 __ leal(out.AsRegister<Register>(), Address(
2271 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2272 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002273 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002274 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2275 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2276 __ addl(out.AsRegister<Register>(), Immediate(value));
2277 } else {
2278 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2279 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002280 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002281 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002282 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002283 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002284 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002285 }
2286
2287 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002288 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002289 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2290 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002291 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002292 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2293 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002295 } else {
2296 DCHECK(second.IsConstant()) << second;
2297 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2298 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2299 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002300 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002301 break;
2302 }
2303
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 case Primitive::kPrimFloat: {
2305 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002307 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2308 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2309 DCHECK(!const_area->NeedsMaterialization());
2310 __ addss(first.AsFpuRegister<XmmRegister>(),
2311 codegen_->LiteralFloatAddress(
2312 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2313 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2314 } else {
2315 DCHECK(second.IsStackSlot());
2316 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002317 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002319 }
2320
2321 case Primitive::kPrimDouble: {
2322 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002324 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2325 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2326 DCHECK(!const_area->NeedsMaterialization());
2327 __ addsd(first.AsFpuRegister<XmmRegister>(),
2328 codegen_->LiteralDoubleAddress(
2329 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2330 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2331 } else {
2332 DCHECK(second.IsDoubleStackSlot());
2333 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002334 }
2335 break;
2336 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002338 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002339 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002340 }
2341}
2342
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002343void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002344 LocationSummary* locations =
2345 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002346 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002347 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002349 locations->SetInAt(0, Location::RequiresRegister());
2350 locations->SetInAt(1, Location::Any());
2351 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 break;
2353 }
Calin Juravle11351682014-10-23 15:38:15 +01002354 case Primitive::kPrimFloat:
2355 case Primitive::kPrimDouble: {
2356 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002357 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002358 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002359 break;
Calin Juravle11351682014-10-23 15:38:15 +01002360 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002361
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002362 default:
Calin Juravle11351682014-10-23 15:38:15 +01002363 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002364 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002365}
2366
2367void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2368 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002369 Location first = locations->InAt(0);
2370 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002371 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002372 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002376 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002377 __ subl(first.AsRegister<Register>(),
2378 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002379 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002380 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002381 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002382 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002383 }
2384
2385 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002386 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002387 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2388 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002389 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002390 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002391 __ sbbl(first.AsRegisterPairHigh<Register>(),
2392 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002393 } else {
2394 DCHECK(second.IsConstant()) << second;
2395 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2396 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2397 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002398 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002399 break;
2400 }
2401
Calin Juravle11351682014-10-23 15:38:15 +01002402 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002403 if (second.IsFpuRegister()) {
2404 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2405 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2406 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2407 DCHECK(!const_area->NeedsMaterialization());
2408 __ subss(first.AsFpuRegister<XmmRegister>(),
2409 codegen_->LiteralFloatAddress(
2410 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2411 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2412 } else {
2413 DCHECK(second.IsStackSlot());
2414 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2415 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002416 break;
Calin Juravle11351682014-10-23 15:38:15 +01002417 }
2418
2419 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002420 if (second.IsFpuRegister()) {
2421 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2422 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2423 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2424 DCHECK(!const_area->NeedsMaterialization());
2425 __ subsd(first.AsFpuRegister<XmmRegister>(),
2426 codegen_->LiteralDoubleAddress(
2427 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2428 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2429 } else {
2430 DCHECK(second.IsDoubleStackSlot());
2431 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2432 }
Calin Juravle11351682014-10-23 15:38:15 +01002433 break;
2434 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436 default:
Calin Juravle11351682014-10-23 15:38:15 +01002437 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002438 }
2439}
2440
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441void LocationsBuilderX86::VisitMul(HMul* mul) {
2442 LocationSummary* locations =
2443 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2444 switch (mul->GetResultType()) {
2445 case Primitive::kPrimInt:
2446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002448 if (mul->InputAt(1)->IsIntConstant()) {
2449 // Can use 3 operand multiply.
2450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2451 } else {
2452 locations->SetOut(Location::SameAsFirstInput());
2453 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454 break;
2455 case Primitive::kPrimLong: {
2456 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002457 locations->SetInAt(1, Location::Any());
2458 locations->SetOut(Location::SameAsFirstInput());
2459 // Needed for imul on 32bits with 64bits output.
2460 locations->AddTemp(Location::RegisterLocation(EAX));
2461 locations->AddTemp(Location::RegisterLocation(EDX));
2462 break;
2463 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002464 case Primitive::kPrimFloat:
2465 case Primitive::kPrimDouble: {
2466 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002467 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002468 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002469 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002470 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002471
2472 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002473 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002474 }
2475}
2476
2477void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2478 LocationSummary* locations = mul->GetLocations();
2479 Location first = locations->InAt(0);
2480 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002481 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002482
2483 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002484 case Primitive::kPrimInt:
2485 // The constant may have ended up in a register, so test explicitly to avoid
2486 // problems where the output may not be the same as the first operand.
2487 if (mul->InputAt(1)->IsIntConstant()) {
2488 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2489 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2490 } else if (second.IsRegister()) {
2491 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002492 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002493 } else {
2494 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002495 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002497 }
2498 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499
2500 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002501 Register in1_hi = first.AsRegisterPairHigh<Register>();
2502 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 Register eax = locations->GetTemp(0).AsRegister<Register>();
2504 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002505
2506 DCHECK_EQ(EAX, eax);
2507 DCHECK_EQ(EDX, edx);
2508
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002509 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002510 // output: in1
2511 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2512 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2513 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002514 if (second.IsConstant()) {
2515 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002516
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002517 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2518 int32_t low_value = Low32Bits(value);
2519 int32_t high_value = High32Bits(value);
2520 Immediate low(low_value);
2521 Immediate high(high_value);
2522
2523 __ movl(eax, high);
2524 // eax <- in1.lo * in2.hi
2525 __ imull(eax, in1_lo);
2526 // in1.hi <- in1.hi * in2.lo
2527 __ imull(in1_hi, low);
2528 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2529 __ addl(in1_hi, eax);
2530 // move in2_lo to eax to prepare for double precision
2531 __ movl(eax, low);
2532 // edx:eax <- in1.lo * in2.lo
2533 __ mull(in1_lo);
2534 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2535 __ addl(in1_hi, edx);
2536 // in1.lo <- (in1.lo * in2.lo)[31:0];
2537 __ movl(in1_lo, eax);
2538 } else if (second.IsRegisterPair()) {
2539 Register in2_hi = second.AsRegisterPairHigh<Register>();
2540 Register in2_lo = second.AsRegisterPairLow<Register>();
2541
2542 __ movl(eax, in2_hi);
2543 // eax <- in1.lo * in2.hi
2544 __ imull(eax, in1_lo);
2545 // in1.hi <- in1.hi * in2.lo
2546 __ imull(in1_hi, in2_lo);
2547 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2548 __ addl(in1_hi, eax);
2549 // move in1_lo to eax to prepare for double precision
2550 __ movl(eax, in1_lo);
2551 // edx:eax <- in1.lo * in2.lo
2552 __ mull(in2_lo);
2553 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2554 __ addl(in1_hi, edx);
2555 // in1.lo <- (in1.lo * in2.lo)[31:0];
2556 __ movl(in1_lo, eax);
2557 } else {
2558 DCHECK(second.IsDoubleStackSlot()) << second;
2559 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2560 Address in2_lo(ESP, second.GetStackIndex());
2561
2562 __ movl(eax, in2_hi);
2563 // eax <- in1.lo * in2.hi
2564 __ imull(eax, in1_lo);
2565 // in1.hi <- in1.hi * in2.lo
2566 __ imull(in1_hi, in2_lo);
2567 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2568 __ addl(in1_hi, eax);
2569 // move in1_lo to eax to prepare for double precision
2570 __ movl(eax, in1_lo);
2571 // edx:eax <- in1.lo * in2.lo
2572 __ mull(in2_lo);
2573 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2574 __ addl(in1_hi, edx);
2575 // in1.lo <- (in1.lo * in2.lo)[31:0];
2576 __ movl(in1_lo, eax);
2577 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002578
2579 break;
2580 }
2581
Calin Juravleb5bfa962014-10-21 18:02:24 +01002582 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002583 DCHECK(first.Equals(locations->Out()));
2584 if (second.IsFpuRegister()) {
2585 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2586 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2587 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2588 DCHECK(!const_area->NeedsMaterialization());
2589 __ mulss(first.AsFpuRegister<XmmRegister>(),
2590 codegen_->LiteralFloatAddress(
2591 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2592 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2593 } else {
2594 DCHECK(second.IsStackSlot());
2595 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2596 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002597 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002598 }
2599
2600 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002601 DCHECK(first.Equals(locations->Out()));
2602 if (second.IsFpuRegister()) {
2603 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2604 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2605 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2606 DCHECK(!const_area->NeedsMaterialization());
2607 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2608 codegen_->LiteralDoubleAddress(
2609 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2610 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2611 } else {
2612 DCHECK(second.IsDoubleStackSlot());
2613 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2614 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002615 break;
2616 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002617
2618 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002619 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002620 }
2621}
2622
Roland Levillain232ade02015-04-20 15:14:36 +01002623void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2624 uint32_t temp_offset,
2625 uint32_t stack_adjustment,
2626 bool is_fp,
2627 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002628 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002629 DCHECK(!is_wide);
2630 if (is_fp) {
2631 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2632 } else {
2633 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2634 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002635 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002636 DCHECK(is_wide);
2637 if (is_fp) {
2638 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2639 } else {
2640 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2641 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002642 } else {
2643 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002644 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002645 Location stack_temp = Location::StackSlot(temp_offset);
2646 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002647 if (is_fp) {
2648 __ flds(Address(ESP, temp_offset));
2649 } else {
2650 __ filds(Address(ESP, temp_offset));
2651 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002652 } else {
2653 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2654 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002655 if (is_fp) {
2656 __ fldl(Address(ESP, temp_offset));
2657 } else {
2658 __ fildl(Address(ESP, temp_offset));
2659 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002660 }
2661 }
2662}
2663
2664void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2665 Primitive::Type type = rem->GetResultType();
2666 bool is_float = type == Primitive::kPrimFloat;
2667 size_t elem_size = Primitive::ComponentSize(type);
2668 LocationSummary* locations = rem->GetLocations();
2669 Location first = locations->InAt(0);
2670 Location second = locations->InAt(1);
2671 Location out = locations->Out();
2672
2673 // Create stack space for 2 elements.
2674 // TODO: enhance register allocator to ask for stack temporaries.
2675 __ subl(ESP, Immediate(2 * elem_size));
2676
2677 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002678 const bool is_wide = !is_float;
2679 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2680 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002681
2682 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002683 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002684 __ Bind(&retry);
2685 __ fprem();
2686
2687 // Move FP status to AX.
2688 __ fstsw();
2689
2690 // And see if the argument reduction is complete. This is signaled by the
2691 // C2 FPU flag bit set to 0.
2692 __ andl(EAX, Immediate(kC2ConditionMask));
2693 __ j(kNotEqual, &retry);
2694
2695 // We have settled on the final value. Retrieve it into an XMM register.
2696 // Store FP top of stack to real stack.
2697 if (is_float) {
2698 __ fsts(Address(ESP, 0));
2699 } else {
2700 __ fstl(Address(ESP, 0));
2701 }
2702
2703 // Pop the 2 items from the FP stack.
2704 __ fucompp();
2705
2706 // Load the value from the stack into an XMM register.
2707 DCHECK(out.IsFpuRegister()) << out;
2708 if (is_float) {
2709 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2710 } else {
2711 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2712 }
2713
2714 // And remove the temporary stack space we allocated.
2715 __ addl(ESP, Immediate(2 * elem_size));
2716}
2717
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002718
2719void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2720 DCHECK(instruction->IsDiv() || instruction->IsRem());
2721
2722 LocationSummary* locations = instruction->GetLocations();
2723 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002724 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002725
2726 Register out_register = locations->Out().AsRegister<Register>();
2727 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002728 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002729
2730 DCHECK(imm == 1 || imm == -1);
2731
2732 if (instruction->IsRem()) {
2733 __ xorl(out_register, out_register);
2734 } else {
2735 __ movl(out_register, input_register);
2736 if (imm == -1) {
2737 __ negl(out_register);
2738 }
2739 }
2740}
2741
2742
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002743void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002744 LocationSummary* locations = instruction->GetLocations();
2745
2746 Register out_register = locations->Out().AsRegister<Register>();
2747 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002748 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002749
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002750 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002751 Register num = locations->GetTemp(0).AsRegister<Register>();
2752
2753 __ leal(num, Address(input_register, std::abs(imm) - 1));
2754 __ testl(input_register, input_register);
2755 __ cmovl(kGreaterEqual, num, input_register);
2756 int shift = CTZ(imm);
2757 __ sarl(num, Immediate(shift));
2758
2759 if (imm < 0) {
2760 __ negl(num);
2761 }
2762
2763 __ movl(out_register, num);
2764}
2765
2766void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2767 DCHECK(instruction->IsDiv() || instruction->IsRem());
2768
2769 LocationSummary* locations = instruction->GetLocations();
2770 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2771
2772 Register eax = locations->InAt(0).AsRegister<Register>();
2773 Register out = locations->Out().AsRegister<Register>();
2774 Register num;
2775 Register edx;
2776
2777 if (instruction->IsDiv()) {
2778 edx = locations->GetTemp(0).AsRegister<Register>();
2779 num = locations->GetTemp(1).AsRegister<Register>();
2780 } else {
2781 edx = locations->Out().AsRegister<Register>();
2782 num = locations->GetTemp(0).AsRegister<Register>();
2783 }
2784
2785 DCHECK_EQ(EAX, eax);
2786 DCHECK_EQ(EDX, edx);
2787 if (instruction->IsDiv()) {
2788 DCHECK_EQ(EAX, out);
2789 } else {
2790 DCHECK_EQ(EDX, out);
2791 }
2792
2793 int64_t magic;
2794 int shift;
2795 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2796
Mark Mendell0c9497d2015-08-21 09:30:05 -04002797 NearLabel ndiv;
2798 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002799 // If numerator is 0, the result is 0, no computation needed.
2800 __ testl(eax, eax);
2801 __ j(kNotEqual, &ndiv);
2802
2803 __ xorl(out, out);
2804 __ jmp(&end);
2805
2806 __ Bind(&ndiv);
2807
2808 // Save the numerator.
2809 __ movl(num, eax);
2810
2811 // EAX = magic
2812 __ movl(eax, Immediate(magic));
2813
2814 // EDX:EAX = magic * numerator
2815 __ imull(num);
2816
2817 if (imm > 0 && magic < 0) {
2818 // EDX += num
2819 __ addl(edx, num);
2820 } else if (imm < 0 && magic > 0) {
2821 __ subl(edx, num);
2822 }
2823
2824 // Shift if needed.
2825 if (shift != 0) {
2826 __ sarl(edx, Immediate(shift));
2827 }
2828
2829 // EDX += 1 if EDX < 0
2830 __ movl(eax, edx);
2831 __ shrl(edx, Immediate(31));
2832 __ addl(edx, eax);
2833
2834 if (instruction->IsRem()) {
2835 __ movl(eax, num);
2836 __ imull(edx, Immediate(imm));
2837 __ subl(eax, edx);
2838 __ movl(edx, eax);
2839 } else {
2840 __ movl(eax, edx);
2841 }
2842 __ Bind(&end);
2843}
2844
Calin Juravlebacfec32014-11-14 15:54:36 +00002845void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2846 DCHECK(instruction->IsDiv() || instruction->IsRem());
2847
2848 LocationSummary* locations = instruction->GetLocations();
2849 Location out = locations->Out();
2850 Location first = locations->InAt(0);
2851 Location second = locations->InAt(1);
2852 bool is_div = instruction->IsDiv();
2853
2854 switch (instruction->GetResultType()) {
2855 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 DCHECK_EQ(EAX, first.AsRegister<Register>());
2857 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002858
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002859 if (instruction->InputAt(1)->IsIntConstant()) {
2860 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002861
2862 if (imm == 0) {
2863 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2864 } else if (imm == 1 || imm == -1) {
2865 DivRemOneOrMinusOne(instruction);
2866 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002867 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002868 } else {
2869 DCHECK(imm <= -2 || imm >= 2);
2870 GenerateDivRemWithAnyConstant(instruction);
2871 }
2872 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002873 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002874 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002875 is_div);
2876 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002877
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002878 Register second_reg = second.AsRegister<Register>();
2879 // 0x80000000/-1 triggers an arithmetic exception!
2880 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2881 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002882
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002883 __ cmpl(second_reg, Immediate(-1));
2884 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002885
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002886 // edx:eax <- sign-extended of eax
2887 __ cdq();
2888 // eax = quotient, edx = remainder
2889 __ idivl(second_reg);
2890 __ Bind(slow_path->GetExitLabel());
2891 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002892 break;
2893 }
2894
2895 case Primitive::kPrimLong: {
2896 InvokeRuntimeCallingConvention calling_convention;
2897 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2898 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2899 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2900 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2901 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2902 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2903
2904 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002905 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2906 instruction,
2907 instruction->GetDexPc(),
2908 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002909 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002910 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2911 instruction,
2912 instruction->GetDexPc(),
2913 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002914 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002915 break;
2916 }
2917
2918 default:
2919 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2920 }
2921}
2922
Calin Juravle7c4954d2014-10-28 16:57:40 +00002923void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002924 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002925 ? LocationSummary::kCall
2926 : LocationSummary::kNoCall;
2927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2928
Calin Juravle7c4954d2014-10-28 16:57:40 +00002929 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002930 case Primitive::kPrimInt: {
2931 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002932 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002933 locations->SetOut(Location::SameAsFirstInput());
2934 // Intel uses edx:eax as the dividend.
2935 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002936 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2937 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2938 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002939 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002940 locations->AddTemp(Location::RequiresRegister());
2941 }
Calin Juravled0d48522014-11-04 16:40:20 +00002942 break;
2943 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002944 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002945 InvokeRuntimeCallingConvention calling_convention;
2946 locations->SetInAt(0, Location::RegisterPairLocation(
2947 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2948 locations->SetInAt(1, Location::RegisterPairLocation(
2949 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2950 // Runtime helper puts the result in EAX, EDX.
2951 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002952 break;
2953 }
2954 case Primitive::kPrimFloat:
2955 case Primitive::kPrimDouble: {
2956 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002957 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002958 locations->SetOut(Location::SameAsFirstInput());
2959 break;
2960 }
2961
2962 default:
2963 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2964 }
2965}
2966
2967void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2968 LocationSummary* locations = div->GetLocations();
2969 Location first = locations->InAt(0);
2970 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002971
2972 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002973 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002974 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002975 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002976 break;
2977 }
2978
2979 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002980 if (second.IsFpuRegister()) {
2981 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2982 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
2983 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
2984 DCHECK(!const_area->NeedsMaterialization());
2985 __ divss(first.AsFpuRegister<XmmRegister>(),
2986 codegen_->LiteralFloatAddress(
2987 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2988 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2989 } else {
2990 DCHECK(second.IsStackSlot());
2991 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2992 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002993 break;
2994 }
2995
2996 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002997 if (second.IsFpuRegister()) {
2998 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2999 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3000 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3001 DCHECK(!const_area->NeedsMaterialization());
3002 __ divsd(first.AsFpuRegister<XmmRegister>(),
3003 codegen_->LiteralDoubleAddress(
3004 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3005 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3006 } else {
3007 DCHECK(second.IsDoubleStackSlot());
3008 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3009 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003010 break;
3011 }
3012
3013 default:
3014 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3015 }
3016}
3017
Calin Juravlebacfec32014-11-14 15:54:36 +00003018void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003019 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003020
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3022 ? LocationSummary::kCall
3023 : LocationSummary::kNoCall;
3024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003025
Calin Juravled2ec87d2014-12-08 14:24:46 +00003026 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003027 case Primitive::kPrimInt: {
3028 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003029 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003030 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003031 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3032 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3033 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003034 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003035 locations->AddTemp(Location::RequiresRegister());
3036 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003037 break;
3038 }
3039 case Primitive::kPrimLong: {
3040 InvokeRuntimeCallingConvention calling_convention;
3041 locations->SetInAt(0, Location::RegisterPairLocation(
3042 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3043 locations->SetInAt(1, Location::RegisterPairLocation(
3044 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3045 // Runtime helper puts the result in EAX, EDX.
3046 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3047 break;
3048 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003049 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003050 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003051 locations->SetInAt(0, Location::Any());
3052 locations->SetInAt(1, Location::Any());
3053 locations->SetOut(Location::RequiresFpuRegister());
3054 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003055 break;
3056 }
3057
3058 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003059 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003060 }
3061}
3062
3063void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3064 Primitive::Type type = rem->GetResultType();
3065 switch (type) {
3066 case Primitive::kPrimInt:
3067 case Primitive::kPrimLong: {
3068 GenerateDivRemIntegral(rem);
3069 break;
3070 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003071 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003072 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003073 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003074 break;
3075 }
3076 default:
3077 LOG(FATAL) << "Unexpected rem type " << type;
3078 }
3079}
3080
Calin Juravled0d48522014-11-04 16:40:20 +00003081void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003082 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3083 ? LocationSummary::kCallOnSlowPath
3084 : LocationSummary::kNoCall;
3085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003086 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003087 case Primitive::kPrimByte:
3088 case Primitive::kPrimChar:
3089 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003090 case Primitive::kPrimInt: {
3091 locations->SetInAt(0, Location::Any());
3092 break;
3093 }
3094 case Primitive::kPrimLong: {
3095 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3096 if (!instruction->IsConstant()) {
3097 locations->AddTemp(Location::RequiresRegister());
3098 }
3099 break;
3100 }
3101 default:
3102 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3103 }
Calin Juravled0d48522014-11-04 16:40:20 +00003104 if (instruction->HasUses()) {
3105 locations->SetOut(Location::SameAsFirstInput());
3106 }
3107}
3108
3109void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003110 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003111 codegen_->AddSlowPath(slow_path);
3112
3113 LocationSummary* locations = instruction->GetLocations();
3114 Location value = locations->InAt(0);
3115
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003116 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003117 case Primitive::kPrimByte:
3118 case Primitive::kPrimChar:
3119 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003120 case Primitive::kPrimInt: {
3121 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003122 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003123 __ j(kEqual, slow_path->GetEntryLabel());
3124 } else if (value.IsStackSlot()) {
3125 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3126 __ j(kEqual, slow_path->GetEntryLabel());
3127 } else {
3128 DCHECK(value.IsConstant()) << value;
3129 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3130 __ jmp(slow_path->GetEntryLabel());
3131 }
3132 }
3133 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003134 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003135 case Primitive::kPrimLong: {
3136 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003138 __ movl(temp, value.AsRegisterPairLow<Register>());
3139 __ orl(temp, value.AsRegisterPairHigh<Register>());
3140 __ j(kEqual, slow_path->GetEntryLabel());
3141 } else {
3142 DCHECK(value.IsConstant()) << value;
3143 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3144 __ jmp(slow_path->GetEntryLabel());
3145 }
3146 }
3147 break;
3148 }
3149 default:
3150 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003151 }
Calin Juravled0d48522014-11-04 16:40:20 +00003152}
3153
Calin Juravle9aec02f2014-11-18 23:06:35 +00003154void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3155 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3156
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3159
3160 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003161 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003162 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003163 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003164 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003165 // The shift count needs to be in CL or a constant.
3166 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003167 locations->SetOut(Location::SameAsFirstInput());
3168 break;
3169 }
3170 default:
3171 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3172 }
3173}
3174
3175void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3176 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3177
3178 LocationSummary* locations = op->GetLocations();
3179 Location first = locations->InAt(0);
3180 Location second = locations->InAt(1);
3181 DCHECK(first.Equals(locations->Out()));
3182
3183 switch (op->GetResultType()) {
3184 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003185 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003186 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003187 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003188 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003189 DCHECK_EQ(ECX, second_reg);
3190 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003191 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003192 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003193 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003194 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003195 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003196 }
3197 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003198 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3199 if (shift == 0) {
3200 return;
3201 }
3202 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003203 if (op->IsShl()) {
3204 __ shll(first_reg, imm);
3205 } else if (op->IsShr()) {
3206 __ sarl(first_reg, imm);
3207 } else {
3208 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003209 }
3210 }
3211 break;
3212 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003213 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003214 if (second.IsRegister()) {
3215 Register second_reg = second.AsRegister<Register>();
3216 DCHECK_EQ(ECX, second_reg);
3217 if (op->IsShl()) {
3218 GenerateShlLong(first, second_reg);
3219 } else if (op->IsShr()) {
3220 GenerateShrLong(first, second_reg);
3221 } else {
3222 GenerateUShrLong(first, second_reg);
3223 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003224 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003225 // Shift by a constant.
3226 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3227 // Nothing to do if the shift is 0, as the input is already the output.
3228 if (shift != 0) {
3229 if (op->IsShl()) {
3230 GenerateShlLong(first, shift);
3231 } else if (op->IsShr()) {
3232 GenerateShrLong(first, shift);
3233 } else {
3234 GenerateUShrLong(first, shift);
3235 }
3236 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003237 }
3238 break;
3239 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003240 default:
3241 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3242 }
3243}
3244
Mark P Mendell73945692015-04-29 14:56:17 +00003245void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3246 Register low = loc.AsRegisterPairLow<Register>();
3247 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003248 if (shift == 1) {
3249 // This is just an addition.
3250 __ addl(low, low);
3251 __ adcl(high, high);
3252 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003253 // Shift by 32 is easy. High gets low, and low gets 0.
3254 codegen_->EmitParallelMoves(
3255 loc.ToLow(),
3256 loc.ToHigh(),
3257 Primitive::kPrimInt,
3258 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3259 loc.ToLow(),
3260 Primitive::kPrimInt);
3261 } else if (shift > 32) {
3262 // Low part becomes 0. High part is low part << (shift-32).
3263 __ movl(high, low);
3264 __ shll(high, Immediate(shift - 32));
3265 __ xorl(low, low);
3266 } else {
3267 // Between 1 and 31.
3268 __ shld(high, low, Immediate(shift));
3269 __ shll(low, Immediate(shift));
3270 }
3271}
3272
Calin Juravle9aec02f2014-11-18 23:06:35 +00003273void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003274 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003275 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3276 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3277 __ testl(shifter, Immediate(32));
3278 __ j(kEqual, &done);
3279 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3280 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3281 __ Bind(&done);
3282}
3283
Mark P Mendell73945692015-04-29 14:56:17 +00003284void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3285 Register low = loc.AsRegisterPairLow<Register>();
3286 Register high = loc.AsRegisterPairHigh<Register>();
3287 if (shift == 32) {
3288 // Need to copy the sign.
3289 DCHECK_NE(low, high);
3290 __ movl(low, high);
3291 __ sarl(high, Immediate(31));
3292 } else if (shift > 32) {
3293 DCHECK_NE(low, high);
3294 // High part becomes sign. Low part is shifted by shift - 32.
3295 __ movl(low, high);
3296 __ sarl(high, Immediate(31));
3297 __ sarl(low, Immediate(shift - 32));
3298 } else {
3299 // Between 1 and 31.
3300 __ shrd(low, high, Immediate(shift));
3301 __ sarl(high, Immediate(shift));
3302 }
3303}
3304
Calin Juravle9aec02f2014-11-18 23:06:35 +00003305void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003306 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003307 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3308 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3309 __ testl(shifter, Immediate(32));
3310 __ j(kEqual, &done);
3311 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3312 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3313 __ Bind(&done);
3314}
3315
Mark P Mendell73945692015-04-29 14:56:17 +00003316void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3317 Register low = loc.AsRegisterPairLow<Register>();
3318 Register high = loc.AsRegisterPairHigh<Register>();
3319 if (shift == 32) {
3320 // Shift by 32 is easy. Low gets high, and high gets 0.
3321 codegen_->EmitParallelMoves(
3322 loc.ToHigh(),
3323 loc.ToLow(),
3324 Primitive::kPrimInt,
3325 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3326 loc.ToHigh(),
3327 Primitive::kPrimInt);
3328 } else if (shift > 32) {
3329 // Low part is high >> (shift - 32). High part becomes 0.
3330 __ movl(low, high);
3331 __ shrl(low, Immediate(shift - 32));
3332 __ xorl(high, high);
3333 } else {
3334 // Between 1 and 31.
3335 __ shrd(low, high, Immediate(shift));
3336 __ shrl(high, Immediate(shift));
3337 }
3338}
3339
Calin Juravle9aec02f2014-11-18 23:06:35 +00003340void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003341 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003342 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3343 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3344 __ testl(shifter, Immediate(32));
3345 __ j(kEqual, &done);
3346 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3347 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3348 __ Bind(&done);
3349}
3350
3351void LocationsBuilderX86::VisitShl(HShl* shl) {
3352 HandleShift(shl);
3353}
3354
3355void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3356 HandleShift(shl);
3357}
3358
3359void LocationsBuilderX86::VisitShr(HShr* shr) {
3360 HandleShift(shr);
3361}
3362
3363void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3364 HandleShift(shr);
3365}
3366
3367void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3368 HandleShift(ushr);
3369}
3370
3371void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3372 HandleShift(ushr);
3373}
3374
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003375void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003376 LocationSummary* locations =
3377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003378 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003379 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003380 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003381 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003382}
3383
3384void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3385 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003386 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003387 // Note: if heap poisoning is enabled, the entry point takes cares
3388 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003389 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3390 instruction,
3391 instruction->GetDexPc(),
3392 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003393 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003394}
3395
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003396void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3397 LocationSummary* locations =
3398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3399 locations->SetOut(Location::RegisterLocation(EAX));
3400 InvokeRuntimeCallingConvention calling_convention;
3401 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003402 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003403 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003404}
3405
3406void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3407 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003408 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3409
Roland Levillain4d027112015-07-01 15:41:14 +01003410 // Note: if heap poisoning is enabled, the entry point takes cares
3411 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003412 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3413 instruction,
3414 instruction->GetDexPc(),
3415 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003416 DCHECK(!codegen_->IsLeafMethod());
3417}
3418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003419void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003420 LocationSummary* locations =
3421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003422 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3423 if (location.IsStackSlot()) {
3424 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3425 } else if (location.IsDoubleStackSlot()) {
3426 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003427 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003428 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003429}
3430
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003431void InstructionCodeGeneratorX86::VisitParameterValue(
3432 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3433}
3434
3435void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3436 LocationSummary* locations =
3437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3438 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3439}
3440
3441void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003442}
3443
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003444void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003445 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003446 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003447 locations->SetInAt(0, Location::RequiresRegister());
3448 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003449}
3450
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003451void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3452 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003453 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003454 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003455 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003456 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003457 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003458 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003459 break;
3460
3461 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003462 __ notl(out.AsRegisterPairLow<Register>());
3463 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003464 break;
3465
3466 default:
3467 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3468 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003469}
3470
David Brazdil66d126e2015-04-03 16:02:44 +01003471void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3472 LocationSummary* locations =
3473 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3474 locations->SetInAt(0, Location::RequiresRegister());
3475 locations->SetOut(Location::SameAsFirstInput());
3476}
3477
3478void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003479 LocationSummary* locations = bool_not->GetLocations();
3480 Location in = locations->InAt(0);
3481 Location out = locations->Out();
3482 DCHECK(in.Equals(out));
3483 __ xorl(out.AsRegister<Register>(), Immediate(1));
3484}
3485
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003486void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003489 switch (compare->InputAt(0)->GetType()) {
3490 case Primitive::kPrimLong: {
3491 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003492 locations->SetInAt(1, Location::Any());
3493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3494 break;
3495 }
3496 case Primitive::kPrimFloat:
3497 case Primitive::kPrimDouble: {
3498 locations->SetInAt(0, Location::RequiresFpuRegister());
3499 locations->SetInAt(1, Location::RequiresFpuRegister());
3500 locations->SetOut(Location::RequiresRegister());
3501 break;
3502 }
3503 default:
3504 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3505 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003506}
3507
3508void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003509 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003510 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003511 Location left = locations->InAt(0);
3512 Location right = locations->InAt(1);
3513
Mark Mendell0c9497d2015-08-21 09:30:05 -04003514 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003515 switch (compare->InputAt(0)->GetType()) {
3516 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003517 Register left_low = left.AsRegisterPairLow<Register>();
3518 Register left_high = left.AsRegisterPairHigh<Register>();
3519 int32_t val_low = 0;
3520 int32_t val_high = 0;
3521 bool right_is_const = false;
3522
3523 if (right.IsConstant()) {
3524 DCHECK(right.GetConstant()->IsLongConstant());
3525 right_is_const = true;
3526 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3527 val_low = Low32Bits(val);
3528 val_high = High32Bits(val);
3529 }
3530
Calin Juravleddb7df22014-11-25 20:56:51 +00003531 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003532 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003533 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003534 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003535 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003536 DCHECK(right_is_const) << right;
3537 if (val_high == 0) {
3538 __ testl(left_high, left_high);
3539 } else {
3540 __ cmpl(left_high, Immediate(val_high));
3541 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003542 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003543 __ j(kLess, &less); // Signed compare.
3544 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003545 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003546 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003547 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003548 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003549 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003550 DCHECK(right_is_const) << right;
3551 if (val_low == 0) {
3552 __ testl(left_low, left_low);
3553 } else {
3554 __ cmpl(left_low, Immediate(val_low));
3555 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003556 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003557 break;
3558 }
3559 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003560 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003561 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3562 break;
3563 }
3564 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003565 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003566 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003567 break;
3568 }
3569 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003570 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003571 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003572 __ movl(out, Immediate(0));
3573 __ j(kEqual, &done);
3574 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3575
3576 __ Bind(&greater);
3577 __ movl(out, Immediate(1));
3578 __ jmp(&done);
3579
3580 __ Bind(&less);
3581 __ movl(out, Immediate(-1));
3582
3583 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003584}
3585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003586void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003587 LocationSummary* locations =
3588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003589 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3590 locations->SetInAt(i, Location::Any());
3591 }
3592 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003593}
3594
3595void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003596 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003597 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003598}
3599
Calin Juravle52c48962014-12-16 17:02:57 +00003600void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3601 /*
3602 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3603 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3604 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3605 */
3606 switch (kind) {
3607 case MemBarrierKind::kAnyAny: {
3608 __ mfence();
3609 break;
3610 }
3611 case MemBarrierKind::kAnyStore:
3612 case MemBarrierKind::kLoadAny:
3613 case MemBarrierKind::kStoreStore: {
3614 // nop
3615 break;
3616 }
3617 default:
3618 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003619 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003620}
3621
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003622
Vladimir Marko58155012015-08-19 12:49:41 +00003623void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3624 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3625 switch (invoke->GetMethodLoadKind()) {
3626 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3627 // temp = thread->string_init_entrypoint
3628 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3629 break;
3630 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3631 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3632 break;
3633 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3634 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3635 break;
3636 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3637 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3638 method_patches_.emplace_back(invoke->GetTargetMethod());
3639 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3640 break;
3641 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3642 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3643 FALLTHROUGH_INTENDED;
3644 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3645 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3646 Register method_reg;
3647 Register reg = temp.AsRegister<Register>();
3648 if (current_method.IsRegister()) {
3649 method_reg = current_method.AsRegister<Register>();
3650 } else {
3651 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3652 DCHECK(!current_method.IsValid());
3653 method_reg = reg;
3654 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3655 }
3656 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003657 __ movl(reg, Address(method_reg,
3658 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003659 // temp = temp[index_in_cache]
3660 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3661 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3662 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003663 }
Vladimir Marko58155012015-08-19 12:49:41 +00003664 }
3665
3666 switch (invoke->GetCodePtrLocation()) {
3667 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3668 __ call(GetFrameEntryLabel());
3669 break;
3670 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3671 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3672 Label* label = &relative_call_patches_.back().label;
3673 __ call(label); // Bind to the patch label, override at link time.
3674 __ Bind(label); // Bind the label at the end of the "call" insn.
3675 break;
3676 }
3677 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3678 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3679 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3680 // (Though the direct CALL ptr16:32 is available for consideration).
3681 FALLTHROUGH_INTENDED;
3682 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3683 // (callee_method + offset_of_quick_compiled_code)()
3684 __ call(Address(callee_method.AsRegister<Register>(),
3685 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3686 kX86WordSize).Int32Value()));
3687 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003688 }
3689
3690 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003691}
3692
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003693void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3694 Register temp = temp_in.AsRegister<Register>();
3695 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3696 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3697 LocationSummary* locations = invoke->GetLocations();
3698 Location receiver = locations->InAt(0);
3699 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3700 // temp = object->GetClass();
3701 DCHECK(receiver.IsRegister());
3702 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3703 MaybeRecordImplicitNullCheck(invoke);
3704 __ MaybeUnpoisonHeapReference(temp);
3705 // temp = temp->GetMethodAt(method_offset);
3706 __ movl(temp, Address(temp, method_offset));
3707 // call temp->GetEntryPoint();
3708 __ call(Address(
3709 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3710}
3711
Vladimir Marko58155012015-08-19 12:49:41 +00003712void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3713 DCHECK(linker_patches->empty());
3714 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3715 for (const MethodPatchInfo<Label>& info : method_patches_) {
3716 // The label points to the end of the "movl" insn but the literal offset for method
3717 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3718 uint32_t literal_offset = info.label.Position() - 4;
3719 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3720 info.target_method.dex_file,
3721 info.target_method.dex_method_index));
3722 }
3723 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3724 // The label points to the end of the "call" insn but the literal offset for method
3725 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3726 uint32_t literal_offset = info.label.Position() - 4;
3727 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3728 info.target_method.dex_file,
3729 info.target_method.dex_method_index));
3730 }
3731}
3732
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003733void CodeGeneratorX86::MarkGCCard(Register temp,
3734 Register card,
3735 Register object,
3736 Register value,
3737 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003738 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003739 if (value_can_be_null) {
3740 __ testl(value, value);
3741 __ j(kEqual, &is_null);
3742 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003743 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3744 __ movl(temp, object);
3745 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003746 __ movb(Address(temp, card, TIMES_1, 0),
3747 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003748 if (value_can_be_null) {
3749 __ Bind(&is_null);
3750 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003751}
3752
Calin Juravle52c48962014-12-16 17:02:57 +00003753void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3754 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003755 LocationSummary* locations =
3756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003757 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003758
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003759 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3760 locations->SetOut(Location::RequiresFpuRegister());
3761 } else {
3762 // The output overlaps in case of long: we don't want the low move to overwrite
3763 // the object's location.
3764 locations->SetOut(Location::RequiresRegister(),
3765 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3766 : Location::kNoOutputOverlap);
3767 }
Calin Juravle52c48962014-12-16 17:02:57 +00003768
3769 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3770 // Long values can be loaded atomically into an XMM using movsd.
3771 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3772 // and then copy the XMM into the output 32bits at a time).
3773 locations->AddTemp(Location::RequiresFpuRegister());
3774 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003775}
3776
Calin Juravle52c48962014-12-16 17:02:57 +00003777void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3778 const FieldInfo& field_info) {
3779 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003780
Calin Juravle52c48962014-12-16 17:02:57 +00003781 LocationSummary* locations = instruction->GetLocations();
3782 Register base = locations->InAt(0).AsRegister<Register>();
3783 Location out = locations->Out();
3784 bool is_volatile = field_info.IsVolatile();
3785 Primitive::Type field_type = field_info.GetFieldType();
3786 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3787
3788 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003789 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003790 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003791 break;
3792 }
3793
3794 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003795 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003796 break;
3797 }
3798
3799 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003800 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003801 break;
3802 }
3803
3804 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003805 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003806 break;
3807 }
3808
3809 case Primitive::kPrimInt:
3810 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003811 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003812 break;
3813 }
3814
3815 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003816 if (is_volatile) {
3817 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3818 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003820 __ movd(out.AsRegisterPairLow<Register>(), temp);
3821 __ psrlq(temp, Immediate(32));
3822 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3823 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003824 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003825 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003826 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003827 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3828 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003829 break;
3830 }
3831
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003832 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003833 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003834 break;
3835 }
3836
3837 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003838 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003839 break;
3840 }
3841
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003842 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003843 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003844 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003845 }
Calin Juravle52c48962014-12-16 17:02:57 +00003846
Calin Juravle77520bc2015-01-12 18:45:46 +00003847 // Longs are handled in the switch.
3848 if (field_type != Primitive::kPrimLong) {
3849 codegen_->MaybeRecordImplicitNullCheck(instruction);
3850 }
3851
Calin Juravle52c48962014-12-16 17:02:57 +00003852 if (is_volatile) {
3853 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3854 }
Roland Levillain4d027112015-07-01 15:41:14 +01003855
3856 if (field_type == Primitive::kPrimNot) {
3857 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3858 }
Calin Juravle52c48962014-12-16 17:02:57 +00003859}
3860
3861void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3862 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3863
3864 LocationSummary* locations =
3865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3866 locations->SetInAt(0, Location::RequiresRegister());
3867 bool is_volatile = field_info.IsVolatile();
3868 Primitive::Type field_type = field_info.GetFieldType();
3869 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3870 || (field_type == Primitive::kPrimByte);
3871
3872 // The register allocator does not support multiple
3873 // inputs that die at entry with one in a specific register.
3874 if (is_byte_type) {
3875 // Ensure the value is in a byte register.
3876 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003877 } else if (Primitive::IsFloatingPointType(field_type)) {
3878 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003879 } else {
3880 locations->SetInAt(1, Location::RequiresRegister());
3881 }
Calin Juravle52c48962014-12-16 17:02:57 +00003882 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003883 // Temporary registers for the write barrier.
3884 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003885 // Ensure the card is in a byte register.
3886 locations->AddTemp(Location::RegisterLocation(ECX));
3887 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3888 // 64bits value can be atomically written to an address with movsd and an XMM register.
3889 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3890 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3891 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3892 // isolated cases when we need this it isn't worth adding the extra complexity.
3893 locations->AddTemp(Location::RequiresFpuRegister());
3894 locations->AddTemp(Location::RequiresFpuRegister());
3895 }
3896}
3897
3898void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003899 const FieldInfo& field_info,
3900 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003901 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3902
3903 LocationSummary* locations = instruction->GetLocations();
3904 Register base = locations->InAt(0).AsRegister<Register>();
3905 Location value = locations->InAt(1);
3906 bool is_volatile = field_info.IsVolatile();
3907 Primitive::Type field_type = field_info.GetFieldType();
3908 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003909 bool needs_write_barrier =
3910 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003911
3912 if (is_volatile) {
3913 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3914 }
3915
3916 switch (field_type) {
3917 case Primitive::kPrimBoolean:
3918 case Primitive::kPrimByte: {
3919 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3920 break;
3921 }
3922
3923 case Primitive::kPrimShort:
3924 case Primitive::kPrimChar: {
3925 __ movw(Address(base, offset), value.AsRegister<Register>());
3926 break;
3927 }
3928
3929 case Primitive::kPrimInt:
3930 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003931 if (kPoisonHeapReferences && needs_write_barrier) {
3932 // Note that in the case where `value` is a null reference,
3933 // we do not enter this block, as the reference does not
3934 // need poisoning.
3935 DCHECK_EQ(field_type, Primitive::kPrimNot);
3936 Register temp = locations->GetTemp(0).AsRegister<Register>();
3937 __ movl(temp, value.AsRegister<Register>());
3938 __ PoisonHeapReference(temp);
3939 __ movl(Address(base, offset), temp);
3940 } else {
3941 __ movl(Address(base, offset), value.AsRegister<Register>());
3942 }
Calin Juravle52c48962014-12-16 17:02:57 +00003943 break;
3944 }
3945
3946 case Primitive::kPrimLong: {
3947 if (is_volatile) {
3948 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3949 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3950 __ movd(temp1, value.AsRegisterPairLow<Register>());
3951 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3952 __ punpckldq(temp1, temp2);
3953 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003955 } else {
3956 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003958 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3959 }
3960 break;
3961 }
3962
3963 case Primitive::kPrimFloat: {
3964 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3965 break;
3966 }
3967
3968 case Primitive::kPrimDouble: {
3969 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3970 break;
3971 }
3972
3973 case Primitive::kPrimVoid:
3974 LOG(FATAL) << "Unreachable type " << field_type;
3975 UNREACHABLE();
3976 }
3977
Calin Juravle77520bc2015-01-12 18:45:46 +00003978 // Longs are handled in the switch.
3979 if (field_type != Primitive::kPrimLong) {
3980 codegen_->MaybeRecordImplicitNullCheck(instruction);
3981 }
3982
Roland Levillain4d027112015-07-01 15:41:14 +01003983 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003984 Register temp = locations->GetTemp(0).AsRegister<Register>();
3985 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003986 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003987 }
3988
Calin Juravle52c48962014-12-16 17:02:57 +00003989 if (is_volatile) {
3990 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3991 }
3992}
3993
3994void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3995 HandleFieldGet(instruction, instruction->GetFieldInfo());
3996}
3997
3998void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3999 HandleFieldGet(instruction, instruction->GetFieldInfo());
4000}
4001
4002void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4003 HandleFieldSet(instruction, instruction->GetFieldInfo());
4004}
4005
4006void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004007 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004008}
4009
4010void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4011 HandleFieldSet(instruction, instruction->GetFieldInfo());
4012}
4013
4014void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004015 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004016}
4017
4018void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4019 HandleFieldGet(instruction, instruction->GetFieldInfo());
4020}
4021
4022void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4023 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004024}
4025
4026void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004027 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4028 ? LocationSummary::kCallOnSlowPath
4029 : LocationSummary::kNoCall;
4030 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4031 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004032 ? Location::RequiresRegister()
4033 : Location::Any();
4034 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004035 if (instruction->HasUses()) {
4036 locations->SetOut(Location::SameAsFirstInput());
4037 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004038}
4039
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004040void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004041 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4042 return;
4043 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004044 LocationSummary* locations = instruction->GetLocations();
4045 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004046
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004047 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4048 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4049}
4050
4051void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004052 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004053 codegen_->AddSlowPath(slow_path);
4054
4055 LocationSummary* locations = instruction->GetLocations();
4056 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057
4058 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004059 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004060 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004061 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004062 } else {
4063 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004064 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004065 __ jmp(slow_path->GetEntryLabel());
4066 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004067 }
4068 __ j(kEqual, slow_path->GetEntryLabel());
4069}
4070
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004071void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004072 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004073 GenerateImplicitNullCheck(instruction);
4074 } else {
4075 GenerateExplicitNullCheck(instruction);
4076 }
4077}
4078
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004080 LocationSummary* locations =
4081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004082 locations->SetInAt(0, Location::RequiresRegister());
4083 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004084 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4085 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4086 } else {
4087 // The output overlaps in case of long: we don't want the low move to overwrite
4088 // the array's location.
4089 locations->SetOut(Location::RequiresRegister(),
4090 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4091 : Location::kNoOutputOverlap);
4092 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004093}
4094
4095void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4096 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098 Location index = locations->InAt(1);
4099
Calin Juravle77520bc2015-01-12 18:45:46 +00004100 Primitive::Type type = instruction->GetType();
4101 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004102 case Primitive::kPrimBoolean: {
4103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004104 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004105 if (index.IsConstant()) {
4106 __ movzxb(out, Address(obj,
4107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004109 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004110 }
4111 break;
4112 }
4113
4114 case Primitive::kPrimByte: {
4115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004116 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004117 if (index.IsConstant()) {
4118 __ movsxb(out, Address(obj,
4119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004121 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 }
4123 break;
4124 }
4125
4126 case Primitive::kPrimShort: {
4127 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 if (index.IsConstant()) {
4130 __ movsxw(out, Address(obj,
4131 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4132 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004134 }
4135 break;
4136 }
4137
4138 case Primitive::kPrimChar: {
4139 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004140 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 if (index.IsConstant()) {
4142 __ movzxw(out, Address(obj,
4143 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4144 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004145 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004146 }
4147 break;
4148 }
4149
4150 case Primitive::kPrimInt:
4151 case Primitive::kPrimNot: {
4152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004154 if (index.IsConstant()) {
4155 __ movl(out, Address(obj,
4156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4157 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004159 }
4160 break;
4161 }
4162
4163 case Primitive::kPrimLong: {
4164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004165 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004166 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 if (index.IsConstant()) {
4168 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004169 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004170 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004171 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004173 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004176 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004177 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004178 }
4179 break;
4180 }
4181
Mark Mendell7c8d0092015-01-26 11:21:33 -05004182 case Primitive::kPrimFloat: {
4183 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4184 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4185 if (index.IsConstant()) {
4186 __ movss(out, Address(obj,
4187 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4188 } else {
4189 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4190 }
4191 break;
4192 }
4193
4194 case Primitive::kPrimDouble: {
4195 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4196 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4197 if (index.IsConstant()) {
4198 __ movsd(out, Address(obj,
4199 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4200 } else {
4201 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4202 }
4203 break;
4204 }
4205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004206 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004207 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004208 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004209 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004210
4211 if (type != Primitive::kPrimLong) {
4212 codegen_->MaybeRecordImplicitNullCheck(instruction);
4213 }
Roland Levillain4d027112015-07-01 15:41:14 +01004214
4215 if (type == Primitive::kPrimNot) {
4216 Register out = locations->Out().AsRegister<Register>();
4217 __ MaybeUnpoisonHeapReference(out);
4218 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004219}
4220
4221void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004222 // This location builder might end up asking to up to four registers, which is
4223 // not currently possible for baseline. The situation in which we need four
4224 // registers cannot be met by baseline though, because it has not run any
4225 // optimization.
4226
Nicolas Geoffray39468442014-09-02 15:17:15 +01004227 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004228 bool needs_write_barrier =
4229 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4230
Mark Mendell5f874182015-03-04 15:42:45 -05004231 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004232
Nicolas Geoffray39468442014-09-02 15:17:15 +01004233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4234 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004235 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004236
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004237 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004238 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4240 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4241 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004243 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4244 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004245 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004246 // In case of a byte operation, the register allocator does not support multiple
4247 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004250 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004251 // Ensure the value is in a byte register.
4252 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004253 } else if (Primitive::IsFloatingPointType(value_type)) {
4254 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004256 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004258 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004259 // Temporary registers for the write barrier.
4260 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004261 // Ensure the card is in a byte register.
4262 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004264 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265}
4266
4267void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4268 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004269 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004271 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004272 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004273 bool needs_runtime_call = locations->WillCall();
4274 bool needs_write_barrier =
4275 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004276
4277 switch (value_type) {
4278 case Primitive::kPrimBoolean:
4279 case Primitive::kPrimByte: {
4280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 if (index.IsConstant()) {
4282 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004283 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004284 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004285 } else {
4286 __ movb(Address(obj, offset),
4287 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4288 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004289 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004290 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004292 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004293 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004294 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004295 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4296 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004299 break;
4300 }
4301
4302 case Primitive::kPrimShort:
4303 case Primitive::kPrimChar: {
4304 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 if (index.IsConstant()) {
4306 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004307 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004308 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004309 } else {
4310 __ movw(Address(obj, offset),
4311 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004313 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004314 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004315 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4316 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004317 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004318 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004319 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4320 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004322 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323 break;
4324 }
4325
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004326 case Primitive::kPrimInt:
4327 case Primitive::kPrimNot: {
4328 if (!needs_runtime_call) {
4329 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4330 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004331 size_t offset =
4332 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004333 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004334 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4335 Register temp = locations->GetTemp(0).AsRegister<Register>();
4336 __ movl(temp, value.AsRegister<Register>());
4337 __ PoisonHeapReference(temp);
4338 __ movl(Address(obj, offset), temp);
4339 } else {
4340 __ movl(Address(obj, offset), value.AsRegister<Register>());
4341 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004342 } else {
4343 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004344 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4345 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4346 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4347 // Note: if heap poisoning is enabled, no need to poison
4348 // (negate) `v` if it is a reference, as it would be null.
4349 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004350 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004351 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004352 DCHECK(index.IsRegister()) << index;
4353 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004354 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4355 Register temp = locations->GetTemp(0).AsRegister<Register>();
4356 __ movl(temp, value.AsRegister<Register>());
4357 __ PoisonHeapReference(temp);
4358 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4359 } else {
4360 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4361 value.AsRegister<Register>());
4362 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004363 } else {
4364 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004365 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4366 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4367 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4368 // Note: if heap poisoning is enabled, no need to poison
4369 // (negate) `v` if it is a reference, as it would be null.
4370 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004371 }
4372 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004373 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004374
4375 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004376 Register temp = locations->GetTemp(0).AsRegister<Register>();
4377 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004378 codegen_->MarkGCCard(
4379 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004380 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004381 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004382 DCHECK_EQ(value_type, Primitive::kPrimNot);
4383 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004384 // Note: if heap poisoning is enabled, pAputObject takes cares
4385 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004386 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4387 instruction,
4388 instruction->GetDexPc(),
4389 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390 }
4391 break;
4392 }
4393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394 case Primitive::kPrimLong: {
4395 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004396 if (index.IsConstant()) {
4397 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004398 if (value.IsRegisterPair()) {
4399 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004400 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004401 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004402 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004403 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004404 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4405 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004406 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004407 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4408 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004409 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004410 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004412 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004414 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004415 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004416 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004417 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004418 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004420 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004422 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004423 Immediate(High32Bits(val)));
4424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 }
4426 break;
4427 }
4428
Mark Mendell7c8d0092015-01-26 11:21:33 -05004429 case Primitive::kPrimFloat: {
4430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4431 DCHECK(value.IsFpuRegister());
4432 if (index.IsConstant()) {
4433 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4434 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4435 } else {
4436 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4437 value.AsFpuRegister<XmmRegister>());
4438 }
4439 break;
4440 }
4441
4442 case Primitive::kPrimDouble: {
4443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4444 DCHECK(value.IsFpuRegister());
4445 if (index.IsConstant()) {
4446 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4447 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4448 } else {
4449 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4450 value.AsFpuRegister<XmmRegister>());
4451 }
4452 break;
4453 }
4454
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455 case Primitive::kPrimVoid:
4456 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004457 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458 }
4459}
4460
4461void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004463 locations->SetInAt(0, Location::RequiresRegister());
4464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465}
4466
4467void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4468 LocationSummary* locations = instruction->GetLocations();
4469 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004470 Register obj = locations->InAt(0).AsRegister<Register>();
4471 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004472 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004473 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474}
4475
4476void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004477 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4478 ? LocationSummary::kCallOnSlowPath
4479 : LocationSummary::kNoCall;
4480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004481 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004482 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004483 if (instruction->HasUses()) {
4484 locations->SetOut(Location::SameAsFirstInput());
4485 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004486}
4487
4488void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4489 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004490 Location index_loc = locations->InAt(0);
4491 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004492 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004493 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004494
Mark Mendell99dbd682015-04-22 16:18:52 -04004495 if (length_loc.IsConstant()) {
4496 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4497 if (index_loc.IsConstant()) {
4498 // BCE will remove the bounds check if we are guarenteed to pass.
4499 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4500 if (index < 0 || index >= length) {
4501 codegen_->AddSlowPath(slow_path);
4502 __ jmp(slow_path->GetEntryLabel());
4503 } else {
4504 // Some optimization after BCE may have generated this, and we should not
4505 // generate a bounds check if it is a valid range.
4506 }
4507 return;
4508 }
4509
4510 // We have to reverse the jump condition because the length is the constant.
4511 Register index_reg = index_loc.AsRegister<Register>();
4512 __ cmpl(index_reg, Immediate(length));
4513 codegen_->AddSlowPath(slow_path);
4514 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004515 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004516 Register length = length_loc.AsRegister<Register>();
4517 if (index_loc.IsConstant()) {
4518 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4519 __ cmpl(length, Immediate(value));
4520 } else {
4521 __ cmpl(length, index_loc.AsRegister<Register>());
4522 }
4523 codegen_->AddSlowPath(slow_path);
4524 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004525 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004526}
4527
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4529 temp->SetLocations(nullptr);
4530}
4531
4532void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4533 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004534 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535}
4536
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004537void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004538 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004539 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004540}
4541
4542void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004543 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4544}
4545
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004546void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4548}
4549
4550void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004551 HBasicBlock* block = instruction->GetBlock();
4552 if (block->GetLoopInformation() != nullptr) {
4553 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4554 // The back edge will generate the suspend check.
4555 return;
4556 }
4557 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4558 // The goto will generate the suspend check.
4559 return;
4560 }
4561 GenerateSuspendCheck(instruction, nullptr);
4562}
4563
4564void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4565 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004566 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004567 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4568 if (slow_path == nullptr) {
4569 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4570 instruction->SetSlowPath(slow_path);
4571 codegen_->AddSlowPath(slow_path);
4572 if (successor != nullptr) {
4573 DCHECK(successor->IsLoopHeader());
4574 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4575 }
4576 } else {
4577 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4578 }
4579
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004580 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004581 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004582 if (successor == nullptr) {
4583 __ j(kNotEqual, slow_path->GetEntryLabel());
4584 __ Bind(slow_path->GetReturnLabel());
4585 } else {
4586 __ j(kEqual, codegen_->GetLabelOf(successor));
4587 __ jmp(slow_path->GetEntryLabel());
4588 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004589}
4590
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004591X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4592 return codegen_->GetAssembler();
4593}
4594
Mark Mendell7c8d0092015-01-26 11:21:33 -05004595void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004596 ScratchRegisterScope ensure_scratch(
4597 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4598 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4599 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4600 __ movl(temp_reg, Address(ESP, src + stack_offset));
4601 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004602}
4603
4604void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004605 ScratchRegisterScope ensure_scratch(
4606 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4607 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4608 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4609 __ movl(temp_reg, Address(ESP, src + stack_offset));
4610 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4611 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4612 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004613}
4614
4615void ParallelMoveResolverX86::EmitMove(size_t index) {
4616 MoveOperands* move = moves_.Get(index);
4617 Location source = move->GetSource();
4618 Location destination = move->GetDestination();
4619
4620 if (source.IsRegister()) {
4621 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004622 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004623 } else {
4624 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004625 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004626 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004627 } else if (source.IsFpuRegister()) {
4628 if (destination.IsFpuRegister()) {
4629 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4630 } else if (destination.IsStackSlot()) {
4631 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4632 } else {
4633 DCHECK(destination.IsDoubleStackSlot());
4634 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4635 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004636 } else if (source.IsStackSlot()) {
4637 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004638 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004639 } else if (destination.IsFpuRegister()) {
4640 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004641 } else {
4642 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004643 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4644 }
4645 } else if (source.IsDoubleStackSlot()) {
4646 if (destination.IsFpuRegister()) {
4647 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4648 } else {
4649 DCHECK(destination.IsDoubleStackSlot()) << destination;
4650 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004651 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004652 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004653 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004654 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004655 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004656 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004657 if (value == 0) {
4658 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4659 } else {
4660 __ movl(destination.AsRegister<Register>(), Immediate(value));
4661 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004662 } else {
4663 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004664 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004665 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004666 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004667 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004668 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004669 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004670 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004671 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4672 if (value == 0) {
4673 // Easy handling of 0.0.
4674 __ xorps(dest, dest);
4675 } else {
4676 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004677 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4678 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4679 __ movl(temp, Immediate(value));
4680 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004681 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004682 } else {
4683 DCHECK(destination.IsStackSlot()) << destination;
4684 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4685 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004686 } else if (constant->IsLongConstant()) {
4687 int64_t value = constant->AsLongConstant()->GetValue();
4688 int32_t low_value = Low32Bits(value);
4689 int32_t high_value = High32Bits(value);
4690 Immediate low(low_value);
4691 Immediate high(high_value);
4692 if (destination.IsDoubleStackSlot()) {
4693 __ movl(Address(ESP, destination.GetStackIndex()), low);
4694 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4695 } else {
4696 __ movl(destination.AsRegisterPairLow<Register>(), low);
4697 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4698 }
4699 } else {
4700 DCHECK(constant->IsDoubleConstant());
4701 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004702 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004703 int32_t low_value = Low32Bits(value);
4704 int32_t high_value = High32Bits(value);
4705 Immediate low(low_value);
4706 Immediate high(high_value);
4707 if (destination.IsFpuRegister()) {
4708 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4709 if (value == 0) {
4710 // Easy handling of 0.0.
4711 __ xorpd(dest, dest);
4712 } else {
4713 __ pushl(high);
4714 __ pushl(low);
4715 __ movsd(dest, Address(ESP, 0));
4716 __ addl(ESP, Immediate(8));
4717 }
4718 } else {
4719 DCHECK(destination.IsDoubleStackSlot()) << destination;
4720 __ movl(Address(ESP, destination.GetStackIndex()), low);
4721 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4722 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004723 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004724 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004725 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004726 }
4727}
4728
Mark Mendella5c19ce2015-04-01 12:51:05 -04004729void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004730 Register suggested_scratch = reg == EAX ? EBX : EAX;
4731 ScratchRegisterScope ensure_scratch(
4732 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4733
4734 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4735 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4736 __ movl(Address(ESP, mem + stack_offset), reg);
4737 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004738}
4739
Mark Mendell7c8d0092015-01-26 11:21:33 -05004740void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004741 ScratchRegisterScope ensure_scratch(
4742 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4743
4744 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4745 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4746 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4747 __ movss(Address(ESP, mem + stack_offset), reg);
4748 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004749}
4750
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004751void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004752 ScratchRegisterScope ensure_scratch1(
4753 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004754
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004755 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4756 ScratchRegisterScope ensure_scratch2(
4757 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004758
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004759 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4760 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4761 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4762 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4763 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4764 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004765}
4766
4767void ParallelMoveResolverX86::EmitSwap(size_t index) {
4768 MoveOperands* move = moves_.Get(index);
4769 Location source = move->GetSource();
4770 Location destination = move->GetDestination();
4771
4772 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004773 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4774 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4775 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4776 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4777 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004778 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004779 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004780 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004781 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004782 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4783 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004784 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4785 // Use XOR Swap algorithm to avoid a temporary.
4786 DCHECK_NE(source.reg(), destination.reg());
4787 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4788 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4789 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4790 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4791 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4792 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4793 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004794 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4795 // Take advantage of the 16 bytes in the XMM register.
4796 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4797 Address stack(ESP, destination.GetStackIndex());
4798 // Load the double into the high doubleword.
4799 __ movhpd(reg, stack);
4800
4801 // Store the low double into the destination.
4802 __ movsd(stack, reg);
4803
4804 // Move the high double to the low double.
4805 __ psrldq(reg, Immediate(8));
4806 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4807 // Take advantage of the 16 bytes in the XMM register.
4808 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4809 Address stack(ESP, source.GetStackIndex());
4810 // Load the double into the high doubleword.
4811 __ movhpd(reg, stack);
4812
4813 // Store the low double into the destination.
4814 __ movsd(stack, reg);
4815
4816 // Move the high double to the low double.
4817 __ psrldq(reg, Immediate(8));
4818 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4819 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4820 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004821 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004822 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004823 }
4824}
4825
4826void ParallelMoveResolverX86::SpillScratch(int reg) {
4827 __ pushl(static_cast<Register>(reg));
4828}
4829
4830void ParallelMoveResolverX86::RestoreScratch(int reg) {
4831 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004832}
4833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004834void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004835 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4836 ? LocationSummary::kCallOnSlowPath
4837 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004838 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004839 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004840 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004841 locations->SetOut(Location::RequiresRegister());
4842}
4843
4844void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004845 LocationSummary* locations = cls->GetLocations();
4846 Register out = locations->Out().AsRegister<Register>();
4847 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004848 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004849 DCHECK(!cls->CanCallRuntime());
4850 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004851 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004852 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004853 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004854 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004855 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004856 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004857 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004858
Andreas Gampe85b62f22015-09-09 13:15:38 -07004859 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004860 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4861 codegen_->AddSlowPath(slow_path);
4862 __ testl(out, out);
4863 __ j(kEqual, slow_path->GetEntryLabel());
4864 if (cls->MustGenerateClinitCheck()) {
4865 GenerateClassInitializationCheck(slow_path, out);
4866 } else {
4867 __ Bind(slow_path->GetExitLabel());
4868 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004869 }
4870}
4871
4872void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4873 LocationSummary* locations =
4874 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4875 locations->SetInAt(0, Location::RequiresRegister());
4876 if (check->HasUses()) {
4877 locations->SetOut(Location::SameAsFirstInput());
4878 }
4879}
4880
4881void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004882 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004883 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004884 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004885 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004886 GenerateClassInitializationCheck(slow_path,
4887 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004888}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004889
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004890void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004891 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4893 Immediate(mirror::Class::kStatusInitialized));
4894 __ j(kLess, slow_path->GetEntryLabel());
4895 __ Bind(slow_path->GetExitLabel());
4896 // No need for memory fence, thanks to the X86 memory model.
4897}
4898
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004899void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4900 LocationSummary* locations =
4901 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004902 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004903 locations->SetOut(Location::RequiresRegister());
4904}
4905
4906void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004907 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004908 codegen_->AddSlowPath(slow_path);
4909
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004910 LocationSummary* locations = load->GetLocations();
4911 Register out = locations->Out().AsRegister<Register>();
4912 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004913 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004914 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004915 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004916 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004917 __ testl(out, out);
4918 __ j(kEqual, slow_path->GetEntryLabel());
4919 __ Bind(slow_path->GetExitLabel());
4920}
4921
David Brazdilcb1c0552015-08-04 16:22:25 +01004922static Address GetExceptionTlsAddress() {
4923 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4924}
4925
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004926void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4927 LocationSummary* locations =
4928 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4929 locations->SetOut(Location::RequiresRegister());
4930}
4931
4932void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004933 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4934}
4935
4936void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4937 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4938}
4939
4940void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4941 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004942}
4943
4944void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4945 LocationSummary* locations =
4946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4947 InvokeRuntimeCallingConvention calling_convention;
4948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4949}
4950
4951void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01004952 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4953 instruction,
4954 instruction->GetDexPc(),
4955 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004956}
4957
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004958void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray75374372015-09-17 17:12:19 +00004959 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4960 ? LocationSummary::kNoCall
4961 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004962 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray75374372015-09-17 17:12:19 +00004963 locations->SetInAt(0, Location::RequiresRegister());
4964 locations->SetInAt(1, Location::Any());
4965 // Note that TypeCheckSlowPathX86 uses this register too.
4966 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004967}
4968
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004969void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004970 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004971 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004972 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004973 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004974 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004975 NearLabel done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004976 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004977
4978 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004979 // Avoid null check if we know obj is not null.
4980 if (instruction->MustDoNullCheck()) {
4981 __ testl(obj, obj);
4982 __ j(kEqual, &zero);
4983 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004984 // Compare the class of `obj` with `cls`.
4985 __ movl(out, Address(obj, class_offset));
4986 __ MaybeUnpoisonHeapReference(out);
4987 if (cls.IsRegister()) {
4988 __ cmpl(out, cls.AsRegister<Register>());
4989 } else {
4990 DCHECK(cls.IsStackSlot()) << cls;
4991 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004992 }
4993
Nicolas Geoffray75374372015-09-17 17:12:19 +00004994 if (instruction->IsClassFinal()) {
4995 // Classes must be equal for the instanceof to succeed.
4996 __ j(kNotEqual, &zero);
4997 __ movl(out, Immediate(1));
4998 __ jmp(&done);
4999 } else {
5000 // If the classes are not equal, we go into a slow path.
5001 DCHECK(locations->OnlyCallsOnSlowPath());
5002 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
5003 codegen_->AddSlowPath(slow_path);
5004 __ j(kNotEqual, slow_path->GetEntryLabel());
5005 __ movl(out, Immediate(1));
5006 __ jmp(&done);
5007 }
5008
5009 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005010 __ Bind(&zero);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005011 __ movl(out, Immediate(0));
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005012 }
5013
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005014 if (slow_path != nullptr) {
5015 __ Bind(slow_path->GetExitLabel());
5016 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005017 __ Bind(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005018}
5019
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005020void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
5021 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray75374372015-09-17 17:12:19 +00005022 instruction, LocationSummary::kCallOnSlowPath);
5023 locations->SetInAt(0, Location::RequiresRegister());
5024 locations->SetInAt(1, Location::Any());
5025 // Note that TypeCheckSlowPathX86 uses this register too.
5026 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005027}
5028
5029void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5030 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005031 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005032 Location cls = locations->InAt(1);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005033 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005034 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005035 SlowPathCode* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005036 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005037
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005038 // Avoid null check if we know obj is not null.
5039 if (instruction->MustDoNullCheck()) {
5040 __ testl(obj, obj);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005041 __ j(kEqual, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005042 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005043 // Compare the class of `obj` with `cls`.
5044 __ movl(temp, Address(obj, class_offset));
5045 __ MaybeUnpoisonHeapReference(temp);
5046 if (cls.IsRegister()) {
5047 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005048 } else {
Nicolas Geoffray75374372015-09-17 17:12:19 +00005049 DCHECK(cls.IsStackSlot()) << cls;
5050 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005051 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005052 // The checkcast succeeds if the classes are equal (fast path).
5053 // Otherwise, we need to go into the slow path to check the types.
5054 __ j(kNotEqual, slow_path->GetEntryLabel());
5055 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005056}
5057
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005058void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5059 LocationSummary* locations =
5060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5061 InvokeRuntimeCallingConvention calling_convention;
5062 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5063}
5064
5065void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005066 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5067 : QUICK_ENTRY_POINT(pUnlockObject),
5068 instruction,
5069 instruction->GetDexPc(),
5070 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005071}
5072
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005073void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5074void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5075void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5076
5077void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5078 LocationSummary* locations =
5079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5080 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5081 || instruction->GetResultType() == Primitive::kPrimLong);
5082 locations->SetInAt(0, Location::RequiresRegister());
5083 locations->SetInAt(1, Location::Any());
5084 locations->SetOut(Location::SameAsFirstInput());
5085}
5086
5087void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5088 HandleBitwiseOperation(instruction);
5089}
5090
5091void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5092 HandleBitwiseOperation(instruction);
5093}
5094
5095void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5096 HandleBitwiseOperation(instruction);
5097}
5098
5099void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5100 LocationSummary* locations = instruction->GetLocations();
5101 Location first = locations->InAt(0);
5102 Location second = locations->InAt(1);
5103 DCHECK(first.Equals(locations->Out()));
5104
5105 if (instruction->GetResultType() == Primitive::kPrimInt) {
5106 if (second.IsRegister()) {
5107 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005108 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005109 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005110 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005111 } else {
5112 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005113 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005114 }
5115 } else if (second.IsConstant()) {
5116 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005117 __ andl(first.AsRegister<Register>(),
5118 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005119 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005120 __ orl(first.AsRegister<Register>(),
5121 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005122 } else {
5123 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005124 __ xorl(first.AsRegister<Register>(),
5125 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005126 }
5127 } else {
5128 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005130 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005132 } else {
5133 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005134 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005135 }
5136 }
5137 } else {
5138 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5139 if (second.IsRegisterPair()) {
5140 if (instruction->IsAnd()) {
5141 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5142 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5143 } else if (instruction->IsOr()) {
5144 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5145 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5146 } else {
5147 DCHECK(instruction->IsXor());
5148 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5149 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5150 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005151 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005152 if (instruction->IsAnd()) {
5153 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5154 __ andl(first.AsRegisterPairHigh<Register>(),
5155 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5156 } else if (instruction->IsOr()) {
5157 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5158 __ orl(first.AsRegisterPairHigh<Register>(),
5159 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5160 } else {
5161 DCHECK(instruction->IsXor());
5162 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5163 __ xorl(first.AsRegisterPairHigh<Register>(),
5164 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5165 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005166 } else {
5167 DCHECK(second.IsConstant()) << second;
5168 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005169 int32_t low_value = Low32Bits(value);
5170 int32_t high_value = High32Bits(value);
5171 Immediate low(low_value);
5172 Immediate high(high_value);
5173 Register first_low = first.AsRegisterPairLow<Register>();
5174 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005175 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005176 if (low_value == 0) {
5177 __ xorl(first_low, first_low);
5178 } else if (low_value != -1) {
5179 __ andl(first_low, low);
5180 }
5181 if (high_value == 0) {
5182 __ xorl(first_high, first_high);
5183 } else if (high_value != -1) {
5184 __ andl(first_high, high);
5185 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005186 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005187 if (low_value != 0) {
5188 __ orl(first_low, low);
5189 }
5190 if (high_value != 0) {
5191 __ orl(first_high, high);
5192 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005193 } else {
5194 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 if (low_value != 0) {
5196 __ xorl(first_low, low);
5197 }
5198 if (high_value != 0) {
5199 __ xorl(first_high, high);
5200 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005201 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005202 }
5203 }
5204}
5205
Calin Juravleb1498f62015-02-16 13:13:29 +00005206void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5207 // Nothing to do, this should be removed during prepare for register allocator.
5208 UNUSED(instruction);
5209 LOG(FATAL) << "Unreachable";
5210}
5211
5212void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5213 // Nothing to do, this should be removed during prepare for register allocator.
5214 UNUSED(instruction);
5215 LOG(FATAL) << "Unreachable";
5216}
5217
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005218void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5219 DCHECK(codegen_->IsBaseline());
5220 LocationSummary* locations =
5221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5222 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5223}
5224
5225void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5226 DCHECK(codegen_->IsBaseline());
5227 // Will be generated at use site.
5228}
5229
Mark Mendell0616ae02015-04-17 12:49:27 -04005230void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5231 HX86ComputeBaseMethodAddress* insn) {
5232 LocationSummary* locations =
5233 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5234 locations->SetOut(Location::RequiresRegister());
5235}
5236
5237void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5238 HX86ComputeBaseMethodAddress* insn) {
5239 LocationSummary* locations = insn->GetLocations();
5240 Register reg = locations->Out().AsRegister<Register>();
5241
5242 // Generate call to next instruction.
5243 Label next_instruction;
5244 __ call(&next_instruction);
5245 __ Bind(&next_instruction);
5246
5247 // Remember this offset for later use with constant area.
5248 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5249
5250 // Grab the return address off the stack.
5251 __ popl(reg);
5252}
5253
5254void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5255 HX86LoadFromConstantTable* insn) {
5256 LocationSummary* locations =
5257 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5258
5259 locations->SetInAt(0, Location::RequiresRegister());
5260 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5261
5262 // If we don't need to be materialized, we only need the inputs to be set.
5263 if (!insn->NeedsMaterialization()) {
5264 return;
5265 }
5266
5267 switch (insn->GetType()) {
5268 case Primitive::kPrimFloat:
5269 case Primitive::kPrimDouble:
5270 locations->SetOut(Location::RequiresFpuRegister());
5271 break;
5272
5273 case Primitive::kPrimInt:
5274 locations->SetOut(Location::RequiresRegister());
5275 break;
5276
5277 default:
5278 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5279 }
5280}
5281
5282void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5283 if (!insn->NeedsMaterialization()) {
5284 return;
5285 }
5286
5287 LocationSummary* locations = insn->GetLocations();
5288 Location out = locations->Out();
5289 Register const_area = locations->InAt(0).AsRegister<Register>();
5290 HConstant *value = insn->GetConstant();
5291
5292 switch (insn->GetType()) {
5293 case Primitive::kPrimFloat:
5294 __ movss(out.AsFpuRegister<XmmRegister>(),
5295 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5296 break;
5297
5298 case Primitive::kPrimDouble:
5299 __ movsd(out.AsFpuRegister<XmmRegister>(),
5300 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5301 break;
5302
5303 case Primitive::kPrimInt:
5304 __ movl(out.AsRegister<Register>(),
5305 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5306 break;
5307
5308 default:
5309 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5310 }
5311}
5312
5313void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5314 // Generate the constant area if needed.
5315 X86Assembler* assembler = GetAssembler();
5316 if (!assembler->IsConstantAreaEmpty()) {
5317 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5318 // byte values.
5319 assembler->Align(4, 0);
5320 constant_area_start_ = assembler->CodeSize();
5321 assembler->AddConstantArea();
5322 }
5323
5324 // And finish up.
5325 CodeGenerator::Finalize(allocator);
5326}
5327
5328/**
5329 * Class to handle late fixup of offsets into constant area.
5330 */
5331class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5332 public:
5333 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5334 : codegen_(codegen), offset_into_constant_area_(offset) {}
5335
5336 private:
5337 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5338 // Patch the correct offset for the instruction. The place to patch is the
5339 // last 4 bytes of the instruction.
5340 // The value to patch is the distance from the offset in the constant area
5341 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5342 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5343 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5344
5345 // Patch in the right value.
5346 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5347 }
5348
5349 const CodeGeneratorX86& codegen_;
5350
5351 // Location in constant area that the fixup refers to.
5352 int offset_into_constant_area_;
5353};
5354
5355Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5356 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5357 return Address(reg, kDummy32BitOffset, fixup);
5358}
5359
5360Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5361 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5362 return Address(reg, kDummy32BitOffset, fixup);
5363}
5364
5365Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5366 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5367 return Address(reg, kDummy32BitOffset, fixup);
5368}
5369
5370Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5371 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5372 return Address(reg, kDummy32BitOffset, fixup);
5373}
5374
5375/**
5376 * Finds instructions that need the constant area base as an input.
5377 */
5378class ConstantHandlerVisitor : public HGraphVisitor {
5379 public:
5380 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5381
5382 private:
5383 void VisitAdd(HAdd* add) OVERRIDE {
5384 BinaryFP(add);
5385 }
5386
5387 void VisitSub(HSub* sub) OVERRIDE {
5388 BinaryFP(sub);
5389 }
5390
5391 void VisitMul(HMul* mul) OVERRIDE {
5392 BinaryFP(mul);
5393 }
5394
5395 void VisitDiv(HDiv* div) OVERRIDE {
5396 BinaryFP(div);
5397 }
5398
5399 void VisitReturn(HReturn* ret) OVERRIDE {
5400 HConstant* value = ret->InputAt(0)->AsConstant();
5401 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5402 ReplaceInput(ret, value, 0, true);
5403 }
5404 }
5405
5406 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5407 HandleInvoke(invoke);
5408 }
5409
5410 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5411 HandleInvoke(invoke);
5412 }
5413
5414 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5415 HandleInvoke(invoke);
5416 }
5417
5418 void BinaryFP(HBinaryOperation* bin) {
5419 HConstant* rhs = bin->InputAt(1)->AsConstant();
5420 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5421 ReplaceInput(bin, rhs, 1, false);
5422 }
5423 }
5424
5425 void InitializeConstantAreaPointer(HInstruction* user) {
5426 // Ensure we only initialize the pointer once.
5427 if (base_ != nullptr) {
5428 return;
5429 }
5430
5431 HGraph* graph = GetGraph();
5432 HBasicBlock* entry = graph->GetEntryBlock();
5433 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5434 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5435 entry->InsertInstructionBefore(base_, insert_pos);
5436 DCHECK(base_ != nullptr);
5437 }
5438
5439 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5440 InitializeConstantAreaPointer(insn);
5441 HGraph* graph = GetGraph();
5442 HBasicBlock* block = insn->GetBlock();
5443 HX86LoadFromConstantTable* load_constant =
5444 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5445 block->InsertInstructionBefore(load_constant, insn);
5446 insn->ReplaceInput(load_constant, input_index);
5447 }
5448
5449 void HandleInvoke(HInvoke* invoke) {
5450 // Ensure that we can load FP arguments from the constant area.
5451 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5452 HConstant* input = invoke->InputAt(i)->AsConstant();
5453 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5454 ReplaceInput(invoke, input, i, true);
5455 }
5456 }
5457 }
5458
5459 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5460 // input to the HX86LoadFromConstantTable instructions.
5461 HX86ComputeBaseMethodAddress* base_;
5462};
5463
5464void ConstantAreaFixups::Run() {
5465 ConstantHandlerVisitor visitor(graph_);
5466 visitor.VisitInsertionOrder();
5467}
5468
Andreas Gampe85b62f22015-09-09 13:15:38 -07005469// TODO: target as memory.
5470void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5471 if (!target.IsValid()) {
5472 DCHECK(type == Primitive::kPrimVoid);
5473 return;
5474 }
5475
5476 DCHECK_NE(type, Primitive::kPrimVoid);
5477
5478 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
5479 if (target.Equals(return_loc)) {
5480 return;
5481 }
5482
5483 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5484 // with the else branch.
5485 if (type == Primitive::kPrimLong) {
5486 HParallelMove parallel_move(GetGraph()->GetArena());
5487 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
5488 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
5489 GetMoveResolver()->EmitNativeCode(&parallel_move);
5490 } else {
5491 // Let the parallel move resolver take care of all of this.
5492 HParallelMove parallel_move(GetGraph()->GetArena());
5493 parallel_move.AddMove(return_loc, target, type, nullptr);
5494 GetMoveResolver()->EmitNativeCode(&parallel_move);
5495 }
5496}
5497
Roland Levillain4d027112015-07-01 15:41:14 +01005498#undef __
5499
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005500} // namespace x86
5501} // namespace art