blob: 963eec2529d4f157ff2c5dc7939b48fe9573d22a [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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001055void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056}
1057
Mark Mendellc4701932015-04-10 13:18:51 -04001058void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1059 Label* true_label,
1060 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001061 if (cond->IsFPConditionTrueIfNaN()) {
1062 __ j(kUnordered, true_label);
1063 } else if (cond->IsFPConditionFalseIfNaN()) {
1064 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001065 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001066 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001067}
1068
1069void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1070 Label* true_label,
1071 Label* false_label) {
1072 LocationSummary* locations = cond->GetLocations();
1073 Location left = locations->InAt(0);
1074 Location right = locations->InAt(1);
1075 IfCondition if_cond = cond->GetCondition();
1076
Mark Mendellc4701932015-04-10 13:18:51 -04001077 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001078 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001079 IfCondition true_high_cond = if_cond;
1080 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001081 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001082
1083 // Set the conditions for the test, remembering that == needs to be
1084 // decided using the low words.
1085 switch (if_cond) {
1086 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001087 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001089 break;
1090 case kCondLT:
1091 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001092 break;
1093 case kCondLE:
1094 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001095 break;
1096 case kCondGT:
1097 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001098 break;
1099 case kCondGE:
1100 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001101 break;
Aart Bike9f37602015-10-09 11:15:55 -07001102 case kCondB:
1103 false_high_cond = kCondA;
1104 break;
1105 case kCondBE:
1106 true_high_cond = kCondB;
1107 break;
1108 case kCondA:
1109 false_high_cond = kCondB;
1110 break;
1111 case kCondAE:
1112 true_high_cond = kCondA;
1113 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001114 }
1115
1116 if (right.IsConstant()) {
1117 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001118 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001119 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001120
1121 if (val_high == 0) {
1122 __ testl(left_high, left_high);
1123 } else {
1124 __ cmpl(left_high, Immediate(val_high));
1125 }
1126 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001127 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001128 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001129 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001130 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001131 __ j(X86Condition(true_high_cond), true_label);
1132 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001133 }
1134 // Must be equal high, so compare the lows.
1135 if (val_low == 0) {
1136 __ testl(left_low, left_low);
1137 } else {
1138 __ cmpl(left_low, Immediate(val_low));
1139 }
1140 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001141 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001142 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001143
1144 __ cmpl(left_high, right_high);
1145 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001146 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001147 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001148 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001149 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001150 __ j(X86Condition(true_high_cond), true_label);
1151 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001152 }
1153 // Must be equal high, so compare the lows.
1154 __ cmpl(left_low, right_low);
1155 }
1156 // The last comparison might be unsigned.
1157 __ j(final_condition, true_label);
1158}
1159
1160void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1161 HCondition* condition,
1162 Label* true_target,
1163 Label* false_target,
1164 Label* always_true_target) {
1165 LocationSummary* locations = condition->GetLocations();
1166 Location left = locations->InAt(0);
1167 Location right = locations->InAt(1);
1168
1169 // We don't want true_target as a nullptr.
1170 if (true_target == nullptr) {
1171 true_target = always_true_target;
1172 }
1173 bool falls_through = (false_target == nullptr);
1174
1175 // FP compares don't like null false_targets.
1176 if (false_target == nullptr) {
1177 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1178 }
1179
1180 Primitive::Type type = condition->InputAt(0)->GetType();
1181 switch (type) {
1182 case Primitive::kPrimLong:
1183 GenerateLongComparesAndJumps(condition, true_target, false_target);
1184 break;
1185 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001186 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1187 GenerateFPJumps(condition, true_target, false_target);
1188 break;
1189 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001190 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1191 GenerateFPJumps(condition, true_target, false_target);
1192 break;
1193 default:
1194 LOG(FATAL) << "Unexpected compare type " << type;
1195 }
1196
1197 if (!falls_through) {
1198 __ jmp(false_target);
1199 }
1200}
1201
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001202void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1203 Label* true_target,
1204 Label* false_target,
1205 Label* always_true_target) {
1206 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001207 if (cond->IsIntConstant()) {
1208 // Constant condition, statically compared against 1.
1209 int32_t cond_value = cond->AsIntConstant()->GetValue();
1210 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001211 if (always_true_target != nullptr) {
1212 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001213 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001214 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001216 DCHECK_EQ(cond_value, 0);
1217 }
1218 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001220 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1221 // Moves do not affect the eflags register, so if the condition is
1222 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001223 // again. We can't use the eflags on long/FP conditions if they are
1224 // materialized due to the complex branching.
1225 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001226 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001227 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1229 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001230 if (!eflags_set) {
1231 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001232 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001233 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001234 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001235 } else {
1236 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1237 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001238 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001239 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001240 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001241 }
1242 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001243 // Condition has not been materialized, use its inputs as the
1244 // comparison and its condition as the branch condition.
1245
Mark Mendellc4701932015-04-10 13:18:51 -04001246 // Is this a long or FP comparison that has been folded into the HCondition?
1247 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1248 // Generate the comparison directly.
1249 GenerateCompareTestAndBranch(instruction->AsIf(),
1250 cond->AsCondition(),
1251 true_target,
1252 false_target,
1253 always_true_target);
1254 return;
1255 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001256
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001257 Location lhs = cond->GetLocations()->InAt(0);
1258 Location rhs = cond->GetLocations()->InAt(1);
1259 // LHS is guaranteed to be in a register (see
1260 // LocationsBuilderX86::VisitCondition).
1261 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001263 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001264 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001265 if (constant == 0) {
1266 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1267 } else {
1268 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1269 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001270 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001271 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001272 }
Aart Bike9f37602015-10-09 11:15:55 -07001273 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001274 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001275 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001276 if (false_target != nullptr) {
1277 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278 }
1279}
1280
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001281void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1282 LocationSummary* locations =
1283 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1284 HInstruction* cond = if_instr->InputAt(0);
1285 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1286 locations->SetInAt(0, Location::Any());
1287 }
1288}
1289
1290void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1291 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1292 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1293 Label* always_true_target = true_target;
1294 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1295 if_instr->IfTrueSuccessor())) {
1296 always_true_target = nullptr;
1297 }
1298 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1299 if_instr->IfFalseSuccessor())) {
1300 false_target = nullptr;
1301 }
1302 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1303}
1304
1305void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1306 LocationSummary* locations = new (GetGraph()->GetArena())
1307 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1308 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001309 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001310 locations->SetInAt(0, Location::Any());
1311 }
1312}
1313
1314void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001315 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001316 DeoptimizationSlowPathX86(deoptimize);
1317 codegen_->AddSlowPath(slow_path);
1318 Label* slow_path_entry = slow_path->GetEntryLabel();
1319 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1320}
1321
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001324}
1325
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1327 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001328}
1329
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001330void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001331 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332}
1333
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001334void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001335 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001336}
1337
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001338void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001339 LocationSummary* locations =
1340 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001341 switch (store->InputAt(1)->GetType()) {
1342 case Primitive::kPrimBoolean:
1343 case Primitive::kPrimByte:
1344 case Primitive::kPrimChar:
1345 case Primitive::kPrimShort:
1346 case Primitive::kPrimInt:
1347 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001349 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1350 break;
1351
1352 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001353 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1355 break;
1356
1357 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001359 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001360}
1361
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001362void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001363}
1364
Roland Levillain0d37cd02015-05-27 16:39:19 +01001365void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001366 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001367 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001368 // Handle the long/FP comparisons made in instruction simplification.
1369 switch (cond->InputAt(0)->GetType()) {
1370 case Primitive::kPrimLong: {
1371 locations->SetInAt(0, Location::RequiresRegister());
1372 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1373 if (cond->NeedsMaterialization()) {
1374 locations->SetOut(Location::RequiresRegister());
1375 }
1376 break;
1377 }
1378 case Primitive::kPrimFloat:
1379 case Primitive::kPrimDouble: {
1380 locations->SetInAt(0, Location::RequiresFpuRegister());
1381 locations->SetInAt(1, Location::RequiresFpuRegister());
1382 if (cond->NeedsMaterialization()) {
1383 locations->SetOut(Location::RequiresRegister());
1384 }
1385 break;
1386 }
1387 default:
1388 locations->SetInAt(0, Location::RequiresRegister());
1389 locations->SetInAt(1, Location::Any());
1390 if (cond->NeedsMaterialization()) {
1391 // We need a byte register.
1392 locations->SetOut(Location::RegisterLocation(ECX));
1393 }
1394 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001395 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396}
1397
Roland Levillain0d37cd02015-05-27 16:39:19 +01001398void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001399 if (!cond->NeedsMaterialization()) {
1400 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001401 }
Mark Mendellc4701932015-04-10 13:18:51 -04001402
1403 LocationSummary* locations = cond->GetLocations();
1404 Location lhs = locations->InAt(0);
1405 Location rhs = locations->InAt(1);
1406 Register reg = locations->Out().AsRegister<Register>();
1407 Label true_label, false_label;
1408
1409 switch (cond->InputAt(0)->GetType()) {
1410 default: {
1411 // Integer case.
1412
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001413 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001414 __ xorl(reg, reg);
1415
1416 if (rhs.IsRegister()) {
1417 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1418 } else if (rhs.IsConstant()) {
1419 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1420 if (constant == 0) {
1421 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1422 } else {
1423 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1424 }
1425 } else {
1426 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1427 }
Aart Bike9f37602015-10-09 11:15:55 -07001428 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001429 return;
1430 }
1431 case Primitive::kPrimLong:
1432 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1433 break;
1434 case Primitive::kPrimFloat:
1435 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1436 GenerateFPJumps(cond, &true_label, &false_label);
1437 break;
1438 case Primitive::kPrimDouble:
1439 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1440 GenerateFPJumps(cond, &true_label, &false_label);
1441 break;
1442 }
1443
1444 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001445 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001446
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001448 __ Bind(&false_label);
1449 __ xorl(reg, reg);
1450 __ jmp(&done_label);
1451
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001453 __ Bind(&true_label);
1454 __ movl(reg, Immediate(1));
1455 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001456}
1457
1458void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1459 VisitCondition(comp);
1460}
1461
1462void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1463 VisitCondition(comp);
1464}
1465
1466void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1467 VisitCondition(comp);
1468}
1469
1470void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1471 VisitCondition(comp);
1472}
1473
1474void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1475 VisitCondition(comp);
1476}
1477
1478void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1479 VisitCondition(comp);
1480}
1481
1482void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1483 VisitCondition(comp);
1484}
1485
1486void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1487 VisitCondition(comp);
1488}
1489
1490void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1491 VisitCondition(comp);
1492}
1493
1494void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1495 VisitCondition(comp);
1496}
1497
1498void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1499 VisitCondition(comp);
1500}
1501
1502void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1503 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504}
1505
Aart Bike9f37602015-10-09 11:15:55 -07001506void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1507 VisitCondition(comp);
1508}
1509
1510void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1511 VisitCondition(comp);
1512}
1513
1514void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1515 VisitCondition(comp);
1516}
1517
1518void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1519 VisitCondition(comp);
1520}
1521
1522void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1523 VisitCondition(comp);
1524}
1525
1526void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1527 VisitCondition(comp);
1528}
1529
1530void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1531 VisitCondition(comp);
1532}
1533
1534void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1535 VisitCondition(comp);
1536}
1537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001539 LocationSummary* locations =
1540 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001541 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542}
1543
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001544void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001545 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546}
1547
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001548void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1551 locations->SetOut(Location::ConstantLocation(constant));
1552}
1553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001554void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001555 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001556}
1557
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001561 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562}
1563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 // Will be generated at use site.
1566}
1567
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001568void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1571 locations->SetOut(Location::ConstantLocation(constant));
1572}
1573
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001574void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001575 // Will be generated at use site.
1576}
1577
1578void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1581 locations->SetOut(Location::ConstantLocation(constant));
1582}
1583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001584void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001585 // Will be generated at use site.
1586}
1587
Calin Juravle27df7582015-04-17 19:12:31 +01001588void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1589 memory_barrier->SetLocations(nullptr);
1590}
1591
1592void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1593 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1594}
1595
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001596void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001597 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001598}
1599
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001600void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001601 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001602}
1603
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 LocationSummary* locations =
1606 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001607 switch (ret->InputAt(0)->GetType()) {
1608 case Primitive::kPrimBoolean:
1609 case Primitive::kPrimByte:
1610 case Primitive::kPrimChar:
1611 case Primitive::kPrimShort:
1612 case Primitive::kPrimInt:
1613 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001614 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001615 break;
1616
1617 case Primitive::kPrimLong:
1618 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001619 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 break;
1621
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble:
1624 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001625 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001626 break;
1627
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001628 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001629 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001630 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001631}
1632
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001633void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001634 if (kIsDebugBuild) {
1635 switch (ret->InputAt(0)->GetType()) {
1636 case Primitive::kPrimBoolean:
1637 case Primitive::kPrimByte:
1638 case Primitive::kPrimChar:
1639 case Primitive::kPrimShort:
1640 case Primitive::kPrimInt:
1641 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643 break;
1644
1645 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001646 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1647 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648 break;
1649
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 case Primitive::kPrimFloat:
1651 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001653 break;
1654
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001655 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001656 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001657 }
1658 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001659 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660}
1661
Calin Juravle175dc732015-08-25 15:42:32 +01001662void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1663 // The trampoline uses the same calling convention as dex calling conventions,
1664 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1665 // the method_idx.
1666 HandleInvoke(invoke);
1667}
1668
1669void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1670 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1671}
1672
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001673void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001674 // When we do not run baseline, explicit clinit checks triggered by static
1675 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1676 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001677
Mark Mendellfb8d2792015-03-31 22:16:59 -04001678 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001679 if (intrinsic.TryDispatch(invoke)) {
1680 return;
1681 }
1682
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001683 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001684
1685 if (codegen_->IsBaseline()) {
1686 // Baseline does not have enough registers if the current method also
1687 // needs a register. We therefore do not require a register for it, and let
1688 // the code generation of the invoke handle it.
1689 LocationSummary* locations = invoke->GetLocations();
1690 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1691 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1692 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1693 }
1694 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001695}
1696
Mark Mendell09ed1a32015-03-25 08:30:06 -04001697static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1698 if (invoke->GetLocations()->Intrinsified()) {
1699 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1700 intrinsic.Dispatch(invoke);
1701 return true;
1702 }
1703 return false;
1704}
1705
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001706void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001707 // When we do not run baseline, explicit clinit checks triggered by static
1708 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1709 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001710
Mark Mendell09ed1a32015-03-25 08:30:06 -04001711 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1712 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001713 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001714
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001715 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001716 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001717 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001718 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001719}
1720
1721void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1722 HandleInvoke(invoke);
1723}
1724
1725void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001726 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001727 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001728}
1729
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001730void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001731 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1732 return;
1733 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001734
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001735 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001736 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001737 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001738}
1739
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001740void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1741 HandleInvoke(invoke);
1742 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001743 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001744}
1745
1746void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1747 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1750 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001751 LocationSummary* locations = invoke->GetLocations();
1752 Location receiver = locations->InAt(0);
1753 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1754
1755 // Set the hidden argument.
1756 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001758
1759 // temp = object->GetClass();
1760 if (receiver.IsStackSlot()) {
1761 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1762 __ movl(temp, Address(temp, class_offset));
1763 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001765 }
Roland Levillain4d027112015-07-01 15:41:14 +01001766 codegen_->MaybeRecordImplicitNullCheck(invoke);
1767 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001768 // temp = temp->GetImtEntryAt(method_offset);
1769 __ movl(temp, Address(temp, method_offset));
1770 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001771 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001772 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001773
1774 DCHECK(!codegen_->IsLeafMethod());
1775 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1776}
1777
Roland Levillain88cb1752014-10-20 16:36:47 +01001778void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1781 switch (neg->GetResultType()) {
1782 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001783 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001784 locations->SetInAt(0, Location::RequiresRegister());
1785 locations->SetOut(Location::SameAsFirstInput());
1786 break;
1787
Roland Levillain88cb1752014-10-20 16:36:47 +01001788 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001789 locations->SetInAt(0, Location::RequiresFpuRegister());
1790 locations->SetOut(Location::SameAsFirstInput());
1791 locations->AddTemp(Location::RequiresRegister());
1792 locations->AddTemp(Location::RequiresFpuRegister());
1793 break;
1794
Roland Levillain88cb1752014-10-20 16:36:47 +01001795 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001796 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001797 locations->SetOut(Location::SameAsFirstInput());
1798 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001799 break;
1800
1801 default:
1802 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1803 }
1804}
1805
1806void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1807 LocationSummary* locations = neg->GetLocations();
1808 Location out = locations->Out();
1809 Location in = locations->InAt(0);
1810 switch (neg->GetResultType()) {
1811 case Primitive::kPrimInt:
1812 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001813 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001814 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001815 break;
1816
1817 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001818 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001819 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001820 __ negl(out.AsRegisterPairLow<Register>());
1821 // Negation is similar to subtraction from zero. The least
1822 // significant byte triggers a borrow when it is different from
1823 // zero; to take it into account, add 1 to the most significant
1824 // byte if the carry flag (CF) is set to 1 after the first NEGL
1825 // operation.
1826 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1827 __ negl(out.AsRegisterPairHigh<Register>());
1828 break;
1829
Roland Levillain5368c212014-11-27 15:03:41 +00001830 case Primitive::kPrimFloat: {
1831 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001832 Register constant = locations->GetTemp(0).AsRegister<Register>();
1833 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001834 // Implement float negation with an exclusive or with value
1835 // 0x80000000 (mask for bit 31, representing the sign of a
1836 // single-precision floating-point number).
1837 __ movl(constant, Immediate(INT32_C(0x80000000)));
1838 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001839 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001840 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001841 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001842
Roland Levillain5368c212014-11-27 15:03:41 +00001843 case Primitive::kPrimDouble: {
1844 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001845 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001846 // Implement double negation with an exclusive or with value
1847 // 0x8000000000000000 (mask for bit 63, representing the sign of
1848 // a double-precision floating-point number).
1849 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001850 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001851 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001852 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001853
1854 default:
1855 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1856 }
1857}
1858
Roland Levillaindff1f282014-11-05 14:15:05 +00001859void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001860 Primitive::Type result_type = conversion->GetResultType();
1861 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001862 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001863
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001864 // The float-to-long and double-to-long type conversions rely on a
1865 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001866 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001867 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1868 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001869 ? LocationSummary::kCall
1870 : LocationSummary::kNoCall;
1871 LocationSummary* locations =
1872 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1873
David Brazdilb2bd1c52015-03-25 11:17:37 +00001874 // The Java language does not allow treating boolean as an integral type but
1875 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001876
Roland Levillaindff1f282014-11-05 14:15:05 +00001877 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001878 case Primitive::kPrimByte:
1879 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001880 case Primitive::kPrimBoolean:
1881 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001882 case Primitive::kPrimShort:
1883 case Primitive::kPrimInt:
1884 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001885 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001886 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1887 // Make the output overlap to please the register allocator. This greatly simplifies
1888 // the validation of the linear scan implementation
1889 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001890 break;
1891
1892 default:
1893 LOG(FATAL) << "Unexpected type conversion from " << input_type
1894 << " to " << result_type;
1895 }
1896 break;
1897
Roland Levillain01a8d712014-11-14 16:27:39 +00001898 case Primitive::kPrimShort:
1899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001900 case Primitive::kPrimBoolean:
1901 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001902 case Primitive::kPrimByte:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimChar:
1905 // Processing a Dex `int-to-short' instruction.
1906 locations->SetInAt(0, Location::Any());
1907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
1914 break;
1915
Roland Levillain946e1432014-11-11 17:35:19 +00001916 case Primitive::kPrimInt:
1917 switch (input_type) {
1918 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001919 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001920 locations->SetInAt(0, Location::Any());
1921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1922 break;
1923
1924 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001925 // Processing a Dex `float-to-int' instruction.
1926 locations->SetInAt(0, Location::RequiresFpuRegister());
1927 locations->SetOut(Location::RequiresRegister());
1928 locations->AddTemp(Location::RequiresFpuRegister());
1929 break;
1930
Roland Levillain946e1432014-11-11 17:35:19 +00001931 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001932 // Processing a Dex `double-to-int' instruction.
1933 locations->SetInAt(0, Location::RequiresFpuRegister());
1934 locations->SetOut(Location::RequiresRegister());
1935 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001936 break;
1937
1938 default:
1939 LOG(FATAL) << "Unexpected type conversion from " << input_type
1940 << " to " << result_type;
1941 }
1942 break;
1943
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 case Primitive::kPrimLong:
1945 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001946 case Primitive::kPrimBoolean:
1947 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001948 case Primitive::kPrimByte:
1949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001951 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001952 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001953 locations->SetInAt(0, Location::RegisterLocation(EAX));
1954 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1955 break;
1956
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001957 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001958 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001959 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001960 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001961 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1962 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1963
Vladimir Marko949c91f2015-01-27 10:48:44 +00001964 // The runtime helper puts the result in EAX, EDX.
1965 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001966 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001967 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001968
1969 default:
1970 LOG(FATAL) << "Unexpected type conversion from " << input_type
1971 << " to " << result_type;
1972 }
1973 break;
1974
Roland Levillain981e4542014-11-14 11:47:14 +00001975 case Primitive::kPrimChar:
1976 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001977 case Primitive::kPrimBoolean:
1978 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001979 case Primitive::kPrimByte:
1980 case Primitive::kPrimShort:
1981 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001982 // Processing a Dex `int-to-char' instruction.
1983 locations->SetInAt(0, Location::Any());
1984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1985 break;
1986
1987 default:
1988 LOG(FATAL) << "Unexpected type conversion from " << input_type
1989 << " to " << result_type;
1990 }
1991 break;
1992
Roland Levillaindff1f282014-11-05 14:15:05 +00001993 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001994 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001995 case Primitive::kPrimBoolean:
1996 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001997 case Primitive::kPrimByte:
1998 case Primitive::kPrimShort:
1999 case Primitive::kPrimInt:
2000 case Primitive::kPrimChar:
2001 // Processing a Dex `int-to-float' instruction.
2002 locations->SetInAt(0, Location::RequiresRegister());
2003 locations->SetOut(Location::RequiresFpuRegister());
2004 break;
2005
2006 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002007 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002008 locations->SetInAt(0, Location::Any());
2009 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002010 break;
2011
Roland Levillaincff13742014-11-17 14:32:17 +00002012 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002013 // Processing a Dex `double-to-float' instruction.
2014 locations->SetInAt(0, Location::RequiresFpuRegister());
2015 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002016 break;
2017
2018 default:
2019 LOG(FATAL) << "Unexpected type conversion from " << input_type
2020 << " to " << result_type;
2021 };
2022 break;
2023
Roland Levillaindff1f282014-11-05 14:15:05 +00002024 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002025 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002026 case Primitive::kPrimBoolean:
2027 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002028 case Primitive::kPrimByte:
2029 case Primitive::kPrimShort:
2030 case Primitive::kPrimInt:
2031 case Primitive::kPrimChar:
2032 // Processing a Dex `int-to-double' instruction.
2033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetOut(Location::RequiresFpuRegister());
2035 break;
2036
2037 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002038 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002039 locations->SetInAt(0, Location::Any());
2040 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002041 break;
2042
Roland Levillaincff13742014-11-17 14:32:17 +00002043 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002044 // Processing a Dex `float-to-double' instruction.
2045 locations->SetInAt(0, Location::RequiresFpuRegister());
2046 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unexpected type conversion from " << input_type
2051 << " to " << result_type;
2052 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected type conversion from " << input_type
2057 << " to " << result_type;
2058 }
2059}
2060
2061void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2062 LocationSummary* locations = conversion->GetLocations();
2063 Location out = locations->Out();
2064 Location in = locations->InAt(0);
2065 Primitive::Type result_type = conversion->GetResultType();
2066 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002067 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002068 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002069 case Primitive::kPrimByte:
2070 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002071 case Primitive::kPrimBoolean:
2072 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002073 case Primitive::kPrimShort:
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002076 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002077 if (in.IsRegister()) {
2078 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002079 } else {
2080 DCHECK(in.GetConstant()->IsIntConstant());
2081 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2082 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2083 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002084 break;
2085
2086 default:
2087 LOG(FATAL) << "Unexpected type conversion from " << input_type
2088 << " to " << result_type;
2089 }
2090 break;
2091
Roland Levillain01a8d712014-11-14 16:27:39 +00002092 case Primitive::kPrimShort:
2093 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002094 case Primitive::kPrimBoolean:
2095 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002096 case Primitive::kPrimByte:
2097 case Primitive::kPrimInt:
2098 case Primitive::kPrimChar:
2099 // Processing a Dex `int-to-short' instruction.
2100 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002102 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002104 } else {
2105 DCHECK(in.GetConstant()->IsIntConstant());
2106 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002108 }
2109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unexpected type conversion from " << input_type
2113 << " to " << result_type;
2114 }
2115 break;
2116
Roland Levillain946e1432014-11-11 17:35:19 +00002117 case Primitive::kPrimInt:
2118 switch (input_type) {
2119 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002120 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002121 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002123 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002125 } else {
2126 DCHECK(in.IsConstant());
2127 DCHECK(in.GetConstant()->IsLongConstant());
2128 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002130 }
2131 break;
2132
Roland Levillain3f8f9362014-12-02 17:45:01 +00002133 case Primitive::kPrimFloat: {
2134 // Processing a Dex `float-to-int' instruction.
2135 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2136 Register output = out.AsRegister<Register>();
2137 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002138 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002139
2140 __ movl(output, Immediate(kPrimIntMax));
2141 // temp = int-to-float(output)
2142 __ cvtsi2ss(temp, output);
2143 // if input >= temp goto done
2144 __ comiss(input, temp);
2145 __ j(kAboveEqual, &done);
2146 // if input == NaN goto nan
2147 __ j(kUnordered, &nan);
2148 // output = float-to-int-truncate(input)
2149 __ cvttss2si(output, input);
2150 __ jmp(&done);
2151 __ Bind(&nan);
2152 // output = 0
2153 __ xorl(output, output);
2154 __ Bind(&done);
2155 break;
2156 }
2157
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002158 case Primitive::kPrimDouble: {
2159 // Processing a Dex `double-to-int' instruction.
2160 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2161 Register output = out.AsRegister<Register>();
2162 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002163 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164
2165 __ movl(output, Immediate(kPrimIntMax));
2166 // temp = int-to-double(output)
2167 __ cvtsi2sd(temp, output);
2168 // if input >= temp goto done
2169 __ comisd(input, temp);
2170 __ j(kAboveEqual, &done);
2171 // if input == NaN goto nan
2172 __ j(kUnordered, &nan);
2173 // output = double-to-int-truncate(input)
2174 __ cvttsd2si(output, input);
2175 __ jmp(&done);
2176 __ Bind(&nan);
2177 // output = 0
2178 __ xorl(output, output);
2179 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002180 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 }
Roland Levillain946e1432014-11-11 17:35:19 +00002182
2183 default:
2184 LOG(FATAL) << "Unexpected type conversion from " << input_type
2185 << " to " << result_type;
2186 }
2187 break;
2188
Roland Levillaindff1f282014-11-05 14:15:05 +00002189 case Primitive::kPrimLong:
2190 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002191 case Primitive::kPrimBoolean:
2192 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002193 case Primitive::kPrimByte:
2194 case Primitive::kPrimShort:
2195 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002196 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002197 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2199 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002200 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 __ cdq();
2202 break;
2203
2204 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002205 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002206 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2207 conversion,
2208 conversion->GetDexPc(),
2209 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002210 break;
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002213 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002214 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2215 conversion,
2216 conversion->GetDexPc(),
2217 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain981e4542014-11-14 11:47:14 +00002226 case Primitive::kPrimChar:
2227 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002228 case Primitive::kPrimBoolean:
2229 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002230 case Primitive::kPrimByte:
2231 case Primitive::kPrimShort:
2232 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002233 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2234 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002236 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002237 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002238 } else {
2239 DCHECK(in.GetConstant()->IsIntConstant());
2240 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002242 }
2243 break;
2244
2245 default:
2246 LOG(FATAL) << "Unexpected type conversion from " << input_type
2247 << " to " << result_type;
2248 }
2249 break;
2250
Roland Levillaindff1f282014-11-05 14:15:05 +00002251 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002252 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002253 case Primitive::kPrimBoolean:
2254 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002255 case Primitive::kPrimByte:
2256 case Primitive::kPrimShort:
2257 case Primitive::kPrimInt:
2258 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002259 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002260 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002261 break;
2262
Roland Levillain6d0e4832014-11-27 18:31:21 +00002263 case Primitive::kPrimLong: {
2264 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002265 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002266
Roland Levillain232ade02015-04-20 15:14:36 +01002267 // Create stack space for the call to
2268 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2269 // TODO: enhance register allocator to ask for stack temporaries.
2270 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2271 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2272 __ subl(ESP, Immediate(adjustment));
2273 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002274
Roland Levillain232ade02015-04-20 15:14:36 +01002275 // Load the value to the FP stack, using temporaries if needed.
2276 PushOntoFPStack(in, 0, adjustment, false, true);
2277
2278 if (out.IsStackSlot()) {
2279 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2280 } else {
2281 __ fstps(Address(ESP, 0));
2282 Location stack_temp = Location::StackSlot(0);
2283 codegen_->Move32(out, stack_temp);
2284 }
2285
2286 // Remove the temporary stack space we allocated.
2287 if (adjustment != 0) {
2288 __ addl(ESP, Immediate(adjustment));
2289 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002290 break;
2291 }
2292
Roland Levillaincff13742014-11-17 14:32:17 +00002293 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002294 // Processing a Dex `double-to-float' instruction.
2295 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002296 break;
2297
2298 default:
2299 LOG(FATAL) << "Unexpected type conversion from " << input_type
2300 << " to " << result_type;
2301 };
2302 break;
2303
Roland Levillaindff1f282014-11-05 14:15:05 +00002304 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002305 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
2311 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002312 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002314 break;
2315
Roland Levillain647b9ed2014-11-27 12:06:00 +00002316 case Primitive::kPrimLong: {
2317 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002318 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002319
Roland Levillain232ade02015-04-20 15:14:36 +01002320 // Create stack space for the call to
2321 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2322 // TODO: enhance register allocator to ask for stack temporaries.
2323 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2324 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2325 __ subl(ESP, Immediate(adjustment));
2326 }
2327
2328 // Load the value to the FP stack, using temporaries if needed.
2329 PushOntoFPStack(in, 0, adjustment, false, true);
2330
2331 if (out.IsDoubleStackSlot()) {
2332 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2333 } else {
2334 __ fstpl(Address(ESP, 0));
2335 Location stack_temp = Location::DoubleStackSlot(0);
2336 codegen_->Move64(out, stack_temp);
2337 }
2338
2339 // Remove the temporary stack space we allocated.
2340 if (adjustment != 0) {
2341 __ addl(ESP, Immediate(adjustment));
2342 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002343 break;
2344 }
2345
Roland Levillaincff13742014-11-17 14:32:17 +00002346 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002347 // Processing a Dex `float-to-double' instruction.
2348 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected type conversion from " << input_type
2353 << " to " << result_type;
2354 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361}
2362
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002363void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002364 LocationSummary* locations =
2365 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002366 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002367 case Primitive::kPrimInt: {
2368 locations->SetInAt(0, Location::RequiresRegister());
2369 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2371 break;
2372 }
2373
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetInAt(1, Location::Any());
2377 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378 break;
2379 }
2380
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002381 case Primitive::kPrimFloat:
2382 case Primitive::kPrimDouble: {
2383 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002384 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002385 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002389 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2391 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002392 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002393}
2394
2395void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2396 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002397 Location first = locations->InAt(0);
2398 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002399 Location out = locations->Out();
2400
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002401 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002402 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002403 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002404 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2405 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002406 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2407 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002408 } else {
2409 __ leal(out.AsRegister<Register>(), Address(
2410 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2411 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002413 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2414 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2415 __ addl(out.AsRegister<Register>(), Immediate(value));
2416 } else {
2417 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2418 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002419 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002420 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002421 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002422 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002423 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002424 }
2425
2426 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002427 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002428 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2429 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002430 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002431 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2432 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002433 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002434 } else {
2435 DCHECK(second.IsConstant()) << second;
2436 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2437 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2438 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002439 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002440 break;
2441 }
2442
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002443 case Primitive::kPrimFloat: {
2444 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002446 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2447 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2448 DCHECK(!const_area->NeedsMaterialization());
2449 __ addss(first.AsFpuRegister<XmmRegister>(),
2450 codegen_->LiteralFloatAddress(
2451 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2452 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2453 } else {
2454 DCHECK(second.IsStackSlot());
2455 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002456 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002457 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458 }
2459
2460 case Primitive::kPrimDouble: {
2461 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002463 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2464 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2465 DCHECK(!const_area->NeedsMaterialization());
2466 __ addsd(first.AsFpuRegister<XmmRegister>(),
2467 codegen_->LiteralDoubleAddress(
2468 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2469 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2470 } else {
2471 DCHECK(second.IsDoubleStackSlot());
2472 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002473 }
2474 break;
2475 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002476
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002477 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002478 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002479 }
2480}
2481
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002482void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002485 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002486 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002487 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002488 locations->SetInAt(0, Location::RequiresRegister());
2489 locations->SetInAt(1, Location::Any());
2490 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002491 break;
2492 }
Calin Juravle11351682014-10-23 15:38:15 +01002493 case Primitive::kPrimFloat:
2494 case Primitive::kPrimDouble: {
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002496 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002497 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002498 break;
Calin Juravle11351682014-10-23 15:38:15 +01002499 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002500
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002501 default:
Calin Juravle11351682014-10-23 15:38:15 +01002502 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002503 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002504}
2505
2506void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2507 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002508 Location first = locations->InAt(0);
2509 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002510 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002511 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002512 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002515 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002516 __ subl(first.AsRegister<Register>(),
2517 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002518 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002519 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002520 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002521 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002522 }
2523
2524 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002525 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002526 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2527 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002528 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002529 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 __ sbbl(first.AsRegisterPairHigh<Register>(),
2531 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002532 } else {
2533 DCHECK(second.IsConstant()) << second;
2534 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2535 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2536 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002537 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002538 break;
2539 }
2540
Calin Juravle11351682014-10-23 15:38:15 +01002541 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002542 if (second.IsFpuRegister()) {
2543 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2544 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2545 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2546 DCHECK(!const_area->NeedsMaterialization());
2547 __ subss(first.AsFpuRegister<XmmRegister>(),
2548 codegen_->LiteralFloatAddress(
2549 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2550 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2551 } else {
2552 DCHECK(second.IsStackSlot());
2553 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2554 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002555 break;
Calin Juravle11351682014-10-23 15:38:15 +01002556 }
2557
2558 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002559 if (second.IsFpuRegister()) {
2560 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2561 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2562 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2563 DCHECK(!const_area->NeedsMaterialization());
2564 __ subsd(first.AsFpuRegister<XmmRegister>(),
2565 codegen_->LiteralDoubleAddress(
2566 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2567 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2568 } else {
2569 DCHECK(second.IsDoubleStackSlot());
2570 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2571 }
Calin Juravle11351682014-10-23 15:38:15 +01002572 break;
2573 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002574
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002575 default:
Calin Juravle11351682014-10-23 15:38:15 +01002576 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002577 }
2578}
2579
Calin Juravle34bacdf2014-10-07 20:23:36 +01002580void LocationsBuilderX86::VisitMul(HMul* mul) {
2581 LocationSummary* locations =
2582 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2583 switch (mul->GetResultType()) {
2584 case Primitive::kPrimInt:
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002587 if (mul->InputAt(1)->IsIntConstant()) {
2588 // Can use 3 operand multiply.
2589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2590 } else {
2591 locations->SetOut(Location::SameAsFirstInput());
2592 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002593 break;
2594 case Primitive::kPrimLong: {
2595 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596 locations->SetInAt(1, Location::Any());
2597 locations->SetOut(Location::SameAsFirstInput());
2598 // Needed for imul on 32bits with 64bits output.
2599 locations->AddTemp(Location::RegisterLocation(EAX));
2600 locations->AddTemp(Location::RegisterLocation(EDX));
2601 break;
2602 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002603 case Primitive::kPrimFloat:
2604 case Primitive::kPrimDouble: {
2605 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002606 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002607 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002608 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002609 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002610
2611 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002612 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002613 }
2614}
2615
2616void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2617 LocationSummary* locations = mul->GetLocations();
2618 Location first = locations->InAt(0);
2619 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002620 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002621
2622 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002623 case Primitive::kPrimInt:
2624 // The constant may have ended up in a register, so test explicitly to avoid
2625 // problems where the output may not be the same as the first operand.
2626 if (mul->InputAt(1)->IsIntConstant()) {
2627 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2628 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2629 } else if (second.IsRegister()) {
2630 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002632 } else {
2633 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002634 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002636 }
2637 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638
2639 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002640 Register in1_hi = first.AsRegisterPairHigh<Register>();
2641 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 Register eax = locations->GetTemp(0).AsRegister<Register>();
2643 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002644
2645 DCHECK_EQ(EAX, eax);
2646 DCHECK_EQ(EDX, edx);
2647
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002648 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002649 // output: in1
2650 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2651 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2652 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002653 if (second.IsConstant()) {
2654 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002655
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002656 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2657 int32_t low_value = Low32Bits(value);
2658 int32_t high_value = High32Bits(value);
2659 Immediate low(low_value);
2660 Immediate high(high_value);
2661
2662 __ movl(eax, high);
2663 // eax <- in1.lo * in2.hi
2664 __ imull(eax, in1_lo);
2665 // in1.hi <- in1.hi * in2.lo
2666 __ imull(in1_hi, low);
2667 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2668 __ addl(in1_hi, eax);
2669 // move in2_lo to eax to prepare for double precision
2670 __ movl(eax, low);
2671 // edx:eax <- in1.lo * in2.lo
2672 __ mull(in1_lo);
2673 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2674 __ addl(in1_hi, edx);
2675 // in1.lo <- (in1.lo * in2.lo)[31:0];
2676 __ movl(in1_lo, eax);
2677 } else if (second.IsRegisterPair()) {
2678 Register in2_hi = second.AsRegisterPairHigh<Register>();
2679 Register in2_lo = second.AsRegisterPairLow<Register>();
2680
2681 __ movl(eax, in2_hi);
2682 // eax <- in1.lo * in2.hi
2683 __ imull(eax, in1_lo);
2684 // in1.hi <- in1.hi * in2.lo
2685 __ imull(in1_hi, in2_lo);
2686 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2687 __ addl(in1_hi, eax);
2688 // move in1_lo to eax to prepare for double precision
2689 __ movl(eax, in1_lo);
2690 // edx:eax <- in1.lo * in2.lo
2691 __ mull(in2_lo);
2692 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2693 __ addl(in1_hi, edx);
2694 // in1.lo <- (in1.lo * in2.lo)[31:0];
2695 __ movl(in1_lo, eax);
2696 } else {
2697 DCHECK(second.IsDoubleStackSlot()) << second;
2698 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2699 Address in2_lo(ESP, second.GetStackIndex());
2700
2701 __ movl(eax, in2_hi);
2702 // eax <- in1.lo * in2.hi
2703 __ imull(eax, in1_lo);
2704 // in1.hi <- in1.hi * in2.lo
2705 __ imull(in1_hi, in2_lo);
2706 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2707 __ addl(in1_hi, eax);
2708 // move in1_lo to eax to prepare for double precision
2709 __ movl(eax, in1_lo);
2710 // edx:eax <- in1.lo * in2.lo
2711 __ mull(in2_lo);
2712 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2713 __ addl(in1_hi, edx);
2714 // in1.lo <- (in1.lo * in2.lo)[31:0];
2715 __ movl(in1_lo, eax);
2716 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717
2718 break;
2719 }
2720
Calin Juravleb5bfa962014-10-21 18:02:24 +01002721 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002722 DCHECK(first.Equals(locations->Out()));
2723 if (second.IsFpuRegister()) {
2724 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2725 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2726 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2727 DCHECK(!const_area->NeedsMaterialization());
2728 __ mulss(first.AsFpuRegister<XmmRegister>(),
2729 codegen_->LiteralFloatAddress(
2730 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2731 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2732 } else {
2733 DCHECK(second.IsStackSlot());
2734 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2735 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002736 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002737 }
2738
2739 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002740 DCHECK(first.Equals(locations->Out()));
2741 if (second.IsFpuRegister()) {
2742 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2743 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2744 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2745 DCHECK(!const_area->NeedsMaterialization());
2746 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2747 codegen_->LiteralDoubleAddress(
2748 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2749 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2750 } else {
2751 DCHECK(second.IsDoubleStackSlot());
2752 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2753 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002754 break;
2755 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002756
2757 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002758 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002759 }
2760}
2761
Roland Levillain232ade02015-04-20 15:14:36 +01002762void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2763 uint32_t temp_offset,
2764 uint32_t stack_adjustment,
2765 bool is_fp,
2766 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002767 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002768 DCHECK(!is_wide);
2769 if (is_fp) {
2770 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2771 } else {
2772 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2773 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002774 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002775 DCHECK(is_wide);
2776 if (is_fp) {
2777 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2778 } else {
2779 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2780 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002781 } else {
2782 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002783 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002784 Location stack_temp = Location::StackSlot(temp_offset);
2785 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002786 if (is_fp) {
2787 __ flds(Address(ESP, temp_offset));
2788 } else {
2789 __ filds(Address(ESP, temp_offset));
2790 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002791 } else {
2792 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2793 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002794 if (is_fp) {
2795 __ fldl(Address(ESP, temp_offset));
2796 } else {
2797 __ fildl(Address(ESP, temp_offset));
2798 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002799 }
2800 }
2801}
2802
2803void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2804 Primitive::Type type = rem->GetResultType();
2805 bool is_float = type == Primitive::kPrimFloat;
2806 size_t elem_size = Primitive::ComponentSize(type);
2807 LocationSummary* locations = rem->GetLocations();
2808 Location first = locations->InAt(0);
2809 Location second = locations->InAt(1);
2810 Location out = locations->Out();
2811
2812 // Create stack space for 2 elements.
2813 // TODO: enhance register allocator to ask for stack temporaries.
2814 __ subl(ESP, Immediate(2 * elem_size));
2815
2816 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002817 const bool is_wide = !is_float;
2818 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2819 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002820
2821 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002822 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002823 __ Bind(&retry);
2824 __ fprem();
2825
2826 // Move FP status to AX.
2827 __ fstsw();
2828
2829 // And see if the argument reduction is complete. This is signaled by the
2830 // C2 FPU flag bit set to 0.
2831 __ andl(EAX, Immediate(kC2ConditionMask));
2832 __ j(kNotEqual, &retry);
2833
2834 // We have settled on the final value. Retrieve it into an XMM register.
2835 // Store FP top of stack to real stack.
2836 if (is_float) {
2837 __ fsts(Address(ESP, 0));
2838 } else {
2839 __ fstl(Address(ESP, 0));
2840 }
2841
2842 // Pop the 2 items from the FP stack.
2843 __ fucompp();
2844
2845 // Load the value from the stack into an XMM register.
2846 DCHECK(out.IsFpuRegister()) << out;
2847 if (is_float) {
2848 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2849 } else {
2850 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2851 }
2852
2853 // And remove the temporary stack space we allocated.
2854 __ addl(ESP, Immediate(2 * elem_size));
2855}
2856
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002857
2858void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2859 DCHECK(instruction->IsDiv() || instruction->IsRem());
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002863 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002864
2865 Register out_register = locations->Out().AsRegister<Register>();
2866 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002867 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002868
2869 DCHECK(imm == 1 || imm == -1);
2870
2871 if (instruction->IsRem()) {
2872 __ xorl(out_register, out_register);
2873 } else {
2874 __ movl(out_register, input_register);
2875 if (imm == -1) {
2876 __ negl(out_register);
2877 }
2878 }
2879}
2880
2881
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002882void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002883 LocationSummary* locations = instruction->GetLocations();
2884
2885 Register out_register = locations->Out().AsRegister<Register>();
2886 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002887 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002888
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002889 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002890 Register num = locations->GetTemp(0).AsRegister<Register>();
2891
2892 __ leal(num, Address(input_register, std::abs(imm) - 1));
2893 __ testl(input_register, input_register);
2894 __ cmovl(kGreaterEqual, num, input_register);
2895 int shift = CTZ(imm);
2896 __ sarl(num, Immediate(shift));
2897
2898 if (imm < 0) {
2899 __ negl(num);
2900 }
2901
2902 __ movl(out_register, num);
2903}
2904
2905void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2906 DCHECK(instruction->IsDiv() || instruction->IsRem());
2907
2908 LocationSummary* locations = instruction->GetLocations();
2909 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2910
2911 Register eax = locations->InAt(0).AsRegister<Register>();
2912 Register out = locations->Out().AsRegister<Register>();
2913 Register num;
2914 Register edx;
2915
2916 if (instruction->IsDiv()) {
2917 edx = locations->GetTemp(0).AsRegister<Register>();
2918 num = locations->GetTemp(1).AsRegister<Register>();
2919 } else {
2920 edx = locations->Out().AsRegister<Register>();
2921 num = locations->GetTemp(0).AsRegister<Register>();
2922 }
2923
2924 DCHECK_EQ(EAX, eax);
2925 DCHECK_EQ(EDX, edx);
2926 if (instruction->IsDiv()) {
2927 DCHECK_EQ(EAX, out);
2928 } else {
2929 DCHECK_EQ(EDX, out);
2930 }
2931
2932 int64_t magic;
2933 int shift;
2934 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2935
Mark Mendell0c9497d2015-08-21 09:30:05 -04002936 NearLabel ndiv;
2937 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002938 // If numerator is 0, the result is 0, no computation needed.
2939 __ testl(eax, eax);
2940 __ j(kNotEqual, &ndiv);
2941
2942 __ xorl(out, out);
2943 __ jmp(&end);
2944
2945 __ Bind(&ndiv);
2946
2947 // Save the numerator.
2948 __ movl(num, eax);
2949
2950 // EAX = magic
2951 __ movl(eax, Immediate(magic));
2952
2953 // EDX:EAX = magic * numerator
2954 __ imull(num);
2955
2956 if (imm > 0 && magic < 0) {
2957 // EDX += num
2958 __ addl(edx, num);
2959 } else if (imm < 0 && magic > 0) {
2960 __ subl(edx, num);
2961 }
2962
2963 // Shift if needed.
2964 if (shift != 0) {
2965 __ sarl(edx, Immediate(shift));
2966 }
2967
2968 // EDX += 1 if EDX < 0
2969 __ movl(eax, edx);
2970 __ shrl(edx, Immediate(31));
2971 __ addl(edx, eax);
2972
2973 if (instruction->IsRem()) {
2974 __ movl(eax, num);
2975 __ imull(edx, Immediate(imm));
2976 __ subl(eax, edx);
2977 __ movl(edx, eax);
2978 } else {
2979 __ movl(eax, edx);
2980 }
2981 __ Bind(&end);
2982}
2983
Calin Juravlebacfec32014-11-14 15:54:36 +00002984void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2985 DCHECK(instruction->IsDiv() || instruction->IsRem());
2986
2987 LocationSummary* locations = instruction->GetLocations();
2988 Location out = locations->Out();
2989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
2991 bool is_div = instruction->IsDiv();
2992
2993 switch (instruction->GetResultType()) {
2994 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 DCHECK_EQ(EAX, first.AsRegister<Register>());
2996 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002997
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002998 if (instruction->InputAt(1)->IsIntConstant()) {
2999 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003000
3001 if (imm == 0) {
3002 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3003 } else if (imm == 1 || imm == -1) {
3004 DivRemOneOrMinusOne(instruction);
3005 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003006 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003007 } else {
3008 DCHECK(imm <= -2 || imm >= 2);
3009 GenerateDivRemWithAnyConstant(instruction);
3010 }
3011 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003012 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003013 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003014 is_div);
3015 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003016
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003017 Register second_reg = second.AsRegister<Register>();
3018 // 0x80000000/-1 triggers an arithmetic exception!
3019 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3020 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003021
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003022 __ cmpl(second_reg, Immediate(-1));
3023 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003024
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025 // edx:eax <- sign-extended of eax
3026 __ cdq();
3027 // eax = quotient, edx = remainder
3028 __ idivl(second_reg);
3029 __ Bind(slow_path->GetExitLabel());
3030 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 break;
3032 }
3033
3034 case Primitive::kPrimLong: {
3035 InvokeRuntimeCallingConvention calling_convention;
3036 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3037 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3038 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3039 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3040 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3041 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3042
3043 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003044 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3045 instruction,
3046 instruction->GetDexPc(),
3047 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003048 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003049 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3050 instruction,
3051 instruction->GetDexPc(),
3052 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003053 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003054 break;
3055 }
3056
3057 default:
3058 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3059 }
3060}
3061
Calin Juravle7c4954d2014-10-28 16:57:40 +00003062void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003063 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003064 ? LocationSummary::kCall
3065 : LocationSummary::kNoCall;
3066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3067
Calin Juravle7c4954d2014-10-28 16:57:40 +00003068 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003069 case Primitive::kPrimInt: {
3070 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003071 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003072 locations->SetOut(Location::SameAsFirstInput());
3073 // Intel uses edx:eax as the dividend.
3074 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003075 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3076 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3077 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003078 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003079 locations->AddTemp(Location::RequiresRegister());
3080 }
Calin Juravled0d48522014-11-04 16:40:20 +00003081 break;
3082 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003083 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003084 InvokeRuntimeCallingConvention calling_convention;
3085 locations->SetInAt(0, Location::RegisterPairLocation(
3086 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3087 locations->SetInAt(1, Location::RegisterPairLocation(
3088 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3089 // Runtime helper puts the result in EAX, EDX.
3090 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003091 break;
3092 }
3093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble: {
3095 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003096 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003097 locations->SetOut(Location::SameAsFirstInput());
3098 break;
3099 }
3100
3101 default:
3102 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3103 }
3104}
3105
3106void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3107 LocationSummary* locations = div->GetLocations();
3108 Location first = locations->InAt(0);
3109 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003110
3111 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003112 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003113 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003114 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003115 break;
3116 }
3117
3118 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003119 if (second.IsFpuRegister()) {
3120 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3121 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3122 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3123 DCHECK(!const_area->NeedsMaterialization());
3124 __ divss(first.AsFpuRegister<XmmRegister>(),
3125 codegen_->LiteralFloatAddress(
3126 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3127 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3128 } else {
3129 DCHECK(second.IsStackSlot());
3130 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3131 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003132 break;
3133 }
3134
3135 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003136 if (second.IsFpuRegister()) {
3137 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3138 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3139 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3140 DCHECK(!const_area->NeedsMaterialization());
3141 __ divsd(first.AsFpuRegister<XmmRegister>(),
3142 codegen_->LiteralDoubleAddress(
3143 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3144 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3145 } else {
3146 DCHECK(second.IsDoubleStackSlot());
3147 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3148 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003149 break;
3150 }
3151
3152 default:
3153 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3154 }
3155}
3156
Calin Juravlebacfec32014-11-14 15:54:36 +00003157void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003158 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003159
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003160 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3161 ? LocationSummary::kCall
3162 : LocationSummary::kNoCall;
3163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003164
Calin Juravled2ec87d2014-12-08 14:24:46 +00003165 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003166 case Primitive::kPrimInt: {
3167 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003168 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003169 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003170 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3171 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3172 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003173 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003174 locations->AddTemp(Location::RequiresRegister());
3175 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003176 break;
3177 }
3178 case Primitive::kPrimLong: {
3179 InvokeRuntimeCallingConvention calling_convention;
3180 locations->SetInAt(0, Location::RegisterPairLocation(
3181 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3182 locations->SetInAt(1, Location::RegisterPairLocation(
3183 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3184 // Runtime helper puts the result in EAX, EDX.
3185 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3186 break;
3187 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003189 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003190 locations->SetInAt(0, Location::Any());
3191 locations->SetInAt(1, Location::Any());
3192 locations->SetOut(Location::RequiresFpuRegister());
3193 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003194 break;
3195 }
3196
3197 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003198 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003199 }
3200}
3201
3202void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3203 Primitive::Type type = rem->GetResultType();
3204 switch (type) {
3205 case Primitive::kPrimInt:
3206 case Primitive::kPrimLong: {
3207 GenerateDivRemIntegral(rem);
3208 break;
3209 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003210 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003211 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003213 break;
3214 }
3215 default:
3216 LOG(FATAL) << "Unexpected rem type " << type;
3217 }
3218}
3219
Calin Juravled0d48522014-11-04 16:40:20 +00003220void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003221 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3222 ? LocationSummary::kCallOnSlowPath
3223 : LocationSummary::kNoCall;
3224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003225 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003226 case Primitive::kPrimByte:
3227 case Primitive::kPrimChar:
3228 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003229 case Primitive::kPrimInt: {
3230 locations->SetInAt(0, Location::Any());
3231 break;
3232 }
3233 case Primitive::kPrimLong: {
3234 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3235 if (!instruction->IsConstant()) {
3236 locations->AddTemp(Location::RequiresRegister());
3237 }
3238 break;
3239 }
3240 default:
3241 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3242 }
Calin Juravled0d48522014-11-04 16:40:20 +00003243 if (instruction->HasUses()) {
3244 locations->SetOut(Location::SameAsFirstInput());
3245 }
3246}
3247
3248void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003249 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003250 codegen_->AddSlowPath(slow_path);
3251
3252 LocationSummary* locations = instruction->GetLocations();
3253 Location value = locations->InAt(0);
3254
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003255 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003256 case Primitive::kPrimByte:
3257 case Primitive::kPrimChar:
3258 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003259 case Primitive::kPrimInt: {
3260 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003262 __ j(kEqual, slow_path->GetEntryLabel());
3263 } else if (value.IsStackSlot()) {
3264 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3265 __ j(kEqual, slow_path->GetEntryLabel());
3266 } else {
3267 DCHECK(value.IsConstant()) << value;
3268 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3269 __ jmp(slow_path->GetEntryLabel());
3270 }
3271 }
3272 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003273 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 case Primitive::kPrimLong: {
3275 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003277 __ movl(temp, value.AsRegisterPairLow<Register>());
3278 __ orl(temp, value.AsRegisterPairHigh<Register>());
3279 __ j(kEqual, slow_path->GetEntryLabel());
3280 } else {
3281 DCHECK(value.IsConstant()) << value;
3282 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3283 __ jmp(slow_path->GetEntryLabel());
3284 }
3285 }
3286 break;
3287 }
3288 default:
3289 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003290 }
Calin Juravled0d48522014-11-04 16:40:20 +00003291}
3292
Calin Juravle9aec02f2014-11-18 23:06:35 +00003293void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3294 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3295
3296 LocationSummary* locations =
3297 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3298
3299 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003300 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003301 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003302 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003303 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003304 // The shift count needs to be in CL or a constant.
3305 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003306 locations->SetOut(Location::SameAsFirstInput());
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3311 }
3312}
3313
3314void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3315 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3316
3317 LocationSummary* locations = op->GetLocations();
3318 Location first = locations->InAt(0);
3319 Location second = locations->InAt(1);
3320 DCHECK(first.Equals(locations->Out()));
3321
3322 switch (op->GetResultType()) {
3323 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003324 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003325 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003328 DCHECK_EQ(ECX, second_reg);
3329 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003330 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003331 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003332 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003333 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003334 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335 }
3336 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003337 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3338 if (shift == 0) {
3339 return;
3340 }
3341 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003342 if (op->IsShl()) {
3343 __ shll(first_reg, imm);
3344 } else if (op->IsShr()) {
3345 __ sarl(first_reg, imm);
3346 } else {
3347 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003348 }
3349 }
3350 break;
3351 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003352 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003353 if (second.IsRegister()) {
3354 Register second_reg = second.AsRegister<Register>();
3355 DCHECK_EQ(ECX, second_reg);
3356 if (op->IsShl()) {
3357 GenerateShlLong(first, second_reg);
3358 } else if (op->IsShr()) {
3359 GenerateShrLong(first, second_reg);
3360 } else {
3361 GenerateUShrLong(first, second_reg);
3362 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003363 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003364 // Shift by a constant.
3365 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3366 // Nothing to do if the shift is 0, as the input is already the output.
3367 if (shift != 0) {
3368 if (op->IsShl()) {
3369 GenerateShlLong(first, shift);
3370 } else if (op->IsShr()) {
3371 GenerateShrLong(first, shift);
3372 } else {
3373 GenerateUShrLong(first, shift);
3374 }
3375 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003376 }
3377 break;
3378 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003379 default:
3380 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3381 }
3382}
3383
Mark P Mendell73945692015-04-29 14:56:17 +00003384void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3385 Register low = loc.AsRegisterPairLow<Register>();
3386 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003387 if (shift == 1) {
3388 // This is just an addition.
3389 __ addl(low, low);
3390 __ adcl(high, high);
3391 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003392 // Shift by 32 is easy. High gets low, and low gets 0.
3393 codegen_->EmitParallelMoves(
3394 loc.ToLow(),
3395 loc.ToHigh(),
3396 Primitive::kPrimInt,
3397 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3398 loc.ToLow(),
3399 Primitive::kPrimInt);
3400 } else if (shift > 32) {
3401 // Low part becomes 0. High part is low part << (shift-32).
3402 __ movl(high, low);
3403 __ shll(high, Immediate(shift - 32));
3404 __ xorl(low, low);
3405 } else {
3406 // Between 1 and 31.
3407 __ shld(high, low, Immediate(shift));
3408 __ shll(low, Immediate(shift));
3409 }
3410}
3411
Calin Juravle9aec02f2014-11-18 23:06:35 +00003412void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003413 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003414 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3415 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3416 __ testl(shifter, Immediate(32));
3417 __ j(kEqual, &done);
3418 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3419 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3420 __ Bind(&done);
3421}
3422
Mark P Mendell73945692015-04-29 14:56:17 +00003423void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3424 Register low = loc.AsRegisterPairLow<Register>();
3425 Register high = loc.AsRegisterPairHigh<Register>();
3426 if (shift == 32) {
3427 // Need to copy the sign.
3428 DCHECK_NE(low, high);
3429 __ movl(low, high);
3430 __ sarl(high, Immediate(31));
3431 } else if (shift > 32) {
3432 DCHECK_NE(low, high);
3433 // High part becomes sign. Low part is shifted by shift - 32.
3434 __ movl(low, high);
3435 __ sarl(high, Immediate(31));
3436 __ sarl(low, Immediate(shift - 32));
3437 } else {
3438 // Between 1 and 31.
3439 __ shrd(low, high, Immediate(shift));
3440 __ sarl(high, Immediate(shift));
3441 }
3442}
3443
Calin Juravle9aec02f2014-11-18 23:06:35 +00003444void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003445 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003446 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3447 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3448 __ testl(shifter, Immediate(32));
3449 __ j(kEqual, &done);
3450 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3451 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3452 __ Bind(&done);
3453}
3454
Mark P Mendell73945692015-04-29 14:56:17 +00003455void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3456 Register low = loc.AsRegisterPairLow<Register>();
3457 Register high = loc.AsRegisterPairHigh<Register>();
3458 if (shift == 32) {
3459 // Shift by 32 is easy. Low gets high, and high gets 0.
3460 codegen_->EmitParallelMoves(
3461 loc.ToHigh(),
3462 loc.ToLow(),
3463 Primitive::kPrimInt,
3464 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3465 loc.ToHigh(),
3466 Primitive::kPrimInt);
3467 } else if (shift > 32) {
3468 // Low part is high >> (shift - 32). High part becomes 0.
3469 __ movl(low, high);
3470 __ shrl(low, Immediate(shift - 32));
3471 __ xorl(high, high);
3472 } else {
3473 // Between 1 and 31.
3474 __ shrd(low, high, Immediate(shift));
3475 __ shrl(high, Immediate(shift));
3476 }
3477}
3478
Calin Juravle9aec02f2014-11-18 23:06:35 +00003479void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003480 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003481 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3482 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3483 __ testl(shifter, Immediate(32));
3484 __ j(kEqual, &done);
3485 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3486 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3487 __ Bind(&done);
3488}
3489
3490void LocationsBuilderX86::VisitShl(HShl* shl) {
3491 HandleShift(shl);
3492}
3493
3494void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3495 HandleShift(shl);
3496}
3497
3498void LocationsBuilderX86::VisitShr(HShr* shr) {
3499 HandleShift(shr);
3500}
3501
3502void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3503 HandleShift(shr);
3504}
3505
3506void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3507 HandleShift(ushr);
3508}
3509
3510void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3511 HandleShift(ushr);
3512}
3513
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003514void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003517 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003518 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003519 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003520 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003521}
3522
3523void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3524 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003525 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003526 // Note: if heap poisoning is enabled, the entry point takes cares
3527 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003528 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3529 instruction,
3530 instruction->GetDexPc(),
3531 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003532 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003533}
3534
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003535void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3536 LocationSummary* locations =
3537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3538 locations->SetOut(Location::RegisterLocation(EAX));
3539 InvokeRuntimeCallingConvention calling_convention;
3540 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003541 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003542 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003543}
3544
3545void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3546 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003547 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3548
Roland Levillain4d027112015-07-01 15:41:14 +01003549 // Note: if heap poisoning is enabled, the entry point takes cares
3550 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003551 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3552 instruction,
3553 instruction->GetDexPc(),
3554 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003555 DCHECK(!codegen_->IsLeafMethod());
3556}
3557
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003558void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003561 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3562 if (location.IsStackSlot()) {
3563 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3564 } else if (location.IsDoubleStackSlot()) {
3565 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003566 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003567 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003568}
3569
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003570void InstructionCodeGeneratorX86::VisitParameterValue(
3571 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3572}
3573
3574void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3577 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3578}
3579
3580void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003581}
3582
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003583void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003584 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003585 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003588}
3589
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003590void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3591 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003592 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003593 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003594 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003595 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003596 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003598 break;
3599
3600 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003601 __ notl(out.AsRegisterPairLow<Register>());
3602 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003603 break;
3604
3605 default:
3606 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3607 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003608}
3609
David Brazdil66d126e2015-04-03 16:02:44 +01003610void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3611 LocationSummary* locations =
3612 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3613 locations->SetInAt(0, Location::RequiresRegister());
3614 locations->SetOut(Location::SameAsFirstInput());
3615}
3616
3617void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003618 LocationSummary* locations = bool_not->GetLocations();
3619 Location in = locations->InAt(0);
3620 Location out = locations->Out();
3621 DCHECK(in.Equals(out));
3622 __ xorl(out.AsRegister<Register>(), Immediate(1));
3623}
3624
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003625void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003628 switch (compare->InputAt(0)->GetType()) {
3629 case Primitive::kPrimLong: {
3630 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003631 locations->SetInAt(1, Location::Any());
3632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3633 break;
3634 }
3635 case Primitive::kPrimFloat:
3636 case Primitive::kPrimDouble: {
3637 locations->SetInAt(0, Location::RequiresFpuRegister());
3638 locations->SetInAt(1, Location::RequiresFpuRegister());
3639 locations->SetOut(Location::RequiresRegister());
3640 break;
3641 }
3642 default:
3643 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3644 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003645}
3646
3647void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003648 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003650 Location left = locations->InAt(0);
3651 Location right = locations->InAt(1);
3652
Mark Mendell0c9497d2015-08-21 09:30:05 -04003653 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003654 switch (compare->InputAt(0)->GetType()) {
3655 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003656 Register left_low = left.AsRegisterPairLow<Register>();
3657 Register left_high = left.AsRegisterPairHigh<Register>();
3658 int32_t val_low = 0;
3659 int32_t val_high = 0;
3660 bool right_is_const = false;
3661
3662 if (right.IsConstant()) {
3663 DCHECK(right.GetConstant()->IsLongConstant());
3664 right_is_const = true;
3665 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3666 val_low = Low32Bits(val);
3667 val_high = High32Bits(val);
3668 }
3669
Calin Juravleddb7df22014-11-25 20:56:51 +00003670 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003671 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003672 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003673 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003674 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003675 DCHECK(right_is_const) << right;
3676 if (val_high == 0) {
3677 __ testl(left_high, left_high);
3678 } else {
3679 __ cmpl(left_high, Immediate(val_high));
3680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003681 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003682 __ j(kLess, &less); // Signed compare.
3683 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003684 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003685 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003686 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003687 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003688 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003689 DCHECK(right_is_const) << right;
3690 if (val_low == 0) {
3691 __ testl(left_low, left_low);
3692 } else {
3693 __ cmpl(left_low, Immediate(val_low));
3694 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003696 break;
3697 }
3698 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003700 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3701 break;
3702 }
3703 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003705 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 break;
3707 }
3708 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003709 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003710 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 __ movl(out, Immediate(0));
3712 __ j(kEqual, &done);
3713 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3714
3715 __ Bind(&greater);
3716 __ movl(out, Immediate(1));
3717 __ jmp(&done);
3718
3719 __ Bind(&less);
3720 __ movl(out, Immediate(-1));
3721
3722 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003723}
3724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003725void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003726 LocationSummary* locations =
3727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003728 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3729 locations->SetInAt(i, Location::Any());
3730 }
3731 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003732}
3733
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003734void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003735 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003736}
3737
Calin Juravle52c48962014-12-16 17:02:57 +00003738void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3739 /*
3740 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3741 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3742 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3743 */
3744 switch (kind) {
3745 case MemBarrierKind::kAnyAny: {
3746 __ mfence();
3747 break;
3748 }
3749 case MemBarrierKind::kAnyStore:
3750 case MemBarrierKind::kLoadAny:
3751 case MemBarrierKind::kStoreStore: {
3752 // nop
3753 break;
3754 }
3755 default:
3756 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003757 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003758}
3759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003760
Vladimir Marko58155012015-08-19 12:49:41 +00003761void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3762 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3763 switch (invoke->GetMethodLoadKind()) {
3764 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3765 // temp = thread->string_init_entrypoint
3766 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3767 break;
3768 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3769 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3770 break;
3771 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3772 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3773 break;
3774 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3775 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3776 method_patches_.emplace_back(invoke->GetTargetMethod());
3777 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3778 break;
3779 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3780 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3781 FALLTHROUGH_INTENDED;
3782 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3783 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3784 Register method_reg;
3785 Register reg = temp.AsRegister<Register>();
3786 if (current_method.IsRegister()) {
3787 method_reg = current_method.AsRegister<Register>();
3788 } else {
3789 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3790 DCHECK(!current_method.IsValid());
3791 method_reg = reg;
3792 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3793 }
3794 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003795 __ movl(reg, Address(method_reg,
3796 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003797 // temp = temp[index_in_cache]
3798 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3799 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3800 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003801 }
Vladimir Marko58155012015-08-19 12:49:41 +00003802 }
3803
3804 switch (invoke->GetCodePtrLocation()) {
3805 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3806 __ call(GetFrameEntryLabel());
3807 break;
3808 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3809 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3810 Label* label = &relative_call_patches_.back().label;
3811 __ call(label); // Bind to the patch label, override at link time.
3812 __ Bind(label); // Bind the label at the end of the "call" insn.
3813 break;
3814 }
3815 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3816 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3817 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3818 // (Though the direct CALL ptr16:32 is available for consideration).
3819 FALLTHROUGH_INTENDED;
3820 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3821 // (callee_method + offset_of_quick_compiled_code)()
3822 __ call(Address(callee_method.AsRegister<Register>(),
3823 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3824 kX86WordSize).Int32Value()));
3825 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003826 }
3827
3828 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003829}
3830
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003831void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3832 Register temp = temp_in.AsRegister<Register>();
3833 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3834 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3835 LocationSummary* locations = invoke->GetLocations();
3836 Location receiver = locations->InAt(0);
3837 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3838 // temp = object->GetClass();
3839 DCHECK(receiver.IsRegister());
3840 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3841 MaybeRecordImplicitNullCheck(invoke);
3842 __ MaybeUnpoisonHeapReference(temp);
3843 // temp = temp->GetMethodAt(method_offset);
3844 __ movl(temp, Address(temp, method_offset));
3845 // call temp->GetEntryPoint();
3846 __ call(Address(
3847 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3848}
3849
Vladimir Marko58155012015-08-19 12:49:41 +00003850void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3851 DCHECK(linker_patches->empty());
3852 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3853 for (const MethodPatchInfo<Label>& info : method_patches_) {
3854 // The label points to the end of the "movl" insn but the literal offset for method
3855 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3856 uint32_t literal_offset = info.label.Position() - 4;
3857 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3858 info.target_method.dex_file,
3859 info.target_method.dex_method_index));
3860 }
3861 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3862 // The label points to the end of the "call" insn but the literal offset for method
3863 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3864 uint32_t literal_offset = info.label.Position() - 4;
3865 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3866 info.target_method.dex_file,
3867 info.target_method.dex_method_index));
3868 }
3869}
3870
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003871void CodeGeneratorX86::MarkGCCard(Register temp,
3872 Register card,
3873 Register object,
3874 Register value,
3875 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003876 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003877 if (value_can_be_null) {
3878 __ testl(value, value);
3879 __ j(kEqual, &is_null);
3880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003881 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3882 __ movl(temp, object);
3883 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003884 __ movb(Address(temp, card, TIMES_1, 0),
3885 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003886 if (value_can_be_null) {
3887 __ Bind(&is_null);
3888 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003889}
3890
Calin Juravle52c48962014-12-16 17:02:57 +00003891void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3892 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003893 LocationSummary* locations =
3894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003895 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003896
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003897 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3898 locations->SetOut(Location::RequiresFpuRegister());
3899 } else {
3900 // The output overlaps in case of long: we don't want the low move to overwrite
3901 // the object's location.
3902 locations->SetOut(Location::RequiresRegister(),
3903 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3904 : Location::kNoOutputOverlap);
3905 }
Calin Juravle52c48962014-12-16 17:02:57 +00003906
3907 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3908 // Long values can be loaded atomically into an XMM using movsd.
3909 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3910 // and then copy the XMM into the output 32bits at a time).
3911 locations->AddTemp(Location::RequiresFpuRegister());
3912 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003913}
3914
Calin Juravle52c48962014-12-16 17:02:57 +00003915void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3916 const FieldInfo& field_info) {
3917 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918
Calin Juravle52c48962014-12-16 17:02:57 +00003919 LocationSummary* locations = instruction->GetLocations();
3920 Register base = locations->InAt(0).AsRegister<Register>();
3921 Location out = locations->Out();
3922 bool is_volatile = field_info.IsVolatile();
3923 Primitive::Type field_type = field_info.GetFieldType();
3924 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3925
3926 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003927 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003928 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003929 break;
3930 }
3931
3932 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003933 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934 break;
3935 }
3936
3937 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003938 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 break;
3940 }
3941
3942 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003943 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003944 break;
3945 }
3946
3947 case Primitive::kPrimInt:
3948 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003949 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003950 break;
3951 }
3952
3953 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003954 if (is_volatile) {
3955 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3956 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003958 __ movd(out.AsRegisterPairLow<Register>(), temp);
3959 __ psrlq(temp, Immediate(32));
3960 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3961 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003962 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003963 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003964 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003965 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3966 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003967 break;
3968 }
3969
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003970 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003971 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003972 break;
3973 }
3974
3975 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003976 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003977 break;
3978 }
3979
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003980 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003981 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003982 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003983 }
Calin Juravle52c48962014-12-16 17:02:57 +00003984
Calin Juravle77520bc2015-01-12 18:45:46 +00003985 // Longs are handled in the switch.
3986 if (field_type != Primitive::kPrimLong) {
3987 codegen_->MaybeRecordImplicitNullCheck(instruction);
3988 }
3989
Calin Juravle52c48962014-12-16 17:02:57 +00003990 if (is_volatile) {
3991 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3992 }
Roland Levillain4d027112015-07-01 15:41:14 +01003993
3994 if (field_type == Primitive::kPrimNot) {
3995 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3996 }
Calin Juravle52c48962014-12-16 17:02:57 +00003997}
3998
3999void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4000 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4001
4002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4004 locations->SetInAt(0, Location::RequiresRegister());
4005 bool is_volatile = field_info.IsVolatile();
4006 Primitive::Type field_type = field_info.GetFieldType();
4007 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4008 || (field_type == Primitive::kPrimByte);
4009
4010 // The register allocator does not support multiple
4011 // inputs that die at entry with one in a specific register.
4012 if (is_byte_type) {
4013 // Ensure the value is in a byte register.
4014 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004015 } else if (Primitive::IsFloatingPointType(field_type)) {
4016 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004017 } else {
4018 locations->SetInAt(1, Location::RequiresRegister());
4019 }
Calin Juravle52c48962014-12-16 17:02:57 +00004020 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01004021 // Temporary registers for the write barrier.
4022 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00004023 // Ensure the card is in a byte register.
4024 locations->AddTemp(Location::RegisterLocation(ECX));
4025 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
4026 // 64bits value can be atomically written to an address with movsd and an XMM register.
4027 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4028 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4029 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4030 // isolated cases when we need this it isn't worth adding the extra complexity.
4031 locations->AddTemp(Location::RequiresFpuRegister());
4032 locations->AddTemp(Location::RequiresFpuRegister());
4033 }
4034}
4035
4036void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004037 const FieldInfo& field_info,
4038 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004039 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4040
4041 LocationSummary* locations = instruction->GetLocations();
4042 Register base = locations->InAt(0).AsRegister<Register>();
4043 Location value = locations->InAt(1);
4044 bool is_volatile = field_info.IsVolatile();
4045 Primitive::Type field_type = field_info.GetFieldType();
4046 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004047 bool needs_write_barrier =
4048 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004049
4050 if (is_volatile) {
4051 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4052 }
4053
4054 switch (field_type) {
4055 case Primitive::kPrimBoolean:
4056 case Primitive::kPrimByte: {
4057 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4058 break;
4059 }
4060
4061 case Primitive::kPrimShort:
4062 case Primitive::kPrimChar: {
4063 __ movw(Address(base, offset), value.AsRegister<Register>());
4064 break;
4065 }
4066
4067 case Primitive::kPrimInt:
4068 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004069 if (kPoisonHeapReferences && needs_write_barrier) {
4070 // Note that in the case where `value` is a null reference,
4071 // we do not enter this block, as the reference does not
4072 // need poisoning.
4073 DCHECK_EQ(field_type, Primitive::kPrimNot);
4074 Register temp = locations->GetTemp(0).AsRegister<Register>();
4075 __ movl(temp, value.AsRegister<Register>());
4076 __ PoisonHeapReference(temp);
4077 __ movl(Address(base, offset), temp);
4078 } else {
4079 __ movl(Address(base, offset), value.AsRegister<Register>());
4080 }
Calin Juravle52c48962014-12-16 17:02:57 +00004081 break;
4082 }
4083
4084 case Primitive::kPrimLong: {
4085 if (is_volatile) {
4086 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4087 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4088 __ movd(temp1, value.AsRegisterPairLow<Register>());
4089 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4090 __ punpckldq(temp1, temp2);
4091 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004093 } else {
4094 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004095 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004096 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4097 }
4098 break;
4099 }
4100
4101 case Primitive::kPrimFloat: {
4102 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4103 break;
4104 }
4105
4106 case Primitive::kPrimDouble: {
4107 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4108 break;
4109 }
4110
4111 case Primitive::kPrimVoid:
4112 LOG(FATAL) << "Unreachable type " << field_type;
4113 UNREACHABLE();
4114 }
4115
Calin Juravle77520bc2015-01-12 18:45:46 +00004116 // Longs are handled in the switch.
4117 if (field_type != Primitive::kPrimLong) {
4118 codegen_->MaybeRecordImplicitNullCheck(instruction);
4119 }
4120
Roland Levillain4d027112015-07-01 15:41:14 +01004121 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004122 Register temp = locations->GetTemp(0).AsRegister<Register>();
4123 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004124 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004125 }
4126
Calin Juravle52c48962014-12-16 17:02:57 +00004127 if (is_volatile) {
4128 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4129 }
4130}
4131
4132void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4133 HandleFieldGet(instruction, instruction->GetFieldInfo());
4134}
4135
4136void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4137 HandleFieldGet(instruction, instruction->GetFieldInfo());
4138}
4139
4140void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4141 HandleFieldSet(instruction, instruction->GetFieldInfo());
4142}
4143
4144void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004145 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004146}
4147
4148void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4149 HandleFieldSet(instruction, instruction->GetFieldInfo());
4150}
4151
4152void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004153 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004154}
4155
4156void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4157 HandleFieldGet(instruction, instruction->GetFieldInfo());
4158}
4159
4160void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4161 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004162}
4163
Calin Juravlee460d1d2015-09-29 04:52:17 +01004164void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4165 HUnresolvedInstanceFieldGet* instruction) {
4166 FieldAccessCallingConventionX86 calling_convention;
4167 codegen_->CreateUnresolvedFieldLocationSummary(
4168 instruction, instruction->GetFieldType(), calling_convention);
4169}
4170
4171void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4172 HUnresolvedInstanceFieldGet* instruction) {
4173 FieldAccessCallingConventionX86 calling_convention;
4174 codegen_->GenerateUnresolvedFieldAccess(instruction,
4175 instruction->GetFieldType(),
4176 instruction->GetFieldIndex(),
4177 instruction->GetDexPc(),
4178 calling_convention);
4179}
4180
4181void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4182 HUnresolvedInstanceFieldSet* instruction) {
4183 FieldAccessCallingConventionX86 calling_convention;
4184 codegen_->CreateUnresolvedFieldLocationSummary(
4185 instruction, instruction->GetFieldType(), calling_convention);
4186}
4187
4188void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4189 HUnresolvedInstanceFieldSet* instruction) {
4190 FieldAccessCallingConventionX86 calling_convention;
4191 codegen_->GenerateUnresolvedFieldAccess(instruction,
4192 instruction->GetFieldType(),
4193 instruction->GetFieldIndex(),
4194 instruction->GetDexPc(),
4195 calling_convention);
4196}
4197
4198void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4199 HUnresolvedStaticFieldGet* instruction) {
4200 FieldAccessCallingConventionX86 calling_convention;
4201 codegen_->CreateUnresolvedFieldLocationSummary(
4202 instruction, instruction->GetFieldType(), calling_convention);
4203}
4204
4205void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4206 HUnresolvedStaticFieldGet* instruction) {
4207 FieldAccessCallingConventionX86 calling_convention;
4208 codegen_->GenerateUnresolvedFieldAccess(instruction,
4209 instruction->GetFieldType(),
4210 instruction->GetFieldIndex(),
4211 instruction->GetDexPc(),
4212 calling_convention);
4213}
4214
4215void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4216 HUnresolvedStaticFieldSet* instruction) {
4217 FieldAccessCallingConventionX86 calling_convention;
4218 codegen_->CreateUnresolvedFieldLocationSummary(
4219 instruction, instruction->GetFieldType(), calling_convention);
4220}
4221
4222void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4223 HUnresolvedStaticFieldSet* instruction) {
4224 FieldAccessCallingConventionX86 calling_convention;
4225 codegen_->GenerateUnresolvedFieldAccess(instruction,
4226 instruction->GetFieldType(),
4227 instruction->GetFieldIndex(),
4228 instruction->GetDexPc(),
4229 calling_convention);
4230}
4231
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004232void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004233 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4234 ? LocationSummary::kCallOnSlowPath
4235 : LocationSummary::kNoCall;
4236 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4237 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004238 ? Location::RequiresRegister()
4239 : Location::Any();
4240 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004241 if (instruction->HasUses()) {
4242 locations->SetOut(Location::SameAsFirstInput());
4243 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244}
4245
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004246void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004247 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4248 return;
4249 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004250 LocationSummary* locations = instruction->GetLocations();
4251 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004252
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004253 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4254 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4255}
4256
4257void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004258 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259 codegen_->AddSlowPath(slow_path);
4260
4261 LocationSummary* locations = instruction->GetLocations();
4262 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004263
4264 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004265 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004266 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004268 } else {
4269 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004270 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004271 __ jmp(slow_path->GetEntryLabel());
4272 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273 }
4274 __ j(kEqual, slow_path->GetEntryLabel());
4275}
4276
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004277void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004278 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004279 GenerateImplicitNullCheck(instruction);
4280 } else {
4281 GenerateExplicitNullCheck(instruction);
4282 }
4283}
4284
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004285void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004286 LocationSummary* locations =
4287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004288 locations->SetInAt(0, Location::RequiresRegister());
4289 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004290 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4291 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4292 } else {
4293 // The output overlaps in case of long: we don't want the low move to overwrite
4294 // the array's location.
4295 locations->SetOut(Location::RequiresRegister(),
4296 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4297 : Location::kNoOutputOverlap);
4298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004299}
4300
4301void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4302 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004303 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304 Location index = locations->InAt(1);
4305
Calin Juravle77520bc2015-01-12 18:45:46 +00004306 Primitive::Type type = instruction->GetType();
4307 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004308 case Primitive::kPrimBoolean: {
4309 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004310 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 if (index.IsConstant()) {
4312 __ movzxb(out, Address(obj,
4313 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4314 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004315 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004316 }
4317 break;
4318 }
4319
4320 case Primitive::kPrimByte: {
4321 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004322 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323 if (index.IsConstant()) {
4324 __ movsxb(out, Address(obj,
4325 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4326 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004327 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004328 }
4329 break;
4330 }
4331
4332 case Primitive::kPrimShort: {
4333 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004334 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335 if (index.IsConstant()) {
4336 __ movsxw(out, Address(obj,
4337 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4338 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004339 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 }
4341 break;
4342 }
4343
4344 case Primitive::kPrimChar: {
4345 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004346 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004347 if (index.IsConstant()) {
4348 __ movzxw(out, Address(obj,
4349 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4350 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 }
4353 break;
4354 }
4355
4356 case Primitive::kPrimInt:
4357 case Primitive::kPrimNot: {
4358 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004359 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004360 if (index.IsConstant()) {
4361 __ movl(out, Address(obj,
4362 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4363 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004364 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004365 }
4366 break;
4367 }
4368
4369 case Primitive::kPrimLong: {
4370 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004371 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004372 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 if (index.IsConstant()) {
4374 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004375 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004377 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004378 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004379 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004380 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004381 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004382 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004383 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004384 }
4385 break;
4386 }
4387
Mark Mendell7c8d0092015-01-26 11:21:33 -05004388 case Primitive::kPrimFloat: {
4389 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4390 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4391 if (index.IsConstant()) {
4392 __ movss(out, Address(obj,
4393 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4394 } else {
4395 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4396 }
4397 break;
4398 }
4399
4400 case Primitive::kPrimDouble: {
4401 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4402 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4403 if (index.IsConstant()) {
4404 __ movsd(out, Address(obj,
4405 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4406 } else {
4407 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4408 }
4409 break;
4410 }
4411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004412 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004414 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004416
4417 if (type != Primitive::kPrimLong) {
4418 codegen_->MaybeRecordImplicitNullCheck(instruction);
4419 }
Roland Levillain4d027112015-07-01 15:41:14 +01004420
4421 if (type == Primitive::kPrimNot) {
4422 Register out = locations->Out().AsRegister<Register>();
4423 __ MaybeUnpoisonHeapReference(out);
4424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425}
4426
4427void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004428 // This location builder might end up asking to up to four registers, which is
4429 // not currently possible for baseline. The situation in which we need four
4430 // registers cannot be met by baseline though, because it has not run any
4431 // optimization.
4432
Nicolas Geoffray39468442014-09-02 15:17:15 +01004433 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004434 bool needs_write_barrier =
4435 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4436
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004437 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004438
Nicolas Geoffray39468442014-09-02 15:17:15 +01004439 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4440 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004441 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004442
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004443 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4444 || (value_type == Primitive::kPrimByte);
4445 // We need the inputs to be different than the output in case of long operation.
4446 // In case of a byte operation, the register allocator does not support multiple
4447 // inputs that die at entry with one in a specific register.
4448 locations->SetInAt(0, Location::RequiresRegister());
4449 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4450 if (is_byte_type) {
4451 // Ensure the value is in a byte register.
4452 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4453 } else if (Primitive::IsFloatingPointType(value_type)) {
4454 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004456 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4457 }
4458 if (needs_write_barrier) {
4459 // Temporary registers for the write barrier.
4460 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4461 // Ensure the card is in a byte register.
4462 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004464}
4465
4466void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4467 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004468 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004469 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004470 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004471 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004472 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4473 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4474 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4475 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004476 bool needs_write_barrier =
4477 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478
4479 switch (value_type) {
4480 case Primitive::kPrimBoolean:
4481 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004482 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4483 Address address = index.IsConstant()
4484 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4485 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4486 if (value.IsRegister()) {
4487 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004488 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004489 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004491 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 break;
4493 }
4494
4495 case Primitive::kPrimShort:
4496 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004497 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4498 Address address = index.IsConstant()
4499 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4500 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4501 if (value.IsRegister()) {
4502 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004503 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004504 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004505 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004506 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507 break;
4508 }
4509
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004510 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4512 Address address = index.IsConstant()
4513 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4514 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4515 if (!value.IsRegister()) {
4516 // Just setting null.
4517 DCHECK(instruction->InputAt(2)->IsNullConstant());
4518 DCHECK(value.IsConstant()) << value;
4519 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004521 DCHECK(!needs_write_barrier);
4522 DCHECK(!may_need_runtime_call);
4523 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004525
4526 DCHECK(needs_write_barrier);
4527 Register register_value = value.AsRegister<Register>();
4528 NearLabel done, not_null, do_put;
4529 SlowPathCode* slow_path = nullptr;
4530 Register temp = locations->GetTemp(0).AsRegister<Register>();
4531 if (may_need_runtime_call) {
4532 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4533 codegen_->AddSlowPath(slow_path);
4534 if (instruction->GetValueCanBeNull()) {
4535 __ testl(register_value, register_value);
4536 __ j(kNotEqual, &not_null);
4537 __ movl(address, Immediate(0));
4538 codegen_->MaybeRecordImplicitNullCheck(instruction);
4539 __ jmp(&done);
4540 __ Bind(&not_null);
4541 }
4542
4543 __ movl(temp, Address(array, class_offset));
4544 codegen_->MaybeRecordImplicitNullCheck(instruction);
4545 __ MaybeUnpoisonHeapReference(temp);
4546 __ movl(temp, Address(temp, component_offset));
4547 // No need to poison/unpoison, we're comparing two poisoned references.
4548 __ cmpl(temp, Address(register_value, class_offset));
4549 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4550 __ j(kEqual, &do_put);
4551 __ MaybeUnpoisonHeapReference(temp);
4552 __ movl(temp, Address(temp, super_offset));
4553 // No need to unpoison, we're comparing against null..
4554 __ testl(temp, temp);
4555 __ j(kNotEqual, slow_path->GetEntryLabel());
4556 __ Bind(&do_put);
4557 } else {
4558 __ j(kNotEqual, slow_path->GetEntryLabel());
4559 }
4560 }
4561
4562 if (kPoisonHeapReferences) {
4563 __ movl(temp, register_value);
4564 __ PoisonHeapReference(temp);
4565 __ movl(address, temp);
4566 } else {
4567 __ movl(address, register_value);
4568 }
4569 if (!may_need_runtime_call) {
4570 codegen_->MaybeRecordImplicitNullCheck(instruction);
4571 }
4572
4573 Register card = locations->GetTemp(1).AsRegister<Register>();
4574 codegen_->MarkGCCard(
4575 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4576 __ Bind(&done);
4577
4578 if (slow_path != nullptr) {
4579 __ Bind(slow_path->GetExitLabel());
4580 }
4581
4582 break;
4583 }
4584 case Primitive::kPrimInt: {
4585 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4586 Address address = index.IsConstant()
4587 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4588 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4589 if (value.IsRegister()) {
4590 __ movl(address, value.AsRegister<Register>());
4591 } else {
4592 DCHECK(value.IsConstant()) << value;
4593 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4594 __ movl(address, Immediate(v));
4595 }
4596 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 break;
4598 }
4599
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600 case Primitive::kPrimLong: {
4601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602 if (index.IsConstant()) {
4603 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004604 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004606 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004607 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004608 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004609 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004610 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004611 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004612 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004613 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004614 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004616 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004617 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004618 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004619 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004620 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004621 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004622 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004623 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004624 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004625 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004626 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004627 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004628 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004629 Immediate(High32Bits(val)));
4630 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 }
4632 break;
4633 }
4634
Mark Mendell7c8d0092015-01-26 11:21:33 -05004635 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004636 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4637 Address address = index.IsConstant()
4638 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4639 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004640 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004641 __ movss(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004642 break;
4643 }
4644
4645 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004646 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4647 Address address = index.IsConstant()
4648 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4649 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004650 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004651 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004652 break;
4653 }
4654
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 case Primitive::kPrimVoid:
4656 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004657 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 }
4659}
4660
4661void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004663 locations->SetInAt(0, Location::RequiresRegister());
4664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665}
4666
4667void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4668 LocationSummary* locations = instruction->GetLocations();
4669 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004670 Register obj = locations->InAt(0).AsRegister<Register>();
4671 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004673 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004674}
4675
4676void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004677 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4678 ? LocationSummary::kCallOnSlowPath
4679 : LocationSummary::kNoCall;
4680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004681 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004682 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004683 if (instruction->HasUses()) {
4684 locations->SetOut(Location::SameAsFirstInput());
4685 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686}
4687
4688void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4689 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004690 Location index_loc = locations->InAt(0);
4691 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004692 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004693 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694
Mark Mendell99dbd682015-04-22 16:18:52 -04004695 if (length_loc.IsConstant()) {
4696 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4697 if (index_loc.IsConstant()) {
4698 // BCE will remove the bounds check if we are guarenteed to pass.
4699 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4700 if (index < 0 || index >= length) {
4701 codegen_->AddSlowPath(slow_path);
4702 __ jmp(slow_path->GetEntryLabel());
4703 } else {
4704 // Some optimization after BCE may have generated this, and we should not
4705 // generate a bounds check if it is a valid range.
4706 }
4707 return;
4708 }
4709
4710 // We have to reverse the jump condition because the length is the constant.
4711 Register index_reg = index_loc.AsRegister<Register>();
4712 __ cmpl(index_reg, Immediate(length));
4713 codegen_->AddSlowPath(slow_path);
4714 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004715 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004716 Register length = length_loc.AsRegister<Register>();
4717 if (index_loc.IsConstant()) {
4718 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4719 __ cmpl(length, Immediate(value));
4720 } else {
4721 __ cmpl(length, index_loc.AsRegister<Register>());
4722 }
4723 codegen_->AddSlowPath(slow_path);
4724 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004725 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726}
4727
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004728void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4729 temp->SetLocations(nullptr);
4730}
4731
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004732void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 // Nothing to do, this is driven by the code generator.
4734}
4735
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004736void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004737 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004738}
4739
4740void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004741 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4742}
4743
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004744void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4745 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4746}
4747
4748void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004749 HBasicBlock* block = instruction->GetBlock();
4750 if (block->GetLoopInformation() != nullptr) {
4751 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4752 // The back edge will generate the suspend check.
4753 return;
4754 }
4755 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4756 // The goto will generate the suspend check.
4757 return;
4758 }
4759 GenerateSuspendCheck(instruction, nullptr);
4760}
4761
4762void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4763 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004764 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004765 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4766 if (slow_path == nullptr) {
4767 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4768 instruction->SetSlowPath(slow_path);
4769 codegen_->AddSlowPath(slow_path);
4770 if (successor != nullptr) {
4771 DCHECK(successor->IsLoopHeader());
4772 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4773 }
4774 } else {
4775 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4776 }
4777
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004778 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004779 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004780 if (successor == nullptr) {
4781 __ j(kNotEqual, slow_path->GetEntryLabel());
4782 __ Bind(slow_path->GetReturnLabel());
4783 } else {
4784 __ j(kEqual, codegen_->GetLabelOf(successor));
4785 __ jmp(slow_path->GetEntryLabel());
4786 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004787}
4788
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004789X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4790 return codegen_->GetAssembler();
4791}
4792
Mark Mendell7c8d0092015-01-26 11:21:33 -05004793void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004794 ScratchRegisterScope ensure_scratch(
4795 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4796 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4797 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4798 __ movl(temp_reg, Address(ESP, src + stack_offset));
4799 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004800}
4801
4802void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004803 ScratchRegisterScope ensure_scratch(
4804 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4805 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4806 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4807 __ movl(temp_reg, Address(ESP, src + stack_offset));
4808 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4809 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4810 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004811}
4812
4813void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004814 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004815 Location source = move->GetSource();
4816 Location destination = move->GetDestination();
4817
4818 if (source.IsRegister()) {
4819 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004820 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004821 } else {
4822 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004823 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004824 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004825 } else if (source.IsFpuRegister()) {
4826 if (destination.IsFpuRegister()) {
4827 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4828 } else if (destination.IsStackSlot()) {
4829 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4830 } else {
4831 DCHECK(destination.IsDoubleStackSlot());
4832 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4833 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004834 } else if (source.IsStackSlot()) {
4835 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004836 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004837 } else if (destination.IsFpuRegister()) {
4838 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004839 } else {
4840 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004841 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4842 }
4843 } else if (source.IsDoubleStackSlot()) {
4844 if (destination.IsFpuRegister()) {
4845 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4846 } else {
4847 DCHECK(destination.IsDoubleStackSlot()) << destination;
4848 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004849 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004850 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004851 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004852 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004853 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004854 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004855 if (value == 0) {
4856 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4857 } else {
4858 __ movl(destination.AsRegister<Register>(), Immediate(value));
4859 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004860 } else {
4861 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004862 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004863 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004864 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004865 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004866 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004867 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004868 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004869 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4870 if (value == 0) {
4871 // Easy handling of 0.0.
4872 __ xorps(dest, dest);
4873 } else {
4874 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004875 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4876 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4877 __ movl(temp, Immediate(value));
4878 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004879 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004880 } else {
4881 DCHECK(destination.IsStackSlot()) << destination;
4882 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004884 } else if (constant->IsLongConstant()) {
4885 int64_t value = constant->AsLongConstant()->GetValue();
4886 int32_t low_value = Low32Bits(value);
4887 int32_t high_value = High32Bits(value);
4888 Immediate low(low_value);
4889 Immediate high(high_value);
4890 if (destination.IsDoubleStackSlot()) {
4891 __ movl(Address(ESP, destination.GetStackIndex()), low);
4892 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4893 } else {
4894 __ movl(destination.AsRegisterPairLow<Register>(), low);
4895 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4896 }
4897 } else {
4898 DCHECK(constant->IsDoubleConstant());
4899 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004900 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004901 int32_t low_value = Low32Bits(value);
4902 int32_t high_value = High32Bits(value);
4903 Immediate low(low_value);
4904 Immediate high(high_value);
4905 if (destination.IsFpuRegister()) {
4906 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4907 if (value == 0) {
4908 // Easy handling of 0.0.
4909 __ xorpd(dest, dest);
4910 } else {
4911 __ pushl(high);
4912 __ pushl(low);
4913 __ movsd(dest, Address(ESP, 0));
4914 __ addl(ESP, Immediate(8));
4915 }
4916 } else {
4917 DCHECK(destination.IsDoubleStackSlot()) << destination;
4918 __ movl(Address(ESP, destination.GetStackIndex()), low);
4919 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4920 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004921 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004922 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004923 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004924 }
4925}
4926
Mark Mendella5c19ce2015-04-01 12:51:05 -04004927void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004928 Register suggested_scratch = reg == EAX ? EBX : EAX;
4929 ScratchRegisterScope ensure_scratch(
4930 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4931
4932 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4933 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4934 __ movl(Address(ESP, mem + stack_offset), reg);
4935 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004936}
4937
Mark Mendell7c8d0092015-01-26 11:21:33 -05004938void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004939 ScratchRegisterScope ensure_scratch(
4940 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4941
4942 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4943 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4944 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4945 __ movss(Address(ESP, mem + stack_offset), reg);
4946 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004947}
4948
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004949void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004950 ScratchRegisterScope ensure_scratch1(
4951 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004952
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004953 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4954 ScratchRegisterScope ensure_scratch2(
4955 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004956
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004957 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4958 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4959 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4960 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4961 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4962 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004963}
4964
4965void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004966 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004967 Location source = move->GetSource();
4968 Location destination = move->GetDestination();
4969
4970 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004971 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4972 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4973 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4974 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4975 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004976 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004977 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004978 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004979 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004980 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4981 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004982 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4983 // Use XOR Swap algorithm to avoid a temporary.
4984 DCHECK_NE(source.reg(), destination.reg());
4985 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4986 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4987 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4988 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4989 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4990 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4991 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004992 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4993 // Take advantage of the 16 bytes in the XMM register.
4994 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4995 Address stack(ESP, destination.GetStackIndex());
4996 // Load the double into the high doubleword.
4997 __ movhpd(reg, stack);
4998
4999 // Store the low double into the destination.
5000 __ movsd(stack, reg);
5001
5002 // Move the high double to the low double.
5003 __ psrldq(reg, Immediate(8));
5004 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5005 // Take advantage of the 16 bytes in the XMM register.
5006 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5007 Address stack(ESP, source.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.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5017 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5018 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005019 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005020 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005021 }
5022}
5023
5024void ParallelMoveResolverX86::SpillScratch(int reg) {
5025 __ pushl(static_cast<Register>(reg));
5026}
5027
5028void ParallelMoveResolverX86::RestoreScratch(int reg) {
5029 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005030}
5031
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005032void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005033 InvokeRuntimeCallingConvention calling_convention;
5034 CodeGenerator::CreateLoadClassLocationSummary(
5035 cls,
5036 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5037 Location::RegisterLocation(EAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005038}
5039
5040void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005041 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005042 if (cls->NeedsAccessCheck()) {
5043 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5044 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5045 cls,
5046 cls->GetDexPc(),
5047 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005048 return;
5049 }
5050
5051 Register out = locations->Out().AsRegister<Register>();
5052 Register current_method = locations->InAt(0).AsRegister<Register>();
5053 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005054 DCHECK(!cls->CanCallRuntime());
5055 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07005056 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005057 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005058 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005059 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01005060 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005061 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005062 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005063
Andreas Gampe85b62f22015-09-09 13:15:38 -07005064 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005065 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5066 codegen_->AddSlowPath(slow_path);
5067 __ testl(out, out);
5068 __ j(kEqual, slow_path->GetEntryLabel());
5069 if (cls->MustGenerateClinitCheck()) {
5070 GenerateClassInitializationCheck(slow_path, out);
5071 } else {
5072 __ Bind(slow_path->GetExitLabel());
5073 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005074 }
5075}
5076
5077void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5078 LocationSummary* locations =
5079 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5080 locations->SetInAt(0, Location::RequiresRegister());
5081 if (check->HasUses()) {
5082 locations->SetOut(Location::SameAsFirstInput());
5083 }
5084}
5085
5086void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005087 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005088 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005089 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005090 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005091 GenerateClassInitializationCheck(slow_path,
5092 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005093}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005094
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005095void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005096 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005097 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5098 Immediate(mirror::Class::kStatusInitialized));
5099 __ j(kLess, slow_path->GetEntryLabel());
5100 __ Bind(slow_path->GetExitLabel());
5101 // No need for memory fence, thanks to the X86 memory model.
5102}
5103
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005104void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5105 LocationSummary* locations =
5106 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005107 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005108 locations->SetOut(Location::RequiresRegister());
5109}
5110
5111void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005112 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005113 codegen_->AddSlowPath(slow_path);
5114
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005115 LocationSummary* locations = load->GetLocations();
5116 Register out = locations->Out().AsRegister<Register>();
5117 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005118 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005119 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005120 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005121 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005122 __ testl(out, out);
5123 __ j(kEqual, slow_path->GetEntryLabel());
5124 __ Bind(slow_path->GetExitLabel());
5125}
5126
David Brazdilcb1c0552015-08-04 16:22:25 +01005127static Address GetExceptionTlsAddress() {
5128 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5129}
5130
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005131void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5132 LocationSummary* locations =
5133 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5134 locations->SetOut(Location::RequiresRegister());
5135}
5136
5137void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005138 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5139}
5140
5141void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5142 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5143}
5144
5145void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5146 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005147}
5148
5149void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5150 LocationSummary* locations =
5151 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5152 InvokeRuntimeCallingConvention calling_convention;
5153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5154}
5155
5156void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005157 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5158 instruction,
5159 instruction->GetDexPc(),
5160 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005161}
5162
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005163void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005164 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5165 switch (instruction->GetTypeCheckKind()) {
5166 case TypeCheckKind::kExactCheck:
5167 case TypeCheckKind::kAbstractClassCheck:
5168 case TypeCheckKind::kClassHierarchyCheck:
5169 case TypeCheckKind::kArrayObjectCheck:
5170 call_kind = LocationSummary::kNoCall;
5171 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005172 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005173 case TypeCheckKind::kInterfaceCheck:
5174 call_kind = LocationSummary::kCall;
5175 break;
5176 case TypeCheckKind::kArrayCheck:
5177 call_kind = LocationSummary::kCallOnSlowPath;
5178 break;
5179 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005180 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005181 if (call_kind != LocationSummary::kCall) {
5182 locations->SetInAt(0, Location::RequiresRegister());
5183 locations->SetInAt(1, Location::Any());
5184 // Note that TypeCheckSlowPathX86 uses this register too.
5185 locations->SetOut(Location::RequiresRegister());
5186 } else {
5187 InvokeRuntimeCallingConvention calling_convention;
5188 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5189 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5190 locations->SetOut(Location::RegisterLocation(EAX));
5191 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005192}
5193
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005194void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005195 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005196 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005197 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005198 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005199 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005200 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5201 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5202 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005203 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005204 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005205
5206 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005207 // Avoid null check if we know obj is not null.
5208 if (instruction->MustDoNullCheck()) {
5209 __ testl(obj, obj);
5210 __ j(kEqual, &zero);
5211 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005212
Calin Juravle98893e12015-10-02 21:05:03 +01005213 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005214 // This is safe, as the register is caller-save, and the object must be in another
5215 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01005216 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
5217 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005218 ? obj
5219 : out;
5220 __ movl(target, Address(obj, class_offset));
5221 __ MaybeUnpoisonHeapReference(target);
5222
5223 switch (instruction->GetTypeCheckKind()) {
5224 case TypeCheckKind::kExactCheck: {
5225 if (cls.IsRegister()) {
5226 __ cmpl(out, cls.AsRegister<Register>());
5227 } else {
5228 DCHECK(cls.IsStackSlot()) << cls;
5229 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5230 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005232 // Classes must be equal for the instanceof to succeed.
5233 __ j(kNotEqual, &zero);
5234 __ movl(out, Immediate(1));
5235 __ jmp(&done);
5236 break;
5237 }
5238 case TypeCheckKind::kAbstractClassCheck: {
5239 // If the class is abstract, we eagerly fetch the super class of the
5240 // object to avoid doing a comparison we know will fail.
5241 NearLabel loop;
5242 __ Bind(&loop);
5243 __ movl(out, Address(out, super_offset));
5244 __ MaybeUnpoisonHeapReference(out);
5245 __ testl(out, out);
5246 // If `out` is null, we use it for the result, and jump to `done`.
5247 __ j(kEqual, &done);
5248 if (cls.IsRegister()) {
5249 __ cmpl(out, cls.AsRegister<Register>());
5250 } else {
5251 DCHECK(cls.IsStackSlot()) << cls;
5252 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5253 }
5254 __ j(kNotEqual, &loop);
5255 __ movl(out, Immediate(1));
5256 if (zero.IsLinked()) {
5257 __ jmp(&done);
5258 }
5259 break;
5260 }
5261 case TypeCheckKind::kClassHierarchyCheck: {
5262 // Walk over the class hierarchy to find a match.
5263 NearLabel loop, success;
5264 __ Bind(&loop);
5265 if (cls.IsRegister()) {
5266 __ cmpl(out, cls.AsRegister<Register>());
5267 } else {
5268 DCHECK(cls.IsStackSlot()) << cls;
5269 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5270 }
5271 __ j(kEqual, &success);
5272 __ movl(out, Address(out, super_offset));
5273 __ MaybeUnpoisonHeapReference(out);
5274 __ testl(out, out);
5275 __ j(kNotEqual, &loop);
5276 // If `out` is null, we use it for the result, and jump to `done`.
5277 __ jmp(&done);
5278 __ Bind(&success);
5279 __ movl(out, Immediate(1));
5280 if (zero.IsLinked()) {
5281 __ jmp(&done);
5282 }
5283 break;
5284 }
5285 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005286 // Do an exact check.
5287 NearLabel exact_check;
5288 if (cls.IsRegister()) {
5289 __ cmpl(out, cls.AsRegister<Register>());
5290 } else {
5291 DCHECK(cls.IsStackSlot()) << cls;
5292 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5293 }
5294 __ j(kEqual, &exact_check);
5295 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005296 __ movl(out, Address(out, component_offset));
5297 __ MaybeUnpoisonHeapReference(out);
5298 __ testl(out, out);
5299 // If `out` is null, we use it for the result, and jump to `done`.
5300 __ j(kEqual, &done);
5301 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5302 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005303 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005304 __ movl(out, Immediate(1));
5305 __ jmp(&done);
5306 break;
5307 }
5308 case TypeCheckKind::kArrayCheck: {
5309 if (cls.IsRegister()) {
5310 __ cmpl(out, cls.AsRegister<Register>());
5311 } else {
5312 DCHECK(cls.IsStackSlot()) << cls;
5313 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5314 }
5315 DCHECK(locations->OnlyCallsOnSlowPath());
5316 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5317 instruction, /* is_fatal */ false);
5318 codegen_->AddSlowPath(slow_path);
5319 __ j(kNotEqual, slow_path->GetEntryLabel());
5320 __ movl(out, Immediate(1));
5321 if (zero.IsLinked()) {
5322 __ jmp(&done);
5323 }
5324 break;
5325 }
Calin Juravle98893e12015-10-02 21:05:03 +01005326 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005327 case TypeCheckKind::kInterfaceCheck:
5328 default: {
5329 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5330 instruction,
5331 instruction->GetDexPc(),
5332 nullptr);
5333 if (zero.IsLinked()) {
5334 __ jmp(&done);
5335 }
5336 break;
5337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005338 }
5339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005340 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005341 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005342 __ xorl(out, out);
5343 }
5344
5345 if (done.IsLinked()) {
5346 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005347 }
5348
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005349 if (slow_path != nullptr) {
5350 __ Bind(slow_path->GetExitLabel());
5351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005352}
5353
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005354void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5356 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5357
5358 switch (instruction->GetTypeCheckKind()) {
5359 case TypeCheckKind::kExactCheck:
5360 case TypeCheckKind::kAbstractClassCheck:
5361 case TypeCheckKind::kClassHierarchyCheck:
5362 case TypeCheckKind::kArrayObjectCheck:
5363 call_kind = throws_into_catch
5364 ? LocationSummary::kCallOnSlowPath
5365 : LocationSummary::kNoCall;
5366 break;
5367 case TypeCheckKind::kInterfaceCheck:
Calin Juravle98893e12015-10-02 21:05:03 +01005368 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005369 call_kind = LocationSummary::kCall;
5370 break;
5371 case TypeCheckKind::kArrayCheck:
5372 call_kind = LocationSummary::kCallOnSlowPath;
5373 break;
5374 }
5375
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005377 instruction, call_kind);
5378 if (call_kind != LocationSummary::kCall) {
5379 locations->SetInAt(0, Location::RequiresRegister());
5380 locations->SetInAt(1, Location::Any());
5381 // Note that TypeCheckSlowPathX86 uses this register too.
5382 locations->AddTemp(Location::RequiresRegister());
5383 } else {
5384 InvokeRuntimeCallingConvention calling_convention;
5385 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5387 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005388}
5389
5390void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5391 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005392 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005393 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005394 Register temp = locations->WillCall()
5395 ? kNoRegister
5396 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005397
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5399 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5400 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5401 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5402 SlowPathCode* slow_path = nullptr;
5403
5404 if (!locations->WillCall()) {
5405 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5406 instruction, !locations->CanCall());
5407 codegen_->AddSlowPath(slow_path);
5408 }
5409
5410 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005411 // Avoid null check if we know obj is not null.
5412 if (instruction->MustDoNullCheck()) {
5413 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005415 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416
5417 if (locations->WillCall()) {
5418 __ movl(obj, Address(obj, class_offset));
5419 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005420 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005421 __ movl(temp, Address(obj, class_offset));
5422 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005423 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424
5425 switch (instruction->GetTypeCheckKind()) {
5426 case TypeCheckKind::kExactCheck:
5427 case TypeCheckKind::kArrayCheck: {
5428 if (cls.IsRegister()) {
5429 __ cmpl(temp, cls.AsRegister<Register>());
5430 } else {
5431 DCHECK(cls.IsStackSlot()) << cls;
5432 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5433 }
5434 // Jump to slow path for throwing the exception or doing a
5435 // more involved array check.
5436 __ j(kNotEqual, slow_path->GetEntryLabel());
5437 break;
5438 }
5439 case TypeCheckKind::kAbstractClassCheck: {
5440 // If the class is abstract, we eagerly fetch the super class of the
5441 // object to avoid doing a comparison we know will fail.
5442 NearLabel loop, success;
5443 __ Bind(&loop);
5444 __ movl(temp, Address(temp, super_offset));
5445 __ MaybeUnpoisonHeapReference(temp);
5446 __ testl(temp, temp);
5447 // Jump to the slow path to throw the exception.
5448 __ j(kEqual, slow_path->GetEntryLabel());
5449 if (cls.IsRegister()) {
5450 __ cmpl(temp, cls.AsRegister<Register>());
5451 } else {
5452 DCHECK(cls.IsStackSlot()) << cls;
5453 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5454 }
5455 __ j(kNotEqual, &loop);
5456 break;
5457 }
5458 case TypeCheckKind::kClassHierarchyCheck: {
5459 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005460 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005461 __ Bind(&loop);
5462 if (cls.IsRegister()) {
5463 __ cmpl(temp, cls.AsRegister<Register>());
5464 } else {
5465 DCHECK(cls.IsStackSlot()) << cls;
5466 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5467 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005468 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005469 __ movl(temp, Address(temp, super_offset));
5470 __ MaybeUnpoisonHeapReference(temp);
5471 __ testl(temp, temp);
5472 __ j(kNotEqual, &loop);
5473 // Jump to the slow path to throw the exception.
5474 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005475 break;
5476 }
5477 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005478 // Do an exact check.
5479 if (cls.IsRegister()) {
5480 __ cmpl(temp, cls.AsRegister<Register>());
5481 } else {
5482 DCHECK(cls.IsStackSlot()) << cls;
5483 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5484 }
5485 __ j(kEqual, &done);
5486 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005487 __ movl(temp, Address(temp, component_offset));
5488 __ MaybeUnpoisonHeapReference(temp);
5489 __ testl(temp, temp);
5490 __ j(kEqual, slow_path->GetEntryLabel());
5491 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5492 __ j(kNotEqual, slow_path->GetEntryLabel());
5493 break;
5494 }
Calin Juravle98893e12015-10-02 21:05:03 +01005495 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005496 case TypeCheckKind::kInterfaceCheck:
5497 default:
5498 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5499 instruction,
5500 instruction->GetDexPc(),
5501 nullptr);
5502 break;
5503 }
5504 __ Bind(&done);
5505
5506 if (slow_path != nullptr) {
5507 __ Bind(slow_path->GetExitLabel());
5508 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005509}
5510
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005511void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5512 LocationSummary* locations =
5513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5514 InvokeRuntimeCallingConvention calling_convention;
5515 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5516}
5517
5518void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005519 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5520 : QUICK_ENTRY_POINT(pUnlockObject),
5521 instruction,
5522 instruction->GetDexPc(),
5523 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005524}
5525
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005526void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5527void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5528void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5529
5530void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5531 LocationSummary* locations =
5532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5533 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5534 || instruction->GetResultType() == Primitive::kPrimLong);
5535 locations->SetInAt(0, Location::RequiresRegister());
5536 locations->SetInAt(1, Location::Any());
5537 locations->SetOut(Location::SameAsFirstInput());
5538}
5539
5540void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5541 HandleBitwiseOperation(instruction);
5542}
5543
5544void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5545 HandleBitwiseOperation(instruction);
5546}
5547
5548void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5549 HandleBitwiseOperation(instruction);
5550}
5551
5552void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5553 LocationSummary* locations = instruction->GetLocations();
5554 Location first = locations->InAt(0);
5555 Location second = locations->InAt(1);
5556 DCHECK(first.Equals(locations->Out()));
5557
5558 if (instruction->GetResultType() == Primitive::kPrimInt) {
5559 if (second.IsRegister()) {
5560 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005561 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005562 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005563 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005564 } else {
5565 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005566 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005567 }
5568 } else if (second.IsConstant()) {
5569 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005570 __ andl(first.AsRegister<Register>(),
5571 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005572 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005573 __ orl(first.AsRegister<Register>(),
5574 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005575 } else {
5576 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005577 __ xorl(first.AsRegister<Register>(),
5578 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005579 }
5580 } else {
5581 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005582 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005583 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005584 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005585 } else {
5586 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005587 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005588 }
5589 }
5590 } else {
5591 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5592 if (second.IsRegisterPair()) {
5593 if (instruction->IsAnd()) {
5594 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5595 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5596 } else if (instruction->IsOr()) {
5597 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5598 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5599 } else {
5600 DCHECK(instruction->IsXor());
5601 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5602 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5603 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005604 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005605 if (instruction->IsAnd()) {
5606 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5607 __ andl(first.AsRegisterPairHigh<Register>(),
5608 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5609 } else if (instruction->IsOr()) {
5610 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5611 __ orl(first.AsRegisterPairHigh<Register>(),
5612 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5613 } else {
5614 DCHECK(instruction->IsXor());
5615 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5616 __ xorl(first.AsRegisterPairHigh<Register>(),
5617 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5618 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005619 } else {
5620 DCHECK(second.IsConstant()) << second;
5621 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005622 int32_t low_value = Low32Bits(value);
5623 int32_t high_value = High32Bits(value);
5624 Immediate low(low_value);
5625 Immediate high(high_value);
5626 Register first_low = first.AsRegisterPairLow<Register>();
5627 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005628 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005629 if (low_value == 0) {
5630 __ xorl(first_low, first_low);
5631 } else if (low_value != -1) {
5632 __ andl(first_low, low);
5633 }
5634 if (high_value == 0) {
5635 __ xorl(first_high, first_high);
5636 } else if (high_value != -1) {
5637 __ andl(first_high, high);
5638 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005639 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005640 if (low_value != 0) {
5641 __ orl(first_low, low);
5642 }
5643 if (high_value != 0) {
5644 __ orl(first_high, high);
5645 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005646 } else {
5647 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005648 if (low_value != 0) {
5649 __ xorl(first_low, low);
5650 }
5651 if (high_value != 0) {
5652 __ xorl(first_high, high);
5653 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005654 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005655 }
5656 }
5657}
5658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005659void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005660 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005661 LOG(FATAL) << "Unreachable";
5662}
5663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005664void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005665 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005666 LOG(FATAL) << "Unreachable";
5667}
5668
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005669void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5670 DCHECK(codegen_->IsBaseline());
5671 LocationSummary* locations =
5672 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5673 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5674}
5675
5676void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5677 DCHECK(codegen_->IsBaseline());
5678 // Will be generated at use site.
5679}
5680
Mark Mendellfe57faa2015-09-18 09:26:15 -04005681// Simple implementation of packed switch - generate cascaded compare/jumps.
5682void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5683 LocationSummary* locations =
5684 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5685 locations->SetInAt(0, Location::RequiresRegister());
5686}
5687
5688void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5689 int32_t lower_bound = switch_instr->GetStartValue();
5690 int32_t num_entries = switch_instr->GetNumEntries();
5691 LocationSummary* locations = switch_instr->GetLocations();
5692 Register value_reg = locations->InAt(0).AsRegister<Register>();
5693 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5694
5695 // Create a series of compare/jumps.
5696 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5697 for (int i = 0; i < num_entries; i++) {
5698 int32_t case_value = lower_bound + i;
5699 if (case_value == 0) {
5700 __ testl(value_reg, value_reg);
5701 } else {
5702 __ cmpl(value_reg, Immediate(case_value));
5703 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005704 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005705 }
5706
5707 // And the default for any other value.
5708 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5709 __ jmp(codegen_->GetLabelOf(default_block));
5710 }
5711}
5712
Mark Mendell805b3b52015-09-18 14:10:29 -04005713void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5714 LocationSummary* locations =
5715 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5716 locations->SetInAt(0, Location::RequiresRegister());
5717
5718 // Constant area pointer.
5719 locations->SetInAt(1, Location::RequiresRegister());
5720
5721 // And the temporary we need.
5722 locations->AddTemp(Location::RequiresRegister());
5723}
5724
5725void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5726 int32_t lower_bound = switch_instr->GetStartValue();
5727 int32_t num_entries = switch_instr->GetNumEntries();
5728 LocationSummary* locations = switch_instr->GetLocations();
5729 Register value_reg = locations->InAt(0).AsRegister<Register>();
5730 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5731
5732 // Optimizing has a jump area.
5733 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5734 Register constant_area = locations->InAt(1).AsRegister<Register>();
5735
5736 // Remove the bias, if needed.
5737 if (lower_bound != 0) {
5738 __ leal(temp_reg, Address(value_reg, -lower_bound));
5739 value_reg = temp_reg;
5740 }
5741
5742 // Is the value in range?
5743 DCHECK_GE(num_entries, 1);
5744 __ cmpl(value_reg, Immediate(num_entries - 1));
5745 __ j(kAbove, codegen_->GetLabelOf(default_block));
5746
5747 // We are in the range of the table.
5748 // Load (target-constant_area) from the jump table, indexing by the value.
5749 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
5750
5751 // Compute the actual target address by adding in constant_area.
5752 __ addl(temp_reg, constant_area);
5753
5754 // And jump.
5755 __ jmp(temp_reg);
5756}
5757
Mark Mendell0616ae02015-04-17 12:49:27 -04005758void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5759 HX86ComputeBaseMethodAddress* insn) {
5760 LocationSummary* locations =
5761 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5762 locations->SetOut(Location::RequiresRegister());
5763}
5764
5765void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5766 HX86ComputeBaseMethodAddress* insn) {
5767 LocationSummary* locations = insn->GetLocations();
5768 Register reg = locations->Out().AsRegister<Register>();
5769
5770 // Generate call to next instruction.
5771 Label next_instruction;
5772 __ call(&next_instruction);
5773 __ Bind(&next_instruction);
5774
5775 // Remember this offset for later use with constant area.
5776 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5777
5778 // Grab the return address off the stack.
5779 __ popl(reg);
5780}
5781
5782void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5783 HX86LoadFromConstantTable* insn) {
5784 LocationSummary* locations =
5785 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5786
5787 locations->SetInAt(0, Location::RequiresRegister());
5788 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5789
5790 // If we don't need to be materialized, we only need the inputs to be set.
5791 if (!insn->NeedsMaterialization()) {
5792 return;
5793 }
5794
5795 switch (insn->GetType()) {
5796 case Primitive::kPrimFloat:
5797 case Primitive::kPrimDouble:
5798 locations->SetOut(Location::RequiresFpuRegister());
5799 break;
5800
5801 case Primitive::kPrimInt:
5802 locations->SetOut(Location::RequiresRegister());
5803 break;
5804
5805 default:
5806 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5807 }
5808}
5809
5810void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5811 if (!insn->NeedsMaterialization()) {
5812 return;
5813 }
5814
5815 LocationSummary* locations = insn->GetLocations();
5816 Location out = locations->Out();
5817 Register const_area = locations->InAt(0).AsRegister<Register>();
5818 HConstant *value = insn->GetConstant();
5819
5820 switch (insn->GetType()) {
5821 case Primitive::kPrimFloat:
5822 __ movss(out.AsFpuRegister<XmmRegister>(),
5823 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5824 break;
5825
5826 case Primitive::kPrimDouble:
5827 __ movsd(out.AsFpuRegister<XmmRegister>(),
5828 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5829 break;
5830
5831 case Primitive::kPrimInt:
5832 __ movl(out.AsRegister<Register>(),
5833 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5834 break;
5835
5836 default:
5837 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5838 }
5839}
5840
Mark Mendell0616ae02015-04-17 12:49:27 -04005841/**
5842 * Class to handle late fixup of offsets into constant area.
5843 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005844class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04005845 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04005846 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
5847 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5848
5849 protected:
5850 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5851
5852 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005853
5854 private:
5855 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5856 // Patch the correct offset for the instruction. The place to patch is the
5857 // last 4 bytes of the instruction.
5858 // The value to patch is the distance from the offset in the constant area
5859 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04005860 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5861 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04005862
5863 // Patch in the right value.
5864 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5865 }
5866
Mark Mendell0616ae02015-04-17 12:49:27 -04005867 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04005868 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005869};
5870
Mark Mendell805b3b52015-09-18 14:10:29 -04005871/**
5872 * Class to handle late fixup of offsets to a jump table that will be created in the
5873 * constant area.
5874 */
5875class JumpTableRIPFixup : public RIPFixup {
5876 public:
5877 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
5878 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
5879
5880 void CreateJumpTable() {
5881 X86Assembler* assembler = codegen_->GetAssembler();
5882
5883 // Ensure that the reference to the jump table has the correct offset.
5884 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5885 SetOffset(offset_in_constant_table);
5886
5887 // The label values in the jump table are computed relative to the
5888 // instruction addressing the constant area.
5889 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
5890
5891 // Populate the jump table with the correct values for the jump table.
5892 int32_t num_entries = switch_instr_->GetNumEntries();
5893 HBasicBlock* block = switch_instr_->GetBlock();
5894 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5895 // The value that we want is the target offset - the position of the table.
5896 for (int32_t i = 0; i < num_entries; i++) {
5897 HBasicBlock* b = successors[i];
5898 Label* l = codegen_->GetLabelOf(b);
5899 DCHECK(l->IsBound());
5900 int32_t offset_to_block = l->Position() - relative_offset;
5901 assembler->AppendInt32(offset_to_block);
5902 }
5903 }
5904
5905 private:
5906 const HX86PackedSwitch* switch_instr_;
5907};
5908
5909void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5910 // Generate the constant area if needed.
5911 X86Assembler* assembler = GetAssembler();
5912 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5913 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5914 // byte values.
5915 assembler->Align(4, 0);
5916 constant_area_start_ = assembler->CodeSize();
5917
5918 // Populate any jump tables.
5919 for (auto jump_table : fixups_to_jump_tables_) {
5920 jump_table->CreateJumpTable();
5921 }
5922
5923 // And now add the constant area to the generated code.
5924 assembler->AddConstantArea();
5925 }
5926
5927 // And finish up.
5928 CodeGenerator::Finalize(allocator);
5929}
5930
Mark Mendell0616ae02015-04-17 12:49:27 -04005931Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5932 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5933 return Address(reg, kDummy32BitOffset, fixup);
5934}
5935
5936Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5937 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5938 return Address(reg, kDummy32BitOffset, fixup);
5939}
5940
5941Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5942 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5943 return Address(reg, kDummy32BitOffset, fixup);
5944}
5945
5946Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5947 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5948 return Address(reg, kDummy32BitOffset, fixup);
5949}
5950
Mark Mendell805b3b52015-09-18 14:10:29 -04005951Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
5952 Register reg,
5953 Register value) {
5954 // Create a fixup to be used to create and address the jump table.
5955 JumpTableRIPFixup* table_fixup =
5956 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5957
5958 // We have to populate the jump tables.
5959 fixups_to_jump_tables_.push_back(table_fixup);
5960
5961 // We want a scaled address, as we are extracting the correct offset from the table.
5962 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
5963}
5964
Andreas Gampe85b62f22015-09-09 13:15:38 -07005965// TODO: target as memory.
5966void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5967 if (!target.IsValid()) {
5968 DCHECK(type == Primitive::kPrimVoid);
5969 return;
5970 }
5971
5972 DCHECK_NE(type, Primitive::kPrimVoid);
5973
5974 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
5975 if (target.Equals(return_loc)) {
5976 return;
5977 }
5978
5979 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5980 // with the else branch.
5981 if (type == Primitive::kPrimLong) {
5982 HParallelMove parallel_move(GetGraph()->GetArena());
5983 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
5984 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
5985 GetMoveResolver()->EmitNativeCode(&parallel_move);
5986 } else {
5987 // Let the parallel move resolver take care of all of this.
5988 HParallelMove parallel_move(GetGraph()->GetArena());
5989 parallel_move.AddMove(return_loc, target, type, nullptr);
5990 GetMoveResolver()->EmitNativeCode(&parallel_move);
5991 }
5992}
5993
Roland Levillain4d027112015-07-01 15:41:14 +01005994#undef __
5995
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005996} // namespace x86
5997} // namespace art