blob: 8b33f562950b2327f3dcf47fa36025491322a0b6 [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 Geoffray85c7bab2015-09-18 13:40:46 +0000290 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
291 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100295 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
296 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 DCHECK(instruction_->IsCheckCast()
298 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
301 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302
303 if (instruction_->IsCheckCast()) {
304 // The codegen for the instruction overwrites `temp`, so put it back in place.
305 Register obj = locations->InAt(0).AsRegister<Register>();
306 Register temp = locations->GetTemp(0).AsRegister<Register>();
307 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
308 __ movl(temp, Address(obj, class_offset));
309 __ MaybeUnpoisonHeapReference(temp);
310 }
311
312 if (!is_fatal_) {
313 SaveLiveRegisters(codegen, locations);
314 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
316 // We're moving two locations to locations that could overlap, so we need a parallel
317 // move resolver.
318 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000319 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
325 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100328 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
329 instruction_,
330 instruction_->GetDexPc(),
331 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 } else {
333 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 __ jmp(GetExitLabel());
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100352
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000354 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Andreas Gampe85b62f22015-09-09 13:15:38 -0700360class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 public:
362 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
363 : instruction_(instruction) {}
364
365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100366 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100367 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 __ Bind(GetEntryLabel());
369 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
371 instruction_,
372 instruction_->GetDexPc(),
373 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
379 HInstruction* const instruction_;
380 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
381};
382
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100383class ArraySetSlowPathX86 : public SlowPathCode {
384 public:
385 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
386
387 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
388 LocationSummary* locations = instruction_->GetLocations();
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 InvokeRuntimeCallingConvention calling_convention;
393 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
394 parallel_move.AddMove(
395 locations->InAt(0),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 nullptr);
399 parallel_move.AddMove(
400 locations->InAt(1),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
402 Primitive::kPrimInt,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(2),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
407 Primitive::kPrimNot,
408 nullptr);
409 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
410
411 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
412 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
423 HInstruction* const instruction_;
424
425 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
426};
427
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100429#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430
Aart Bike9f37602015-10-09 11:15:55 -0700431inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700432 switch (cond) {
433 case kCondEQ: return kEqual;
434 case kCondNE: return kNotEqual;
435 case kCondLT: return kLess;
436 case kCondLE: return kLessEqual;
437 case kCondGT: return kGreater;
438 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700439 case kCondB: return kBelow;
440 case kCondBE: return kBelowEqual;
441 case kCondA: return kAbove;
442 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700443 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100444 LOG(FATAL) << "Unreachable";
445 UNREACHABLE();
446}
447
Aart Bike9f37602015-10-09 11:15:55 -0700448// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100449inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
450 switch (cond) {
451 case kCondEQ: return kEqual;
452 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700453 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100454 case kCondLT: return kBelow;
455 case kCondLE: return kBelowEqual;
456 case kCondGT: return kAbove;
457 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700458 // Unsigned remain unchanged.
459 case kCondB: return kBelow;
460 case kCondBE: return kBelowEqual;
461 case kCondA: return kAbove;
462 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100463 }
464 LOG(FATAL) << "Unreachable";
465 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700466}
467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100468void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100469 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470}
471
472void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100473 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474}
475
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100476size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
477 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
478 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100479}
480
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100481size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
482 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
483 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100484}
485
Mark Mendell7c8d0092015-01-26 11:21:33 -0500486size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
487 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
488 return GetFloatingPointSpillSlotSize();
489}
490
491size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
492 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
493 return GetFloatingPointSpillSlotSize();
494}
495
Calin Juravle175dc732015-08-25 15:42:32 +0100496void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
497 HInstruction* instruction,
498 uint32_t dex_pc,
499 SlowPathCode* slow_path) {
500 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
501 instruction,
502 dex_pc,
503 slow_path);
504}
505
506void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100507 HInstruction* instruction,
508 uint32_t dex_pc,
509 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100510 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100511 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100512 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100513}
514
Mark Mendellfb8d2792015-03-31 22:16:59 -0400515CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
516 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100517 const CompilerOptions& compiler_options,
518 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500519 : CodeGenerator(graph,
520 kNumberOfCpuRegisters,
521 kNumberOfXmmRegisters,
522 kNumberOfRegisterPairs,
523 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
524 arraysize(kCoreCalleeSaves))
525 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100526 0,
527 compiler_options,
528 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100529 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100531 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400532 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000533 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100534 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400535 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
536 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000537 // Use a fake return address register to mimic Quick.
538 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100539}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100541Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 switch (type) {
543 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100544 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545 X86ManagedRegister pair =
546 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100547 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
548 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100549 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
550 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100551 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 case Primitive::kPrimByte:
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimChar:
558 case Primitive::kPrimShort:
559 case Primitive::kPrimInt:
560 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100561 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100562 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100563 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100564 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
565 X86ManagedRegister current =
566 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
567 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100568 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100569 }
570 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100572 }
573
574 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100576 return Location::FpuRegisterLocation(
577 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100579
580 case Primitive::kPrimVoid:
581 LOG(FATAL) << "Unreachable type " << type;
582 }
583
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100585}
586
Mark Mendell5f874182015-03-04 15:42:45 -0500587void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100589 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100590
591 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100592 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593
Mark Mendell5f874182015-03-04 15:42:45 -0500594 if (is_baseline) {
595 blocked_core_registers_[EBP] = true;
596 blocked_core_registers_[ESI] = true;
597 blocked_core_registers_[EDI] = true;
598 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100599
600 UpdateBlockedPairRegisters();
601}
602
603void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
604 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
605 X86ManagedRegister current =
606 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
607 if (blocked_core_registers_[current.AsRegisterPairLow()]
608 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
609 blocked_register_pairs_[i] = true;
610 }
611 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100612}
613
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100614InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
615 : HGraphVisitor(graph),
616 assembler_(codegen->GetAssembler()),
617 codegen_(codegen) {}
618
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100619static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100620 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100621}
622
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000623void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000625 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000626 bool skip_overflow_check =
627 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000628 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000629
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000630 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100631 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100632 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100633 }
634
Mark Mendell5f874182015-03-04 15:42:45 -0500635 if (HasEmptyFrame()) {
636 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000637 }
Mark Mendell5f874182015-03-04 15:42:45 -0500638
639 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
640 Register reg = kCoreCalleeSaves[i];
641 if (allocated_registers_.ContainsCoreRegister(reg)) {
642 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100643 __ cfi().AdjustCFAOffset(kX86WordSize);
644 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500645 }
646 }
647
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100648 int adjust = GetFrameSize() - FrameEntrySpillSize();
649 __ subl(ESP, Immediate(adjust));
650 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100651 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000652}
653
654void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100655 __ cfi().RememberState();
656 if (!HasEmptyFrame()) {
657 int adjust = GetFrameSize() - FrameEntrySpillSize();
658 __ addl(ESP, Immediate(adjust));
659 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500660
David Srbeckyc34dc932015-04-12 09:27:43 +0100661 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
662 Register reg = kCoreCalleeSaves[i];
663 if (allocated_registers_.ContainsCoreRegister(reg)) {
664 __ popl(reg);
665 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
666 __ cfi().Restore(DWARFReg(reg));
667 }
Mark Mendell5f874182015-03-04 15:42:45 -0500668 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000669 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100670 __ ret();
671 __ cfi().RestoreState();
672 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673}
674
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100675void CodeGeneratorX86::Bind(HBasicBlock* block) {
676 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000677}
678
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100679Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
680 switch (load->GetType()) {
681 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100683 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100684
685 case Primitive::kPrimInt:
686 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100687 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100689
690 case Primitive::kPrimBoolean:
691 case Primitive::kPrimByte:
692 case Primitive::kPrimChar:
693 case Primitive::kPrimShort:
694 case Primitive::kPrimVoid:
695 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700696 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100697 }
698
699 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700700 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100701}
702
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100703Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
704 switch (type) {
705 case Primitive::kPrimBoolean:
706 case Primitive::kPrimByte:
707 case Primitive::kPrimChar:
708 case Primitive::kPrimShort:
709 case Primitive::kPrimInt:
710 case Primitive::kPrimNot:
711 return Location::RegisterLocation(EAX);
712
713 case Primitive::kPrimLong:
714 return Location::RegisterPairLocation(EAX, EDX);
715
716 case Primitive::kPrimVoid:
717 return Location::NoLocation();
718
719 case Primitive::kPrimDouble:
720 case Primitive::kPrimFloat:
721 return Location::FpuRegisterLocation(XMM0);
722 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100723
724 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100725}
726
727Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
728 return Location::RegisterLocation(kMethodRegisterArgument);
729}
730
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100731Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100732 switch (type) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot: {
739 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000740 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100741 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100742 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100743 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000744 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100745 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100746 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100747
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000748 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100749 uint32_t index = gp_index_;
750 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000751 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100752 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100753 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
754 calling_convention.GetRegisterPairAt(index));
755 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100756 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000757 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
758 }
759 }
760
761 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100762 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000763 stack_index_++;
764 if (index < calling_convention.GetNumberOfFpuRegisters()) {
765 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
766 } else {
767 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
768 }
769 }
770
771 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100772 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000773 stack_index_ += 2;
774 if (index < calling_convention.GetNumberOfFpuRegisters()) {
775 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
776 } else {
777 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100778 }
779 }
780
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100781 case Primitive::kPrimVoid:
782 LOG(FATAL) << "Unexpected parameter type " << type;
783 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100784 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100785 return Location();
786}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788void CodeGeneratorX86::Move32(Location destination, Location source) {
789 if (source.Equals(destination)) {
790 return;
791 }
792 if (destination.IsRegister()) {
793 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000794 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000796 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 } else {
798 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 } else if (destination.IsFpuRegister()) {
802 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100804 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 } else {
807 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000808 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000811 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000813 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500816 } else if (source.IsConstant()) {
817 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000818 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500819 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 } else {
821 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100822 __ pushl(Address(ESP, source.GetStackIndex()));
823 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 }
825 }
826}
827
828void CodeGeneratorX86::Move64(Location destination, Location source) {
829 if (source.Equals(destination)) {
830 return;
831 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 if (destination.IsRegisterPair()) {
833 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000834 EmitParallelMoves(
835 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
836 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100837 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000838 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100839 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
840 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100841 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100842 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
843 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
844 __ psrlq(src_reg, Immediate(32));
845 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000847 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100849 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
850 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
852 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500854 if (source.IsFpuRegister()) {
855 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
856 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000857 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +0100858 } else if (source.IsRegisterPair()) {
859 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
860 // Create stack space for 2 elements.
861 __ subl(ESP, Immediate(2 * elem_size));
862 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
863 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
864 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
865 // And remove the temporary stack space we allocated.
866 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 } else {
868 LOG(FATAL) << "Unimplemented";
869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000871 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100872 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000873 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100874 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100876 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000879 } else if (source.IsConstant()) {
880 HConstant* constant = source.GetConstant();
881 int64_t value;
882 if (constant->IsLongConstant()) {
883 value = constant->AsLongConstant()->GetValue();
884 } else {
885 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000886 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000887 }
888 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
889 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000891 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000892 EmitParallelMoves(
893 Location::StackSlot(source.GetStackIndex()),
894 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100895 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000896 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100897 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
898 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899 }
900 }
901}
902
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100903void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000904 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100905 if (instruction->IsCurrentMethod()) {
906 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
907 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000908 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100909 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000910 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000911 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
912 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000913 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000914 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000915 } else if (location.IsStackSlot()) {
916 __ movl(Address(ESP, location.GetStackIndex()), imm);
917 } else {
918 DCHECK(location.IsConstant());
919 DCHECK_EQ(location.GetConstant(), const_to_move);
920 }
921 } else if (const_to_move->IsLongConstant()) {
922 int64_t value = const_to_move->AsLongConstant()->GetValue();
923 if (location.IsRegisterPair()) {
924 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
925 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
926 } else if (location.IsDoubleStackSlot()) {
927 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000928 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
929 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000930 } else {
931 DCHECK(location.IsConstant());
932 DCHECK_EQ(location.GetConstant(), instruction);
933 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000935 } else if (instruction->IsTemporary()) {
936 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000937 if (temp_location.IsStackSlot()) {
938 Move32(location, temp_location);
939 } else {
940 DCHECK(temp_location.IsDoubleStackSlot());
941 Move64(location, temp_location);
942 }
Roland Levillain476df552014-10-09 17:51:36 +0100943 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100944 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 switch (instruction->GetType()) {
946 case Primitive::kPrimBoolean:
947 case Primitive::kPrimByte:
948 case Primitive::kPrimChar:
949 case Primitive::kPrimShort:
950 case Primitive::kPrimInt:
951 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 case Primitive::kPrimFloat:
953 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954 break;
955
956 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100957 case Primitive::kPrimDouble:
958 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 break;
960
961 default:
962 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
963 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100965 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966 switch (instruction->GetType()) {
967 case Primitive::kPrimBoolean:
968 case Primitive::kPrimByte:
969 case Primitive::kPrimChar:
970 case Primitive::kPrimShort:
971 case Primitive::kPrimInt:
972 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000974 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 break;
976
977 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 break;
981
982 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000985 }
986}
987
Calin Juravle175dc732015-08-25 15:42:32 +0100988void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
989 DCHECK(location.IsRegister());
990 __ movl(location.AsRegister<Register>(), Immediate(value));
991}
992
Calin Juravlee460d1d2015-09-29 04:52:17 +0100993void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
994 if (Primitive::Is64BitType(dst_type)) {
995 Move64(dst, src);
996 } else {
997 Move32(dst, src);
998 }
999}
1000
1001void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1002 if (location.IsRegister()) {
1003 locations->AddTemp(location);
1004 } else if (location.IsRegisterPair()) {
1005 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1006 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1007 } else {
1008 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1009 }
1010}
1011
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001013 DCHECK(!successor->IsExitBlock());
1014
1015 HBasicBlock* block = got->GetBlock();
1016 HInstruction* previous = got->GetPrevious();
1017
1018 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001019 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001020 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1021 return;
1022 }
1023
1024 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1025 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1026 }
1027 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001029 }
1030}
1031
David Brazdilfc6a86a2015-06-26 10:33:45 +00001032void LocationsBuilderX86::VisitGoto(HGoto* got) {
1033 got->SetLocations(nullptr);
1034}
1035
1036void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1037 HandleGoto(got, got->GetSuccessor());
1038}
1039
1040void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1041 try_boundary->SetLocations(nullptr);
1042}
1043
1044void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1045 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1046 if (!successor->IsExitBlock()) {
1047 HandleGoto(try_boundary, successor);
1048 }
1049}
1050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053}
1054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001056 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001057}
1058
Mark Mendellc4701932015-04-10 13:18:51 -04001059void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1060 Label* true_label,
1061 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001062 if (cond->IsFPConditionTrueIfNaN()) {
1063 __ j(kUnordered, true_label);
1064 } else if (cond->IsFPConditionFalseIfNaN()) {
1065 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001066 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001067 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001068}
1069
1070void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1071 Label* true_label,
1072 Label* false_label) {
1073 LocationSummary* locations = cond->GetLocations();
1074 Location left = locations->InAt(0);
1075 Location right = locations->InAt(1);
1076 IfCondition if_cond = cond->GetCondition();
1077
Mark Mendellc4701932015-04-10 13:18:51 -04001078 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001079 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001080 IfCondition true_high_cond = if_cond;
1081 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001082 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001083
1084 // Set the conditions for the test, remembering that == needs to be
1085 // decided using the low words.
1086 switch (if_cond) {
1087 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001088 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001089 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001090 break;
1091 case kCondLT:
1092 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001093 break;
1094 case kCondLE:
1095 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001096 break;
1097 case kCondGT:
1098 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001099 break;
1100 case kCondGE:
1101 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001102 break;
Aart Bike9f37602015-10-09 11:15:55 -07001103 case kCondB:
1104 false_high_cond = kCondA;
1105 break;
1106 case kCondBE:
1107 true_high_cond = kCondB;
1108 break;
1109 case kCondA:
1110 false_high_cond = kCondB;
1111 break;
1112 case kCondAE:
1113 true_high_cond = kCondA;
1114 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001115 }
1116
1117 if (right.IsConstant()) {
1118 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001119 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001121
1122 if (val_high == 0) {
1123 __ testl(left_high, left_high);
1124 } else {
1125 __ cmpl(left_high, Immediate(val_high));
1126 }
1127 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001128 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001129 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001130 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001131 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001132 __ j(X86Condition(true_high_cond), true_label);
1133 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001134 }
1135 // Must be equal high, so compare the lows.
1136 if (val_low == 0) {
1137 __ testl(left_low, left_low);
1138 } else {
1139 __ cmpl(left_low, Immediate(val_low));
1140 }
1141 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001142 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001143 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001144
1145 __ cmpl(left_high, right_high);
1146 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001147 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001148 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001149 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001150 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001151 __ j(X86Condition(true_high_cond), true_label);
1152 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001153 }
1154 // Must be equal high, so compare the lows.
1155 __ cmpl(left_low, right_low);
1156 }
1157 // The last comparison might be unsigned.
1158 __ j(final_condition, true_label);
1159}
1160
1161void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1162 HCondition* condition,
1163 Label* true_target,
1164 Label* false_target,
1165 Label* always_true_target) {
1166 LocationSummary* locations = condition->GetLocations();
1167 Location left = locations->InAt(0);
1168 Location right = locations->InAt(1);
1169
1170 // We don't want true_target as a nullptr.
1171 if (true_target == nullptr) {
1172 true_target = always_true_target;
1173 }
1174 bool falls_through = (false_target == nullptr);
1175
1176 // FP compares don't like null false_targets.
1177 if (false_target == nullptr) {
1178 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1179 }
1180
1181 Primitive::Type type = condition->InputAt(0)->GetType();
1182 switch (type) {
1183 case Primitive::kPrimLong:
1184 GenerateLongComparesAndJumps(condition, true_target, false_target);
1185 break;
1186 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001187 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1188 GenerateFPJumps(condition, true_target, false_target);
1189 break;
1190 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001191 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1192 GenerateFPJumps(condition, true_target, false_target);
1193 break;
1194 default:
1195 LOG(FATAL) << "Unexpected compare type " << type;
1196 }
1197
1198 if (!falls_through) {
1199 __ jmp(false_target);
1200 }
1201}
1202
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001203void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1204 Label* true_target,
1205 Label* false_target,
1206 Label* always_true_target) {
1207 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001208 if (cond->IsIntConstant()) {
1209 // Constant condition, statically compared against 1.
1210 int32_t cond_value = cond->AsIntConstant()->GetValue();
1211 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001212 if (always_true_target != nullptr) {
1213 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001214 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001215 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001217 DCHECK_EQ(cond_value, 0);
1218 }
1219 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001220 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001221 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1222 // Moves do not affect the eflags register, so if the condition is
1223 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001224 // again. We can't use the eflags on long/FP conditions if they are
1225 // materialized due to the complex branching.
1226 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001227 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001228 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1230 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001231 if (!eflags_set) {
1232 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001233 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001234 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001235 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001236 } else {
1237 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1238 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001239 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001240 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001241 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001242 }
1243 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001244 // Condition has not been materialized, use its inputs as the
1245 // comparison and its condition as the branch condition.
1246
Mark Mendellc4701932015-04-10 13:18:51 -04001247 // Is this a long or FP comparison that has been folded into the HCondition?
1248 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1249 // Generate the comparison directly.
1250 GenerateCompareTestAndBranch(instruction->AsIf(),
1251 cond->AsCondition(),
1252 true_target,
1253 false_target,
1254 always_true_target);
1255 return;
1256 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001257
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001258 Location lhs = cond->GetLocations()->InAt(0);
1259 Location rhs = cond->GetLocations()->InAt(1);
1260 // LHS is guaranteed to be in a register (see
1261 // LocationsBuilderX86::VisitCondition).
1262 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001263 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001264 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001265 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001266 if (constant == 0) {
1267 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1268 } else {
1269 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1270 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001271 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001272 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001273 }
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001275 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001276 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001277 if (false_target != nullptr) {
1278 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 }
1280}
1281
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001282void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1283 LocationSummary* locations =
1284 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1285 HInstruction* cond = if_instr->InputAt(0);
1286 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1287 locations->SetInAt(0, Location::Any());
1288 }
1289}
1290
1291void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1292 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1293 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1294 Label* always_true_target = true_target;
1295 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1296 if_instr->IfTrueSuccessor())) {
1297 always_true_target = nullptr;
1298 }
1299 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1300 if_instr->IfFalseSuccessor())) {
1301 false_target = nullptr;
1302 }
1303 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1304}
1305
1306void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1307 LocationSummary* locations = new (GetGraph()->GetArena())
1308 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1309 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001310 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001311 locations->SetInAt(0, Location::Any());
1312 }
1313}
1314
1315void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001316 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001317 DeoptimizationSlowPathX86(deoptimize);
1318 codegen_->AddSlowPath(slow_path);
1319 Label* slow_path_entry = slow_path->GetEntryLabel();
1320 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1321}
1322
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001325}
1326
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001327void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1328 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001329}
1330
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001332 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333}
1334
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001335void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001336 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001337 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001338}
1339
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001340void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001341 LocationSummary* locations =
1342 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001343 switch (store->InputAt(1)->GetType()) {
1344 case Primitive::kPrimBoolean:
1345 case Primitive::kPrimByte:
1346 case Primitive::kPrimChar:
1347 case Primitive::kPrimShort:
1348 case Primitive::kPrimInt:
1349 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001350 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001351 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1352 break;
1353
1354 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001355 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001356 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1357 break;
1358
1359 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001360 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001361 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001362}
1363
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001364void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001365 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366}
1367
Roland Levillain0d37cd02015-05-27 16:39:19 +01001368void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001369 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001370 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001371 // Handle the long/FP comparisons made in instruction simplification.
1372 switch (cond->InputAt(0)->GetType()) {
1373 case Primitive::kPrimLong: {
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1376 if (cond->NeedsMaterialization()) {
1377 locations->SetOut(Location::RequiresRegister());
1378 }
1379 break;
1380 }
1381 case Primitive::kPrimFloat:
1382 case Primitive::kPrimDouble: {
1383 locations->SetInAt(0, Location::RequiresFpuRegister());
1384 locations->SetInAt(1, Location::RequiresFpuRegister());
1385 if (cond->NeedsMaterialization()) {
1386 locations->SetOut(Location::RequiresRegister());
1387 }
1388 break;
1389 }
1390 default:
1391 locations->SetInAt(0, Location::RequiresRegister());
1392 locations->SetInAt(1, Location::Any());
1393 if (cond->NeedsMaterialization()) {
1394 // We need a byte register.
1395 locations->SetOut(Location::RegisterLocation(ECX));
1396 }
1397 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001398 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001399}
1400
Roland Levillain0d37cd02015-05-27 16:39:19 +01001401void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001402 if (!cond->NeedsMaterialization()) {
1403 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001404 }
Mark Mendellc4701932015-04-10 13:18:51 -04001405
1406 LocationSummary* locations = cond->GetLocations();
1407 Location lhs = locations->InAt(0);
1408 Location rhs = locations->InAt(1);
1409 Register reg = locations->Out().AsRegister<Register>();
1410 Label true_label, false_label;
1411
1412 switch (cond->InputAt(0)->GetType()) {
1413 default: {
1414 // Integer case.
1415
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001416 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001417 __ xorl(reg, reg);
1418
1419 if (rhs.IsRegister()) {
1420 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1421 } else if (rhs.IsConstant()) {
1422 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1423 if (constant == 0) {
1424 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1425 } else {
1426 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1427 }
1428 } else {
1429 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1430 }
Aart Bike9f37602015-10-09 11:15:55 -07001431 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001432 return;
1433 }
1434 case Primitive::kPrimLong:
1435 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1436 break;
1437 case Primitive::kPrimFloat:
1438 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1439 GenerateFPJumps(cond, &true_label, &false_label);
1440 break;
1441 case Primitive::kPrimDouble:
1442 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1443 GenerateFPJumps(cond, &true_label, &false_label);
1444 break;
1445 }
1446
1447 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001448 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001449
Roland Levillain4fa13f62015-07-06 18:11:54 +01001450 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001451 __ Bind(&false_label);
1452 __ xorl(reg, reg);
1453 __ jmp(&done_label);
1454
Roland Levillain4fa13f62015-07-06 18:11:54 +01001455 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001456 __ Bind(&true_label);
1457 __ movl(reg, Immediate(1));
1458 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001459}
1460
1461void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1462 VisitCondition(comp);
1463}
1464
1465void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1466 VisitCondition(comp);
1467}
1468
1469void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1470 VisitCondition(comp);
1471}
1472
1473void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1474 VisitCondition(comp);
1475}
1476
1477void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1478 VisitCondition(comp);
1479}
1480
1481void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1482 VisitCondition(comp);
1483}
1484
1485void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1486 VisitCondition(comp);
1487}
1488
1489void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1490 VisitCondition(comp);
1491}
1492
1493void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1494 VisitCondition(comp);
1495}
1496
1497void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1498 VisitCondition(comp);
1499}
1500
1501void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1502 VisitCondition(comp);
1503}
1504
1505void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1506 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001507}
1508
Aart Bike9f37602015-10-09 11:15:55 -07001509void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1510 VisitCondition(comp);
1511}
1512
1513void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1514 VisitCondition(comp);
1515}
1516
1517void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1518 VisitCondition(comp);
1519}
1520
1521void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1522 VisitCondition(comp);
1523}
1524
1525void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1526 VisitCondition(comp);
1527}
1528
1529void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1530 VisitCondition(comp);
1531}
1532
1533void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1534 VisitCondition(comp);
1535}
1536
1537void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1538 VisitCondition(comp);
1539}
1540
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001541void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001542 LocationSummary* locations =
1543 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001544 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001545}
1546
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001547void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001548 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001549 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001550}
1551
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001552void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1553 LocationSummary* locations =
1554 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1555 locations->SetOut(Location::ConstantLocation(constant));
1556}
1557
1558void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1559 // Will be generated at use site.
1560 UNUSED(constant);
1561}
1562
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001564 LocationSummary* locations =
1565 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001566 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567}
1568
1569void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1570 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001571 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001572}
1573
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001574void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1575 LocationSummary* locations =
1576 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1577 locations->SetOut(Location::ConstantLocation(constant));
1578}
1579
1580void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1581 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001582 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001583}
1584
1585void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1586 LocationSummary* locations =
1587 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1588 locations->SetOut(Location::ConstantLocation(constant));
1589}
1590
1591void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1592 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001593 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001594}
1595
Calin Juravle27df7582015-04-17 19:12:31 +01001596void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1597 memory_barrier->SetLocations(nullptr);
1598}
1599
1600void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1601 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1602}
1603
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001605 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606}
1607
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001608void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001609 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001610 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001611}
1612
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001613void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001614 LocationSummary* locations =
1615 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001616 switch (ret->InputAt(0)->GetType()) {
1617 case Primitive::kPrimBoolean:
1618 case Primitive::kPrimByte:
1619 case Primitive::kPrimChar:
1620 case Primitive::kPrimShort:
1621 case Primitive::kPrimInt:
1622 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624 break;
1625
1626 case Primitive::kPrimLong:
1627 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001628 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001629 break;
1630
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001631 case Primitive::kPrimFloat:
1632 case Primitive::kPrimDouble:
1633 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001634 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001635 break;
1636
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001637 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001638 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001639 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640}
1641
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001642void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643 if (kIsDebugBuild) {
1644 switch (ret->InputAt(0)->GetType()) {
1645 case Primitive::kPrimBoolean:
1646 case Primitive::kPrimByte:
1647 case Primitive::kPrimChar:
1648 case Primitive::kPrimShort:
1649 case Primitive::kPrimInt:
1650 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001651 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001652 break;
1653
1654 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001655 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1656 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001657 break;
1658
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001659 case Primitive::kPrimFloat:
1660 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 break;
1663
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001664 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001665 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001666 }
1667 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001668 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669}
1670
Calin Juravle175dc732015-08-25 15:42:32 +01001671void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1672 // The trampoline uses the same calling convention as dex calling conventions,
1673 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1674 // the method_idx.
1675 HandleInvoke(invoke);
1676}
1677
1678void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1679 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1680}
1681
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001682void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001683 // When we do not run baseline, explicit clinit checks triggered by static
1684 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1685 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001686
Mark Mendellfb8d2792015-03-31 22:16:59 -04001687 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001688 if (intrinsic.TryDispatch(invoke)) {
1689 return;
1690 }
1691
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001692 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001693
1694 if (codegen_->IsBaseline()) {
1695 // Baseline does not have enough registers if the current method also
1696 // needs a register. We therefore do not require a register for it, and let
1697 // the code generation of the invoke handle it.
1698 LocationSummary* locations = invoke->GetLocations();
1699 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1700 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1701 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1702 }
1703 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001704}
1705
Mark Mendell09ed1a32015-03-25 08:30:06 -04001706static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1707 if (invoke->GetLocations()->Intrinsified()) {
1708 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1709 intrinsic.Dispatch(invoke);
1710 return true;
1711 }
1712 return false;
1713}
1714
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001715void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001716 // When we do not run baseline, explicit clinit checks triggered by static
1717 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1718 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001719
Mark Mendell09ed1a32015-03-25 08:30:06 -04001720 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1721 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001722 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001723
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001724 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001725 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001726 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001727 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001728}
1729
1730void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1731 HandleInvoke(invoke);
1732}
1733
1734void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001735 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001736 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001737}
1738
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001739void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001740 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1741 return;
1742 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001743
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001744 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001745 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001746 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001747}
1748
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001749void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1750 HandleInvoke(invoke);
1751 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001752 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001753}
1754
1755void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1756 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001758 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1759 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001760 LocationSummary* locations = invoke->GetLocations();
1761 Location receiver = locations->InAt(0);
1762 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1763
1764 // Set the hidden argument.
1765 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001766 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001767
1768 // temp = object->GetClass();
1769 if (receiver.IsStackSlot()) {
1770 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1771 __ movl(temp, Address(temp, class_offset));
1772 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001774 }
Roland Levillain4d027112015-07-01 15:41:14 +01001775 codegen_->MaybeRecordImplicitNullCheck(invoke);
1776 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001777 // temp = temp->GetImtEntryAt(method_offset);
1778 __ movl(temp, Address(temp, method_offset));
1779 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001780 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001781 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001782
1783 DCHECK(!codegen_->IsLeafMethod());
1784 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1785}
1786
Roland Levillain88cb1752014-10-20 16:36:47 +01001787void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1790 switch (neg->GetResultType()) {
1791 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001792 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001793 locations->SetInAt(0, Location::RequiresRegister());
1794 locations->SetOut(Location::SameAsFirstInput());
1795 break;
1796
Roland Levillain88cb1752014-10-20 16:36:47 +01001797 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001798 locations->SetInAt(0, Location::RequiresFpuRegister());
1799 locations->SetOut(Location::SameAsFirstInput());
1800 locations->AddTemp(Location::RequiresRegister());
1801 locations->AddTemp(Location::RequiresFpuRegister());
1802 break;
1803
Roland Levillain88cb1752014-10-20 16:36:47 +01001804 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001805 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001806 locations->SetOut(Location::SameAsFirstInput());
1807 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001808 break;
1809
1810 default:
1811 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1812 }
1813}
1814
1815void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1816 LocationSummary* locations = neg->GetLocations();
1817 Location out = locations->Out();
1818 Location in = locations->InAt(0);
1819 switch (neg->GetResultType()) {
1820 case Primitive::kPrimInt:
1821 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001822 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001823 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001824 break;
1825
1826 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001827 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001828 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001829 __ negl(out.AsRegisterPairLow<Register>());
1830 // Negation is similar to subtraction from zero. The least
1831 // significant byte triggers a borrow when it is different from
1832 // zero; to take it into account, add 1 to the most significant
1833 // byte if the carry flag (CF) is set to 1 after the first NEGL
1834 // operation.
1835 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1836 __ negl(out.AsRegisterPairHigh<Register>());
1837 break;
1838
Roland Levillain5368c212014-11-27 15:03:41 +00001839 case Primitive::kPrimFloat: {
1840 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 Register constant = locations->GetTemp(0).AsRegister<Register>();
1842 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001843 // Implement float negation with an exclusive or with value
1844 // 0x80000000 (mask for bit 31, representing the sign of a
1845 // single-precision floating-point number).
1846 __ movl(constant, Immediate(INT32_C(0x80000000)));
1847 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001848 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001849 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001850 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001851
Roland Levillain5368c212014-11-27 15:03:41 +00001852 case Primitive::kPrimDouble: {
1853 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001855 // Implement double negation with an exclusive or with value
1856 // 0x8000000000000000 (mask for bit 63, representing the sign of
1857 // a double-precision floating-point number).
1858 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001860 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001861 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001862
1863 default:
1864 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1865 }
1866}
1867
Roland Levillaindff1f282014-11-05 14:15:05 +00001868void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001869 Primitive::Type result_type = conversion->GetResultType();
1870 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001871 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001872
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001873 // The float-to-long and double-to-long type conversions rely on a
1874 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001875 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001876 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1877 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001878 ? LocationSummary::kCall
1879 : LocationSummary::kNoCall;
1880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1882
David Brazdilb2bd1c52015-03-25 11:17:37 +00001883 // The Java language does not allow treating boolean as an integral type but
1884 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001885
Roland Levillaindff1f282014-11-05 14:15:05 +00001886 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001887 case Primitive::kPrimByte:
1888 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001889 case Primitive::kPrimBoolean:
1890 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001891 case Primitive::kPrimShort:
1892 case Primitive::kPrimInt:
1893 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001894 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001895 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1896 // Make the output overlap to please the register allocator. This greatly simplifies
1897 // the validation of the linear scan implementation
1898 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001899 break;
1900
1901 default:
1902 LOG(FATAL) << "Unexpected type conversion from " << input_type
1903 << " to " << result_type;
1904 }
1905 break;
1906
Roland Levillain01a8d712014-11-14 16:27:39 +00001907 case Primitive::kPrimShort:
1908 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001909 case Primitive::kPrimBoolean:
1910 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001911 case Primitive::kPrimByte:
1912 case Primitive::kPrimInt:
1913 case Primitive::kPrimChar:
1914 // Processing a Dex `int-to-short' instruction.
1915 locations->SetInAt(0, Location::Any());
1916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1917 break;
1918
1919 default:
1920 LOG(FATAL) << "Unexpected type conversion from " << input_type
1921 << " to " << result_type;
1922 }
1923 break;
1924
Roland Levillain946e1432014-11-11 17:35:19 +00001925 case Primitive::kPrimInt:
1926 switch (input_type) {
1927 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001928 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001929 locations->SetInAt(0, Location::Any());
1930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1931 break;
1932
1933 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001934 // Processing a Dex `float-to-int' instruction.
1935 locations->SetInAt(0, Location::RequiresFpuRegister());
1936 locations->SetOut(Location::RequiresRegister());
1937 locations->AddTemp(Location::RequiresFpuRegister());
1938 break;
1939
Roland Levillain946e1432014-11-11 17:35:19 +00001940 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001941 // Processing a Dex `double-to-int' instruction.
1942 locations->SetInAt(0, Location::RequiresFpuRegister());
1943 locations->SetOut(Location::RequiresRegister());
1944 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 }
1951 break;
1952
Roland Levillaindff1f282014-11-05 14:15:05 +00001953 case Primitive::kPrimLong:
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 Levillaindff1f282014-11-05 14:15:05 +00001957 case Primitive::kPrimByte:
1958 case Primitive::kPrimShort:
1959 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001960 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001961 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001962 locations->SetInAt(0, Location::RegisterLocation(EAX));
1963 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1964 break;
1965
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001966 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001967 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001968 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001969 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001970 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1971 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1972
Vladimir Marko949c91f2015-01-27 10:48:44 +00001973 // The runtime helper puts the result in EAX, EDX.
1974 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001975 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001976 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001977
1978 default:
1979 LOG(FATAL) << "Unexpected type conversion from " << input_type
1980 << " to " << result_type;
1981 }
1982 break;
1983
Roland Levillain981e4542014-11-14 11:47:14 +00001984 case Primitive::kPrimChar:
1985 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001986 case Primitive::kPrimBoolean:
1987 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001988 case Primitive::kPrimByte:
1989 case Primitive::kPrimShort:
1990 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001991 // Processing a Dex `int-to-char' instruction.
1992 locations->SetInAt(0, Location::Any());
1993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1994 break;
1995
1996 default:
1997 LOG(FATAL) << "Unexpected type conversion from " << input_type
1998 << " to " << result_type;
1999 }
2000 break;
2001
Roland Levillaindff1f282014-11-05 14:15:05 +00002002 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002003 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002004 case Primitive::kPrimBoolean:
2005 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002006 case Primitive::kPrimByte:
2007 case Primitive::kPrimShort:
2008 case Primitive::kPrimInt:
2009 case Primitive::kPrimChar:
2010 // Processing a Dex `int-to-float' instruction.
2011 locations->SetInAt(0, Location::RequiresRegister());
2012 locations->SetOut(Location::RequiresFpuRegister());
2013 break;
2014
2015 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002016 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002017 locations->SetInAt(0, Location::Any());
2018 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002019 break;
2020
Roland Levillaincff13742014-11-17 14:32:17 +00002021 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002022 // Processing a Dex `double-to-float' instruction.
2023 locations->SetInAt(0, Location::RequiresFpuRegister());
2024 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 };
2031 break;
2032
Roland Levillaindff1f282014-11-05 14:15:05 +00002033 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002034 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002035 case Primitive::kPrimBoolean:
2036 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002037 case Primitive::kPrimByte:
2038 case Primitive::kPrimShort:
2039 case Primitive::kPrimInt:
2040 case Primitive::kPrimChar:
2041 // Processing a Dex `int-to-double' instruction.
2042 locations->SetInAt(0, Location::RequiresRegister());
2043 locations->SetOut(Location::RequiresFpuRegister());
2044 break;
2045
2046 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002047 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002048 locations->SetInAt(0, Location::Any());
2049 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002050 break;
2051
Roland Levillaincff13742014-11-17 14:32:17 +00002052 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002053 // Processing a Dex `float-to-double' instruction.
2054 locations->SetInAt(0, Location::RequiresFpuRegister());
2055 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002056 break;
2057
2058 default:
2059 LOG(FATAL) << "Unexpected type conversion from " << input_type
2060 << " to " << result_type;
2061 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002062 break;
2063
2064 default:
2065 LOG(FATAL) << "Unexpected type conversion from " << input_type
2066 << " to " << result_type;
2067 }
2068}
2069
2070void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2071 LocationSummary* locations = conversion->GetLocations();
2072 Location out = locations->Out();
2073 Location in = locations->InAt(0);
2074 Primitive::Type result_type = conversion->GetResultType();
2075 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002076 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002077 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002078 case Primitive::kPrimByte:
2079 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002080 case Primitive::kPrimBoolean:
2081 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002082 case Primitive::kPrimShort:
2083 case Primitive::kPrimInt:
2084 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002085 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002086 if (in.IsRegister()) {
2087 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002088 } else {
2089 DCHECK(in.GetConstant()->IsIntConstant());
2090 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2091 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2092 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002093 break;
2094
2095 default:
2096 LOG(FATAL) << "Unexpected type conversion from " << input_type
2097 << " to " << result_type;
2098 }
2099 break;
2100
Roland Levillain01a8d712014-11-14 16:27:39 +00002101 case Primitive::kPrimShort:
2102 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002103 case Primitive::kPrimBoolean:
2104 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002105 case Primitive::kPrimByte:
2106 case Primitive::kPrimInt:
2107 case Primitive::kPrimChar:
2108 // Processing a Dex `int-to-short' instruction.
2109 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002110 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002111 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002112 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002113 } else {
2114 DCHECK(in.GetConstant()->IsIntConstant());
2115 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002117 }
2118 break;
2119
2120 default:
2121 LOG(FATAL) << "Unexpected type conversion from " << input_type
2122 << " to " << result_type;
2123 }
2124 break;
2125
Roland Levillain946e1432014-11-11 17:35:19 +00002126 case Primitive::kPrimInt:
2127 switch (input_type) {
2128 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002129 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002130 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002132 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002134 } else {
2135 DCHECK(in.IsConstant());
2136 DCHECK(in.GetConstant()->IsLongConstant());
2137 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002139 }
2140 break;
2141
Roland Levillain3f8f9362014-12-02 17:45:01 +00002142 case Primitive::kPrimFloat: {
2143 // Processing a Dex `float-to-int' instruction.
2144 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2145 Register output = out.AsRegister<Register>();
2146 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002147 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002148
2149 __ movl(output, Immediate(kPrimIntMax));
2150 // temp = int-to-float(output)
2151 __ cvtsi2ss(temp, output);
2152 // if input >= temp goto done
2153 __ comiss(input, temp);
2154 __ j(kAboveEqual, &done);
2155 // if input == NaN goto nan
2156 __ j(kUnordered, &nan);
2157 // output = float-to-int-truncate(input)
2158 __ cvttss2si(output, input);
2159 __ jmp(&done);
2160 __ Bind(&nan);
2161 // output = 0
2162 __ xorl(output, output);
2163 __ Bind(&done);
2164 break;
2165 }
2166
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002167 case Primitive::kPrimDouble: {
2168 // Processing a Dex `double-to-int' instruction.
2169 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2170 Register output = out.AsRegister<Register>();
2171 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002172 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002173
2174 __ movl(output, Immediate(kPrimIntMax));
2175 // temp = int-to-double(output)
2176 __ cvtsi2sd(temp, output);
2177 // if input >= temp goto done
2178 __ comisd(input, temp);
2179 __ j(kAboveEqual, &done);
2180 // if input == NaN goto nan
2181 __ j(kUnordered, &nan);
2182 // output = double-to-int-truncate(input)
2183 __ cvttsd2si(output, input);
2184 __ jmp(&done);
2185 __ Bind(&nan);
2186 // output = 0
2187 __ xorl(output, output);
2188 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002189 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002190 }
Roland Levillain946e1432014-11-11 17:35:19 +00002191
2192 default:
2193 LOG(FATAL) << "Unexpected type conversion from " << input_type
2194 << " to " << result_type;
2195 }
2196 break;
2197
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 case Primitive::kPrimLong:
2199 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 case Primitive::kPrimByte:
2203 case Primitive::kPrimShort:
2204 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002205 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002206 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002207 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2208 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002209 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002210 __ cdq();
2211 break;
2212
2213 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002214 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002215 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2216 conversion,
2217 conversion->GetDexPc(),
2218 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002219 break;
2220
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002223 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2224 conversion,
2225 conversion->GetDexPc(),
2226 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 break;
2228
2229 default:
2230 LOG(FATAL) << "Unexpected type conversion from " << input_type
2231 << " to " << result_type;
2232 }
2233 break;
2234
Roland Levillain981e4542014-11-14 11:47:14 +00002235 case Primitive::kPrimChar:
2236 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002237 case Primitive::kPrimBoolean:
2238 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002239 case Primitive::kPrimByte:
2240 case Primitive::kPrimShort:
2241 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002242 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2243 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002245 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002246 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002247 } else {
2248 DCHECK(in.GetConstant()->IsIntConstant());
2249 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002250 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002251 }
2252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected type conversion from " << input_type
2256 << " to " << result_type;
2257 }
2258 break;
2259
Roland Levillaindff1f282014-11-05 14:15:05 +00002260 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002261 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002262 case Primitive::kPrimBoolean:
2263 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002264 case Primitive::kPrimByte:
2265 case Primitive::kPrimShort:
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002268 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002269 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002270 break;
2271
Roland Levillain6d0e4832014-11-27 18:31:21 +00002272 case Primitive::kPrimLong: {
2273 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002274 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275
Roland Levillain232ade02015-04-20 15:14:36 +01002276 // Create stack space for the call to
2277 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2278 // TODO: enhance register allocator to ask for stack temporaries.
2279 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2280 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2281 __ subl(ESP, Immediate(adjustment));
2282 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002283
Roland Levillain232ade02015-04-20 15:14:36 +01002284 // Load the value to the FP stack, using temporaries if needed.
2285 PushOntoFPStack(in, 0, adjustment, false, true);
2286
2287 if (out.IsStackSlot()) {
2288 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2289 } else {
2290 __ fstps(Address(ESP, 0));
2291 Location stack_temp = Location::StackSlot(0);
2292 codegen_->Move32(out, stack_temp);
2293 }
2294
2295 // Remove the temporary stack space we allocated.
2296 if (adjustment != 0) {
2297 __ addl(ESP, Immediate(adjustment));
2298 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002299 break;
2300 }
2301
Roland Levillaincff13742014-11-17 14:32:17 +00002302 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002303 // Processing a Dex `double-to-float' instruction.
2304 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002305 break;
2306
2307 default:
2308 LOG(FATAL) << "Unexpected type conversion from " << input_type
2309 << " to " << result_type;
2310 };
2311 break;
2312
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002314 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002315 case Primitive::kPrimBoolean:
2316 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002317 case Primitive::kPrimByte:
2318 case Primitive::kPrimShort:
2319 case Primitive::kPrimInt:
2320 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002321 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002322 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002323 break;
2324
Roland Levillain647b9ed2014-11-27 12:06:00 +00002325 case Primitive::kPrimLong: {
2326 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002327 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002328
Roland Levillain232ade02015-04-20 15:14:36 +01002329 // Create stack space for the call to
2330 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2331 // TODO: enhance register allocator to ask for stack temporaries.
2332 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2333 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2334 __ subl(ESP, Immediate(adjustment));
2335 }
2336
2337 // Load the value to the FP stack, using temporaries if needed.
2338 PushOntoFPStack(in, 0, adjustment, false, true);
2339
2340 if (out.IsDoubleStackSlot()) {
2341 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2342 } else {
2343 __ fstpl(Address(ESP, 0));
2344 Location stack_temp = Location::DoubleStackSlot(0);
2345 codegen_->Move64(out, stack_temp);
2346 }
2347
2348 // Remove the temporary stack space we allocated.
2349 if (adjustment != 0) {
2350 __ addl(ESP, Immediate(adjustment));
2351 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002352 break;
2353 }
2354
Roland Levillaincff13742014-11-17 14:32:17 +00002355 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002356 // Processing a Dex `float-to-double' instruction.
2357 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002358 break;
2359
2360 default:
2361 LOG(FATAL) << "Unexpected type conversion from " << input_type
2362 << " to " << result_type;
2363 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
2370}
2371
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002372void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002373 LocationSummary* locations =
2374 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002375 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002376 case Primitive::kPrimInt: {
2377 locations->SetInAt(0, Location::RequiresRegister());
2378 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2380 break;
2381 }
2382
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002383 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetInAt(1, Location::Any());
2386 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002387 break;
2388 }
2389
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 case Primitive::kPrimFloat:
2391 case Primitive::kPrimDouble: {
2392 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002393 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002394 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002396 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002397
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002398 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002399 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2400 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002401 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002402}
2403
2404void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2405 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002406 Location first = locations->InAt(0);
2407 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002408 Location out = locations->Out();
2409
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002410 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002411 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002413 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2414 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002415 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2416 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002417 } else {
2418 __ leal(out.AsRegister<Register>(), Address(
2419 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2420 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002421 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002422 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2423 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2424 __ addl(out.AsRegister<Register>(), Immediate(value));
2425 } else {
2426 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2427 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002428 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002429 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002431 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002432 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002433 }
2434
2435 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002436 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002437 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2438 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002439 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002440 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2441 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002442 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002443 } else {
2444 DCHECK(second.IsConstant()) << second;
2445 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2446 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2447 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002448 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002449 break;
2450 }
2451
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002452 case Primitive::kPrimFloat: {
2453 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002455 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2456 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2457 DCHECK(!const_area->NeedsMaterialization());
2458 __ addss(first.AsFpuRegister<XmmRegister>(),
2459 codegen_->LiteralFloatAddress(
2460 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2461 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2462 } else {
2463 DCHECK(second.IsStackSlot());
2464 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002465 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002466 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002467 }
2468
2469 case Primitive::kPrimDouble: {
2470 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002472 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2473 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2474 DCHECK(!const_area->NeedsMaterialization());
2475 __ addsd(first.AsFpuRegister<XmmRegister>(),
2476 codegen_->LiteralDoubleAddress(
2477 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2478 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2479 } else {
2480 DCHECK(second.IsDoubleStackSlot());
2481 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002482 }
2483 break;
2484 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002485
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002486 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002487 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488 }
2489}
2490
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002491void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002492 LocationSummary* locations =
2493 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002494 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002495 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002496 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002497 locations->SetInAt(0, Location::RequiresRegister());
2498 locations->SetInAt(1, Location::Any());
2499 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002500 break;
2501 }
Calin Juravle11351682014-10-23 15:38:15 +01002502 case Primitive::kPrimFloat:
2503 case Primitive::kPrimDouble: {
2504 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002505 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002506 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002507 break;
Calin Juravle11351682014-10-23 15:38:15 +01002508 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002509
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002510 default:
Calin Juravle11351682014-10-23 15:38:15 +01002511 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002512 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002513}
2514
2515void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2516 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002517 Location first = locations->InAt(0);
2518 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002519 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002520 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002521 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002522 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002524 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002525 __ subl(first.AsRegister<Register>(),
2526 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002527 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002529 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002530 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002531 }
2532
2533 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002534 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002535 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2536 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002537 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002538 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002539 __ sbbl(first.AsRegisterPairHigh<Register>(),
2540 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002541 } else {
2542 DCHECK(second.IsConstant()) << second;
2543 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2544 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2545 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002546 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002547 break;
2548 }
2549
Calin Juravle11351682014-10-23 15:38:15 +01002550 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002551 if (second.IsFpuRegister()) {
2552 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2553 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2554 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2555 DCHECK(!const_area->NeedsMaterialization());
2556 __ subss(first.AsFpuRegister<XmmRegister>(),
2557 codegen_->LiteralFloatAddress(
2558 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2559 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2560 } else {
2561 DCHECK(second.IsStackSlot());
2562 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2563 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002564 break;
Calin Juravle11351682014-10-23 15:38:15 +01002565 }
2566
2567 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002568 if (second.IsFpuRegister()) {
2569 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2570 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2571 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2572 DCHECK(!const_area->NeedsMaterialization());
2573 __ subsd(first.AsFpuRegister<XmmRegister>(),
2574 codegen_->LiteralDoubleAddress(
2575 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2576 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2577 } else {
2578 DCHECK(second.IsDoubleStackSlot());
2579 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2580 }
Calin Juravle11351682014-10-23 15:38:15 +01002581 break;
2582 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002584 default:
Calin Juravle11351682014-10-23 15:38:15 +01002585 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002586 }
2587}
2588
Calin Juravle34bacdf2014-10-07 20:23:36 +01002589void LocationsBuilderX86::VisitMul(HMul* mul) {
2590 LocationSummary* locations =
2591 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2592 switch (mul->GetResultType()) {
2593 case Primitive::kPrimInt:
2594 locations->SetInAt(0, Location::RequiresRegister());
2595 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002596 if (mul->InputAt(1)->IsIntConstant()) {
2597 // Can use 3 operand multiply.
2598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2599 } else {
2600 locations->SetOut(Location::SameAsFirstInput());
2601 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002602 break;
2603 case Primitive::kPrimLong: {
2604 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002605 locations->SetInAt(1, Location::Any());
2606 locations->SetOut(Location::SameAsFirstInput());
2607 // Needed for imul on 32bits with 64bits output.
2608 locations->AddTemp(Location::RegisterLocation(EAX));
2609 locations->AddTemp(Location::RegisterLocation(EDX));
2610 break;
2611 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002612 case Primitive::kPrimFloat:
2613 case Primitive::kPrimDouble: {
2614 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002615 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002616 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002617 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002618 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002619
2620 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002621 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002622 }
2623}
2624
2625void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2626 LocationSummary* locations = mul->GetLocations();
2627 Location first = locations->InAt(0);
2628 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002629 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002630
2631 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002632 case Primitive::kPrimInt:
2633 // The constant may have ended up in a register, so test explicitly to avoid
2634 // problems where the output may not be the same as the first operand.
2635 if (mul->InputAt(1)->IsIntConstant()) {
2636 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2637 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2638 } else if (second.IsRegister()) {
2639 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641 } else {
2642 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002643 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645 }
2646 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002647
2648 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002649 Register in1_hi = first.AsRegisterPairHigh<Register>();
2650 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 Register eax = locations->GetTemp(0).AsRegister<Register>();
2652 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002653
2654 DCHECK_EQ(EAX, eax);
2655 DCHECK_EQ(EDX, edx);
2656
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002657 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 // output: in1
2659 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2660 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2661 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002662 if (second.IsConstant()) {
2663 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002664
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002665 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2666 int32_t low_value = Low32Bits(value);
2667 int32_t high_value = High32Bits(value);
2668 Immediate low(low_value);
2669 Immediate high(high_value);
2670
2671 __ movl(eax, high);
2672 // eax <- in1.lo * in2.hi
2673 __ imull(eax, in1_lo);
2674 // in1.hi <- in1.hi * in2.lo
2675 __ imull(in1_hi, low);
2676 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2677 __ addl(in1_hi, eax);
2678 // move in2_lo to eax to prepare for double precision
2679 __ movl(eax, low);
2680 // edx:eax <- in1.lo * in2.lo
2681 __ mull(in1_lo);
2682 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2683 __ addl(in1_hi, edx);
2684 // in1.lo <- (in1.lo * in2.lo)[31:0];
2685 __ movl(in1_lo, eax);
2686 } else if (second.IsRegisterPair()) {
2687 Register in2_hi = second.AsRegisterPairHigh<Register>();
2688 Register in2_lo = second.AsRegisterPairLow<Register>();
2689
2690 __ movl(eax, in2_hi);
2691 // eax <- in1.lo * in2.hi
2692 __ imull(eax, in1_lo);
2693 // in1.hi <- in1.hi * in2.lo
2694 __ imull(in1_hi, in2_lo);
2695 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2696 __ addl(in1_hi, eax);
2697 // move in1_lo to eax to prepare for double precision
2698 __ movl(eax, in1_lo);
2699 // edx:eax <- in1.lo * in2.lo
2700 __ mull(in2_lo);
2701 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2702 __ addl(in1_hi, edx);
2703 // in1.lo <- (in1.lo * in2.lo)[31:0];
2704 __ movl(in1_lo, eax);
2705 } else {
2706 DCHECK(second.IsDoubleStackSlot()) << second;
2707 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2708 Address in2_lo(ESP, second.GetStackIndex());
2709
2710 __ movl(eax, in2_hi);
2711 // eax <- in1.lo * in2.hi
2712 __ imull(eax, in1_lo);
2713 // in1.hi <- in1.hi * in2.lo
2714 __ imull(in1_hi, in2_lo);
2715 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2716 __ addl(in1_hi, eax);
2717 // move in1_lo to eax to prepare for double precision
2718 __ movl(eax, in1_lo);
2719 // edx:eax <- in1.lo * in2.lo
2720 __ mull(in2_lo);
2721 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2722 __ addl(in1_hi, edx);
2723 // in1.lo <- (in1.lo * in2.lo)[31:0];
2724 __ movl(in1_lo, eax);
2725 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726
2727 break;
2728 }
2729
Calin Juravleb5bfa962014-10-21 18:02:24 +01002730 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002731 DCHECK(first.Equals(locations->Out()));
2732 if (second.IsFpuRegister()) {
2733 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2734 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2735 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2736 DCHECK(!const_area->NeedsMaterialization());
2737 __ mulss(first.AsFpuRegister<XmmRegister>(),
2738 codegen_->LiteralFloatAddress(
2739 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2740 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2741 } else {
2742 DCHECK(second.IsStackSlot());
2743 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2744 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002745 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002746 }
2747
2748 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002749 DCHECK(first.Equals(locations->Out()));
2750 if (second.IsFpuRegister()) {
2751 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2752 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2753 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2754 DCHECK(!const_area->NeedsMaterialization());
2755 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2756 codegen_->LiteralDoubleAddress(
2757 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2758 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2759 } else {
2760 DCHECK(second.IsDoubleStackSlot());
2761 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2762 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002763 break;
2764 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765
2766 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002767 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002768 }
2769}
2770
Roland Levillain232ade02015-04-20 15:14:36 +01002771void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2772 uint32_t temp_offset,
2773 uint32_t stack_adjustment,
2774 bool is_fp,
2775 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002776 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002777 DCHECK(!is_wide);
2778 if (is_fp) {
2779 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2780 } else {
2781 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2782 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002783 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002784 DCHECK(is_wide);
2785 if (is_fp) {
2786 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2787 } else {
2788 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2789 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002790 } else {
2791 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002792 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002793 Location stack_temp = Location::StackSlot(temp_offset);
2794 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002795 if (is_fp) {
2796 __ flds(Address(ESP, temp_offset));
2797 } else {
2798 __ filds(Address(ESP, temp_offset));
2799 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002800 } else {
2801 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2802 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002803 if (is_fp) {
2804 __ fldl(Address(ESP, temp_offset));
2805 } else {
2806 __ fildl(Address(ESP, temp_offset));
2807 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002808 }
2809 }
2810}
2811
2812void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2813 Primitive::Type type = rem->GetResultType();
2814 bool is_float = type == Primitive::kPrimFloat;
2815 size_t elem_size = Primitive::ComponentSize(type);
2816 LocationSummary* locations = rem->GetLocations();
2817 Location first = locations->InAt(0);
2818 Location second = locations->InAt(1);
2819 Location out = locations->Out();
2820
2821 // Create stack space for 2 elements.
2822 // TODO: enhance register allocator to ask for stack temporaries.
2823 __ subl(ESP, Immediate(2 * elem_size));
2824
2825 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002826 const bool is_wide = !is_float;
2827 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2828 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002829
2830 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002831 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002832 __ Bind(&retry);
2833 __ fprem();
2834
2835 // Move FP status to AX.
2836 __ fstsw();
2837
2838 // And see if the argument reduction is complete. This is signaled by the
2839 // C2 FPU flag bit set to 0.
2840 __ andl(EAX, Immediate(kC2ConditionMask));
2841 __ j(kNotEqual, &retry);
2842
2843 // We have settled on the final value. Retrieve it into an XMM register.
2844 // Store FP top of stack to real stack.
2845 if (is_float) {
2846 __ fsts(Address(ESP, 0));
2847 } else {
2848 __ fstl(Address(ESP, 0));
2849 }
2850
2851 // Pop the 2 items from the FP stack.
2852 __ fucompp();
2853
2854 // Load the value from the stack into an XMM register.
2855 DCHECK(out.IsFpuRegister()) << out;
2856 if (is_float) {
2857 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2858 } else {
2859 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2860 }
2861
2862 // And remove the temporary stack space we allocated.
2863 __ addl(ESP, Immediate(2 * elem_size));
2864}
2865
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002866
2867void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2868 DCHECK(instruction->IsDiv() || instruction->IsRem());
2869
2870 LocationSummary* locations = instruction->GetLocations();
2871 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002872 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002873
2874 Register out_register = locations->Out().AsRegister<Register>();
2875 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002876 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002877
2878 DCHECK(imm == 1 || imm == -1);
2879
2880 if (instruction->IsRem()) {
2881 __ xorl(out_register, out_register);
2882 } else {
2883 __ movl(out_register, input_register);
2884 if (imm == -1) {
2885 __ negl(out_register);
2886 }
2887 }
2888}
2889
2890
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002891void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002892 LocationSummary* locations = instruction->GetLocations();
2893
2894 Register out_register = locations->Out().AsRegister<Register>();
2895 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002896 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002897
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002898 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002899 Register num = locations->GetTemp(0).AsRegister<Register>();
2900
2901 __ leal(num, Address(input_register, std::abs(imm) - 1));
2902 __ testl(input_register, input_register);
2903 __ cmovl(kGreaterEqual, num, input_register);
2904 int shift = CTZ(imm);
2905 __ sarl(num, Immediate(shift));
2906
2907 if (imm < 0) {
2908 __ negl(num);
2909 }
2910
2911 __ movl(out_register, num);
2912}
2913
2914void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2915 DCHECK(instruction->IsDiv() || instruction->IsRem());
2916
2917 LocationSummary* locations = instruction->GetLocations();
2918 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2919
2920 Register eax = locations->InAt(0).AsRegister<Register>();
2921 Register out = locations->Out().AsRegister<Register>();
2922 Register num;
2923 Register edx;
2924
2925 if (instruction->IsDiv()) {
2926 edx = locations->GetTemp(0).AsRegister<Register>();
2927 num = locations->GetTemp(1).AsRegister<Register>();
2928 } else {
2929 edx = locations->Out().AsRegister<Register>();
2930 num = locations->GetTemp(0).AsRegister<Register>();
2931 }
2932
2933 DCHECK_EQ(EAX, eax);
2934 DCHECK_EQ(EDX, edx);
2935 if (instruction->IsDiv()) {
2936 DCHECK_EQ(EAX, out);
2937 } else {
2938 DCHECK_EQ(EDX, out);
2939 }
2940
2941 int64_t magic;
2942 int shift;
2943 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2944
Mark Mendell0c9497d2015-08-21 09:30:05 -04002945 NearLabel ndiv;
2946 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002947 // If numerator is 0, the result is 0, no computation needed.
2948 __ testl(eax, eax);
2949 __ j(kNotEqual, &ndiv);
2950
2951 __ xorl(out, out);
2952 __ jmp(&end);
2953
2954 __ Bind(&ndiv);
2955
2956 // Save the numerator.
2957 __ movl(num, eax);
2958
2959 // EAX = magic
2960 __ movl(eax, Immediate(magic));
2961
2962 // EDX:EAX = magic * numerator
2963 __ imull(num);
2964
2965 if (imm > 0 && magic < 0) {
2966 // EDX += num
2967 __ addl(edx, num);
2968 } else if (imm < 0 && magic > 0) {
2969 __ subl(edx, num);
2970 }
2971
2972 // Shift if needed.
2973 if (shift != 0) {
2974 __ sarl(edx, Immediate(shift));
2975 }
2976
2977 // EDX += 1 if EDX < 0
2978 __ movl(eax, edx);
2979 __ shrl(edx, Immediate(31));
2980 __ addl(edx, eax);
2981
2982 if (instruction->IsRem()) {
2983 __ movl(eax, num);
2984 __ imull(edx, Immediate(imm));
2985 __ subl(eax, edx);
2986 __ movl(edx, eax);
2987 } else {
2988 __ movl(eax, edx);
2989 }
2990 __ Bind(&end);
2991}
2992
Calin Juravlebacfec32014-11-14 15:54:36 +00002993void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2994 DCHECK(instruction->IsDiv() || instruction->IsRem());
2995
2996 LocationSummary* locations = instruction->GetLocations();
2997 Location out = locations->Out();
2998 Location first = locations->InAt(0);
2999 Location second = locations->InAt(1);
3000 bool is_div = instruction->IsDiv();
3001
3002 switch (instruction->GetResultType()) {
3003 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 DCHECK_EQ(EAX, first.AsRegister<Register>());
3005 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003006
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003007 if (instruction->InputAt(1)->IsIntConstant()) {
3008 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003009
3010 if (imm == 0) {
3011 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3012 } else if (imm == 1 || imm == -1) {
3013 DivRemOneOrMinusOne(instruction);
3014 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003015 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003016 } else {
3017 DCHECK(imm <= -2 || imm >= 2);
3018 GenerateDivRemWithAnyConstant(instruction);
3019 }
3020 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003021 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003022 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003023 is_div);
3024 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003025
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003026 Register second_reg = second.AsRegister<Register>();
3027 // 0x80000000/-1 triggers an arithmetic exception!
3028 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3029 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003030
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003031 __ cmpl(second_reg, Immediate(-1));
3032 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003033
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003034 // edx:eax <- sign-extended of eax
3035 __ cdq();
3036 // eax = quotient, edx = remainder
3037 __ idivl(second_reg);
3038 __ Bind(slow_path->GetExitLabel());
3039 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003040 break;
3041 }
3042
3043 case Primitive::kPrimLong: {
3044 InvokeRuntimeCallingConvention calling_convention;
3045 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3046 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3047 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3048 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3049 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3050 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3051
3052 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003053 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3054 instruction,
3055 instruction->GetDexPc(),
3056 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003057 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003058 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3059 instruction,
3060 instruction->GetDexPc(),
3061 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003062 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003063 break;
3064 }
3065
3066 default:
3067 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3068 }
3069}
3070
Calin Juravle7c4954d2014-10-28 16:57:40 +00003071void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003072 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003073 ? LocationSummary::kCall
3074 : LocationSummary::kNoCall;
3075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3076
Calin Juravle7c4954d2014-10-28 16:57:40 +00003077 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003078 case Primitive::kPrimInt: {
3079 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003081 locations->SetOut(Location::SameAsFirstInput());
3082 // Intel uses edx:eax as the dividend.
3083 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003084 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3085 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3086 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003087 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003088 locations->AddTemp(Location::RequiresRegister());
3089 }
Calin Juravled0d48522014-11-04 16:40:20 +00003090 break;
3091 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003092 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003093 InvokeRuntimeCallingConvention calling_convention;
3094 locations->SetInAt(0, Location::RegisterPairLocation(
3095 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3096 locations->SetInAt(1, Location::RegisterPairLocation(
3097 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3098 // Runtime helper puts the result in EAX, EDX.
3099 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003100 break;
3101 }
3102 case Primitive::kPrimFloat:
3103 case Primitive::kPrimDouble: {
3104 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003105 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003106 locations->SetOut(Location::SameAsFirstInput());
3107 break;
3108 }
3109
3110 default:
3111 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3112 }
3113}
3114
3115void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3116 LocationSummary* locations = div->GetLocations();
3117 Location first = locations->InAt(0);
3118 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003119
3120 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003121 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003122 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003123 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003124 break;
3125 }
3126
3127 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003128 if (second.IsFpuRegister()) {
3129 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3130 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3131 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3132 DCHECK(!const_area->NeedsMaterialization());
3133 __ divss(first.AsFpuRegister<XmmRegister>(),
3134 codegen_->LiteralFloatAddress(
3135 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3136 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3137 } else {
3138 DCHECK(second.IsStackSlot());
3139 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3140 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003141 break;
3142 }
3143
3144 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003145 if (second.IsFpuRegister()) {
3146 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3147 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3148 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3149 DCHECK(!const_area->NeedsMaterialization());
3150 __ divsd(first.AsFpuRegister<XmmRegister>(),
3151 codegen_->LiteralDoubleAddress(
3152 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3153 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3154 } else {
3155 DCHECK(second.IsDoubleStackSlot());
3156 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3157 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003158 break;
3159 }
3160
3161 default:
3162 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3163 }
3164}
3165
Calin Juravlebacfec32014-11-14 15:54:36 +00003166void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003167 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003168
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003169 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3170 ? LocationSummary::kCall
3171 : LocationSummary::kNoCall;
3172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003173
Calin Juravled2ec87d2014-12-08 14:24:46 +00003174 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003175 case Primitive::kPrimInt: {
3176 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003177 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003178 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003179 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3180 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3181 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003182 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003183 locations->AddTemp(Location::RequiresRegister());
3184 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003185 break;
3186 }
3187 case Primitive::kPrimLong: {
3188 InvokeRuntimeCallingConvention calling_convention;
3189 locations->SetInAt(0, Location::RegisterPairLocation(
3190 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3191 locations->SetInAt(1, Location::RegisterPairLocation(
3192 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3193 // Runtime helper puts the result in EAX, EDX.
3194 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3195 break;
3196 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003197 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003198 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003199 locations->SetInAt(0, Location::Any());
3200 locations->SetInAt(1, Location::Any());
3201 locations->SetOut(Location::RequiresFpuRegister());
3202 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003203 break;
3204 }
3205
3206 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003207 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003208 }
3209}
3210
3211void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3212 Primitive::Type type = rem->GetResultType();
3213 switch (type) {
3214 case Primitive::kPrimInt:
3215 case Primitive::kPrimLong: {
3216 GenerateDivRemIntegral(rem);
3217 break;
3218 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003219 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003220 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003221 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003222 break;
3223 }
3224 default:
3225 LOG(FATAL) << "Unexpected rem type " << type;
3226 }
3227}
3228
Calin Juravled0d48522014-11-04 16:40:20 +00003229void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003230 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3231 ? LocationSummary::kCallOnSlowPath
3232 : LocationSummary::kNoCall;
3233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003234 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003235 case Primitive::kPrimByte:
3236 case Primitive::kPrimChar:
3237 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003238 case Primitive::kPrimInt: {
3239 locations->SetInAt(0, Location::Any());
3240 break;
3241 }
3242 case Primitive::kPrimLong: {
3243 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3244 if (!instruction->IsConstant()) {
3245 locations->AddTemp(Location::RequiresRegister());
3246 }
3247 break;
3248 }
3249 default:
3250 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3251 }
Calin Juravled0d48522014-11-04 16:40:20 +00003252 if (instruction->HasUses()) {
3253 locations->SetOut(Location::SameAsFirstInput());
3254 }
3255}
3256
3257void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003258 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003259 codegen_->AddSlowPath(slow_path);
3260
3261 LocationSummary* locations = instruction->GetLocations();
3262 Location value = locations->InAt(0);
3263
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003264 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003265 case Primitive::kPrimByte:
3266 case Primitive::kPrimChar:
3267 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003268 case Primitive::kPrimInt: {
3269 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003271 __ j(kEqual, slow_path->GetEntryLabel());
3272 } else if (value.IsStackSlot()) {
3273 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3274 __ j(kEqual, slow_path->GetEntryLabel());
3275 } else {
3276 DCHECK(value.IsConstant()) << value;
3277 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3278 __ jmp(slow_path->GetEntryLabel());
3279 }
3280 }
3281 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003282 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003283 case Primitive::kPrimLong: {
3284 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003285 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003286 __ movl(temp, value.AsRegisterPairLow<Register>());
3287 __ orl(temp, value.AsRegisterPairHigh<Register>());
3288 __ j(kEqual, slow_path->GetEntryLabel());
3289 } else {
3290 DCHECK(value.IsConstant()) << value;
3291 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3292 __ jmp(slow_path->GetEntryLabel());
3293 }
3294 }
3295 break;
3296 }
3297 default:
3298 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003299 }
Calin Juravled0d48522014-11-04 16:40:20 +00003300}
3301
Calin Juravle9aec02f2014-11-18 23:06:35 +00003302void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3303 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3304
3305 LocationSummary* locations =
3306 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3307
3308 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003309 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003310 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003311 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003312 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003313 // The shift count needs to be in CL or a constant.
3314 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003315 locations->SetOut(Location::SameAsFirstInput());
3316 break;
3317 }
3318 default:
3319 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3320 }
3321}
3322
3323void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3324 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3325
3326 LocationSummary* locations = op->GetLocations();
3327 Location first = locations->InAt(0);
3328 Location second = locations->InAt(1);
3329 DCHECK(first.Equals(locations->Out()));
3330
3331 switch (op->GetResultType()) {
3332 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003333 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003334 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003337 DCHECK_EQ(ECX, second_reg);
3338 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003339 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003340 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003341 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003342 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003343 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003344 }
3345 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003346 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3347 if (shift == 0) {
3348 return;
3349 }
3350 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003351 if (op->IsShl()) {
3352 __ shll(first_reg, imm);
3353 } else if (op->IsShr()) {
3354 __ sarl(first_reg, imm);
3355 } else {
3356 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003357 }
3358 }
3359 break;
3360 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003361 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003362 if (second.IsRegister()) {
3363 Register second_reg = second.AsRegister<Register>();
3364 DCHECK_EQ(ECX, second_reg);
3365 if (op->IsShl()) {
3366 GenerateShlLong(first, second_reg);
3367 } else if (op->IsShr()) {
3368 GenerateShrLong(first, second_reg);
3369 } else {
3370 GenerateUShrLong(first, second_reg);
3371 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003372 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003373 // Shift by a constant.
3374 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3375 // Nothing to do if the shift is 0, as the input is already the output.
3376 if (shift != 0) {
3377 if (op->IsShl()) {
3378 GenerateShlLong(first, shift);
3379 } else if (op->IsShr()) {
3380 GenerateShrLong(first, shift);
3381 } else {
3382 GenerateUShrLong(first, shift);
3383 }
3384 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003385 }
3386 break;
3387 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003388 default:
3389 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3390 }
3391}
3392
Mark P Mendell73945692015-04-29 14:56:17 +00003393void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3394 Register low = loc.AsRegisterPairLow<Register>();
3395 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003396 if (shift == 1) {
3397 // This is just an addition.
3398 __ addl(low, low);
3399 __ adcl(high, high);
3400 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003401 // Shift by 32 is easy. High gets low, and low gets 0.
3402 codegen_->EmitParallelMoves(
3403 loc.ToLow(),
3404 loc.ToHigh(),
3405 Primitive::kPrimInt,
3406 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3407 loc.ToLow(),
3408 Primitive::kPrimInt);
3409 } else if (shift > 32) {
3410 // Low part becomes 0. High part is low part << (shift-32).
3411 __ movl(high, low);
3412 __ shll(high, Immediate(shift - 32));
3413 __ xorl(low, low);
3414 } else {
3415 // Between 1 and 31.
3416 __ shld(high, low, Immediate(shift));
3417 __ shll(low, Immediate(shift));
3418 }
3419}
3420
Calin Juravle9aec02f2014-11-18 23:06:35 +00003421void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003422 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003423 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3424 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3425 __ testl(shifter, Immediate(32));
3426 __ j(kEqual, &done);
3427 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3428 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3429 __ Bind(&done);
3430}
3431
Mark P Mendell73945692015-04-29 14:56:17 +00003432void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3433 Register low = loc.AsRegisterPairLow<Register>();
3434 Register high = loc.AsRegisterPairHigh<Register>();
3435 if (shift == 32) {
3436 // Need to copy the sign.
3437 DCHECK_NE(low, high);
3438 __ movl(low, high);
3439 __ sarl(high, Immediate(31));
3440 } else if (shift > 32) {
3441 DCHECK_NE(low, high);
3442 // High part becomes sign. Low part is shifted by shift - 32.
3443 __ movl(low, high);
3444 __ sarl(high, Immediate(31));
3445 __ sarl(low, Immediate(shift - 32));
3446 } else {
3447 // Between 1 and 31.
3448 __ shrd(low, high, Immediate(shift));
3449 __ sarl(high, Immediate(shift));
3450 }
3451}
3452
Calin Juravle9aec02f2014-11-18 23:06:35 +00003453void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003454 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003455 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3456 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3457 __ testl(shifter, Immediate(32));
3458 __ j(kEqual, &done);
3459 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3460 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3461 __ Bind(&done);
3462}
3463
Mark P Mendell73945692015-04-29 14:56:17 +00003464void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3465 Register low = loc.AsRegisterPairLow<Register>();
3466 Register high = loc.AsRegisterPairHigh<Register>();
3467 if (shift == 32) {
3468 // Shift by 32 is easy. Low gets high, and high gets 0.
3469 codegen_->EmitParallelMoves(
3470 loc.ToHigh(),
3471 loc.ToLow(),
3472 Primitive::kPrimInt,
3473 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3474 loc.ToHigh(),
3475 Primitive::kPrimInt);
3476 } else if (shift > 32) {
3477 // Low part is high >> (shift - 32). High part becomes 0.
3478 __ movl(low, high);
3479 __ shrl(low, Immediate(shift - 32));
3480 __ xorl(high, high);
3481 } else {
3482 // Between 1 and 31.
3483 __ shrd(low, high, Immediate(shift));
3484 __ shrl(high, Immediate(shift));
3485 }
3486}
3487
Calin Juravle9aec02f2014-11-18 23:06:35 +00003488void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003489 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003490 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3491 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3492 __ testl(shifter, Immediate(32));
3493 __ j(kEqual, &done);
3494 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3495 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3496 __ Bind(&done);
3497}
3498
3499void LocationsBuilderX86::VisitShl(HShl* shl) {
3500 HandleShift(shl);
3501}
3502
3503void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3504 HandleShift(shl);
3505}
3506
3507void LocationsBuilderX86::VisitShr(HShr* shr) {
3508 HandleShift(shr);
3509}
3510
3511void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3512 HandleShift(shr);
3513}
3514
3515void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3516 HandleShift(ushr);
3517}
3518
3519void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3520 HandleShift(ushr);
3521}
3522
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003523void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003524 LocationSummary* locations =
3525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003526 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003527 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003528 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003529 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003530}
3531
3532void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3533 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003534 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003535 // Note: if heap poisoning is enabled, the entry point takes cares
3536 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003537 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3538 instruction,
3539 instruction->GetDexPc(),
3540 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003541 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003542}
3543
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003544void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3545 LocationSummary* locations =
3546 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3547 locations->SetOut(Location::RegisterLocation(EAX));
3548 InvokeRuntimeCallingConvention calling_convention;
3549 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003550 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003551 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003552}
3553
3554void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3555 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003556 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3557
Roland Levillain4d027112015-07-01 15:41:14 +01003558 // Note: if heap poisoning is enabled, the entry point takes cares
3559 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003560 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3561 instruction,
3562 instruction->GetDexPc(),
3563 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003564 DCHECK(!codegen_->IsLeafMethod());
3565}
3566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003567void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003570 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3571 if (location.IsStackSlot()) {
3572 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573 } else if (location.IsDoubleStackSlot()) {
3574 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003575 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003576 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003577}
3578
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003579void InstructionCodeGeneratorX86::VisitParameterValue(
3580 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3581}
3582
3583void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3584 LocationSummary* locations =
3585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3586 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3587}
3588
3589void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003590}
3591
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003592void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003593 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003594 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003595 locations->SetInAt(0, Location::RequiresRegister());
3596 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003597}
3598
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003599void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3600 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003601 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003602 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003603 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003604 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003605 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003607 break;
3608
3609 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003610 __ notl(out.AsRegisterPairLow<Register>());
3611 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003612 break;
3613
3614 default:
3615 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3616 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003617}
3618
David Brazdil66d126e2015-04-03 16:02:44 +01003619void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3620 LocationSummary* locations =
3621 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3622 locations->SetInAt(0, Location::RequiresRegister());
3623 locations->SetOut(Location::SameAsFirstInput());
3624}
3625
3626void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003627 LocationSummary* locations = bool_not->GetLocations();
3628 Location in = locations->InAt(0);
3629 Location out = locations->Out();
3630 DCHECK(in.Equals(out));
3631 __ xorl(out.AsRegister<Register>(), Immediate(1));
3632}
3633
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003634void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003637 switch (compare->InputAt(0)->GetType()) {
3638 case Primitive::kPrimLong: {
3639 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003640 locations->SetInAt(1, Location::Any());
3641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3642 break;
3643 }
3644 case Primitive::kPrimFloat:
3645 case Primitive::kPrimDouble: {
3646 locations->SetInAt(0, Location::RequiresFpuRegister());
3647 locations->SetInAt(1, Location::RequiresFpuRegister());
3648 locations->SetOut(Location::RequiresRegister());
3649 break;
3650 }
3651 default:
3652 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3653 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003654}
3655
3656void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003657 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003659 Location left = locations->InAt(0);
3660 Location right = locations->InAt(1);
3661
Mark Mendell0c9497d2015-08-21 09:30:05 -04003662 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003663 switch (compare->InputAt(0)->GetType()) {
3664 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003665 Register left_low = left.AsRegisterPairLow<Register>();
3666 Register left_high = left.AsRegisterPairHigh<Register>();
3667 int32_t val_low = 0;
3668 int32_t val_high = 0;
3669 bool right_is_const = false;
3670
3671 if (right.IsConstant()) {
3672 DCHECK(right.GetConstant()->IsLongConstant());
3673 right_is_const = true;
3674 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3675 val_low = Low32Bits(val);
3676 val_high = High32Bits(val);
3677 }
3678
Calin Juravleddb7df22014-11-25 20:56:51 +00003679 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003680 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003681 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003682 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003683 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003684 DCHECK(right_is_const) << right;
3685 if (val_high == 0) {
3686 __ testl(left_high, left_high);
3687 } else {
3688 __ cmpl(left_high, Immediate(val_high));
3689 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003690 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003691 __ j(kLess, &less); // Signed compare.
3692 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003693 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003694 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003695 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003696 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003697 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003698 DCHECK(right_is_const) << right;
3699 if (val_low == 0) {
3700 __ testl(left_low, left_low);
3701 } else {
3702 __ cmpl(left_low, Immediate(val_low));
3703 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003704 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003705 break;
3706 }
3707 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003709 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3710 break;
3711 }
3712 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003713 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003714 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003715 break;
3716 }
3717 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003718 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003719 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003720 __ movl(out, Immediate(0));
3721 __ j(kEqual, &done);
3722 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3723
3724 __ Bind(&greater);
3725 __ movl(out, Immediate(1));
3726 __ jmp(&done);
3727
3728 __ Bind(&less);
3729 __ movl(out, Immediate(-1));
3730
3731 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003732}
3733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003734void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003735 LocationSummary* locations =
3736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003737 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3738 locations->SetInAt(i, Location::Any());
3739 }
3740 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003741}
3742
3743void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003744 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003745 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003746}
3747
Calin Juravle52c48962014-12-16 17:02:57 +00003748void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3749 /*
3750 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3751 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3752 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3753 */
3754 switch (kind) {
3755 case MemBarrierKind::kAnyAny: {
3756 __ mfence();
3757 break;
3758 }
3759 case MemBarrierKind::kAnyStore:
3760 case MemBarrierKind::kLoadAny:
3761 case MemBarrierKind::kStoreStore: {
3762 // nop
3763 break;
3764 }
3765 default:
3766 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003767 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003768}
3769
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003770
Vladimir Marko58155012015-08-19 12:49:41 +00003771void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3772 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3773 switch (invoke->GetMethodLoadKind()) {
3774 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3775 // temp = thread->string_init_entrypoint
3776 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3777 break;
3778 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3779 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3780 break;
3781 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3782 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3783 break;
3784 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3785 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3786 method_patches_.emplace_back(invoke->GetTargetMethod());
3787 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3788 break;
3789 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3790 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3791 FALLTHROUGH_INTENDED;
3792 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3793 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3794 Register method_reg;
3795 Register reg = temp.AsRegister<Register>();
3796 if (current_method.IsRegister()) {
3797 method_reg = current_method.AsRegister<Register>();
3798 } else {
3799 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3800 DCHECK(!current_method.IsValid());
3801 method_reg = reg;
3802 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3803 }
3804 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003805 __ movl(reg, Address(method_reg,
3806 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003807 // temp = temp[index_in_cache]
3808 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3809 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3810 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003811 }
Vladimir Marko58155012015-08-19 12:49:41 +00003812 }
3813
3814 switch (invoke->GetCodePtrLocation()) {
3815 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3816 __ call(GetFrameEntryLabel());
3817 break;
3818 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3819 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3820 Label* label = &relative_call_patches_.back().label;
3821 __ call(label); // Bind to the patch label, override at link time.
3822 __ Bind(label); // Bind the label at the end of the "call" insn.
3823 break;
3824 }
3825 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3826 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3827 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3828 // (Though the direct CALL ptr16:32 is available for consideration).
3829 FALLTHROUGH_INTENDED;
3830 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3831 // (callee_method + offset_of_quick_compiled_code)()
3832 __ call(Address(callee_method.AsRegister<Register>(),
3833 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3834 kX86WordSize).Int32Value()));
3835 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003836 }
3837
3838 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003839}
3840
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003841void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3842 Register temp = temp_in.AsRegister<Register>();
3843 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3844 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3845 LocationSummary* locations = invoke->GetLocations();
3846 Location receiver = locations->InAt(0);
3847 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3848 // temp = object->GetClass();
3849 DCHECK(receiver.IsRegister());
3850 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3851 MaybeRecordImplicitNullCheck(invoke);
3852 __ MaybeUnpoisonHeapReference(temp);
3853 // temp = temp->GetMethodAt(method_offset);
3854 __ movl(temp, Address(temp, method_offset));
3855 // call temp->GetEntryPoint();
3856 __ call(Address(
3857 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3858}
3859
Vladimir Marko58155012015-08-19 12:49:41 +00003860void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3861 DCHECK(linker_patches->empty());
3862 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3863 for (const MethodPatchInfo<Label>& info : method_patches_) {
3864 // The label points to the end of the "movl" insn but the literal offset for method
3865 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3866 uint32_t literal_offset = info.label.Position() - 4;
3867 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3868 info.target_method.dex_file,
3869 info.target_method.dex_method_index));
3870 }
3871 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3872 // The label points to the end of the "call" insn but the literal offset for method
3873 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3874 uint32_t literal_offset = info.label.Position() - 4;
3875 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3876 info.target_method.dex_file,
3877 info.target_method.dex_method_index));
3878 }
3879}
3880
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003881void CodeGeneratorX86::MarkGCCard(Register temp,
3882 Register card,
3883 Register object,
3884 Register value,
3885 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003886 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003887 if (value_can_be_null) {
3888 __ testl(value, value);
3889 __ j(kEqual, &is_null);
3890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3892 __ movl(temp, object);
3893 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003894 __ movb(Address(temp, card, TIMES_1, 0),
3895 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003896 if (value_can_be_null) {
3897 __ Bind(&is_null);
3898 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003899}
3900
Calin Juravle52c48962014-12-16 17:02:57 +00003901void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3902 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003903 LocationSummary* locations =
3904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003905 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003906
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003907 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3908 locations->SetOut(Location::RequiresFpuRegister());
3909 } else {
3910 // The output overlaps in case of long: we don't want the low move to overwrite
3911 // the object's location.
3912 locations->SetOut(Location::RequiresRegister(),
3913 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3914 : Location::kNoOutputOverlap);
3915 }
Calin Juravle52c48962014-12-16 17:02:57 +00003916
3917 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3918 // Long values can be loaded atomically into an XMM using movsd.
3919 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3920 // and then copy the XMM into the output 32bits at a time).
3921 locations->AddTemp(Location::RequiresFpuRegister());
3922 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923}
3924
Calin Juravle52c48962014-12-16 17:02:57 +00003925void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3926 const FieldInfo& field_info) {
3927 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928
Calin Juravle52c48962014-12-16 17:02:57 +00003929 LocationSummary* locations = instruction->GetLocations();
3930 Register base = locations->InAt(0).AsRegister<Register>();
3931 Location out = locations->Out();
3932 bool is_volatile = field_info.IsVolatile();
3933 Primitive::Type field_type = field_info.GetFieldType();
3934 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3935
3936 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003937 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003938 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 break;
3940 }
3941
3942 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003943 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003944 break;
3945 }
3946
3947 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003948 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003949 break;
3950 }
3951
3952 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003953 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003954 break;
3955 }
3956
3957 case Primitive::kPrimInt:
3958 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003959 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 break;
3961 }
3962
3963 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003964 if (is_volatile) {
3965 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3966 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003967 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003968 __ movd(out.AsRegisterPairLow<Register>(), temp);
3969 __ psrlq(temp, Immediate(32));
3970 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3971 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003972 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003973 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003975 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3976 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003977 break;
3978 }
3979
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003980 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003981 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003982 break;
3983 }
3984
3985 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003986 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003987 break;
3988 }
3989
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003990 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003991 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003992 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003993 }
Calin Juravle52c48962014-12-16 17:02:57 +00003994
Calin Juravle77520bc2015-01-12 18:45:46 +00003995 // Longs are handled in the switch.
3996 if (field_type != Primitive::kPrimLong) {
3997 codegen_->MaybeRecordImplicitNullCheck(instruction);
3998 }
3999
Calin Juravle52c48962014-12-16 17:02:57 +00004000 if (is_volatile) {
4001 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4002 }
Roland Levillain4d027112015-07-01 15:41:14 +01004003
4004 if (field_type == Primitive::kPrimNot) {
4005 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
4006 }
Calin Juravle52c48962014-12-16 17:02:57 +00004007}
4008
4009void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4010 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4011
4012 LocationSummary* locations =
4013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4014 locations->SetInAt(0, Location::RequiresRegister());
4015 bool is_volatile = field_info.IsVolatile();
4016 Primitive::Type field_type = field_info.GetFieldType();
4017 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4018 || (field_type == Primitive::kPrimByte);
4019
4020 // The register allocator does not support multiple
4021 // inputs that die at entry with one in a specific register.
4022 if (is_byte_type) {
4023 // Ensure the value is in a byte register.
4024 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004025 } else if (Primitive::IsFloatingPointType(field_type)) {
4026 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004027 } else {
4028 locations->SetInAt(1, Location::RequiresRegister());
4029 }
Calin Juravle52c48962014-12-16 17:02:57 +00004030 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01004031 // Temporary registers for the write barrier.
4032 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00004033 // Ensure the card is in a byte register.
4034 locations->AddTemp(Location::RegisterLocation(ECX));
4035 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
4036 // 64bits value can be atomically written to an address with movsd and an XMM register.
4037 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4038 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4039 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4040 // isolated cases when we need this it isn't worth adding the extra complexity.
4041 locations->AddTemp(Location::RequiresFpuRegister());
4042 locations->AddTemp(Location::RequiresFpuRegister());
4043 }
4044}
4045
4046void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004047 const FieldInfo& field_info,
4048 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004049 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4050
4051 LocationSummary* locations = instruction->GetLocations();
4052 Register base = locations->InAt(0).AsRegister<Register>();
4053 Location value = locations->InAt(1);
4054 bool is_volatile = field_info.IsVolatile();
4055 Primitive::Type field_type = field_info.GetFieldType();
4056 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004057 bool needs_write_barrier =
4058 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004059
4060 if (is_volatile) {
4061 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4062 }
4063
4064 switch (field_type) {
4065 case Primitive::kPrimBoolean:
4066 case Primitive::kPrimByte: {
4067 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4068 break;
4069 }
4070
4071 case Primitive::kPrimShort:
4072 case Primitive::kPrimChar: {
4073 __ movw(Address(base, offset), value.AsRegister<Register>());
4074 break;
4075 }
4076
4077 case Primitive::kPrimInt:
4078 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004079 if (kPoisonHeapReferences && needs_write_barrier) {
4080 // Note that in the case where `value` is a null reference,
4081 // we do not enter this block, as the reference does not
4082 // need poisoning.
4083 DCHECK_EQ(field_type, Primitive::kPrimNot);
4084 Register temp = locations->GetTemp(0).AsRegister<Register>();
4085 __ movl(temp, value.AsRegister<Register>());
4086 __ PoisonHeapReference(temp);
4087 __ movl(Address(base, offset), temp);
4088 } else {
4089 __ movl(Address(base, offset), value.AsRegister<Register>());
4090 }
Calin Juravle52c48962014-12-16 17:02:57 +00004091 break;
4092 }
4093
4094 case Primitive::kPrimLong: {
4095 if (is_volatile) {
4096 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4097 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4098 __ movd(temp1, value.AsRegisterPairLow<Register>());
4099 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4100 __ punpckldq(temp1, temp2);
4101 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004103 } else {
4104 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004105 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004106 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4107 }
4108 break;
4109 }
4110
4111 case Primitive::kPrimFloat: {
4112 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4113 break;
4114 }
4115
4116 case Primitive::kPrimDouble: {
4117 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4118 break;
4119 }
4120
4121 case Primitive::kPrimVoid:
4122 LOG(FATAL) << "Unreachable type " << field_type;
4123 UNREACHABLE();
4124 }
4125
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 // Longs are handled in the switch.
4127 if (field_type != Primitive::kPrimLong) {
4128 codegen_->MaybeRecordImplicitNullCheck(instruction);
4129 }
4130
Roland Levillain4d027112015-07-01 15:41:14 +01004131 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004132 Register temp = locations->GetTemp(0).AsRegister<Register>();
4133 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004134 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 }
4136
Calin Juravle52c48962014-12-16 17:02:57 +00004137 if (is_volatile) {
4138 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4139 }
4140}
4141
4142void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4143 HandleFieldGet(instruction, instruction->GetFieldInfo());
4144}
4145
4146void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4147 HandleFieldGet(instruction, instruction->GetFieldInfo());
4148}
4149
4150void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4151 HandleFieldSet(instruction, instruction->GetFieldInfo());
4152}
4153
4154void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004155 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004156}
4157
4158void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4159 HandleFieldSet(instruction, instruction->GetFieldInfo());
4160}
4161
4162void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004163 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004164}
4165
4166void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4167 HandleFieldGet(instruction, instruction->GetFieldInfo());
4168}
4169
4170void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4171 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172}
4173
Calin Juravlee460d1d2015-09-29 04:52:17 +01004174void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4175 HUnresolvedInstanceFieldGet* instruction) {
4176 FieldAccessCallingConventionX86 calling_convention;
4177 codegen_->CreateUnresolvedFieldLocationSummary(
4178 instruction, instruction->GetFieldType(), calling_convention);
4179}
4180
4181void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4182 HUnresolvedInstanceFieldGet* instruction) {
4183 FieldAccessCallingConventionX86 calling_convention;
4184 codegen_->GenerateUnresolvedFieldAccess(instruction,
4185 instruction->GetFieldType(),
4186 instruction->GetFieldIndex(),
4187 instruction->GetDexPc(),
4188 calling_convention);
4189}
4190
4191void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4192 HUnresolvedInstanceFieldSet* instruction) {
4193 FieldAccessCallingConventionX86 calling_convention;
4194 codegen_->CreateUnresolvedFieldLocationSummary(
4195 instruction, instruction->GetFieldType(), calling_convention);
4196}
4197
4198void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4199 HUnresolvedInstanceFieldSet* instruction) {
4200 FieldAccessCallingConventionX86 calling_convention;
4201 codegen_->GenerateUnresolvedFieldAccess(instruction,
4202 instruction->GetFieldType(),
4203 instruction->GetFieldIndex(),
4204 instruction->GetDexPc(),
4205 calling_convention);
4206}
4207
4208void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4209 HUnresolvedStaticFieldGet* instruction) {
4210 FieldAccessCallingConventionX86 calling_convention;
4211 codegen_->CreateUnresolvedFieldLocationSummary(
4212 instruction, instruction->GetFieldType(), calling_convention);
4213}
4214
4215void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4216 HUnresolvedStaticFieldGet* instruction) {
4217 FieldAccessCallingConventionX86 calling_convention;
4218 codegen_->GenerateUnresolvedFieldAccess(instruction,
4219 instruction->GetFieldType(),
4220 instruction->GetFieldIndex(),
4221 instruction->GetDexPc(),
4222 calling_convention);
4223}
4224
4225void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4226 HUnresolvedStaticFieldSet* instruction) {
4227 FieldAccessCallingConventionX86 calling_convention;
4228 codegen_->CreateUnresolvedFieldLocationSummary(
4229 instruction, instruction->GetFieldType(), calling_convention);
4230}
4231
4232void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4233 HUnresolvedStaticFieldSet* instruction) {
4234 FieldAccessCallingConventionX86 calling_convention;
4235 codegen_->GenerateUnresolvedFieldAccess(instruction,
4236 instruction->GetFieldType(),
4237 instruction->GetFieldIndex(),
4238 instruction->GetDexPc(),
4239 calling_convention);
4240}
4241
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004243 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4244 ? LocationSummary::kCallOnSlowPath
4245 : LocationSummary::kNoCall;
4246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4247 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004248 ? Location::RequiresRegister()
4249 : Location::Any();
4250 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004251 if (instruction->HasUses()) {
4252 locations->SetOut(Location::SameAsFirstInput());
4253 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254}
4255
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004256void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004257 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4258 return;
4259 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004260 LocationSummary* locations = instruction->GetLocations();
4261 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004262
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004263 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4264 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4265}
4266
4267void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004268 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 codegen_->AddSlowPath(slow_path);
4270
4271 LocationSummary* locations = instruction->GetLocations();
4272 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273
4274 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004275 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004276 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004278 } else {
4279 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004280 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004281 __ jmp(slow_path->GetEntryLabel());
4282 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283 }
4284 __ j(kEqual, slow_path->GetEntryLabel());
4285}
4286
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004287void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004288 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004289 GenerateImplicitNullCheck(instruction);
4290 } else {
4291 GenerateExplicitNullCheck(instruction);
4292 }
4293}
4294
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004295void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004296 LocationSummary* locations =
4297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004298 locations->SetInAt(0, Location::RequiresRegister());
4299 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004300 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4301 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4302 } else {
4303 // The output overlaps in case of long: we don't want the low move to overwrite
4304 // the array's location.
4305 locations->SetOut(Location::RequiresRegister(),
4306 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4307 : Location::kNoOutputOverlap);
4308 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309}
4310
4311void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4312 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004313 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004314 Location index = locations->InAt(1);
4315
Calin Juravle77520bc2015-01-12 18:45:46 +00004316 Primitive::Type type = instruction->GetType();
4317 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004318 case Primitive::kPrimBoolean: {
4319 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004320 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004321 if (index.IsConstant()) {
4322 __ movzxb(out, Address(obj,
4323 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4324 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004325 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004326 }
4327 break;
4328 }
4329
4330 case Primitive::kPrimByte: {
4331 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004332 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004333 if (index.IsConstant()) {
4334 __ movsxb(out, Address(obj,
4335 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4336 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004337 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 }
4339 break;
4340 }
4341
4342 case Primitive::kPrimShort: {
4343 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004344 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004345 if (index.IsConstant()) {
4346 __ movsxw(out, Address(obj,
4347 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4348 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004349 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350 }
4351 break;
4352 }
4353
4354 case Primitive::kPrimChar: {
4355 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004357 if (index.IsConstant()) {
4358 __ movzxw(out, Address(obj,
4359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4360 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004361 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004362 }
4363 break;
4364 }
4365
4366 case Primitive::kPrimInt:
4367 case Primitive::kPrimNot: {
4368 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004369 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 if (index.IsConstant()) {
4371 __ movl(out, Address(obj,
4372 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4373 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004374 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004375 }
4376 break;
4377 }
4378
4379 case Primitive::kPrimLong: {
4380 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004381 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004382 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 if (index.IsConstant()) {
4384 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004385 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004386 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004387 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004388 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004389 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004391 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004392 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394 }
4395 break;
4396 }
4397
Mark Mendell7c8d0092015-01-26 11:21:33 -05004398 case Primitive::kPrimFloat: {
4399 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4400 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4401 if (index.IsConstant()) {
4402 __ movss(out, Address(obj,
4403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4404 } else {
4405 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4406 }
4407 break;
4408 }
4409
4410 case Primitive::kPrimDouble: {
4411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4412 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4413 if (index.IsConstant()) {
4414 __ movsd(out, Address(obj,
4415 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4416 } else {
4417 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4418 }
4419 break;
4420 }
4421
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004422 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004423 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004424 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004426
4427 if (type != Primitive::kPrimLong) {
4428 codegen_->MaybeRecordImplicitNullCheck(instruction);
4429 }
Roland Levillain4d027112015-07-01 15:41:14 +01004430
4431 if (type == Primitive::kPrimNot) {
4432 Register out = locations->Out().AsRegister<Register>();
4433 __ MaybeUnpoisonHeapReference(out);
4434 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004435}
4436
4437void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004438 // This location builder might end up asking to up to four registers, which is
4439 // not currently possible for baseline. The situation in which we need four
4440 // registers cannot be met by baseline though, because it has not run any
4441 // optimization.
4442
Nicolas Geoffray39468442014-09-02 15:17:15 +01004443 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004444 bool needs_write_barrier =
4445 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4446
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004447 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004448
Nicolas Geoffray39468442014-09-02 15:17:15 +01004449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4450 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004451 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004452
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004453 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4454 || (value_type == Primitive::kPrimByte);
4455 // We need the inputs to be different than the output in case of long operation.
4456 // In case of a byte operation, the register allocator does not support multiple
4457 // inputs that die at entry with one in a specific register.
4458 locations->SetInAt(0, Location::RequiresRegister());
4459 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4460 if (is_byte_type) {
4461 // Ensure the value is in a byte register.
4462 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4463 } else if (Primitive::IsFloatingPointType(value_type)) {
4464 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004466 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4467 }
4468 if (needs_write_barrier) {
4469 // Temporary registers for the write barrier.
4470 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4471 // Ensure the card is in a byte register.
4472 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474}
4475
4476void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4477 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004478 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004480 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004481 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004482 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4483 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4484 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4485 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004486 bool needs_write_barrier =
4487 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004488
4489 switch (value_type) {
4490 case Primitive::kPrimBoolean:
4491 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004492 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4493 Address address = index.IsConstant()
4494 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4495 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4496 if (value.IsRegister()) {
4497 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004498 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004499 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004502 break;
4503 }
4504
4505 case Primitive::kPrimShort:
4506 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004507 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4508 Address address = index.IsConstant()
4509 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4510 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4511 if (value.IsRegister()) {
4512 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004513 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004514 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004515 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 break;
4518 }
4519
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004520 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004521 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4522 Address address = index.IsConstant()
4523 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4524 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4525 if (!value.IsRegister()) {
4526 // Just setting null.
4527 DCHECK(instruction->InputAt(2)->IsNullConstant());
4528 DCHECK(value.IsConstant()) << value;
4529 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004530 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004531 DCHECK(!needs_write_barrier);
4532 DCHECK(!may_need_runtime_call);
4533 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004535
4536 DCHECK(needs_write_barrier);
4537 Register register_value = value.AsRegister<Register>();
4538 NearLabel done, not_null, do_put;
4539 SlowPathCode* slow_path = nullptr;
4540 Register temp = locations->GetTemp(0).AsRegister<Register>();
4541 if (may_need_runtime_call) {
4542 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4543 codegen_->AddSlowPath(slow_path);
4544 if (instruction->GetValueCanBeNull()) {
4545 __ testl(register_value, register_value);
4546 __ j(kNotEqual, &not_null);
4547 __ movl(address, Immediate(0));
4548 codegen_->MaybeRecordImplicitNullCheck(instruction);
4549 __ jmp(&done);
4550 __ Bind(&not_null);
4551 }
4552
4553 __ movl(temp, Address(array, class_offset));
4554 codegen_->MaybeRecordImplicitNullCheck(instruction);
4555 __ MaybeUnpoisonHeapReference(temp);
4556 __ movl(temp, Address(temp, component_offset));
4557 // No need to poison/unpoison, we're comparing two poisoned references.
4558 __ cmpl(temp, Address(register_value, class_offset));
4559 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4560 __ j(kEqual, &do_put);
4561 __ MaybeUnpoisonHeapReference(temp);
4562 __ movl(temp, Address(temp, super_offset));
4563 // No need to unpoison, we're comparing against null..
4564 __ testl(temp, temp);
4565 __ j(kNotEqual, slow_path->GetEntryLabel());
4566 __ Bind(&do_put);
4567 } else {
4568 __ j(kNotEqual, slow_path->GetEntryLabel());
4569 }
4570 }
4571
4572 if (kPoisonHeapReferences) {
4573 __ movl(temp, register_value);
4574 __ PoisonHeapReference(temp);
4575 __ movl(address, temp);
4576 } else {
4577 __ movl(address, register_value);
4578 }
4579 if (!may_need_runtime_call) {
4580 codegen_->MaybeRecordImplicitNullCheck(instruction);
4581 }
4582
4583 Register card = locations->GetTemp(1).AsRegister<Register>();
4584 codegen_->MarkGCCard(
4585 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4586 __ Bind(&done);
4587
4588 if (slow_path != nullptr) {
4589 __ Bind(slow_path->GetExitLabel());
4590 }
4591
4592 break;
4593 }
4594 case Primitive::kPrimInt: {
4595 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4596 Address address = index.IsConstant()
4597 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4598 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4599 if (value.IsRegister()) {
4600 __ movl(address, value.AsRegister<Register>());
4601 } else {
4602 DCHECK(value.IsConstant()) << value;
4603 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4604 __ movl(address, Immediate(v));
4605 }
4606 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 break;
4608 }
4609
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 case Primitive::kPrimLong: {
4611 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004612 if (index.IsConstant()) {
4613 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004614 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004615 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004616 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004617 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004618 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004619 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004620 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004621 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004622 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004623 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004626 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004628 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004629 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004630 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004631 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004632 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004633 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004634 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004635 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004636 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004639 Immediate(High32Bits(val)));
4640 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004641 }
4642 break;
4643 }
4644
Mark Mendell7c8d0092015-01-26 11:21:33 -05004645 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004646 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4647 Address address = index.IsConstant()
4648 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4649 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004650 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004651 __ movss(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004652 break;
4653 }
4654
4655 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004656 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4657 Address address = index.IsConstant()
4658 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4659 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004660 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004662 break;
4663 }
4664
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665 case Primitive::kPrimVoid:
4666 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004667 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004668 }
4669}
4670
4671void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004673 locations->SetInAt(0, Location::RequiresRegister());
4674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675}
4676
4677void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4678 LocationSummary* locations = instruction->GetLocations();
4679 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004680 Register obj = locations->InAt(0).AsRegister<Register>();
4681 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684}
4685
4686void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004687 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4688 ? LocationSummary::kCallOnSlowPath
4689 : LocationSummary::kNoCall;
4690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004691 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004692 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004693 if (instruction->HasUses()) {
4694 locations->SetOut(Location::SameAsFirstInput());
4695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004696}
4697
4698void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4699 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004700 Location index_loc = locations->InAt(0);
4701 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004702 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004703 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704
Mark Mendell99dbd682015-04-22 16:18:52 -04004705 if (length_loc.IsConstant()) {
4706 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4707 if (index_loc.IsConstant()) {
4708 // BCE will remove the bounds check if we are guarenteed to pass.
4709 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4710 if (index < 0 || index >= length) {
4711 codegen_->AddSlowPath(slow_path);
4712 __ jmp(slow_path->GetEntryLabel());
4713 } else {
4714 // Some optimization after BCE may have generated this, and we should not
4715 // generate a bounds check if it is a valid range.
4716 }
4717 return;
4718 }
4719
4720 // We have to reverse the jump condition because the length is the constant.
4721 Register index_reg = index_loc.AsRegister<Register>();
4722 __ cmpl(index_reg, Immediate(length));
4723 codegen_->AddSlowPath(slow_path);
4724 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004725 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004726 Register length = length_loc.AsRegister<Register>();
4727 if (index_loc.IsConstant()) {
4728 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4729 __ cmpl(length, Immediate(value));
4730 } else {
4731 __ cmpl(length, index_loc.AsRegister<Register>());
4732 }
4733 codegen_->AddSlowPath(slow_path);
4734 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004735 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004736}
4737
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004738void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4739 temp->SetLocations(nullptr);
4740}
4741
4742void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4743 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004744 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004745}
4746
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004747void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004748 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004749 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004750}
4751
4752void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004753 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4754}
4755
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004756void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4758}
4759
4760void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004761 HBasicBlock* block = instruction->GetBlock();
4762 if (block->GetLoopInformation() != nullptr) {
4763 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4764 // The back edge will generate the suspend check.
4765 return;
4766 }
4767 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4768 // The goto will generate the suspend check.
4769 return;
4770 }
4771 GenerateSuspendCheck(instruction, nullptr);
4772}
4773
4774void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4775 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004776 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004777 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4778 if (slow_path == nullptr) {
4779 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4780 instruction->SetSlowPath(slow_path);
4781 codegen_->AddSlowPath(slow_path);
4782 if (successor != nullptr) {
4783 DCHECK(successor->IsLoopHeader());
4784 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4785 }
4786 } else {
4787 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4788 }
4789
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004790 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004791 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004792 if (successor == nullptr) {
4793 __ j(kNotEqual, slow_path->GetEntryLabel());
4794 __ Bind(slow_path->GetReturnLabel());
4795 } else {
4796 __ j(kEqual, codegen_->GetLabelOf(successor));
4797 __ jmp(slow_path->GetEntryLabel());
4798 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004799}
4800
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004801X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4802 return codegen_->GetAssembler();
4803}
4804
Mark Mendell7c8d0092015-01-26 11:21:33 -05004805void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004806 ScratchRegisterScope ensure_scratch(
4807 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4808 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4809 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4810 __ movl(temp_reg, Address(ESP, src + stack_offset));
4811 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004812}
4813
4814void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004815 ScratchRegisterScope ensure_scratch(
4816 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4817 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4818 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4819 __ movl(temp_reg, Address(ESP, src + stack_offset));
4820 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4821 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4822 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004823}
4824
4825void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004826 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004827 Location source = move->GetSource();
4828 Location destination = move->GetDestination();
4829
4830 if (source.IsRegister()) {
4831 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004832 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004833 } else {
4834 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004835 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004836 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004837 } else if (source.IsFpuRegister()) {
4838 if (destination.IsFpuRegister()) {
4839 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4840 } else if (destination.IsStackSlot()) {
4841 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4842 } else {
4843 DCHECK(destination.IsDoubleStackSlot());
4844 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4845 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004846 } else if (source.IsStackSlot()) {
4847 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004848 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004849 } else if (destination.IsFpuRegister()) {
4850 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004851 } else {
4852 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004853 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4854 }
4855 } else if (source.IsDoubleStackSlot()) {
4856 if (destination.IsFpuRegister()) {
4857 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4858 } else {
4859 DCHECK(destination.IsDoubleStackSlot()) << destination;
4860 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004861 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004862 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004863 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004864 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004865 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004866 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004867 if (value == 0) {
4868 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4869 } else {
4870 __ movl(destination.AsRegister<Register>(), Immediate(value));
4871 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004872 } else {
4873 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004874 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004875 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004876 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004877 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004878 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004879 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004880 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004881 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4882 if (value == 0) {
4883 // Easy handling of 0.0.
4884 __ xorps(dest, dest);
4885 } else {
4886 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004887 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4888 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4889 __ movl(temp, Immediate(value));
4890 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004891 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004892 } else {
4893 DCHECK(destination.IsStackSlot()) << destination;
4894 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4895 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004896 } else if (constant->IsLongConstant()) {
4897 int64_t value = constant->AsLongConstant()->GetValue();
4898 int32_t low_value = Low32Bits(value);
4899 int32_t high_value = High32Bits(value);
4900 Immediate low(low_value);
4901 Immediate high(high_value);
4902 if (destination.IsDoubleStackSlot()) {
4903 __ movl(Address(ESP, destination.GetStackIndex()), low);
4904 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4905 } else {
4906 __ movl(destination.AsRegisterPairLow<Register>(), low);
4907 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4908 }
4909 } else {
4910 DCHECK(constant->IsDoubleConstant());
4911 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004912 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004913 int32_t low_value = Low32Bits(value);
4914 int32_t high_value = High32Bits(value);
4915 Immediate low(low_value);
4916 Immediate high(high_value);
4917 if (destination.IsFpuRegister()) {
4918 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4919 if (value == 0) {
4920 // Easy handling of 0.0.
4921 __ xorpd(dest, dest);
4922 } else {
4923 __ pushl(high);
4924 __ pushl(low);
4925 __ movsd(dest, Address(ESP, 0));
4926 __ addl(ESP, Immediate(8));
4927 }
4928 } else {
4929 DCHECK(destination.IsDoubleStackSlot()) << destination;
4930 __ movl(Address(ESP, destination.GetStackIndex()), low);
4931 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4932 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004933 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004934 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004935 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004936 }
4937}
4938
Mark Mendella5c19ce2015-04-01 12:51:05 -04004939void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004940 Register suggested_scratch = reg == EAX ? EBX : EAX;
4941 ScratchRegisterScope ensure_scratch(
4942 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4943
4944 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4945 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4946 __ movl(Address(ESP, mem + stack_offset), reg);
4947 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004948}
4949
Mark Mendell7c8d0092015-01-26 11:21:33 -05004950void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004951 ScratchRegisterScope ensure_scratch(
4952 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4953
4954 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4955 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4956 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4957 __ movss(Address(ESP, mem + stack_offset), reg);
4958 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004959}
4960
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004961void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004962 ScratchRegisterScope ensure_scratch1(
4963 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004964
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004965 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4966 ScratchRegisterScope ensure_scratch2(
4967 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004968
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004969 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4970 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4971 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4972 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4973 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4974 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004975}
4976
4977void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004978 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004979 Location source = move->GetSource();
4980 Location destination = move->GetDestination();
4981
4982 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004983 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4984 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4985 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4986 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4987 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004988 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004990 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004991 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004992 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4993 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004994 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4995 // Use XOR Swap algorithm to avoid a temporary.
4996 DCHECK_NE(source.reg(), destination.reg());
4997 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4998 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4999 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5000 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5001 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5002 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5003 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005004 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5005 // Take advantage of the 16 bytes in the XMM register.
5006 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5007 Address stack(ESP, destination.GetStackIndex());
5008 // Load the double into the high doubleword.
5009 __ movhpd(reg, stack);
5010
5011 // Store the low double into the destination.
5012 __ movsd(stack, reg);
5013
5014 // Move the high double to the low double.
5015 __ psrldq(reg, Immediate(8));
5016 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5017 // Take advantage of the 16 bytes in the XMM register.
5018 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5019 Address stack(ESP, source.GetStackIndex());
5020 // Load the double into the high doubleword.
5021 __ movhpd(reg, stack);
5022
5023 // Store the low double into the destination.
5024 __ movsd(stack, reg);
5025
5026 // Move the high double to the low double.
5027 __ psrldq(reg, Immediate(8));
5028 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5029 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5030 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005031 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005032 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005033 }
5034}
5035
5036void ParallelMoveResolverX86::SpillScratch(int reg) {
5037 __ pushl(static_cast<Register>(reg));
5038}
5039
5040void ParallelMoveResolverX86::RestoreScratch(int reg) {
5041 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005042}
5043
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005044void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005045 InvokeRuntimeCallingConvention calling_convention;
5046 CodeGenerator::CreateLoadClassLocationSummary(
5047 cls,
5048 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5049 Location::RegisterLocation(EAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050}
5051
5052void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005053 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005054 if (cls->NeedsAccessCheck()) {
5055 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5056 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5057 cls,
5058 cls->GetDexPc(),
5059 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005060 return;
5061 }
5062
5063 Register out = locations->Out().AsRegister<Register>();
5064 Register current_method = locations->InAt(0).AsRegister<Register>();
5065 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005066 DCHECK(!cls->CanCallRuntime());
5067 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07005068 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005069 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005070 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005071 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01005072 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005073 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005074 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005075
Andreas Gampe85b62f22015-09-09 13:15:38 -07005076 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005077 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5078 codegen_->AddSlowPath(slow_path);
5079 __ testl(out, out);
5080 __ j(kEqual, slow_path->GetEntryLabel());
5081 if (cls->MustGenerateClinitCheck()) {
5082 GenerateClassInitializationCheck(slow_path, out);
5083 } else {
5084 __ Bind(slow_path->GetExitLabel());
5085 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005086 }
5087}
5088
5089void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5090 LocationSummary* locations =
5091 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5092 locations->SetInAt(0, Location::RequiresRegister());
5093 if (check->HasUses()) {
5094 locations->SetOut(Location::SameAsFirstInput());
5095 }
5096}
5097
5098void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005099 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005100 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005101 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005102 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005103 GenerateClassInitializationCheck(slow_path,
5104 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005105}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005106
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005107void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005108 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005109 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5110 Immediate(mirror::Class::kStatusInitialized));
5111 __ j(kLess, slow_path->GetEntryLabel());
5112 __ Bind(slow_path->GetExitLabel());
5113 // No need for memory fence, thanks to the X86 memory model.
5114}
5115
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005116void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5117 LocationSummary* locations =
5118 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005119 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005120 locations->SetOut(Location::RequiresRegister());
5121}
5122
5123void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005124 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005125 codegen_->AddSlowPath(slow_path);
5126
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005127 LocationSummary* locations = load->GetLocations();
5128 Register out = locations->Out().AsRegister<Register>();
5129 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005130 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005131 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005132 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005133 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005134 __ testl(out, out);
5135 __ j(kEqual, slow_path->GetEntryLabel());
5136 __ Bind(slow_path->GetExitLabel());
5137}
5138
David Brazdilcb1c0552015-08-04 16:22:25 +01005139static Address GetExceptionTlsAddress() {
5140 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5141}
5142
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005143void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5144 LocationSummary* locations =
5145 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5146 locations->SetOut(Location::RequiresRegister());
5147}
5148
5149void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005150 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5151}
5152
5153void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5154 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5155}
5156
5157void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5158 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005159}
5160
5161void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5162 LocationSummary* locations =
5163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5164 InvokeRuntimeCallingConvention calling_convention;
5165 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5166}
5167
5168void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005169 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5170 instruction,
5171 instruction->GetDexPc(),
5172 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005173}
5174
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005175void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005176 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5177 switch (instruction->GetTypeCheckKind()) {
5178 case TypeCheckKind::kExactCheck:
5179 case TypeCheckKind::kAbstractClassCheck:
5180 case TypeCheckKind::kClassHierarchyCheck:
5181 case TypeCheckKind::kArrayObjectCheck:
5182 call_kind = LocationSummary::kNoCall;
5183 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005184 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005185 case TypeCheckKind::kInterfaceCheck:
5186 call_kind = LocationSummary::kCall;
5187 break;
5188 case TypeCheckKind::kArrayCheck:
5189 call_kind = LocationSummary::kCallOnSlowPath;
5190 break;
5191 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005193 if (call_kind != LocationSummary::kCall) {
5194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetInAt(1, Location::Any());
5196 // Note that TypeCheckSlowPathX86 uses this register too.
5197 locations->SetOut(Location::RequiresRegister());
5198 } else {
5199 InvokeRuntimeCallingConvention calling_convention;
5200 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5201 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5202 locations->SetOut(Location::RegisterLocation(EAX));
5203 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005204}
5205
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005206void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005207 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005209 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005211 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005212 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5213 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5214 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005215 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005216 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005217
5218 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005219 // Avoid null check if we know obj is not null.
5220 if (instruction->MustDoNullCheck()) {
5221 __ testl(obj, obj);
5222 __ j(kEqual, &zero);
5223 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005224
Calin Juravle98893e12015-10-02 21:05:03 +01005225 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005226 // This is safe, as the register is caller-save, and the object must be in another
5227 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01005228 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
5229 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005230 ? obj
5231 : out;
5232 __ movl(target, Address(obj, class_offset));
5233 __ MaybeUnpoisonHeapReference(target);
5234
5235 switch (instruction->GetTypeCheckKind()) {
5236 case TypeCheckKind::kExactCheck: {
5237 if (cls.IsRegister()) {
5238 __ cmpl(out, cls.AsRegister<Register>());
5239 } else {
5240 DCHECK(cls.IsStackSlot()) << cls;
5241 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5242 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005243
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005244 // Classes must be equal for the instanceof to succeed.
5245 __ j(kNotEqual, &zero);
5246 __ movl(out, Immediate(1));
5247 __ jmp(&done);
5248 break;
5249 }
5250 case TypeCheckKind::kAbstractClassCheck: {
5251 // If the class is abstract, we eagerly fetch the super class of the
5252 // object to avoid doing a comparison we know will fail.
5253 NearLabel loop;
5254 __ Bind(&loop);
5255 __ movl(out, Address(out, super_offset));
5256 __ MaybeUnpoisonHeapReference(out);
5257 __ testl(out, out);
5258 // If `out` is null, we use it for the result, and jump to `done`.
5259 __ j(kEqual, &done);
5260 if (cls.IsRegister()) {
5261 __ cmpl(out, cls.AsRegister<Register>());
5262 } else {
5263 DCHECK(cls.IsStackSlot()) << cls;
5264 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5265 }
5266 __ j(kNotEqual, &loop);
5267 __ movl(out, Immediate(1));
5268 if (zero.IsLinked()) {
5269 __ jmp(&done);
5270 }
5271 break;
5272 }
5273 case TypeCheckKind::kClassHierarchyCheck: {
5274 // Walk over the class hierarchy to find a match.
5275 NearLabel loop, success;
5276 __ Bind(&loop);
5277 if (cls.IsRegister()) {
5278 __ cmpl(out, cls.AsRegister<Register>());
5279 } else {
5280 DCHECK(cls.IsStackSlot()) << cls;
5281 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5282 }
5283 __ j(kEqual, &success);
5284 __ movl(out, Address(out, super_offset));
5285 __ MaybeUnpoisonHeapReference(out);
5286 __ testl(out, out);
5287 __ j(kNotEqual, &loop);
5288 // If `out` is null, we use it for the result, and jump to `done`.
5289 __ jmp(&done);
5290 __ Bind(&success);
5291 __ movl(out, Immediate(1));
5292 if (zero.IsLinked()) {
5293 __ jmp(&done);
5294 }
5295 break;
5296 }
5297 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005298 // Do an exact check.
5299 NearLabel exact_check;
5300 if (cls.IsRegister()) {
5301 __ cmpl(out, cls.AsRegister<Register>());
5302 } else {
5303 DCHECK(cls.IsStackSlot()) << cls;
5304 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5305 }
5306 __ j(kEqual, &exact_check);
5307 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005308 __ movl(out, Address(out, component_offset));
5309 __ MaybeUnpoisonHeapReference(out);
5310 __ testl(out, out);
5311 // If `out` is null, we use it for the result, and jump to `done`.
5312 __ j(kEqual, &done);
5313 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5314 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005315 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005316 __ movl(out, Immediate(1));
5317 __ jmp(&done);
5318 break;
5319 }
5320 case TypeCheckKind::kArrayCheck: {
5321 if (cls.IsRegister()) {
5322 __ cmpl(out, cls.AsRegister<Register>());
5323 } else {
5324 DCHECK(cls.IsStackSlot()) << cls;
5325 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5326 }
5327 DCHECK(locations->OnlyCallsOnSlowPath());
5328 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5329 instruction, /* is_fatal */ false);
5330 codegen_->AddSlowPath(slow_path);
5331 __ j(kNotEqual, slow_path->GetEntryLabel());
5332 __ movl(out, Immediate(1));
5333 if (zero.IsLinked()) {
5334 __ jmp(&done);
5335 }
5336 break;
5337 }
Calin Juravle98893e12015-10-02 21:05:03 +01005338 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005339 case TypeCheckKind::kInterfaceCheck:
5340 default: {
5341 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5342 instruction,
5343 instruction->GetDexPc(),
5344 nullptr);
5345 if (zero.IsLinked()) {
5346 __ jmp(&done);
5347 }
5348 break;
5349 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005350 }
5351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005352 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005353 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005354 __ xorl(out, out);
5355 }
5356
5357 if (done.IsLinked()) {
5358 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005359 }
5360
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005361 if (slow_path != nullptr) {
5362 __ Bind(slow_path->GetExitLabel());
5363 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005364}
5365
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005366void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005367 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5368 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5369
5370 switch (instruction->GetTypeCheckKind()) {
5371 case TypeCheckKind::kExactCheck:
5372 case TypeCheckKind::kAbstractClassCheck:
5373 case TypeCheckKind::kClassHierarchyCheck:
5374 case TypeCheckKind::kArrayObjectCheck:
5375 call_kind = throws_into_catch
5376 ? LocationSummary::kCallOnSlowPath
5377 : LocationSummary::kNoCall;
5378 break;
5379 case TypeCheckKind::kInterfaceCheck:
Calin Juravle98893e12015-10-02 21:05:03 +01005380 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005381 call_kind = LocationSummary::kCall;
5382 break;
5383 case TypeCheckKind::kArrayCheck:
5384 call_kind = LocationSummary::kCallOnSlowPath;
5385 break;
5386 }
5387
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005389 instruction, call_kind);
5390 if (call_kind != LocationSummary::kCall) {
5391 locations->SetInAt(0, Location::RequiresRegister());
5392 locations->SetInAt(1, Location::Any());
5393 // Note that TypeCheckSlowPathX86 uses this register too.
5394 locations->AddTemp(Location::RequiresRegister());
5395 } else {
5396 InvokeRuntimeCallingConvention calling_convention;
5397 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5398 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5399 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005400}
5401
5402void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5403 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005404 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005405 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005406 Register temp = locations->WillCall()
5407 ? kNoRegister
5408 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005409
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005410 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5411 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5412 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5413 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5414 SlowPathCode* slow_path = nullptr;
5415
5416 if (!locations->WillCall()) {
5417 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5418 instruction, !locations->CanCall());
5419 codegen_->AddSlowPath(slow_path);
5420 }
5421
5422 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005423 // Avoid null check if we know obj is not null.
5424 if (instruction->MustDoNullCheck()) {
5425 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005426 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005427 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005428
5429 if (locations->WillCall()) {
5430 __ movl(obj, Address(obj, class_offset));
5431 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005432 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005433 __ movl(temp, Address(obj, class_offset));
5434 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005435 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005436
5437 switch (instruction->GetTypeCheckKind()) {
5438 case TypeCheckKind::kExactCheck:
5439 case TypeCheckKind::kArrayCheck: {
5440 if (cls.IsRegister()) {
5441 __ cmpl(temp, cls.AsRegister<Register>());
5442 } else {
5443 DCHECK(cls.IsStackSlot()) << cls;
5444 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5445 }
5446 // Jump to slow path for throwing the exception or doing a
5447 // more involved array check.
5448 __ j(kNotEqual, slow_path->GetEntryLabel());
5449 break;
5450 }
5451 case TypeCheckKind::kAbstractClassCheck: {
5452 // If the class is abstract, we eagerly fetch the super class of the
5453 // object to avoid doing a comparison we know will fail.
5454 NearLabel loop, success;
5455 __ Bind(&loop);
5456 __ movl(temp, Address(temp, super_offset));
5457 __ MaybeUnpoisonHeapReference(temp);
5458 __ testl(temp, temp);
5459 // Jump to the slow path to throw the exception.
5460 __ j(kEqual, slow_path->GetEntryLabel());
5461 if (cls.IsRegister()) {
5462 __ cmpl(temp, cls.AsRegister<Register>());
5463 } else {
5464 DCHECK(cls.IsStackSlot()) << cls;
5465 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5466 }
5467 __ j(kNotEqual, &loop);
5468 break;
5469 }
5470 case TypeCheckKind::kClassHierarchyCheck: {
5471 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005472 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005473 __ Bind(&loop);
5474 if (cls.IsRegister()) {
5475 __ cmpl(temp, cls.AsRegister<Register>());
5476 } else {
5477 DCHECK(cls.IsStackSlot()) << cls;
5478 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5479 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005480 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005481 __ movl(temp, Address(temp, super_offset));
5482 __ MaybeUnpoisonHeapReference(temp);
5483 __ testl(temp, temp);
5484 __ j(kNotEqual, &loop);
5485 // Jump to the slow path to throw the exception.
5486 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005487 break;
5488 }
5489 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005490 // Do an exact check.
5491 if (cls.IsRegister()) {
5492 __ cmpl(temp, cls.AsRegister<Register>());
5493 } else {
5494 DCHECK(cls.IsStackSlot()) << cls;
5495 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5496 }
5497 __ j(kEqual, &done);
5498 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 __ movl(temp, Address(temp, component_offset));
5500 __ MaybeUnpoisonHeapReference(temp);
5501 __ testl(temp, temp);
5502 __ j(kEqual, slow_path->GetEntryLabel());
5503 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5504 __ j(kNotEqual, slow_path->GetEntryLabel());
5505 break;
5506 }
Calin Juravle98893e12015-10-02 21:05:03 +01005507 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 case TypeCheckKind::kInterfaceCheck:
5509 default:
5510 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5511 instruction,
5512 instruction->GetDexPc(),
5513 nullptr);
5514 break;
5515 }
5516 __ Bind(&done);
5517
5518 if (slow_path != nullptr) {
5519 __ Bind(slow_path->GetExitLabel());
5520 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005521}
5522
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005523void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5524 LocationSummary* locations =
5525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5526 InvokeRuntimeCallingConvention calling_convention;
5527 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5528}
5529
5530void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005531 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5532 : QUICK_ENTRY_POINT(pUnlockObject),
5533 instruction,
5534 instruction->GetDexPc(),
5535 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005536}
5537
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005538void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5539void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5540void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5541
5542void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5543 LocationSummary* locations =
5544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5545 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5546 || instruction->GetResultType() == Primitive::kPrimLong);
5547 locations->SetInAt(0, Location::RequiresRegister());
5548 locations->SetInAt(1, Location::Any());
5549 locations->SetOut(Location::SameAsFirstInput());
5550}
5551
5552void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5553 HandleBitwiseOperation(instruction);
5554}
5555
5556void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5557 HandleBitwiseOperation(instruction);
5558}
5559
5560void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5561 HandleBitwiseOperation(instruction);
5562}
5563
5564void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5565 LocationSummary* locations = instruction->GetLocations();
5566 Location first = locations->InAt(0);
5567 Location second = locations->InAt(1);
5568 DCHECK(first.Equals(locations->Out()));
5569
5570 if (instruction->GetResultType() == Primitive::kPrimInt) {
5571 if (second.IsRegister()) {
5572 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005573 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005574 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005575 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005576 } else {
5577 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005578 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005579 }
5580 } else if (second.IsConstant()) {
5581 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005582 __ andl(first.AsRegister<Register>(),
5583 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005584 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005585 __ orl(first.AsRegister<Register>(),
5586 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005587 } else {
5588 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005589 __ xorl(first.AsRegister<Register>(),
5590 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005591 }
5592 } else {
5593 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005594 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005595 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005596 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005597 } else {
5598 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005599 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005600 }
5601 }
5602 } else {
5603 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5604 if (second.IsRegisterPair()) {
5605 if (instruction->IsAnd()) {
5606 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5607 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5608 } else if (instruction->IsOr()) {
5609 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5610 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5611 } else {
5612 DCHECK(instruction->IsXor());
5613 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5614 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5615 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005616 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005617 if (instruction->IsAnd()) {
5618 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5619 __ andl(first.AsRegisterPairHigh<Register>(),
5620 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5621 } else if (instruction->IsOr()) {
5622 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5623 __ orl(first.AsRegisterPairHigh<Register>(),
5624 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5625 } else {
5626 DCHECK(instruction->IsXor());
5627 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5628 __ xorl(first.AsRegisterPairHigh<Register>(),
5629 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5630 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005631 } else {
5632 DCHECK(second.IsConstant()) << second;
5633 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005634 int32_t low_value = Low32Bits(value);
5635 int32_t high_value = High32Bits(value);
5636 Immediate low(low_value);
5637 Immediate high(high_value);
5638 Register first_low = first.AsRegisterPairLow<Register>();
5639 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005640 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005641 if (low_value == 0) {
5642 __ xorl(first_low, first_low);
5643 } else if (low_value != -1) {
5644 __ andl(first_low, low);
5645 }
5646 if (high_value == 0) {
5647 __ xorl(first_high, first_high);
5648 } else if (high_value != -1) {
5649 __ andl(first_high, high);
5650 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005651 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005652 if (low_value != 0) {
5653 __ orl(first_low, low);
5654 }
5655 if (high_value != 0) {
5656 __ orl(first_high, high);
5657 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005658 } else {
5659 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005660 if (low_value != 0) {
5661 __ xorl(first_low, low);
5662 }
5663 if (high_value != 0) {
5664 __ xorl(first_high, high);
5665 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005666 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005667 }
5668 }
5669}
5670
Calin Juravleb1498f62015-02-16 13:13:29 +00005671void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5672 // Nothing to do, this should be removed during prepare for register allocator.
5673 UNUSED(instruction);
5674 LOG(FATAL) << "Unreachable";
5675}
5676
5677void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5678 // Nothing to do, this should be removed during prepare for register allocator.
5679 UNUSED(instruction);
5680 LOG(FATAL) << "Unreachable";
5681}
5682
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005683void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5684 DCHECK(codegen_->IsBaseline());
5685 LocationSummary* locations =
5686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5687 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5688}
5689
5690void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5691 DCHECK(codegen_->IsBaseline());
5692 // Will be generated at use site.
5693}
5694
Mark Mendellfe57faa2015-09-18 09:26:15 -04005695// Simple implementation of packed switch - generate cascaded compare/jumps.
5696void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5697 LocationSummary* locations =
5698 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5699 locations->SetInAt(0, Location::RequiresRegister());
5700}
5701
5702void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5703 int32_t lower_bound = switch_instr->GetStartValue();
5704 int32_t num_entries = switch_instr->GetNumEntries();
5705 LocationSummary* locations = switch_instr->GetLocations();
5706 Register value_reg = locations->InAt(0).AsRegister<Register>();
5707 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5708
5709 // Create a series of compare/jumps.
5710 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5711 for (int i = 0; i < num_entries; i++) {
5712 int32_t case_value = lower_bound + i;
5713 if (case_value == 0) {
5714 __ testl(value_reg, value_reg);
5715 } else {
5716 __ cmpl(value_reg, Immediate(case_value));
5717 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005718 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005719 }
5720
5721 // And the default for any other value.
5722 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5723 __ jmp(codegen_->GetLabelOf(default_block));
5724 }
5725}
5726
Mark Mendell805b3b52015-09-18 14:10:29 -04005727void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5728 LocationSummary* locations =
5729 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5730 locations->SetInAt(0, Location::RequiresRegister());
5731
5732 // Constant area pointer.
5733 locations->SetInAt(1, Location::RequiresRegister());
5734
5735 // And the temporary we need.
5736 locations->AddTemp(Location::RequiresRegister());
5737}
5738
5739void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5740 int32_t lower_bound = switch_instr->GetStartValue();
5741 int32_t num_entries = switch_instr->GetNumEntries();
5742 LocationSummary* locations = switch_instr->GetLocations();
5743 Register value_reg = locations->InAt(0).AsRegister<Register>();
5744 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5745
5746 // Optimizing has a jump area.
5747 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5748 Register constant_area = locations->InAt(1).AsRegister<Register>();
5749
5750 // Remove the bias, if needed.
5751 if (lower_bound != 0) {
5752 __ leal(temp_reg, Address(value_reg, -lower_bound));
5753 value_reg = temp_reg;
5754 }
5755
5756 // Is the value in range?
5757 DCHECK_GE(num_entries, 1);
5758 __ cmpl(value_reg, Immediate(num_entries - 1));
5759 __ j(kAbove, codegen_->GetLabelOf(default_block));
5760
5761 // We are in the range of the table.
5762 // Load (target-constant_area) from the jump table, indexing by the value.
5763 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
5764
5765 // Compute the actual target address by adding in constant_area.
5766 __ addl(temp_reg, constant_area);
5767
5768 // And jump.
5769 __ jmp(temp_reg);
5770}
5771
Mark Mendell0616ae02015-04-17 12:49:27 -04005772void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5773 HX86ComputeBaseMethodAddress* insn) {
5774 LocationSummary* locations =
5775 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5776 locations->SetOut(Location::RequiresRegister());
5777}
5778
5779void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5780 HX86ComputeBaseMethodAddress* insn) {
5781 LocationSummary* locations = insn->GetLocations();
5782 Register reg = locations->Out().AsRegister<Register>();
5783
5784 // Generate call to next instruction.
5785 Label next_instruction;
5786 __ call(&next_instruction);
5787 __ Bind(&next_instruction);
5788
5789 // Remember this offset for later use with constant area.
5790 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5791
5792 // Grab the return address off the stack.
5793 __ popl(reg);
5794}
5795
5796void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5797 HX86LoadFromConstantTable* insn) {
5798 LocationSummary* locations =
5799 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5800
5801 locations->SetInAt(0, Location::RequiresRegister());
5802 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5803
5804 // If we don't need to be materialized, we only need the inputs to be set.
5805 if (!insn->NeedsMaterialization()) {
5806 return;
5807 }
5808
5809 switch (insn->GetType()) {
5810 case Primitive::kPrimFloat:
5811 case Primitive::kPrimDouble:
5812 locations->SetOut(Location::RequiresFpuRegister());
5813 break;
5814
5815 case Primitive::kPrimInt:
5816 locations->SetOut(Location::RequiresRegister());
5817 break;
5818
5819 default:
5820 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5821 }
5822}
5823
5824void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5825 if (!insn->NeedsMaterialization()) {
5826 return;
5827 }
5828
5829 LocationSummary* locations = insn->GetLocations();
5830 Location out = locations->Out();
5831 Register const_area = locations->InAt(0).AsRegister<Register>();
5832 HConstant *value = insn->GetConstant();
5833
5834 switch (insn->GetType()) {
5835 case Primitive::kPrimFloat:
5836 __ movss(out.AsFpuRegister<XmmRegister>(),
5837 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5838 break;
5839
5840 case Primitive::kPrimDouble:
5841 __ movsd(out.AsFpuRegister<XmmRegister>(),
5842 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5843 break;
5844
5845 case Primitive::kPrimInt:
5846 __ movl(out.AsRegister<Register>(),
5847 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5848 break;
5849
5850 default:
5851 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5852 }
5853}
5854
Mark Mendell0616ae02015-04-17 12:49:27 -04005855/**
5856 * Class to handle late fixup of offsets into constant area.
5857 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005858class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04005859 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04005860 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
5861 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5862
5863 protected:
5864 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5865
5866 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005867
5868 private:
5869 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5870 // Patch the correct offset for the instruction. The place to patch is the
5871 // last 4 bytes of the instruction.
5872 // The value to patch is the distance from the offset in the constant area
5873 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04005874 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5875 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04005876
5877 // Patch in the right value.
5878 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5879 }
5880
Mark Mendell0616ae02015-04-17 12:49:27 -04005881 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04005882 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005883};
5884
Mark Mendell805b3b52015-09-18 14:10:29 -04005885/**
5886 * Class to handle late fixup of offsets to a jump table that will be created in the
5887 * constant area.
5888 */
5889class JumpTableRIPFixup : public RIPFixup {
5890 public:
5891 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
5892 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
5893
5894 void CreateJumpTable() {
5895 X86Assembler* assembler = codegen_->GetAssembler();
5896
5897 // Ensure that the reference to the jump table has the correct offset.
5898 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5899 SetOffset(offset_in_constant_table);
5900
5901 // The label values in the jump table are computed relative to the
5902 // instruction addressing the constant area.
5903 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
5904
5905 // Populate the jump table with the correct values for the jump table.
5906 int32_t num_entries = switch_instr_->GetNumEntries();
5907 HBasicBlock* block = switch_instr_->GetBlock();
5908 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5909 // The value that we want is the target offset - the position of the table.
5910 for (int32_t i = 0; i < num_entries; i++) {
5911 HBasicBlock* b = successors[i];
5912 Label* l = codegen_->GetLabelOf(b);
5913 DCHECK(l->IsBound());
5914 int32_t offset_to_block = l->Position() - relative_offset;
5915 assembler->AppendInt32(offset_to_block);
5916 }
5917 }
5918
5919 private:
5920 const HX86PackedSwitch* switch_instr_;
5921};
5922
5923void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5924 // Generate the constant area if needed.
5925 X86Assembler* assembler = GetAssembler();
5926 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5927 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5928 // byte values.
5929 assembler->Align(4, 0);
5930 constant_area_start_ = assembler->CodeSize();
5931
5932 // Populate any jump tables.
5933 for (auto jump_table : fixups_to_jump_tables_) {
5934 jump_table->CreateJumpTable();
5935 }
5936
5937 // And now add the constant area to the generated code.
5938 assembler->AddConstantArea();
5939 }
5940
5941 // And finish up.
5942 CodeGenerator::Finalize(allocator);
5943}
5944
Mark Mendell0616ae02015-04-17 12:49:27 -04005945Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5946 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5947 return Address(reg, kDummy32BitOffset, fixup);
5948}
5949
5950Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5951 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5952 return Address(reg, kDummy32BitOffset, fixup);
5953}
5954
5955Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5956 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5957 return Address(reg, kDummy32BitOffset, fixup);
5958}
5959
5960Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5961 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5962 return Address(reg, kDummy32BitOffset, fixup);
5963}
5964
Mark Mendell805b3b52015-09-18 14:10:29 -04005965Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
5966 Register reg,
5967 Register value) {
5968 // Create a fixup to be used to create and address the jump table.
5969 JumpTableRIPFixup* table_fixup =
5970 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5971
5972 // We have to populate the jump tables.
5973 fixups_to_jump_tables_.push_back(table_fixup);
5974
5975 // We want a scaled address, as we are extracting the correct offset from the table.
5976 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
5977}
5978
Andreas Gampe85b62f22015-09-09 13:15:38 -07005979// TODO: target as memory.
5980void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5981 if (!target.IsValid()) {
5982 DCHECK(type == Primitive::kPrimVoid);
5983 return;
5984 }
5985
5986 DCHECK_NE(type, Primitive::kPrimVoid);
5987
5988 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
5989 if (target.Equals(return_loc)) {
5990 return;
5991 }
5992
5993 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5994 // with the else branch.
5995 if (type == Primitive::kPrimLong) {
5996 HParallelMove parallel_move(GetGraph()->GetArena());
5997 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
5998 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
5999 GetMoveResolver()->EmitNativeCode(&parallel_move);
6000 } else {
6001 // Let the parallel move resolver take care of all of this.
6002 HParallelMove parallel_move(GetGraph()->GetArena());
6003 parallel_move.AddMove(return_loc, target, type, nullptr);
6004 GetMoveResolver()->EmitNativeCode(&parallel_move);
6005 }
6006}
6007
Roland Levillain4d027112015-07-01 15:41:14 +01006008#undef __
6009
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006010} // namespace x86
6011} // namespace art