blob: db4a8548f4b2d354a07327e792cba6b88892d343 [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
Roland Levillain4fa13f62015-07-06 18:11:54 +0100431inline Condition X86SignedCondition(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;
Dave Allison20dfc792014-06-16 20:44:29 -0700439 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100440 LOG(FATAL) << "Unreachable";
441 UNREACHABLE();
442}
443
444inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
445 switch (cond) {
446 case kCondEQ: return kEqual;
447 case kCondNE: return kNotEqual;
448 case kCondLT: return kBelow;
449 case kCondLE: return kBelowEqual;
450 case kCondGT: return kAbove;
451 case kCondGE: return kAboveEqual;
452 }
453 LOG(FATAL) << "Unreachable";
454 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700455}
456
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100458 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100459}
460
461void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100462 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100463}
464
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100465size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
466 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
467 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100468}
469
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100470size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
471 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
472 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100473}
474
Mark Mendell7c8d0092015-01-26 11:21:33 -0500475size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
476 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
477 return GetFloatingPointSpillSlotSize();
478}
479
480size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
481 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
482 return GetFloatingPointSpillSlotSize();
483}
484
Calin Juravle175dc732015-08-25 15:42:32 +0100485void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
486 HInstruction* instruction,
487 uint32_t dex_pc,
488 SlowPathCode* slow_path) {
489 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
490 instruction,
491 dex_pc,
492 slow_path);
493}
494
495void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100496 HInstruction* instruction,
497 uint32_t dex_pc,
498 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100499 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100500 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100501 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100502}
503
Mark Mendellfb8d2792015-03-31 22:16:59 -0400504CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
505 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100506 const CompilerOptions& compiler_options,
507 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500508 : CodeGenerator(graph,
509 kNumberOfCpuRegisters,
510 kNumberOfXmmRegisters,
511 kNumberOfRegisterPairs,
512 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
513 arraysize(kCoreCalleeSaves))
514 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100515 0,
516 compiler_options,
517 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100518 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100519 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100520 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400521 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000522 isa_features_(isa_features),
523 method_patches_(graph->GetArena()->Adapter()),
524 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000525 // Use a fake return address register to mimic Quick.
526 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100527}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100528
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100529Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 switch (type) {
531 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100532 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533 X86ManagedRegister pair =
534 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100535 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
536 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100537 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
538 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100539 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100541 }
542
543 case Primitive::kPrimByte:
544 case Primitive::kPrimBoolean:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimInt:
548 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100549 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100550 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100551 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100552 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
553 X86ManagedRegister current =
554 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
555 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100556 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100557 }
558 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100559 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100560 }
561
562 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100564 return Location::FpuRegisterLocation(
565 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100567
568 case Primitive::kPrimVoid:
569 LOG(FATAL) << "Unreachable type " << type;
570 }
571
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100573}
574
Mark Mendell5f874182015-03-04 15:42:45 -0500575void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100576 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100577 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100578
579 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100580 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100581
Mark Mendell5f874182015-03-04 15:42:45 -0500582 if (is_baseline) {
583 blocked_core_registers_[EBP] = true;
584 blocked_core_registers_[ESI] = true;
585 blocked_core_registers_[EDI] = true;
586 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100587
588 UpdateBlockedPairRegisters();
589}
590
591void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
592 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
593 X86ManagedRegister current =
594 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
595 if (blocked_core_registers_[current.AsRegisterPairLow()]
596 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
597 blocked_register_pairs_[i] = true;
598 }
599 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100600}
601
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100602InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
603 : HGraphVisitor(graph),
604 assembler_(codegen->GetAssembler()),
605 codegen_(codegen) {}
606
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100607static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100608 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100609}
610
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000611void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100612 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000613 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000614 bool skip_overflow_check =
615 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000616 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000617
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000618 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100619 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100620 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100621 }
622
Mark Mendell5f874182015-03-04 15:42:45 -0500623 if (HasEmptyFrame()) {
624 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000625 }
Mark Mendell5f874182015-03-04 15:42:45 -0500626
627 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
628 Register reg = kCoreCalleeSaves[i];
629 if (allocated_registers_.ContainsCoreRegister(reg)) {
630 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100631 __ cfi().AdjustCFAOffset(kX86WordSize);
632 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500633 }
634 }
635
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100636 int adjust = GetFrameSize() - FrameEntrySpillSize();
637 __ subl(ESP, Immediate(adjust));
638 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100639 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640}
641
642void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100643 __ cfi().RememberState();
644 if (!HasEmptyFrame()) {
645 int adjust = GetFrameSize() - FrameEntrySpillSize();
646 __ addl(ESP, Immediate(adjust));
647 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500648
David Srbeckyc34dc932015-04-12 09:27:43 +0100649 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
650 Register reg = kCoreCalleeSaves[i];
651 if (allocated_registers_.ContainsCoreRegister(reg)) {
652 __ popl(reg);
653 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
654 __ cfi().Restore(DWARFReg(reg));
655 }
Mark Mendell5f874182015-03-04 15:42:45 -0500656 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000657 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100658 __ ret();
659 __ cfi().RestoreState();
660 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000661}
662
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100663void CodeGeneratorX86::Bind(HBasicBlock* block) {
664 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000665}
666
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100667Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
668 switch (load->GetType()) {
669 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100671 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100672
673 case Primitive::kPrimInt:
674 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100677
678 case Primitive::kPrimBoolean:
679 case Primitive::kPrimByte:
680 case Primitive::kPrimChar:
681 case Primitive::kPrimShort:
682 case Primitive::kPrimVoid:
683 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700684 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100685 }
686
687 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700688 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100689}
690
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100691Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
692 switch (type) {
693 case Primitive::kPrimBoolean:
694 case Primitive::kPrimByte:
695 case Primitive::kPrimChar:
696 case Primitive::kPrimShort:
697 case Primitive::kPrimInt:
698 case Primitive::kPrimNot:
699 return Location::RegisterLocation(EAX);
700
701 case Primitive::kPrimLong:
702 return Location::RegisterPairLocation(EAX, EDX);
703
704 case Primitive::kPrimVoid:
705 return Location::NoLocation();
706
707 case Primitive::kPrimDouble:
708 case Primitive::kPrimFloat:
709 return Location::FpuRegisterLocation(XMM0);
710 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100711
712 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100713}
714
715Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
716 return Location::RegisterLocation(kMethodRegisterArgument);
717}
718
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100719Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100720 switch (type) {
721 case Primitive::kPrimBoolean:
722 case Primitive::kPrimByte:
723 case Primitive::kPrimChar:
724 case Primitive::kPrimShort:
725 case Primitive::kPrimInt:
726 case Primitive::kPrimNot: {
727 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000728 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100729 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100730 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100731 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000732 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100733 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100734 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100735
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000736 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100737 uint32_t index = gp_index_;
738 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000739 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100740 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100741 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
742 calling_convention.GetRegisterPairAt(index));
743 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100744 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000745 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
746 }
747 }
748
749 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100750 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000751 stack_index_++;
752 if (index < calling_convention.GetNumberOfFpuRegisters()) {
753 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
754 } else {
755 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
756 }
757 }
758
759 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000761 stack_index_ += 2;
762 if (index < calling_convention.GetNumberOfFpuRegisters()) {
763 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
764 } else {
765 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100766 }
767 }
768
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100769 case Primitive::kPrimVoid:
770 LOG(FATAL) << "Unexpected parameter type " << type;
771 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100772 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100773 return Location();
774}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100775
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100776void CodeGeneratorX86::Move32(Location destination, Location source) {
777 if (source.Equals(destination)) {
778 return;
779 }
780 if (destination.IsRegister()) {
781 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000782 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000784 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100785 } else {
786 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000787 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 } else if (destination.IsFpuRegister()) {
790 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000791 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100792 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000793 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100794 } else {
795 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000796 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100797 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000799 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000801 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500804 } else if (source.IsConstant()) {
805 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000806 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500807 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100808 } else {
809 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100810 __ pushl(Address(ESP, source.GetStackIndex()));
811 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 }
813 }
814}
815
816void CodeGeneratorX86::Move64(Location destination, Location source) {
817 if (source.Equals(destination)) {
818 return;
819 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100820 if (destination.IsRegisterPair()) {
821 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000822 EmitParallelMoves(
823 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
824 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100825 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000826 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100827 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
828 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100829 } else if (source.IsFpuRegister()) {
830 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000832 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100833 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100834 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
835 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100836 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
837 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100838 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500839 if (source.IsFpuRegister()) {
840 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
841 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100843 } else {
844 LOG(FATAL) << "Unimplemented";
845 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000847 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100848 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000849 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100852 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000854 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000855 } else if (source.IsConstant()) {
856 HConstant* constant = source.GetConstant();
857 int64_t value;
858 if (constant->IsLongConstant()) {
859 value = constant->AsLongConstant()->GetValue();
860 } else {
861 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000862 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000863 }
864 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
865 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100866 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000867 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000868 EmitParallelMoves(
869 Location::StackSlot(source.GetStackIndex()),
870 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100871 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000872 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100873 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
874 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 }
876 }
877}
878
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100879void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000880 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100881 if (instruction->IsCurrentMethod()) {
882 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
883 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000884 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100885 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000886 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000887 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
888 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000889 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000890 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000891 } else if (location.IsStackSlot()) {
892 __ movl(Address(ESP, location.GetStackIndex()), imm);
893 } else {
894 DCHECK(location.IsConstant());
895 DCHECK_EQ(location.GetConstant(), const_to_move);
896 }
897 } else if (const_to_move->IsLongConstant()) {
898 int64_t value = const_to_move->AsLongConstant()->GetValue();
899 if (location.IsRegisterPair()) {
900 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
901 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
902 } else if (location.IsDoubleStackSlot()) {
903 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000904 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
905 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000906 } else {
907 DCHECK(location.IsConstant());
908 DCHECK_EQ(location.GetConstant(), instruction);
909 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100910 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000911 } else if (instruction->IsTemporary()) {
912 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000913 if (temp_location.IsStackSlot()) {
914 Move32(location, temp_location);
915 } else {
916 DCHECK(temp_location.IsDoubleStackSlot());
917 Move64(location, temp_location);
918 }
Roland Levillain476df552014-10-09 17:51:36 +0100919 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100921 switch (instruction->GetType()) {
922 case Primitive::kPrimBoolean:
923 case Primitive::kPrimByte:
924 case Primitive::kPrimChar:
925 case Primitive::kPrimShort:
926 case Primitive::kPrimInt:
927 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 case Primitive::kPrimFloat:
929 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930 break;
931
932 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100933 case Primitive::kPrimDouble:
934 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100935 break;
936
937 default:
938 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
939 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000940 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100941 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 switch (instruction->GetType()) {
943 case Primitive::kPrimBoolean:
944 case Primitive::kPrimByte:
945 case Primitive::kPrimChar:
946 case Primitive::kPrimShort:
947 case Primitive::kPrimInt:
948 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000950 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 break;
952
953 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100954 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000955 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 break;
957
958 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100959 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000961 }
962}
963
Calin Juravle175dc732015-08-25 15:42:32 +0100964void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
965 DCHECK(location.IsRegister());
966 __ movl(location.AsRegister<Register>(), Immediate(value));
967}
968
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100970 DCHECK(!successor->IsExitBlock());
971
972 HBasicBlock* block = got->GetBlock();
973 HInstruction* previous = got->GetPrevious();
974
975 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000976 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100977 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
978 return;
979 }
980
981 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
982 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
983 }
984 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000986 }
987}
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989void LocationsBuilderX86::VisitGoto(HGoto* got) {
990 got->SetLocations(nullptr);
991}
992
993void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
994 HandleGoto(got, got->GetSuccessor());
995}
996
997void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
998 try_boundary->SetLocations(nullptr);
999}
1000
1001void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1002 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1003 if (!successor->IsExitBlock()) {
1004 HandleGoto(try_boundary, successor);
1005 }
1006}
1007
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001008void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001009 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001010}
1011
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001012void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001014}
1015
Mark Mendellc4701932015-04-10 13:18:51 -04001016void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1017 Label* true_label,
1018 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001019 if (cond->IsFPConditionTrueIfNaN()) {
1020 __ j(kUnordered, true_label);
1021 } else if (cond->IsFPConditionFalseIfNaN()) {
1022 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001023 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001024 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001025}
1026
1027void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1028 Label* true_label,
1029 Label* false_label) {
1030 LocationSummary* locations = cond->GetLocations();
1031 Location left = locations->InAt(0);
1032 Location right = locations->InAt(1);
1033 IfCondition if_cond = cond->GetCondition();
1034
Mark Mendellc4701932015-04-10 13:18:51 -04001035 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001036 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001037 IfCondition true_high_cond = if_cond;
1038 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001039 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -04001040
1041 // Set the conditions for the test, remembering that == needs to be
1042 // decided using the low words.
1043 switch (if_cond) {
1044 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001045 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001046 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001047 break;
1048 case kCondLT:
1049 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001050 break;
1051 case kCondLE:
1052 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001053 break;
1054 case kCondGT:
1055 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001056 break;
1057 case kCondGE:
1058 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001059 break;
1060 }
1061
1062 if (right.IsConstant()) {
1063 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001064 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001065 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001066
1067 if (val_high == 0) {
1068 __ testl(left_high, left_high);
1069 } else {
1070 __ cmpl(left_high, Immediate(val_high));
1071 }
1072 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001073 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001074 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001075 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001076 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001077 __ j(X86SignedCondition(true_high_cond), true_label);
1078 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001079 }
1080 // Must be equal high, so compare the lows.
1081 if (val_low == 0) {
1082 __ testl(left_low, left_low);
1083 } else {
1084 __ cmpl(left_low, Immediate(val_low));
1085 }
1086 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001087 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001089
1090 __ cmpl(left_high, right_high);
1091 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001092 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001093 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001094 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001095 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001096 __ j(X86SignedCondition(true_high_cond), true_label);
1097 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001098 }
1099 // Must be equal high, so compare the lows.
1100 __ cmpl(left_low, right_low);
1101 }
1102 // The last comparison might be unsigned.
1103 __ j(final_condition, true_label);
1104}
1105
1106void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1107 HCondition* condition,
1108 Label* true_target,
1109 Label* false_target,
1110 Label* always_true_target) {
1111 LocationSummary* locations = condition->GetLocations();
1112 Location left = locations->InAt(0);
1113 Location right = locations->InAt(1);
1114
1115 // We don't want true_target as a nullptr.
1116 if (true_target == nullptr) {
1117 true_target = always_true_target;
1118 }
1119 bool falls_through = (false_target == nullptr);
1120
1121 // FP compares don't like null false_targets.
1122 if (false_target == nullptr) {
1123 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1124 }
1125
1126 Primitive::Type type = condition->InputAt(0)->GetType();
1127 switch (type) {
1128 case Primitive::kPrimLong:
1129 GenerateLongComparesAndJumps(condition, true_target, false_target);
1130 break;
1131 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001132 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1133 GenerateFPJumps(condition, true_target, false_target);
1134 break;
1135 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001136 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1137 GenerateFPJumps(condition, true_target, false_target);
1138 break;
1139 default:
1140 LOG(FATAL) << "Unexpected compare type " << type;
1141 }
1142
1143 if (!falls_through) {
1144 __ jmp(false_target);
1145 }
1146}
1147
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001148void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1149 Label* true_target,
1150 Label* false_target,
1151 Label* always_true_target) {
1152 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001153 if (cond->IsIntConstant()) {
1154 // Constant condition, statically compared against 1.
1155 int32_t cond_value = cond->AsIntConstant()->GetValue();
1156 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001157 if (always_true_target != nullptr) {
1158 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001159 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001160 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001161 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001162 DCHECK_EQ(cond_value, 0);
1163 }
1164 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001165 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001166 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1167 // Moves do not affect the eflags register, so if the condition is
1168 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001169 // again. We can't use the eflags on long/FP conditions if they are
1170 // materialized due to the complex branching.
1171 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001172 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001173 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001174 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1175 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001176 if (!eflags_set) {
1177 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001178 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001179 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001180 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001181 } else {
1182 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1183 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001184 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001185 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001186 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001187 }
1188 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001189 // Condition has not been materialized, use its inputs as the
1190 // comparison and its condition as the branch condition.
1191
Mark Mendellc4701932015-04-10 13:18:51 -04001192 // Is this a long or FP comparison that has been folded into the HCondition?
1193 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1194 // Generate the comparison directly.
1195 GenerateCompareTestAndBranch(instruction->AsIf(),
1196 cond->AsCondition(),
1197 true_target,
1198 false_target,
1199 always_true_target);
1200 return;
1201 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001202
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001203 Location lhs = cond->GetLocations()->InAt(0);
1204 Location rhs = cond->GetLocations()->InAt(1);
1205 // LHS is guaranteed to be in a register (see
1206 // LocationsBuilderX86::VisitCondition).
1207 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001209 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001210 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001211 if (constant == 0) {
1212 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1213 } else {
1214 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1215 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001216 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001218 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001220 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001221 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001222 if (false_target != nullptr) {
1223 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001224 }
1225}
1226
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001227void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1228 LocationSummary* locations =
1229 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1230 HInstruction* cond = if_instr->InputAt(0);
1231 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1232 locations->SetInAt(0, Location::Any());
1233 }
1234}
1235
1236void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1237 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1238 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1239 Label* always_true_target = true_target;
1240 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1241 if_instr->IfTrueSuccessor())) {
1242 always_true_target = nullptr;
1243 }
1244 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1245 if_instr->IfFalseSuccessor())) {
1246 false_target = nullptr;
1247 }
1248 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1249}
1250
1251void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1252 LocationSummary* locations = new (GetGraph()->GetArena())
1253 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1254 HInstruction* cond = deoptimize->InputAt(0);
1255 DCHECK(cond->IsCondition());
1256 if (cond->AsCondition()->NeedsMaterialization()) {
1257 locations->SetInAt(0, Location::Any());
1258 }
1259}
1260
1261void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001262 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001263 DeoptimizationSlowPathX86(deoptimize);
1264 codegen_->AddSlowPath(slow_path);
1265 Label* slow_path_entry = slow_path->GetEntryLabel();
1266 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1267}
1268
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001269void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001270 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001271}
1272
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001273void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1274 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001275}
1276
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001277void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001278 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279}
1280
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001281void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001282 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001283 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001284}
1285
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 LocationSummary* locations =
1288 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 switch (store->InputAt(1)->GetType()) {
1290 case Primitive::kPrimBoolean:
1291 case Primitive::kPrimByte:
1292 case Primitive::kPrimChar:
1293 case Primitive::kPrimShort:
1294 case Primitive::kPrimInt:
1295 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001296 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1298 break;
1299
1300 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1303 break;
1304
1305 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001308}
1309
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001310void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001311 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001312}
1313
Roland Levillain0d37cd02015-05-27 16:39:19 +01001314void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001315 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001316 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001317 // Handle the long/FP comparisons made in instruction simplification.
1318 switch (cond->InputAt(0)->GetType()) {
1319 case Primitive::kPrimLong: {
1320 locations->SetInAt(0, Location::RequiresRegister());
1321 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1322 if (cond->NeedsMaterialization()) {
1323 locations->SetOut(Location::RequiresRegister());
1324 }
1325 break;
1326 }
1327 case Primitive::kPrimFloat:
1328 case Primitive::kPrimDouble: {
1329 locations->SetInAt(0, Location::RequiresFpuRegister());
1330 locations->SetInAt(1, Location::RequiresFpuRegister());
1331 if (cond->NeedsMaterialization()) {
1332 locations->SetOut(Location::RequiresRegister());
1333 }
1334 break;
1335 }
1336 default:
1337 locations->SetInAt(0, Location::RequiresRegister());
1338 locations->SetInAt(1, Location::Any());
1339 if (cond->NeedsMaterialization()) {
1340 // We need a byte register.
1341 locations->SetOut(Location::RegisterLocation(ECX));
1342 }
1343 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001344 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001345}
1346
Roland Levillain0d37cd02015-05-27 16:39:19 +01001347void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001348 if (!cond->NeedsMaterialization()) {
1349 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001350 }
Mark Mendellc4701932015-04-10 13:18:51 -04001351
1352 LocationSummary* locations = cond->GetLocations();
1353 Location lhs = locations->InAt(0);
1354 Location rhs = locations->InAt(1);
1355 Register reg = locations->Out().AsRegister<Register>();
1356 Label true_label, false_label;
1357
1358 switch (cond->InputAt(0)->GetType()) {
1359 default: {
1360 // Integer case.
1361
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001362 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001363 __ xorl(reg, reg);
1364
1365 if (rhs.IsRegister()) {
1366 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1367 } else if (rhs.IsConstant()) {
1368 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1369 if (constant == 0) {
1370 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1371 } else {
1372 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1373 }
1374 } else {
1375 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1376 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001377 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001378 return;
1379 }
1380 case Primitive::kPrimLong:
1381 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1382 break;
1383 case Primitive::kPrimFloat:
1384 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1385 GenerateFPJumps(cond, &true_label, &false_label);
1386 break;
1387 case Primitive::kPrimDouble:
1388 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1389 GenerateFPJumps(cond, &true_label, &false_label);
1390 break;
1391 }
1392
1393 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001394 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001395
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001397 __ Bind(&false_label);
1398 __ xorl(reg, reg);
1399 __ jmp(&done_label);
1400
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001402 __ Bind(&true_label);
1403 __ movl(reg, Immediate(1));
1404 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001405}
1406
1407void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1408 VisitCondition(comp);
1409}
1410
1411void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1412 VisitCondition(comp);
1413}
1414
1415void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1416 VisitCondition(comp);
1417}
1418
1419void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1420 VisitCondition(comp);
1421}
1422
1423void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1424 VisitCondition(comp);
1425}
1426
1427void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1428 VisitCondition(comp);
1429}
1430
1431void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1432 VisitCondition(comp);
1433}
1434
1435void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1436 VisitCondition(comp);
1437}
1438
1439void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1440 VisitCondition(comp);
1441}
1442
1443void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1444 VisitCondition(comp);
1445}
1446
1447void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1448 VisitCondition(comp);
1449}
1450
1451void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1452 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001453}
1454
1455void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001456 LocationSummary* locations =
1457 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001458 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459}
1460
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001461void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001462 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001463 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001464}
1465
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001466void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1469 locations->SetOut(Location::ConstantLocation(constant));
1470}
1471
1472void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1473 // Will be generated at use site.
1474 UNUSED(constant);
1475}
1476
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001477void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001478 LocationSummary* locations =
1479 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001480 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001481}
1482
1483void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1484 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001485 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001486}
1487
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001488void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1489 LocationSummary* locations =
1490 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1491 locations->SetOut(Location::ConstantLocation(constant));
1492}
1493
1494void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1495 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001497}
1498
1499void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1500 LocationSummary* locations =
1501 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1502 locations->SetOut(Location::ConstantLocation(constant));
1503}
1504
1505void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1506 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001507 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001508}
1509
Calin Juravle27df7582015-04-17 19:12:31 +01001510void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1511 memory_barrier->SetLocations(nullptr);
1512}
1513
1514void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1515 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1516}
1517
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001519 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001520}
1521
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001522void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001523 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001524 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001525}
1526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001528 LocationSummary* locations =
1529 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530 switch (ret->InputAt(0)->GetType()) {
1531 case Primitive::kPrimBoolean:
1532 case Primitive::kPrimByte:
1533 case Primitive::kPrimChar:
1534 case Primitive::kPrimShort:
1535 case Primitive::kPrimInt:
1536 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001537 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001538 break;
1539
1540 case Primitive::kPrimLong:
1541 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001542 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001543 break;
1544
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001545 case Primitive::kPrimFloat:
1546 case Primitive::kPrimDouble:
1547 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001549 break;
1550
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001552 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001553 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554}
1555
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001556void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 if (kIsDebugBuild) {
1558 switch (ret->InputAt(0)->GetType()) {
1559 case Primitive::kPrimBoolean:
1560 case Primitive::kPrimByte:
1561 case Primitive::kPrimChar:
1562 case Primitive::kPrimShort:
1563 case Primitive::kPrimInt:
1564 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001565 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 break;
1567
1568 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001569 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1570 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 break;
1572
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001573 case Primitive::kPrimFloat:
1574 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001576 break;
1577
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001579 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580 }
1581 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001582 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001583}
1584
Calin Juravle175dc732015-08-25 15:42:32 +01001585void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1586 // The trampoline uses the same calling convention as dex calling conventions,
1587 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1588 // the method_idx.
1589 HandleInvoke(invoke);
1590}
1591
1592void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1593 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1594}
1595
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001596void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001597 // When we do not run baseline, explicit clinit checks triggered by static
1598 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1599 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001600
Mark Mendellfb8d2792015-03-31 22:16:59 -04001601 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001602 if (intrinsic.TryDispatch(invoke)) {
1603 return;
1604 }
1605
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001606 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001607
1608 if (codegen_->IsBaseline()) {
1609 // Baseline does not have enough registers if the current method also
1610 // needs a register. We therefore do not require a register for it, and let
1611 // the code generation of the invoke handle it.
1612 LocationSummary* locations = invoke->GetLocations();
1613 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1614 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1615 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1616 }
1617 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001618}
1619
Mark Mendell09ed1a32015-03-25 08:30:06 -04001620static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1621 if (invoke->GetLocations()->Intrinsified()) {
1622 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1623 intrinsic.Dispatch(invoke);
1624 return true;
1625 }
1626 return false;
1627}
1628
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001629void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001630 // When we do not run baseline, explicit clinit checks triggered by static
1631 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1632 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001633
Mark Mendell09ed1a32015-03-25 08:30:06 -04001634 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1635 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001636 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001637
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001638 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001639 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001640 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001641 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001642}
1643
1644void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1645 HandleInvoke(invoke);
1646}
1647
1648void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001649 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001650 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001651}
1652
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001653void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001654 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1655 return;
1656 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001657
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001658 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001659 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001660 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001661}
1662
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001663void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1664 HandleInvoke(invoke);
1665 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001666 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001667}
1668
1669void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1670 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001671 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001672 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1673 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001674 LocationSummary* locations = invoke->GetLocations();
1675 Location receiver = locations->InAt(0);
1676 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1677
1678 // Set the hidden argument.
1679 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001681
1682 // temp = object->GetClass();
1683 if (receiver.IsStackSlot()) {
1684 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1685 __ movl(temp, Address(temp, class_offset));
1686 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001687 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001688 }
Roland Levillain4d027112015-07-01 15:41:14 +01001689 codegen_->MaybeRecordImplicitNullCheck(invoke);
1690 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001691 // temp = temp->GetImtEntryAt(method_offset);
1692 __ movl(temp, Address(temp, method_offset));
1693 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001694 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001695 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001696
1697 DCHECK(!codegen_->IsLeafMethod());
1698 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1699}
1700
Roland Levillain88cb1752014-10-20 16:36:47 +01001701void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1702 LocationSummary* locations =
1703 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1704 switch (neg->GetResultType()) {
1705 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001706 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001707 locations->SetInAt(0, Location::RequiresRegister());
1708 locations->SetOut(Location::SameAsFirstInput());
1709 break;
1710
Roland Levillain88cb1752014-10-20 16:36:47 +01001711 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001712 locations->SetInAt(0, Location::RequiresFpuRegister());
1713 locations->SetOut(Location::SameAsFirstInput());
1714 locations->AddTemp(Location::RequiresRegister());
1715 locations->AddTemp(Location::RequiresFpuRegister());
1716 break;
1717
Roland Levillain88cb1752014-10-20 16:36:47 +01001718 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001719 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001720 locations->SetOut(Location::SameAsFirstInput());
1721 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1726 }
1727}
1728
1729void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1730 LocationSummary* locations = neg->GetLocations();
1731 Location out = locations->Out();
1732 Location in = locations->InAt(0);
1733 switch (neg->GetResultType()) {
1734 case Primitive::kPrimInt:
1735 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001736 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001737 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001738 break;
1739
1740 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001741 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001742 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001743 __ negl(out.AsRegisterPairLow<Register>());
1744 // Negation is similar to subtraction from zero. The least
1745 // significant byte triggers a borrow when it is different from
1746 // zero; to take it into account, add 1 to the most significant
1747 // byte if the carry flag (CF) is set to 1 after the first NEGL
1748 // operation.
1749 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1750 __ negl(out.AsRegisterPairHigh<Register>());
1751 break;
1752
Roland Levillain5368c212014-11-27 15:03:41 +00001753 case Primitive::kPrimFloat: {
1754 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 Register constant = locations->GetTemp(0).AsRegister<Register>();
1756 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001757 // Implement float negation with an exclusive or with value
1758 // 0x80000000 (mask for bit 31, representing the sign of a
1759 // single-precision floating-point number).
1760 __ movl(constant, Immediate(INT32_C(0x80000000)));
1761 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001762 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001763 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001764 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001765
Roland Levillain5368c212014-11-27 15:03:41 +00001766 case Primitive::kPrimDouble: {
1767 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001768 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001769 // Implement double negation with an exclusive or with value
1770 // 0x8000000000000000 (mask for bit 63, representing the sign of
1771 // a double-precision floating-point number).
1772 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001774 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001775 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001776
1777 default:
1778 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1779 }
1780}
1781
Roland Levillaindff1f282014-11-05 14:15:05 +00001782void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001783 Primitive::Type result_type = conversion->GetResultType();
1784 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001785 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001786
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001787 // The float-to-long and double-to-long type conversions rely on a
1788 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001789 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001790 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1791 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001792 ? LocationSummary::kCall
1793 : LocationSummary::kNoCall;
1794 LocationSummary* locations =
1795 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1796
David Brazdilb2bd1c52015-03-25 11:17:37 +00001797 // The Java language does not allow treating boolean as an integral type but
1798 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001799
Roland Levillaindff1f282014-11-05 14:15:05 +00001800 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001801 case Primitive::kPrimByte:
1802 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001803 case Primitive::kPrimBoolean:
1804 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001805 case Primitive::kPrimShort:
1806 case Primitive::kPrimInt:
1807 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001808 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001809 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1810 // Make the output overlap to please the register allocator. This greatly simplifies
1811 // the validation of the linear scan implementation
1812 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001813 break;
1814
1815 default:
1816 LOG(FATAL) << "Unexpected type conversion from " << input_type
1817 << " to " << result_type;
1818 }
1819 break;
1820
Roland Levillain01a8d712014-11-14 16:27:39 +00001821 case Primitive::kPrimShort:
1822 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001823 case Primitive::kPrimBoolean:
1824 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001825 case Primitive::kPrimByte:
1826 case Primitive::kPrimInt:
1827 case Primitive::kPrimChar:
1828 // Processing a Dex `int-to-short' instruction.
1829 locations->SetInAt(0, Location::Any());
1830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1831 break;
1832
1833 default:
1834 LOG(FATAL) << "Unexpected type conversion from " << input_type
1835 << " to " << result_type;
1836 }
1837 break;
1838
Roland Levillain946e1432014-11-11 17:35:19 +00001839 case Primitive::kPrimInt:
1840 switch (input_type) {
1841 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001842 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001843 locations->SetInAt(0, Location::Any());
1844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1845 break;
1846
1847 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001848 // Processing a Dex `float-to-int' instruction.
1849 locations->SetInAt(0, Location::RequiresFpuRegister());
1850 locations->SetOut(Location::RequiresRegister());
1851 locations->AddTemp(Location::RequiresFpuRegister());
1852 break;
1853
Roland Levillain946e1432014-11-11 17:35:19 +00001854 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001855 // Processing a Dex `double-to-int' instruction.
1856 locations->SetInAt(0, Location::RequiresFpuRegister());
1857 locations->SetOut(Location::RequiresRegister());
1858 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001859 break;
1860
1861 default:
1862 LOG(FATAL) << "Unexpected type conversion from " << input_type
1863 << " to " << result_type;
1864 }
1865 break;
1866
Roland Levillaindff1f282014-11-05 14:15:05 +00001867 case Primitive::kPrimLong:
1868 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001869 case Primitive::kPrimBoolean:
1870 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001874 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001875 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001876 locations->SetInAt(0, Location::RegisterLocation(EAX));
1877 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1878 break;
1879
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001880 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001881 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001882 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001883 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001884 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1885 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1886
Vladimir Marko949c91f2015-01-27 10:48:44 +00001887 // The runtime helper puts the result in EAX, EDX.
1888 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001889 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001890 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001891
1892 default:
1893 LOG(FATAL) << "Unexpected type conversion from " << input_type
1894 << " to " << result_type;
1895 }
1896 break;
1897
Roland Levillain981e4542014-11-14 11:47:14 +00001898 case Primitive::kPrimChar:
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 Levillain981e4542014-11-14 11:47:14 +00001902 case Primitive::kPrimByte:
1903 case Primitive::kPrimShort:
1904 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001905 // Processing a Dex `int-to-char' 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 Levillaindff1f282014-11-05 14:15:05 +00001916 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001917 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001918 case Primitive::kPrimBoolean:
1919 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001920 case Primitive::kPrimByte:
1921 case Primitive::kPrimShort:
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimChar:
1924 // Processing a Dex `int-to-float' instruction.
1925 locations->SetInAt(0, Location::RequiresRegister());
1926 locations->SetOut(Location::RequiresFpuRegister());
1927 break;
1928
1929 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001930 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001931 locations->SetInAt(0, Location::Any());
1932 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001933 break;
1934
Roland Levillaincff13742014-11-17 14:32:17 +00001935 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001936 // Processing a Dex `double-to-float' instruction.
1937 locations->SetInAt(0, Location::RequiresFpuRegister());
1938 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001939 break;
1940
1941 default:
1942 LOG(FATAL) << "Unexpected type conversion from " << input_type
1943 << " to " << result_type;
1944 };
1945 break;
1946
Roland Levillaindff1f282014-11-05 14:15:05 +00001947 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001948 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001949 case Primitive::kPrimBoolean:
1950 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001951 case Primitive::kPrimByte:
1952 case Primitive::kPrimShort:
1953 case Primitive::kPrimInt:
1954 case Primitive::kPrimChar:
1955 // Processing a Dex `int-to-double' instruction.
1956 locations->SetInAt(0, Location::RequiresRegister());
1957 locations->SetOut(Location::RequiresFpuRegister());
1958 break;
1959
1960 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001961 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001962 locations->SetInAt(0, Location::Any());
1963 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001964 break;
1965
Roland Levillaincff13742014-11-17 14:32:17 +00001966 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001967 // Processing a Dex `float-to-double' instruction.
1968 locations->SetInAt(0, Location::RequiresFpuRegister());
1969 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001970 break;
1971
1972 default:
1973 LOG(FATAL) << "Unexpected type conversion from " << input_type
1974 << " to " << result_type;
1975 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001976 break;
1977
1978 default:
1979 LOG(FATAL) << "Unexpected type conversion from " << input_type
1980 << " to " << result_type;
1981 }
1982}
1983
1984void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1985 LocationSummary* locations = conversion->GetLocations();
1986 Location out = locations->Out();
1987 Location in = locations->InAt(0);
1988 Primitive::Type result_type = conversion->GetResultType();
1989 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001990 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001991 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001992 case Primitive::kPrimByte:
1993 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001994 case Primitive::kPrimBoolean:
1995 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001996 case Primitive::kPrimShort:
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001999 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002000 if (in.IsRegister()) {
2001 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002002 } else {
2003 DCHECK(in.GetConstant()->IsIntConstant());
2004 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2005 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2006 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002007 break;
2008
2009 default:
2010 LOG(FATAL) << "Unexpected type conversion from " << input_type
2011 << " to " << result_type;
2012 }
2013 break;
2014
Roland Levillain01a8d712014-11-14 16:27:39 +00002015 case Primitive::kPrimShort:
2016 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002017 case Primitive::kPrimBoolean:
2018 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002019 case Primitive::kPrimByte:
2020 case Primitive::kPrimInt:
2021 case Primitive::kPrimChar:
2022 // Processing a Dex `int-to-short' instruction.
2023 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002024 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002025 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002027 } else {
2028 DCHECK(in.GetConstant()->IsIntConstant());
2029 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002030 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002031 }
2032 break;
2033
2034 default:
2035 LOG(FATAL) << "Unexpected type conversion from " << input_type
2036 << " to " << result_type;
2037 }
2038 break;
2039
Roland Levillain946e1432014-11-11 17:35:19 +00002040 case Primitive::kPrimInt:
2041 switch (input_type) {
2042 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002043 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002044 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002046 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002048 } else {
2049 DCHECK(in.IsConstant());
2050 DCHECK(in.GetConstant()->IsLongConstant());
2051 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002053 }
2054 break;
2055
Roland Levillain3f8f9362014-12-02 17:45:01 +00002056 case Primitive::kPrimFloat: {
2057 // Processing a Dex `float-to-int' instruction.
2058 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2059 Register output = out.AsRegister<Register>();
2060 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002061 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002062
2063 __ movl(output, Immediate(kPrimIntMax));
2064 // temp = int-to-float(output)
2065 __ cvtsi2ss(temp, output);
2066 // if input >= temp goto done
2067 __ comiss(input, temp);
2068 __ j(kAboveEqual, &done);
2069 // if input == NaN goto nan
2070 __ j(kUnordered, &nan);
2071 // output = float-to-int-truncate(input)
2072 __ cvttss2si(output, input);
2073 __ jmp(&done);
2074 __ Bind(&nan);
2075 // output = 0
2076 __ xorl(output, output);
2077 __ Bind(&done);
2078 break;
2079 }
2080
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002081 case Primitive::kPrimDouble: {
2082 // Processing a Dex `double-to-int' instruction.
2083 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2084 Register output = out.AsRegister<Register>();
2085 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002086 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002087
2088 __ movl(output, Immediate(kPrimIntMax));
2089 // temp = int-to-double(output)
2090 __ cvtsi2sd(temp, output);
2091 // if input >= temp goto done
2092 __ comisd(input, temp);
2093 __ j(kAboveEqual, &done);
2094 // if input == NaN goto nan
2095 __ j(kUnordered, &nan);
2096 // output = double-to-int-truncate(input)
2097 __ cvttsd2si(output, input);
2098 __ jmp(&done);
2099 __ Bind(&nan);
2100 // output = 0
2101 __ xorl(output, output);
2102 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002103 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002104 }
Roland Levillain946e1432014-11-11 17:35:19 +00002105
2106 default:
2107 LOG(FATAL) << "Unexpected type conversion from " << input_type
2108 << " to " << result_type;
2109 }
2110 break;
2111
Roland Levillaindff1f282014-11-05 14:15:05 +00002112 case Primitive::kPrimLong:
2113 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002114 case Primitive::kPrimBoolean:
2115 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002116 case Primitive::kPrimByte:
2117 case Primitive::kPrimShort:
2118 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002119 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002120 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002121 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2122 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002124 __ cdq();
2125 break;
2126
2127 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002128 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002129 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2130 conversion,
2131 conversion->GetDexPc(),
2132 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002133 break;
2134
Roland Levillaindff1f282014-11-05 14:15:05 +00002135 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002137 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2138 conversion,
2139 conversion->GetDexPc(),
2140 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 break;
2142
2143 default:
2144 LOG(FATAL) << "Unexpected type conversion from " << input_type
2145 << " to " << result_type;
2146 }
2147 break;
2148
Roland Levillain981e4542014-11-14 11:47:14 +00002149 case Primitive::kPrimChar:
2150 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002151 case Primitive::kPrimBoolean:
2152 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002153 case Primitive::kPrimByte:
2154 case Primitive::kPrimShort:
2155 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002156 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2157 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002159 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002160 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002161 } else {
2162 DCHECK(in.GetConstant()->IsIntConstant());
2163 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002165 }
2166 break;
2167
2168 default:
2169 LOG(FATAL) << "Unexpected type conversion from " << input_type
2170 << " to " << result_type;
2171 }
2172 break;
2173
Roland Levillaindff1f282014-11-05 14:15:05 +00002174 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002175 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002176 case Primitive::kPrimBoolean:
2177 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002178 case Primitive::kPrimByte:
2179 case Primitive::kPrimShort:
2180 case Primitive::kPrimInt:
2181 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002182 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002184 break;
2185
Roland Levillain6d0e4832014-11-27 18:31:21 +00002186 case Primitive::kPrimLong: {
2187 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002188 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002189
Roland Levillain232ade02015-04-20 15:14:36 +01002190 // Create stack space for the call to
2191 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2192 // TODO: enhance register allocator to ask for stack temporaries.
2193 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2194 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2195 __ subl(ESP, Immediate(adjustment));
2196 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002197
Roland Levillain232ade02015-04-20 15:14:36 +01002198 // Load the value to the FP stack, using temporaries if needed.
2199 PushOntoFPStack(in, 0, adjustment, false, true);
2200
2201 if (out.IsStackSlot()) {
2202 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2203 } else {
2204 __ fstps(Address(ESP, 0));
2205 Location stack_temp = Location::StackSlot(0);
2206 codegen_->Move32(out, stack_temp);
2207 }
2208
2209 // Remove the temporary stack space we allocated.
2210 if (adjustment != 0) {
2211 __ addl(ESP, Immediate(adjustment));
2212 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002213 break;
2214 }
2215
Roland Levillaincff13742014-11-17 14:32:17 +00002216 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002217 // Processing a Dex `double-to-float' instruction.
2218 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002219 break;
2220
2221 default:
2222 LOG(FATAL) << "Unexpected type conversion from " << input_type
2223 << " to " << result_type;
2224 };
2225 break;
2226
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002228 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002229 case Primitive::kPrimBoolean:
2230 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002231 case Primitive::kPrimByte:
2232 case Primitive::kPrimShort:
2233 case Primitive::kPrimInt:
2234 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002235 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002237 break;
2238
Roland Levillain647b9ed2014-11-27 12:06:00 +00002239 case Primitive::kPrimLong: {
2240 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002241 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002242
Roland Levillain232ade02015-04-20 15:14:36 +01002243 // Create stack space for the call to
2244 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2245 // TODO: enhance register allocator to ask for stack temporaries.
2246 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2247 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2248 __ subl(ESP, Immediate(adjustment));
2249 }
2250
2251 // Load the value to the FP stack, using temporaries if needed.
2252 PushOntoFPStack(in, 0, adjustment, false, true);
2253
2254 if (out.IsDoubleStackSlot()) {
2255 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2256 } else {
2257 __ fstpl(Address(ESP, 0));
2258 Location stack_temp = Location::DoubleStackSlot(0);
2259 codegen_->Move64(out, stack_temp);
2260 }
2261
2262 // Remove the temporary stack space we allocated.
2263 if (adjustment != 0) {
2264 __ addl(ESP, Immediate(adjustment));
2265 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002266 break;
2267 }
2268
Roland Levillaincff13742014-11-17 14:32:17 +00002269 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002270 // Processing a Dex `float-to-double' instruction.
2271 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 break;
2279
2280 default:
2281 LOG(FATAL) << "Unexpected type conversion from " << input_type
2282 << " to " << result_type;
2283 }
2284}
2285
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002286void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002287 LocationSummary* locations =
2288 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002289 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002290 case Primitive::kPrimInt: {
2291 locations->SetInAt(0, Location::RequiresRegister());
2292 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2294 break;
2295 }
2296
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::Any());
2300 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002301 break;
2302 }
2303
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 case Primitive::kPrimFloat:
2305 case Primitive::kPrimDouble: {
2306 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002307 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002308 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002309 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002310 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002312 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2314 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002315 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002316}
2317
2318void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2319 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 Location first = locations->InAt(0);
2321 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002322 Location out = locations->Out();
2323
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002324 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002325 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002326 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002327 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2328 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002329 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2330 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002331 } else {
2332 __ leal(out.AsRegister<Register>(), Address(
2333 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2334 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002336 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2337 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2338 __ addl(out.AsRegister<Register>(), Immediate(value));
2339 } else {
2340 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2341 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002342 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002343 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002344 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002345 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002346 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347 }
2348
2349 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002350 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002351 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2352 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002353 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002354 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2355 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002356 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002357 } else {
2358 DCHECK(second.IsConstant()) << second;
2359 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2360 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2361 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002362 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002363 break;
2364 }
2365
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002366 case Primitive::kPrimFloat: {
2367 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002369 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2370 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2371 DCHECK(!const_area->NeedsMaterialization());
2372 __ addss(first.AsFpuRegister<XmmRegister>(),
2373 codegen_->LiteralFloatAddress(
2374 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2375 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2376 } else {
2377 DCHECK(second.IsStackSlot());
2378 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002379 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002380 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002381 }
2382
2383 case Primitive::kPrimDouble: {
2384 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002386 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2387 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2388 DCHECK(!const_area->NeedsMaterialization());
2389 __ addsd(first.AsFpuRegister<XmmRegister>(),
2390 codegen_->LiteralDoubleAddress(
2391 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2392 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2393 } else {
2394 DCHECK(second.IsDoubleStackSlot());
2395 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002396 }
2397 break;
2398 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002399
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002400 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002401 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002402 }
2403}
2404
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002405void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002406 LocationSummary* locations =
2407 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002408 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002409 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002410 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetInAt(1, Location::Any());
2413 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002414 break;
2415 }
Calin Juravle11351682014-10-23 15:38:15 +01002416 case Primitive::kPrimFloat:
2417 case Primitive::kPrimDouble: {
2418 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002419 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002420 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002421 break;
Calin Juravle11351682014-10-23 15:38:15 +01002422 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002424 default:
Calin Juravle11351682014-10-23 15:38:15 +01002425 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002427}
2428
2429void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2430 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002431 Location first = locations->InAt(0);
2432 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002433 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002436 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002438 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002439 __ subl(first.AsRegister<Register>(),
2440 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002441 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002443 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002444 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002445 }
2446
2447 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002448 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002449 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2450 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002451 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002452 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002453 __ sbbl(first.AsRegisterPairHigh<Register>(),
2454 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002455 } else {
2456 DCHECK(second.IsConstant()) << second;
2457 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2458 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2459 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002460 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002461 break;
2462 }
2463
Calin Juravle11351682014-10-23 15:38:15 +01002464 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002465 if (second.IsFpuRegister()) {
2466 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2467 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2468 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2469 DCHECK(!const_area->NeedsMaterialization());
2470 __ subss(first.AsFpuRegister<XmmRegister>(),
2471 codegen_->LiteralFloatAddress(
2472 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2473 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2474 } else {
2475 DCHECK(second.IsStackSlot());
2476 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2477 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002478 break;
Calin Juravle11351682014-10-23 15:38:15 +01002479 }
2480
2481 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002482 if (second.IsFpuRegister()) {
2483 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2484 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2485 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2486 DCHECK(!const_area->NeedsMaterialization());
2487 __ subsd(first.AsFpuRegister<XmmRegister>(),
2488 codegen_->LiteralDoubleAddress(
2489 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2490 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2491 } else {
2492 DCHECK(second.IsDoubleStackSlot());
2493 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2494 }
Calin Juravle11351682014-10-23 15:38:15 +01002495 break;
2496 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002497
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002498 default:
Calin Juravle11351682014-10-23 15:38:15 +01002499 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002500 }
2501}
2502
Calin Juravle34bacdf2014-10-07 20:23:36 +01002503void LocationsBuilderX86::VisitMul(HMul* mul) {
2504 LocationSummary* locations =
2505 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2506 switch (mul->GetResultType()) {
2507 case Primitive::kPrimInt:
2508 locations->SetInAt(0, Location::RequiresRegister());
2509 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002510 if (mul->InputAt(1)->IsIntConstant()) {
2511 // Can use 3 operand multiply.
2512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2513 } else {
2514 locations->SetOut(Location::SameAsFirstInput());
2515 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002516 break;
2517 case Primitive::kPrimLong: {
2518 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002519 locations->SetInAt(1, Location::Any());
2520 locations->SetOut(Location::SameAsFirstInput());
2521 // Needed for imul on 32bits with 64bits output.
2522 locations->AddTemp(Location::RegisterLocation(EAX));
2523 locations->AddTemp(Location::RegisterLocation(EDX));
2524 break;
2525 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002526 case Primitive::kPrimFloat:
2527 case Primitive::kPrimDouble: {
2528 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002529 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002530 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002531 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002532 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002533
2534 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002535 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002536 }
2537}
2538
2539void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2540 LocationSummary* locations = mul->GetLocations();
2541 Location first = locations->InAt(0);
2542 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002543 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002544
2545 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002546 case Primitive::kPrimInt:
2547 // The constant may have ended up in a register, so test explicitly to avoid
2548 // problems where the output may not be the same as the first operand.
2549 if (mul->InputAt(1)->IsIntConstant()) {
2550 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2551 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2552 } else if (second.IsRegister()) {
2553 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002555 } else {
2556 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002557 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002559 }
2560 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002561
2562 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002563 Register in1_hi = first.AsRegisterPairHigh<Register>();
2564 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 Register eax = locations->GetTemp(0).AsRegister<Register>();
2566 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002567
2568 DCHECK_EQ(EAX, eax);
2569 DCHECK_EQ(EDX, edx);
2570
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002571 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002572 // output: in1
2573 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2574 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2575 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002576 if (second.IsConstant()) {
2577 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002578
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002579 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2580 int32_t low_value = Low32Bits(value);
2581 int32_t high_value = High32Bits(value);
2582 Immediate low(low_value);
2583 Immediate high(high_value);
2584
2585 __ movl(eax, high);
2586 // eax <- in1.lo * in2.hi
2587 __ imull(eax, in1_lo);
2588 // in1.hi <- in1.hi * in2.lo
2589 __ imull(in1_hi, low);
2590 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2591 __ addl(in1_hi, eax);
2592 // move in2_lo to eax to prepare for double precision
2593 __ movl(eax, low);
2594 // edx:eax <- in1.lo * in2.lo
2595 __ mull(in1_lo);
2596 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2597 __ addl(in1_hi, edx);
2598 // in1.lo <- (in1.lo * in2.lo)[31:0];
2599 __ movl(in1_lo, eax);
2600 } else if (second.IsRegisterPair()) {
2601 Register in2_hi = second.AsRegisterPairHigh<Register>();
2602 Register in2_lo = second.AsRegisterPairLow<Register>();
2603
2604 __ movl(eax, in2_hi);
2605 // eax <- in1.lo * in2.hi
2606 __ imull(eax, in1_lo);
2607 // in1.hi <- in1.hi * in2.lo
2608 __ imull(in1_hi, in2_lo);
2609 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2610 __ addl(in1_hi, eax);
2611 // move in1_lo to eax to prepare for double precision
2612 __ movl(eax, in1_lo);
2613 // edx:eax <- in1.lo * in2.lo
2614 __ mull(in2_lo);
2615 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2616 __ addl(in1_hi, edx);
2617 // in1.lo <- (in1.lo * in2.lo)[31:0];
2618 __ movl(in1_lo, eax);
2619 } else {
2620 DCHECK(second.IsDoubleStackSlot()) << second;
2621 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2622 Address in2_lo(ESP, second.GetStackIndex());
2623
2624 __ movl(eax, in2_hi);
2625 // eax <- in1.lo * in2.hi
2626 __ imull(eax, in1_lo);
2627 // in1.hi <- in1.hi * in2.lo
2628 __ imull(in1_hi, in2_lo);
2629 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2630 __ addl(in1_hi, eax);
2631 // move in1_lo to eax to prepare for double precision
2632 __ movl(eax, in1_lo);
2633 // edx:eax <- in1.lo * in2.lo
2634 __ mull(in2_lo);
2635 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2636 __ addl(in1_hi, edx);
2637 // in1.lo <- (in1.lo * in2.lo)[31:0];
2638 __ movl(in1_lo, eax);
2639 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002640
2641 break;
2642 }
2643
Calin Juravleb5bfa962014-10-21 18:02:24 +01002644 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002645 DCHECK(first.Equals(locations->Out()));
2646 if (second.IsFpuRegister()) {
2647 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2648 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2649 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2650 DCHECK(!const_area->NeedsMaterialization());
2651 __ mulss(first.AsFpuRegister<XmmRegister>(),
2652 codegen_->LiteralFloatAddress(
2653 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2654 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2655 } else {
2656 DCHECK(second.IsStackSlot());
2657 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2658 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002659 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002660 }
2661
2662 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002663 DCHECK(first.Equals(locations->Out()));
2664 if (second.IsFpuRegister()) {
2665 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2666 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2667 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2668 DCHECK(!const_area->NeedsMaterialization());
2669 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2670 codegen_->LiteralDoubleAddress(
2671 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2672 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2673 } else {
2674 DCHECK(second.IsDoubleStackSlot());
2675 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2676 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002677 break;
2678 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002679
2680 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002681 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002682 }
2683}
2684
Roland Levillain232ade02015-04-20 15:14:36 +01002685void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2686 uint32_t temp_offset,
2687 uint32_t stack_adjustment,
2688 bool is_fp,
2689 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002690 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002691 DCHECK(!is_wide);
2692 if (is_fp) {
2693 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2694 } else {
2695 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2696 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002697 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002698 DCHECK(is_wide);
2699 if (is_fp) {
2700 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2701 } else {
2702 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2703 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002704 } else {
2705 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002706 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002707 Location stack_temp = Location::StackSlot(temp_offset);
2708 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002709 if (is_fp) {
2710 __ flds(Address(ESP, temp_offset));
2711 } else {
2712 __ filds(Address(ESP, temp_offset));
2713 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002714 } else {
2715 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2716 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002717 if (is_fp) {
2718 __ fldl(Address(ESP, temp_offset));
2719 } else {
2720 __ fildl(Address(ESP, temp_offset));
2721 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002722 }
2723 }
2724}
2725
2726void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2727 Primitive::Type type = rem->GetResultType();
2728 bool is_float = type == Primitive::kPrimFloat;
2729 size_t elem_size = Primitive::ComponentSize(type);
2730 LocationSummary* locations = rem->GetLocations();
2731 Location first = locations->InAt(0);
2732 Location second = locations->InAt(1);
2733 Location out = locations->Out();
2734
2735 // Create stack space for 2 elements.
2736 // TODO: enhance register allocator to ask for stack temporaries.
2737 __ subl(ESP, Immediate(2 * elem_size));
2738
2739 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002740 const bool is_wide = !is_float;
2741 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2742 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002743
2744 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002745 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002746 __ Bind(&retry);
2747 __ fprem();
2748
2749 // Move FP status to AX.
2750 __ fstsw();
2751
2752 // And see if the argument reduction is complete. This is signaled by the
2753 // C2 FPU flag bit set to 0.
2754 __ andl(EAX, Immediate(kC2ConditionMask));
2755 __ j(kNotEqual, &retry);
2756
2757 // We have settled on the final value. Retrieve it into an XMM register.
2758 // Store FP top of stack to real stack.
2759 if (is_float) {
2760 __ fsts(Address(ESP, 0));
2761 } else {
2762 __ fstl(Address(ESP, 0));
2763 }
2764
2765 // Pop the 2 items from the FP stack.
2766 __ fucompp();
2767
2768 // Load the value from the stack into an XMM register.
2769 DCHECK(out.IsFpuRegister()) << out;
2770 if (is_float) {
2771 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2772 } else {
2773 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2774 }
2775
2776 // And remove the temporary stack space we allocated.
2777 __ addl(ESP, Immediate(2 * elem_size));
2778}
2779
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002780
2781void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2782 DCHECK(instruction->IsDiv() || instruction->IsRem());
2783
2784 LocationSummary* locations = instruction->GetLocations();
2785 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002786 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002787
2788 Register out_register = locations->Out().AsRegister<Register>();
2789 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002790 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002791
2792 DCHECK(imm == 1 || imm == -1);
2793
2794 if (instruction->IsRem()) {
2795 __ xorl(out_register, out_register);
2796 } else {
2797 __ movl(out_register, input_register);
2798 if (imm == -1) {
2799 __ negl(out_register);
2800 }
2801 }
2802}
2803
2804
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002805void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002806 LocationSummary* locations = instruction->GetLocations();
2807
2808 Register out_register = locations->Out().AsRegister<Register>();
2809 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002810 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002811
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002812 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002813 Register num = locations->GetTemp(0).AsRegister<Register>();
2814
2815 __ leal(num, Address(input_register, std::abs(imm) - 1));
2816 __ testl(input_register, input_register);
2817 __ cmovl(kGreaterEqual, num, input_register);
2818 int shift = CTZ(imm);
2819 __ sarl(num, Immediate(shift));
2820
2821 if (imm < 0) {
2822 __ negl(num);
2823 }
2824
2825 __ movl(out_register, num);
2826}
2827
2828void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2829 DCHECK(instruction->IsDiv() || instruction->IsRem());
2830
2831 LocationSummary* locations = instruction->GetLocations();
2832 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2833
2834 Register eax = locations->InAt(0).AsRegister<Register>();
2835 Register out = locations->Out().AsRegister<Register>();
2836 Register num;
2837 Register edx;
2838
2839 if (instruction->IsDiv()) {
2840 edx = locations->GetTemp(0).AsRegister<Register>();
2841 num = locations->GetTemp(1).AsRegister<Register>();
2842 } else {
2843 edx = locations->Out().AsRegister<Register>();
2844 num = locations->GetTemp(0).AsRegister<Register>();
2845 }
2846
2847 DCHECK_EQ(EAX, eax);
2848 DCHECK_EQ(EDX, edx);
2849 if (instruction->IsDiv()) {
2850 DCHECK_EQ(EAX, out);
2851 } else {
2852 DCHECK_EQ(EDX, out);
2853 }
2854
2855 int64_t magic;
2856 int shift;
2857 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2858
Mark Mendell0c9497d2015-08-21 09:30:05 -04002859 NearLabel ndiv;
2860 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002861 // If numerator is 0, the result is 0, no computation needed.
2862 __ testl(eax, eax);
2863 __ j(kNotEqual, &ndiv);
2864
2865 __ xorl(out, out);
2866 __ jmp(&end);
2867
2868 __ Bind(&ndiv);
2869
2870 // Save the numerator.
2871 __ movl(num, eax);
2872
2873 // EAX = magic
2874 __ movl(eax, Immediate(magic));
2875
2876 // EDX:EAX = magic * numerator
2877 __ imull(num);
2878
2879 if (imm > 0 && magic < 0) {
2880 // EDX += num
2881 __ addl(edx, num);
2882 } else if (imm < 0 && magic > 0) {
2883 __ subl(edx, num);
2884 }
2885
2886 // Shift if needed.
2887 if (shift != 0) {
2888 __ sarl(edx, Immediate(shift));
2889 }
2890
2891 // EDX += 1 if EDX < 0
2892 __ movl(eax, edx);
2893 __ shrl(edx, Immediate(31));
2894 __ addl(edx, eax);
2895
2896 if (instruction->IsRem()) {
2897 __ movl(eax, num);
2898 __ imull(edx, Immediate(imm));
2899 __ subl(eax, edx);
2900 __ movl(edx, eax);
2901 } else {
2902 __ movl(eax, edx);
2903 }
2904 __ Bind(&end);
2905}
2906
Calin Juravlebacfec32014-11-14 15:54:36 +00002907void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2908 DCHECK(instruction->IsDiv() || instruction->IsRem());
2909
2910 LocationSummary* locations = instruction->GetLocations();
2911 Location out = locations->Out();
2912 Location first = locations->InAt(0);
2913 Location second = locations->InAt(1);
2914 bool is_div = instruction->IsDiv();
2915
2916 switch (instruction->GetResultType()) {
2917 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002918 DCHECK_EQ(EAX, first.AsRegister<Register>());
2919 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002920
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002921 if (instruction->InputAt(1)->IsIntConstant()) {
2922 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002923
2924 if (imm == 0) {
2925 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2926 } else if (imm == 1 || imm == -1) {
2927 DivRemOneOrMinusOne(instruction);
2928 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002929 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002930 } else {
2931 DCHECK(imm <= -2 || imm >= 2);
2932 GenerateDivRemWithAnyConstant(instruction);
2933 }
2934 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002935 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002936 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002937 is_div);
2938 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002939
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002940 Register second_reg = second.AsRegister<Register>();
2941 // 0x80000000/-1 triggers an arithmetic exception!
2942 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2943 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002944
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002945 __ cmpl(second_reg, Immediate(-1));
2946 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002947
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002948 // edx:eax <- sign-extended of eax
2949 __ cdq();
2950 // eax = quotient, edx = remainder
2951 __ idivl(second_reg);
2952 __ Bind(slow_path->GetExitLabel());
2953 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002954 break;
2955 }
2956
2957 case Primitive::kPrimLong: {
2958 InvokeRuntimeCallingConvention calling_convention;
2959 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2960 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2961 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2962 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2963 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2964 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2965
2966 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002967 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2968 instruction,
2969 instruction->GetDexPc(),
2970 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002971 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002972 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2973 instruction,
2974 instruction->GetDexPc(),
2975 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002976 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002977 break;
2978 }
2979
2980 default:
2981 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2982 }
2983}
2984
Calin Juravle7c4954d2014-10-28 16:57:40 +00002985void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002986 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002987 ? LocationSummary::kCall
2988 : LocationSummary::kNoCall;
2989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2990
Calin Juravle7c4954d2014-10-28 16:57:40 +00002991 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002992 case Primitive::kPrimInt: {
2993 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002994 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002995 locations->SetOut(Location::SameAsFirstInput());
2996 // Intel uses edx:eax as the dividend.
2997 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002998 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2999 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3000 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003001 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003002 locations->AddTemp(Location::RequiresRegister());
3003 }
Calin Juravled0d48522014-11-04 16:40:20 +00003004 break;
3005 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003006 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003007 InvokeRuntimeCallingConvention calling_convention;
3008 locations->SetInAt(0, Location::RegisterPairLocation(
3009 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3010 locations->SetInAt(1, Location::RegisterPairLocation(
3011 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3012 // Runtime helper puts the result in EAX, EDX.
3013 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003014 break;
3015 }
3016 case Primitive::kPrimFloat:
3017 case Primitive::kPrimDouble: {
3018 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003019 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003020 locations->SetOut(Location::SameAsFirstInput());
3021 break;
3022 }
3023
3024 default:
3025 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3026 }
3027}
3028
3029void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3030 LocationSummary* locations = div->GetLocations();
3031 Location first = locations->InAt(0);
3032 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003033
3034 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003035 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003036 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003037 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003038 break;
3039 }
3040
3041 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003042 if (second.IsFpuRegister()) {
3043 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3044 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3045 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3046 DCHECK(!const_area->NeedsMaterialization());
3047 __ divss(first.AsFpuRegister<XmmRegister>(),
3048 codegen_->LiteralFloatAddress(
3049 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3050 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3051 } else {
3052 DCHECK(second.IsStackSlot());
3053 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3054 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003055 break;
3056 }
3057
3058 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003059 if (second.IsFpuRegister()) {
3060 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3061 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3062 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3063 DCHECK(!const_area->NeedsMaterialization());
3064 __ divsd(first.AsFpuRegister<XmmRegister>(),
3065 codegen_->LiteralDoubleAddress(
3066 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3067 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3068 } else {
3069 DCHECK(second.IsDoubleStackSlot());
3070 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3071 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003072 break;
3073 }
3074
3075 default:
3076 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3077 }
3078}
3079
Calin Juravlebacfec32014-11-14 15:54:36 +00003080void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003081 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003082
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003083 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3084 ? LocationSummary::kCall
3085 : LocationSummary::kNoCall;
3086 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003087
Calin Juravled2ec87d2014-12-08 14:24:46 +00003088 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003089 case Primitive::kPrimInt: {
3090 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003091 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003092 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003093 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3094 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3095 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003096 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003097 locations->AddTemp(Location::RequiresRegister());
3098 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 break;
3100 }
3101 case Primitive::kPrimLong: {
3102 InvokeRuntimeCallingConvention calling_convention;
3103 locations->SetInAt(0, Location::RegisterPairLocation(
3104 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3105 locations->SetInAt(1, Location::RegisterPairLocation(
3106 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3107 // Runtime helper puts the result in EAX, EDX.
3108 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3109 break;
3110 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003111 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003112 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003113 locations->SetInAt(0, Location::Any());
3114 locations->SetInAt(1, Location::Any());
3115 locations->SetOut(Location::RequiresFpuRegister());
3116 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003117 break;
3118 }
3119
3120 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003121 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003122 }
3123}
3124
3125void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3126 Primitive::Type type = rem->GetResultType();
3127 switch (type) {
3128 case Primitive::kPrimInt:
3129 case Primitive::kPrimLong: {
3130 GenerateDivRemIntegral(rem);
3131 break;
3132 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003133 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003134 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003135 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003136 break;
3137 }
3138 default:
3139 LOG(FATAL) << "Unexpected rem type " << type;
3140 }
3141}
3142
Calin Juravled0d48522014-11-04 16:40:20 +00003143void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003144 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3145 ? LocationSummary::kCallOnSlowPath
3146 : LocationSummary::kNoCall;
3147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003148 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003149 case Primitive::kPrimByte:
3150 case Primitive::kPrimChar:
3151 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003152 case Primitive::kPrimInt: {
3153 locations->SetInAt(0, Location::Any());
3154 break;
3155 }
3156 case Primitive::kPrimLong: {
3157 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3158 if (!instruction->IsConstant()) {
3159 locations->AddTemp(Location::RequiresRegister());
3160 }
3161 break;
3162 }
3163 default:
3164 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3165 }
Calin Juravled0d48522014-11-04 16:40:20 +00003166 if (instruction->HasUses()) {
3167 locations->SetOut(Location::SameAsFirstInput());
3168 }
3169}
3170
3171void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003172 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003173 codegen_->AddSlowPath(slow_path);
3174
3175 LocationSummary* locations = instruction->GetLocations();
3176 Location value = locations->InAt(0);
3177
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003178 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003179 case Primitive::kPrimByte:
3180 case Primitive::kPrimChar:
3181 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003182 case Primitive::kPrimInt: {
3183 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003184 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003185 __ j(kEqual, slow_path->GetEntryLabel());
3186 } else if (value.IsStackSlot()) {
3187 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3188 __ j(kEqual, slow_path->GetEntryLabel());
3189 } else {
3190 DCHECK(value.IsConstant()) << value;
3191 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3192 __ jmp(slow_path->GetEntryLabel());
3193 }
3194 }
3195 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003196 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003197 case Primitive::kPrimLong: {
3198 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003199 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003200 __ movl(temp, value.AsRegisterPairLow<Register>());
3201 __ orl(temp, value.AsRegisterPairHigh<Register>());
3202 __ j(kEqual, slow_path->GetEntryLabel());
3203 } else {
3204 DCHECK(value.IsConstant()) << value;
3205 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3206 __ jmp(slow_path->GetEntryLabel());
3207 }
3208 }
3209 break;
3210 }
3211 default:
3212 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003213 }
Calin Juravled0d48522014-11-04 16:40:20 +00003214}
3215
Calin Juravle9aec02f2014-11-18 23:06:35 +00003216void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3217 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3218
3219 LocationSummary* locations =
3220 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3221
3222 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003223 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003224 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003225 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003226 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003227 // The shift count needs to be in CL or a constant.
3228 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003229 locations->SetOut(Location::SameAsFirstInput());
3230 break;
3231 }
3232 default:
3233 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3234 }
3235}
3236
3237void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3238 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3239
3240 LocationSummary* locations = op->GetLocations();
3241 Location first = locations->InAt(0);
3242 Location second = locations->InAt(1);
3243 DCHECK(first.Equals(locations->Out()));
3244
3245 switch (op->GetResultType()) {
3246 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003247 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003248 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003249 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003251 DCHECK_EQ(ECX, second_reg);
3252 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003253 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003254 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003255 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003256 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003257 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003258 }
3259 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003260 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3261 if (shift == 0) {
3262 return;
3263 }
3264 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003265 if (op->IsShl()) {
3266 __ shll(first_reg, imm);
3267 } else if (op->IsShr()) {
3268 __ sarl(first_reg, imm);
3269 } else {
3270 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003271 }
3272 }
3273 break;
3274 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003275 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003276 if (second.IsRegister()) {
3277 Register second_reg = second.AsRegister<Register>();
3278 DCHECK_EQ(ECX, second_reg);
3279 if (op->IsShl()) {
3280 GenerateShlLong(first, second_reg);
3281 } else if (op->IsShr()) {
3282 GenerateShrLong(first, second_reg);
3283 } else {
3284 GenerateUShrLong(first, second_reg);
3285 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003286 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003287 // Shift by a constant.
3288 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3289 // Nothing to do if the shift is 0, as the input is already the output.
3290 if (shift != 0) {
3291 if (op->IsShl()) {
3292 GenerateShlLong(first, shift);
3293 } else if (op->IsShr()) {
3294 GenerateShrLong(first, shift);
3295 } else {
3296 GenerateUShrLong(first, shift);
3297 }
3298 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003299 }
3300 break;
3301 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003302 default:
3303 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3304 }
3305}
3306
Mark P Mendell73945692015-04-29 14:56:17 +00003307void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3308 Register low = loc.AsRegisterPairLow<Register>();
3309 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003310 if (shift == 1) {
3311 // This is just an addition.
3312 __ addl(low, low);
3313 __ adcl(high, high);
3314 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003315 // Shift by 32 is easy. High gets low, and low gets 0.
3316 codegen_->EmitParallelMoves(
3317 loc.ToLow(),
3318 loc.ToHigh(),
3319 Primitive::kPrimInt,
3320 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3321 loc.ToLow(),
3322 Primitive::kPrimInt);
3323 } else if (shift > 32) {
3324 // Low part becomes 0. High part is low part << (shift-32).
3325 __ movl(high, low);
3326 __ shll(high, Immediate(shift - 32));
3327 __ xorl(low, low);
3328 } else {
3329 // Between 1 and 31.
3330 __ shld(high, low, Immediate(shift));
3331 __ shll(low, Immediate(shift));
3332 }
3333}
3334
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003336 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003337 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3338 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3339 __ testl(shifter, Immediate(32));
3340 __ j(kEqual, &done);
3341 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3342 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3343 __ Bind(&done);
3344}
3345
Mark P Mendell73945692015-04-29 14:56:17 +00003346void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3347 Register low = loc.AsRegisterPairLow<Register>();
3348 Register high = loc.AsRegisterPairHigh<Register>();
3349 if (shift == 32) {
3350 // Need to copy the sign.
3351 DCHECK_NE(low, high);
3352 __ movl(low, high);
3353 __ sarl(high, Immediate(31));
3354 } else if (shift > 32) {
3355 DCHECK_NE(low, high);
3356 // High part becomes sign. Low part is shifted by shift - 32.
3357 __ movl(low, high);
3358 __ sarl(high, Immediate(31));
3359 __ sarl(low, Immediate(shift - 32));
3360 } else {
3361 // Between 1 and 31.
3362 __ shrd(low, high, Immediate(shift));
3363 __ sarl(high, Immediate(shift));
3364 }
3365}
3366
Calin Juravle9aec02f2014-11-18 23:06:35 +00003367void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003368 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003369 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3370 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3371 __ testl(shifter, Immediate(32));
3372 __ j(kEqual, &done);
3373 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3374 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3375 __ Bind(&done);
3376}
3377
Mark P Mendell73945692015-04-29 14:56:17 +00003378void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3379 Register low = loc.AsRegisterPairLow<Register>();
3380 Register high = loc.AsRegisterPairHigh<Register>();
3381 if (shift == 32) {
3382 // Shift by 32 is easy. Low gets high, and high gets 0.
3383 codegen_->EmitParallelMoves(
3384 loc.ToHigh(),
3385 loc.ToLow(),
3386 Primitive::kPrimInt,
3387 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3388 loc.ToHigh(),
3389 Primitive::kPrimInt);
3390 } else if (shift > 32) {
3391 // Low part is high >> (shift - 32). High part becomes 0.
3392 __ movl(low, high);
3393 __ shrl(low, Immediate(shift - 32));
3394 __ xorl(high, high);
3395 } else {
3396 // Between 1 and 31.
3397 __ shrd(low, high, Immediate(shift));
3398 __ shrl(high, Immediate(shift));
3399 }
3400}
3401
Calin Juravle9aec02f2014-11-18 23:06:35 +00003402void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003403 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003404 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3405 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3406 __ testl(shifter, Immediate(32));
3407 __ j(kEqual, &done);
3408 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3409 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3410 __ Bind(&done);
3411}
3412
3413void LocationsBuilderX86::VisitShl(HShl* shl) {
3414 HandleShift(shl);
3415}
3416
3417void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3418 HandleShift(shl);
3419}
3420
3421void LocationsBuilderX86::VisitShr(HShr* shr) {
3422 HandleShift(shr);
3423}
3424
3425void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3426 HandleShift(shr);
3427}
3428
3429void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3430 HandleShift(ushr);
3431}
3432
3433void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3434 HandleShift(ushr);
3435}
3436
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003437void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003438 LocationSummary* locations =
3439 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003440 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003441 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003442 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003443 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003444}
3445
3446void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3447 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003448 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003449 // Note: if heap poisoning is enabled, the entry point takes cares
3450 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003451 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3452 instruction,
3453 instruction->GetDexPc(),
3454 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003455 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003456}
3457
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003458void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3461 locations->SetOut(Location::RegisterLocation(EAX));
3462 InvokeRuntimeCallingConvention calling_convention;
3463 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003465 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003466}
3467
3468void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3469 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003470 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3471
Roland Levillain4d027112015-07-01 15:41:14 +01003472 // Note: if heap poisoning is enabled, the entry point takes cares
3473 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003474 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3475 instruction,
3476 instruction->GetDexPc(),
3477 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003478 DCHECK(!codegen_->IsLeafMethod());
3479}
3480
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003481void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003482 LocationSummary* locations =
3483 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003484 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3485 if (location.IsStackSlot()) {
3486 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3487 } else if (location.IsDoubleStackSlot()) {
3488 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003489 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003490 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003491}
3492
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003493void InstructionCodeGeneratorX86::VisitParameterValue(
3494 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3495}
3496
3497void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3498 LocationSummary* locations =
3499 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3500 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3501}
3502
3503void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003504}
3505
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003506void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003507 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003508 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003509 locations->SetInAt(0, Location::RequiresRegister());
3510 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003511}
3512
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003513void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3514 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003515 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003516 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003517 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003518 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003519 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003520 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003521 break;
3522
3523 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003524 __ notl(out.AsRegisterPairLow<Register>());
3525 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003526 break;
3527
3528 default:
3529 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3530 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003531}
3532
David Brazdil66d126e2015-04-03 16:02:44 +01003533void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3534 LocationSummary* locations =
3535 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3536 locations->SetInAt(0, Location::RequiresRegister());
3537 locations->SetOut(Location::SameAsFirstInput());
3538}
3539
3540void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003541 LocationSummary* locations = bool_not->GetLocations();
3542 Location in = locations->InAt(0);
3543 Location out = locations->Out();
3544 DCHECK(in.Equals(out));
3545 __ xorl(out.AsRegister<Register>(), Immediate(1));
3546}
3547
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003548void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003549 LocationSummary* locations =
3550 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003551 switch (compare->InputAt(0)->GetType()) {
3552 case Primitive::kPrimLong: {
3553 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003554 locations->SetInAt(1, Location::Any());
3555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3556 break;
3557 }
3558 case Primitive::kPrimFloat:
3559 case Primitive::kPrimDouble: {
3560 locations->SetInAt(0, Location::RequiresFpuRegister());
3561 locations->SetInAt(1, Location::RequiresFpuRegister());
3562 locations->SetOut(Location::RequiresRegister());
3563 break;
3564 }
3565 default:
3566 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3567 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003568}
3569
3570void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003571 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003573 Location left = locations->InAt(0);
3574 Location right = locations->InAt(1);
3575
Mark Mendell0c9497d2015-08-21 09:30:05 -04003576 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003577 switch (compare->InputAt(0)->GetType()) {
3578 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003579 Register left_low = left.AsRegisterPairLow<Register>();
3580 Register left_high = left.AsRegisterPairHigh<Register>();
3581 int32_t val_low = 0;
3582 int32_t val_high = 0;
3583 bool right_is_const = false;
3584
3585 if (right.IsConstant()) {
3586 DCHECK(right.GetConstant()->IsLongConstant());
3587 right_is_const = true;
3588 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3589 val_low = Low32Bits(val);
3590 val_high = High32Bits(val);
3591 }
3592
Calin Juravleddb7df22014-11-25 20:56:51 +00003593 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003594 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003595 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003596 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003597 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003598 DCHECK(right_is_const) << right;
3599 if (val_high == 0) {
3600 __ testl(left_high, left_high);
3601 } else {
3602 __ cmpl(left_high, Immediate(val_high));
3603 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003604 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003605 __ j(kLess, &less); // Signed compare.
3606 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003607 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003608 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003609 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003610 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003611 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003612 DCHECK(right_is_const) << right;
3613 if (val_low == 0) {
3614 __ testl(left_low, left_low);
3615 } else {
3616 __ cmpl(left_low, Immediate(val_low));
3617 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003618 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003619 break;
3620 }
3621 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003622 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003623 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3624 break;
3625 }
3626 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003628 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003629 break;
3630 }
3631 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003632 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003633 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003634 __ movl(out, Immediate(0));
3635 __ j(kEqual, &done);
3636 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3637
3638 __ Bind(&greater);
3639 __ movl(out, Immediate(1));
3640 __ jmp(&done);
3641
3642 __ Bind(&less);
3643 __ movl(out, Immediate(-1));
3644
3645 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003646}
3647
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003648void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003649 LocationSummary* locations =
3650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003651 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3652 locations->SetInAt(i, Location::Any());
3653 }
3654 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003655}
3656
3657void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003658 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003659 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003660}
3661
Calin Juravle52c48962014-12-16 17:02:57 +00003662void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3663 /*
3664 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3665 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3666 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3667 */
3668 switch (kind) {
3669 case MemBarrierKind::kAnyAny: {
3670 __ mfence();
3671 break;
3672 }
3673 case MemBarrierKind::kAnyStore:
3674 case MemBarrierKind::kLoadAny:
3675 case MemBarrierKind::kStoreStore: {
3676 // nop
3677 break;
3678 }
3679 default:
3680 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003681 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003682}
3683
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003684
Vladimir Marko58155012015-08-19 12:49:41 +00003685void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3686 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3687 switch (invoke->GetMethodLoadKind()) {
3688 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3689 // temp = thread->string_init_entrypoint
3690 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3691 break;
3692 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3693 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3694 break;
3695 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3696 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3697 break;
3698 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3699 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3700 method_patches_.emplace_back(invoke->GetTargetMethod());
3701 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3702 break;
3703 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3704 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3705 FALLTHROUGH_INTENDED;
3706 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3707 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3708 Register method_reg;
3709 Register reg = temp.AsRegister<Register>();
3710 if (current_method.IsRegister()) {
3711 method_reg = current_method.AsRegister<Register>();
3712 } else {
3713 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3714 DCHECK(!current_method.IsValid());
3715 method_reg = reg;
3716 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3717 }
3718 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003719 __ movl(reg, Address(method_reg,
3720 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003721 // temp = temp[index_in_cache]
3722 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3723 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3724 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003725 }
Vladimir Marko58155012015-08-19 12:49:41 +00003726 }
3727
3728 switch (invoke->GetCodePtrLocation()) {
3729 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3730 __ call(GetFrameEntryLabel());
3731 break;
3732 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3733 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3734 Label* label = &relative_call_patches_.back().label;
3735 __ call(label); // Bind to the patch label, override at link time.
3736 __ Bind(label); // Bind the label at the end of the "call" insn.
3737 break;
3738 }
3739 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3740 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3741 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3742 // (Though the direct CALL ptr16:32 is available for consideration).
3743 FALLTHROUGH_INTENDED;
3744 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3745 // (callee_method + offset_of_quick_compiled_code)()
3746 __ call(Address(callee_method.AsRegister<Register>(),
3747 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3748 kX86WordSize).Int32Value()));
3749 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003750 }
3751
3752 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003753}
3754
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003755void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3756 Register temp = temp_in.AsRegister<Register>();
3757 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3758 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3759 LocationSummary* locations = invoke->GetLocations();
3760 Location receiver = locations->InAt(0);
3761 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3762 // temp = object->GetClass();
3763 DCHECK(receiver.IsRegister());
3764 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3765 MaybeRecordImplicitNullCheck(invoke);
3766 __ MaybeUnpoisonHeapReference(temp);
3767 // temp = temp->GetMethodAt(method_offset);
3768 __ movl(temp, Address(temp, method_offset));
3769 // call temp->GetEntryPoint();
3770 __ call(Address(
3771 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3772}
3773
Vladimir Marko58155012015-08-19 12:49:41 +00003774void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3775 DCHECK(linker_patches->empty());
3776 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3777 for (const MethodPatchInfo<Label>& info : method_patches_) {
3778 // The label points to the end of the "movl" insn but the literal offset for method
3779 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3780 uint32_t literal_offset = info.label.Position() - 4;
3781 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3782 info.target_method.dex_file,
3783 info.target_method.dex_method_index));
3784 }
3785 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3786 // The label points to the end of the "call" insn but the literal offset for method
3787 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3788 uint32_t literal_offset = info.label.Position() - 4;
3789 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3790 info.target_method.dex_file,
3791 info.target_method.dex_method_index));
3792 }
3793}
3794
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003795void CodeGeneratorX86::MarkGCCard(Register temp,
3796 Register card,
3797 Register object,
3798 Register value,
3799 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003800 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003801 if (value_can_be_null) {
3802 __ testl(value, value);
3803 __ j(kEqual, &is_null);
3804 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3806 __ movl(temp, object);
3807 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003808 __ movb(Address(temp, card, TIMES_1, 0),
3809 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003810 if (value_can_be_null) {
3811 __ Bind(&is_null);
3812 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003813}
3814
Calin Juravle52c48962014-12-16 17:02:57 +00003815void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3816 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003817 LocationSummary* locations =
3818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003819 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003820
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003821 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3822 locations->SetOut(Location::RequiresFpuRegister());
3823 } else {
3824 // The output overlaps in case of long: we don't want the low move to overwrite
3825 // the object's location.
3826 locations->SetOut(Location::RequiresRegister(),
3827 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3828 : Location::kNoOutputOverlap);
3829 }
Calin Juravle52c48962014-12-16 17:02:57 +00003830
3831 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3832 // Long values can be loaded atomically into an XMM using movsd.
3833 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3834 // and then copy the XMM into the output 32bits at a time).
3835 locations->AddTemp(Location::RequiresFpuRegister());
3836 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003837}
3838
Calin Juravle52c48962014-12-16 17:02:57 +00003839void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3840 const FieldInfo& field_info) {
3841 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003842
Calin Juravle52c48962014-12-16 17:02:57 +00003843 LocationSummary* locations = instruction->GetLocations();
3844 Register base = locations->InAt(0).AsRegister<Register>();
3845 Location out = locations->Out();
3846 bool is_volatile = field_info.IsVolatile();
3847 Primitive::Type field_type = field_info.GetFieldType();
3848 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3849
3850 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003851 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003852 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853 break;
3854 }
3855
3856 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003857 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858 break;
3859 }
3860
3861 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003862 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003863 break;
3864 }
3865
3866 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003867 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003868 break;
3869 }
3870
3871 case Primitive::kPrimInt:
3872 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003873 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003874 break;
3875 }
3876
3877 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003878 if (is_volatile) {
3879 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3880 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003882 __ movd(out.AsRegisterPairLow<Register>(), temp);
3883 __ psrlq(temp, Immediate(32));
3884 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3885 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003886 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003887 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003888 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003889 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3890 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003891 break;
3892 }
3893
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003894 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003895 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003896 break;
3897 }
3898
3899 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003900 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003901 break;
3902 }
3903
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003904 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003905 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003906 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003907 }
Calin Juravle52c48962014-12-16 17:02:57 +00003908
Calin Juravle77520bc2015-01-12 18:45:46 +00003909 // Longs are handled in the switch.
3910 if (field_type != Primitive::kPrimLong) {
3911 codegen_->MaybeRecordImplicitNullCheck(instruction);
3912 }
3913
Calin Juravle52c48962014-12-16 17:02:57 +00003914 if (is_volatile) {
3915 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3916 }
Roland Levillain4d027112015-07-01 15:41:14 +01003917
3918 if (field_type == Primitive::kPrimNot) {
3919 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3920 }
Calin Juravle52c48962014-12-16 17:02:57 +00003921}
3922
3923void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3924 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3925
3926 LocationSummary* locations =
3927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3928 locations->SetInAt(0, Location::RequiresRegister());
3929 bool is_volatile = field_info.IsVolatile();
3930 Primitive::Type field_type = field_info.GetFieldType();
3931 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3932 || (field_type == Primitive::kPrimByte);
3933
3934 // The register allocator does not support multiple
3935 // inputs that die at entry with one in a specific register.
3936 if (is_byte_type) {
3937 // Ensure the value is in a byte register.
3938 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003939 } else if (Primitive::IsFloatingPointType(field_type)) {
3940 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003941 } else {
3942 locations->SetInAt(1, Location::RequiresRegister());
3943 }
Calin Juravle52c48962014-12-16 17:02:57 +00003944 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003945 // Temporary registers for the write barrier.
3946 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003947 // Ensure the card is in a byte register.
3948 locations->AddTemp(Location::RegisterLocation(ECX));
3949 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3950 // 64bits value can be atomically written to an address with movsd and an XMM register.
3951 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3952 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3953 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3954 // isolated cases when we need this it isn't worth adding the extra complexity.
3955 locations->AddTemp(Location::RequiresFpuRegister());
3956 locations->AddTemp(Location::RequiresFpuRegister());
3957 }
3958}
3959
3960void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003961 const FieldInfo& field_info,
3962 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003963 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3964
3965 LocationSummary* locations = instruction->GetLocations();
3966 Register base = locations->InAt(0).AsRegister<Register>();
3967 Location value = locations->InAt(1);
3968 bool is_volatile = field_info.IsVolatile();
3969 Primitive::Type field_type = field_info.GetFieldType();
3970 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003971 bool needs_write_barrier =
3972 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003973
3974 if (is_volatile) {
3975 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3976 }
3977
3978 switch (field_type) {
3979 case Primitive::kPrimBoolean:
3980 case Primitive::kPrimByte: {
3981 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3982 break;
3983 }
3984
3985 case Primitive::kPrimShort:
3986 case Primitive::kPrimChar: {
3987 __ movw(Address(base, offset), value.AsRegister<Register>());
3988 break;
3989 }
3990
3991 case Primitive::kPrimInt:
3992 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003993 if (kPoisonHeapReferences && needs_write_barrier) {
3994 // Note that in the case where `value` is a null reference,
3995 // we do not enter this block, as the reference does not
3996 // need poisoning.
3997 DCHECK_EQ(field_type, Primitive::kPrimNot);
3998 Register temp = locations->GetTemp(0).AsRegister<Register>();
3999 __ movl(temp, value.AsRegister<Register>());
4000 __ PoisonHeapReference(temp);
4001 __ movl(Address(base, offset), temp);
4002 } else {
4003 __ movl(Address(base, offset), value.AsRegister<Register>());
4004 }
Calin Juravle52c48962014-12-16 17:02:57 +00004005 break;
4006 }
4007
4008 case Primitive::kPrimLong: {
4009 if (is_volatile) {
4010 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4011 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4012 __ movd(temp1, value.AsRegisterPairLow<Register>());
4013 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4014 __ punpckldq(temp1, temp2);
4015 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004016 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004017 } else {
4018 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004019 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004020 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4021 }
4022 break;
4023 }
4024
4025 case Primitive::kPrimFloat: {
4026 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4027 break;
4028 }
4029
4030 case Primitive::kPrimDouble: {
4031 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4032 break;
4033 }
4034
4035 case Primitive::kPrimVoid:
4036 LOG(FATAL) << "Unreachable type " << field_type;
4037 UNREACHABLE();
4038 }
4039
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 // Longs are handled in the switch.
4041 if (field_type != Primitive::kPrimLong) {
4042 codegen_->MaybeRecordImplicitNullCheck(instruction);
4043 }
4044
Roland Levillain4d027112015-07-01 15:41:14 +01004045 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004046 Register temp = locations->GetTemp(0).AsRegister<Register>();
4047 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004048 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004049 }
4050
Calin Juravle52c48962014-12-16 17:02:57 +00004051 if (is_volatile) {
4052 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4053 }
4054}
4055
4056void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4057 HandleFieldGet(instruction, instruction->GetFieldInfo());
4058}
4059
4060void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4061 HandleFieldGet(instruction, instruction->GetFieldInfo());
4062}
4063
4064void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4065 HandleFieldSet(instruction, instruction->GetFieldInfo());
4066}
4067
4068void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004069 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004070}
4071
4072void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4073 HandleFieldSet(instruction, instruction->GetFieldInfo());
4074}
4075
4076void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004077 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004078}
4079
4080void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4081 HandleFieldGet(instruction, instruction->GetFieldInfo());
4082}
4083
4084void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4085 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004086}
4087
4088void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004089 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4090 ? LocationSummary::kCallOnSlowPath
4091 : LocationSummary::kNoCall;
4092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4093 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004094 ? Location::RequiresRegister()
4095 : Location::Any();
4096 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004097 if (instruction->HasUses()) {
4098 locations->SetOut(Location::SameAsFirstInput());
4099 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100}
4101
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004102void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004103 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4104 return;
4105 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004106 LocationSummary* locations = instruction->GetLocations();
4107 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004108
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004109 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4110 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4111}
4112
4113void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004114 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115 codegen_->AddSlowPath(slow_path);
4116
4117 LocationSummary* locations = instruction->GetLocations();
4118 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119
4120 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004121 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004122 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004123 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004124 } else {
4125 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004126 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004127 __ jmp(slow_path->GetEntryLabel());
4128 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129 }
4130 __ j(kEqual, slow_path->GetEntryLabel());
4131}
4132
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004133void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004134 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004135 GenerateImplicitNullCheck(instruction);
4136 } else {
4137 GenerateExplicitNullCheck(instruction);
4138 }
4139}
4140
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004142 LocationSummary* locations =
4143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004144 locations->SetInAt(0, Location::RequiresRegister());
4145 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004146 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4147 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4148 } else {
4149 // The output overlaps in case of long: we don't want the low move to overwrite
4150 // the array's location.
4151 locations->SetOut(Location::RequiresRegister(),
4152 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4153 : Location::kNoOutputOverlap);
4154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004155}
4156
4157void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4158 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004159 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004160 Location index = locations->InAt(1);
4161
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 Primitive::Type type = instruction->GetType();
4163 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004164 case Primitive::kPrimBoolean: {
4165 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004166 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004167 if (index.IsConstant()) {
4168 __ movzxb(out, Address(obj,
4169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4170 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004172 }
4173 break;
4174 }
4175
4176 case Primitive::kPrimByte: {
4177 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004178 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 if (index.IsConstant()) {
4180 __ movsxb(out, Address(obj,
4181 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4182 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 }
4185 break;
4186 }
4187
4188 case Primitive::kPrimShort: {
4189 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004190 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004191 if (index.IsConstant()) {
4192 __ movsxw(out, Address(obj,
4193 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4194 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004195 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004196 }
4197 break;
4198 }
4199
4200 case Primitive::kPrimChar: {
4201 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004202 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004203 if (index.IsConstant()) {
4204 __ movzxw(out, Address(obj,
4205 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4206 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004208 }
4209 break;
4210 }
4211
4212 case Primitive::kPrimInt:
4213 case Primitive::kPrimNot: {
4214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004216 if (index.IsConstant()) {
4217 __ movl(out, Address(obj,
4218 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4219 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004220 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221 }
4222 break;
4223 }
4224
4225 case Primitive::kPrimLong: {
4226 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004227 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004228 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229 if (index.IsConstant()) {
4230 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004231 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004233 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004234 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004235 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004236 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004238 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004239 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240 }
4241 break;
4242 }
4243
Mark Mendell7c8d0092015-01-26 11:21:33 -05004244 case Primitive::kPrimFloat: {
4245 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4246 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4247 if (index.IsConstant()) {
4248 __ movss(out, Address(obj,
4249 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4250 } else {
4251 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4252 }
4253 break;
4254 }
4255
4256 case Primitive::kPrimDouble: {
4257 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4258 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4259 if (index.IsConstant()) {
4260 __ movsd(out, Address(obj,
4261 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4262 } else {
4263 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4264 }
4265 break;
4266 }
4267
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004268 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004269 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004270 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004272
4273 if (type != Primitive::kPrimLong) {
4274 codegen_->MaybeRecordImplicitNullCheck(instruction);
4275 }
Roland Levillain4d027112015-07-01 15:41:14 +01004276
4277 if (type == Primitive::kPrimNot) {
4278 Register out = locations->Out().AsRegister<Register>();
4279 __ MaybeUnpoisonHeapReference(out);
4280 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281}
4282
4283void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004284 // This location builder might end up asking to up to four registers, which is
4285 // not currently possible for baseline. The situation in which we need four
4286 // registers cannot be met by baseline though, because it has not run any
4287 // optimization.
4288
Nicolas Geoffray39468442014-09-02 15:17:15 +01004289 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004290 bool needs_write_barrier =
4291 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4292
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004293 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004294
Nicolas Geoffray39468442014-09-02 15:17:15 +01004295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4296 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004297 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004298
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004299 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4300 || (value_type == Primitive::kPrimByte);
4301 // We need the inputs to be different than the output in case of long operation.
4302 // In case of a byte operation, the register allocator does not support multiple
4303 // inputs that die at entry with one in a specific register.
4304 locations->SetInAt(0, Location::RequiresRegister());
4305 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4306 if (is_byte_type) {
4307 // Ensure the value is in a byte register.
4308 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4309 } else if (Primitive::IsFloatingPointType(value_type)) {
4310 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004312 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4313 }
4314 if (needs_write_barrier) {
4315 // Temporary registers for the write barrier.
4316 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4317 // Ensure the card is in a byte register.
4318 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004319 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004320}
4321
4322void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4323 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004324 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004326 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004327 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004328 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4329 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4330 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4331 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004332 bool needs_write_barrier =
4333 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334
4335 switch (value_type) {
4336 case Primitive::kPrimBoolean:
4337 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004338 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4339 Address address = index.IsConstant()
4340 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4341 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4342 if (value.IsRegister()) {
4343 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004344 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004345 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004347 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348 break;
4349 }
4350
4351 case Primitive::kPrimShort:
4352 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004353 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4354 Address address = index.IsConstant()
4355 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4356 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4357 if (value.IsRegister()) {
4358 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004359 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004360 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004362 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 break;
4364 }
4365
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004366 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004367 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4368 Address address = index.IsConstant()
4369 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4370 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4371 if (!value.IsRegister()) {
4372 // Just setting null.
4373 DCHECK(instruction->InputAt(2)->IsNullConstant());
4374 DCHECK(value.IsConstant()) << value;
4375 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004377 DCHECK(!needs_write_barrier);
4378 DCHECK(!may_need_runtime_call);
4379 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004381
4382 DCHECK(needs_write_barrier);
4383 Register register_value = value.AsRegister<Register>();
4384 NearLabel done, not_null, do_put;
4385 SlowPathCode* slow_path = nullptr;
4386 Register temp = locations->GetTemp(0).AsRegister<Register>();
4387 if (may_need_runtime_call) {
4388 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4389 codegen_->AddSlowPath(slow_path);
4390 if (instruction->GetValueCanBeNull()) {
4391 __ testl(register_value, register_value);
4392 __ j(kNotEqual, &not_null);
4393 __ movl(address, Immediate(0));
4394 codegen_->MaybeRecordImplicitNullCheck(instruction);
4395 __ jmp(&done);
4396 __ Bind(&not_null);
4397 }
4398
4399 __ movl(temp, Address(array, class_offset));
4400 codegen_->MaybeRecordImplicitNullCheck(instruction);
4401 __ MaybeUnpoisonHeapReference(temp);
4402 __ movl(temp, Address(temp, component_offset));
4403 // No need to poison/unpoison, we're comparing two poisoned references.
4404 __ cmpl(temp, Address(register_value, class_offset));
4405 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4406 __ j(kEqual, &do_put);
4407 __ MaybeUnpoisonHeapReference(temp);
4408 __ movl(temp, Address(temp, super_offset));
4409 // No need to unpoison, we're comparing against null..
4410 __ testl(temp, temp);
4411 __ j(kNotEqual, slow_path->GetEntryLabel());
4412 __ Bind(&do_put);
4413 } else {
4414 __ j(kNotEqual, slow_path->GetEntryLabel());
4415 }
4416 }
4417
4418 if (kPoisonHeapReferences) {
4419 __ movl(temp, register_value);
4420 __ PoisonHeapReference(temp);
4421 __ movl(address, temp);
4422 } else {
4423 __ movl(address, register_value);
4424 }
4425 if (!may_need_runtime_call) {
4426 codegen_->MaybeRecordImplicitNullCheck(instruction);
4427 }
4428
4429 Register card = locations->GetTemp(1).AsRegister<Register>();
4430 codegen_->MarkGCCard(
4431 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4432 __ Bind(&done);
4433
4434 if (slow_path != nullptr) {
4435 __ Bind(slow_path->GetExitLabel());
4436 }
4437
4438 break;
4439 }
4440 case Primitive::kPrimInt: {
4441 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4442 Address address = index.IsConstant()
4443 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4444 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4445 if (value.IsRegister()) {
4446 __ movl(address, value.AsRegister<Register>());
4447 } else {
4448 DCHECK(value.IsConstant()) << value;
4449 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4450 __ movl(address, Immediate(v));
4451 }
4452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 break;
4454 }
4455
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 case Primitive::kPrimLong: {
4457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458 if (index.IsConstant()) {
4459 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004460 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004461 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004462 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004463 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004464 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004465 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004466 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004467 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004469 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004470 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004472 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004473 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004474 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004476 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004477 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004479 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004480 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004481 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004482 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004484 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004485 Immediate(High32Bits(val)));
4486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 }
4488 break;
4489 }
4490
Mark Mendell7c8d0092015-01-26 11:21:33 -05004491 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004492 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4493 Address address = index.IsConstant()
4494 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4495 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004496 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004497 __ movss(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004498 break;
4499 }
4500
4501 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004502 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4503 Address address = index.IsConstant()
4504 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4505 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004506 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004507 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004508 break;
4509 }
4510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511 case Primitive::kPrimVoid:
4512 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004513 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 }
4515}
4516
4517void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004519 locations->SetInAt(0, Location::RequiresRegister());
4520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004521}
4522
4523void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4524 LocationSummary* locations = instruction->GetLocations();
4525 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004526 Register obj = locations->InAt(0).AsRegister<Register>();
4527 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530}
4531
4532void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004533 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4534 ? LocationSummary::kCallOnSlowPath
4535 : LocationSummary::kNoCall;
4536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004537 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004538 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004539 if (instruction->HasUses()) {
4540 locations->SetOut(Location::SameAsFirstInput());
4541 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542}
4543
4544void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4545 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004546 Location index_loc = locations->InAt(0);
4547 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004548 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004549 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550
Mark Mendell99dbd682015-04-22 16:18:52 -04004551 if (length_loc.IsConstant()) {
4552 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4553 if (index_loc.IsConstant()) {
4554 // BCE will remove the bounds check if we are guarenteed to pass.
4555 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4556 if (index < 0 || index >= length) {
4557 codegen_->AddSlowPath(slow_path);
4558 __ jmp(slow_path->GetEntryLabel());
4559 } else {
4560 // Some optimization after BCE may have generated this, and we should not
4561 // generate a bounds check if it is a valid range.
4562 }
4563 return;
4564 }
4565
4566 // We have to reverse the jump condition because the length is the constant.
4567 Register index_reg = index_loc.AsRegister<Register>();
4568 __ cmpl(index_reg, Immediate(length));
4569 codegen_->AddSlowPath(slow_path);
4570 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004571 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004572 Register length = length_loc.AsRegister<Register>();
4573 if (index_loc.IsConstant()) {
4574 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4575 __ cmpl(length, Immediate(value));
4576 } else {
4577 __ cmpl(length, index_loc.AsRegister<Register>());
4578 }
4579 codegen_->AddSlowPath(slow_path);
4580 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582}
4583
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4585 temp->SetLocations(nullptr);
4586}
4587
4588void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4589 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004590 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591}
4592
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004593void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004594 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004595 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004596}
4597
4598void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004599 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4600}
4601
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004602void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4604}
4605
4606void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004607 HBasicBlock* block = instruction->GetBlock();
4608 if (block->GetLoopInformation() != nullptr) {
4609 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4610 // The back edge will generate the suspend check.
4611 return;
4612 }
4613 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4614 // The goto will generate the suspend check.
4615 return;
4616 }
4617 GenerateSuspendCheck(instruction, nullptr);
4618}
4619
4620void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4621 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004622 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004623 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4624 if (slow_path == nullptr) {
4625 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4626 instruction->SetSlowPath(slow_path);
4627 codegen_->AddSlowPath(slow_path);
4628 if (successor != nullptr) {
4629 DCHECK(successor->IsLoopHeader());
4630 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4631 }
4632 } else {
4633 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4634 }
4635
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004636 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004637 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004638 if (successor == nullptr) {
4639 __ j(kNotEqual, slow_path->GetEntryLabel());
4640 __ Bind(slow_path->GetReturnLabel());
4641 } else {
4642 __ j(kEqual, codegen_->GetLabelOf(successor));
4643 __ jmp(slow_path->GetEntryLabel());
4644 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004645}
4646
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004647X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4648 return codegen_->GetAssembler();
4649}
4650
Mark Mendell7c8d0092015-01-26 11:21:33 -05004651void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004652 ScratchRegisterScope ensure_scratch(
4653 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4654 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4655 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4656 __ movl(temp_reg, Address(ESP, src + stack_offset));
4657 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004658}
4659
4660void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004661 ScratchRegisterScope ensure_scratch(
4662 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4663 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4664 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4665 __ movl(temp_reg, Address(ESP, src + stack_offset));
4666 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4667 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4668 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004669}
4670
4671void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004672 DCHECK_LT(index, moves_.size());
4673 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004674 Location source = move->GetSource();
4675 Location destination = move->GetDestination();
4676
4677 if (source.IsRegister()) {
4678 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004679 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004680 } else {
4681 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004682 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004683 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004684 } else if (source.IsFpuRegister()) {
4685 if (destination.IsFpuRegister()) {
4686 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4687 } else if (destination.IsStackSlot()) {
4688 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4689 } else {
4690 DCHECK(destination.IsDoubleStackSlot());
4691 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4692 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004693 } else if (source.IsStackSlot()) {
4694 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004695 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004696 } else if (destination.IsFpuRegister()) {
4697 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004698 } else {
4699 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004700 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4701 }
4702 } else if (source.IsDoubleStackSlot()) {
4703 if (destination.IsFpuRegister()) {
4704 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4705 } else {
4706 DCHECK(destination.IsDoubleStackSlot()) << destination;
4707 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004708 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004709 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004710 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004711 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004712 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004713 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004714 if (value == 0) {
4715 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4716 } else {
4717 __ movl(destination.AsRegister<Register>(), Immediate(value));
4718 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004719 } else {
4720 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004721 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004722 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004723 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004724 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004725 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004726 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004727 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004728 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4729 if (value == 0) {
4730 // Easy handling of 0.0.
4731 __ xorps(dest, dest);
4732 } else {
4733 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004734 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4735 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4736 __ movl(temp, Immediate(value));
4737 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004738 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004739 } else {
4740 DCHECK(destination.IsStackSlot()) << destination;
4741 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4742 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004743 } else if (constant->IsLongConstant()) {
4744 int64_t value = constant->AsLongConstant()->GetValue();
4745 int32_t low_value = Low32Bits(value);
4746 int32_t high_value = High32Bits(value);
4747 Immediate low(low_value);
4748 Immediate high(high_value);
4749 if (destination.IsDoubleStackSlot()) {
4750 __ movl(Address(ESP, destination.GetStackIndex()), low);
4751 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4752 } else {
4753 __ movl(destination.AsRegisterPairLow<Register>(), low);
4754 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4755 }
4756 } else {
4757 DCHECK(constant->IsDoubleConstant());
4758 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004759 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004760 int32_t low_value = Low32Bits(value);
4761 int32_t high_value = High32Bits(value);
4762 Immediate low(low_value);
4763 Immediate high(high_value);
4764 if (destination.IsFpuRegister()) {
4765 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4766 if (value == 0) {
4767 // Easy handling of 0.0.
4768 __ xorpd(dest, dest);
4769 } else {
4770 __ pushl(high);
4771 __ pushl(low);
4772 __ movsd(dest, Address(ESP, 0));
4773 __ addl(ESP, Immediate(8));
4774 }
4775 } else {
4776 DCHECK(destination.IsDoubleStackSlot()) << destination;
4777 __ movl(Address(ESP, destination.GetStackIndex()), low);
4778 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4779 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004780 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004781 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004782 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004783 }
4784}
4785
Mark Mendella5c19ce2015-04-01 12:51:05 -04004786void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004787 Register suggested_scratch = reg == EAX ? EBX : EAX;
4788 ScratchRegisterScope ensure_scratch(
4789 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4790
4791 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4792 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4793 __ movl(Address(ESP, mem + stack_offset), reg);
4794 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004795}
4796
Mark Mendell7c8d0092015-01-26 11:21:33 -05004797void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004798 ScratchRegisterScope ensure_scratch(
4799 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4800
4801 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4802 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4803 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4804 __ movss(Address(ESP, mem + stack_offset), reg);
4805 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004806}
4807
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004808void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004809 ScratchRegisterScope ensure_scratch1(
4810 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004811
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004812 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4813 ScratchRegisterScope ensure_scratch2(
4814 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004815
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004816 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4817 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4818 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4819 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4820 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4821 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004822}
4823
4824void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004825 DCHECK_LT(index, moves_.size());
4826 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004827 Location source = move->GetSource();
4828 Location destination = move->GetDestination();
4829
4830 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004831 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4832 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4833 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4834 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4835 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004836 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004837 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004838 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004839 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004840 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4841 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004842 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4843 // Use XOR Swap algorithm to avoid a temporary.
4844 DCHECK_NE(source.reg(), destination.reg());
4845 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4846 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4847 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4848 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4849 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4850 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4851 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004852 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4853 // Take advantage of the 16 bytes in the XMM register.
4854 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4855 Address stack(ESP, destination.GetStackIndex());
4856 // Load the double into the high doubleword.
4857 __ movhpd(reg, stack);
4858
4859 // Store the low double into the destination.
4860 __ movsd(stack, reg);
4861
4862 // Move the high double to the low double.
4863 __ psrldq(reg, Immediate(8));
4864 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4865 // Take advantage of the 16 bytes in the XMM register.
4866 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4867 Address stack(ESP, source.GetStackIndex());
4868 // Load the double into the high doubleword.
4869 __ movhpd(reg, stack);
4870
4871 // Store the low double into the destination.
4872 __ movsd(stack, reg);
4873
4874 // Move the high double to the low double.
4875 __ psrldq(reg, Immediate(8));
4876 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4877 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4878 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004879 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004880 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004881 }
4882}
4883
4884void ParallelMoveResolverX86::SpillScratch(int reg) {
4885 __ pushl(static_cast<Register>(reg));
4886}
4887
4888void ParallelMoveResolverX86::RestoreScratch(int reg) {
4889 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004890}
4891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004892void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004893 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4894 ? LocationSummary::kCallOnSlowPath
4895 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004896 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004897 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004898 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004899 locations->SetOut(Location::RequiresRegister());
4900}
4901
4902void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004903 LocationSummary* locations = cls->GetLocations();
4904 Register out = locations->Out().AsRegister<Register>();
4905 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004906 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004907 DCHECK(!cls->CanCallRuntime());
4908 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004909 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004910 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004911 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004912 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004913 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004914 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004915 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004916
Andreas Gampe85b62f22015-09-09 13:15:38 -07004917 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004918 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4919 codegen_->AddSlowPath(slow_path);
4920 __ testl(out, out);
4921 __ j(kEqual, slow_path->GetEntryLabel());
4922 if (cls->MustGenerateClinitCheck()) {
4923 GenerateClassInitializationCheck(slow_path, out);
4924 } else {
4925 __ Bind(slow_path->GetExitLabel());
4926 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004927 }
4928}
4929
4930void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4931 LocationSummary* locations =
4932 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4933 locations->SetInAt(0, Location::RequiresRegister());
4934 if (check->HasUses()) {
4935 locations->SetOut(Location::SameAsFirstInput());
4936 }
4937}
4938
4939void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004940 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004941 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004942 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004943 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004944 GenerateClassInitializationCheck(slow_path,
4945 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004946}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004947
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004948void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004949 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004950 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4951 Immediate(mirror::Class::kStatusInitialized));
4952 __ j(kLess, slow_path->GetEntryLabel());
4953 __ Bind(slow_path->GetExitLabel());
4954 // No need for memory fence, thanks to the X86 memory model.
4955}
4956
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004957void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4958 LocationSummary* locations =
4959 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004960 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004961 locations->SetOut(Location::RequiresRegister());
4962}
4963
4964void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004965 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004966 codegen_->AddSlowPath(slow_path);
4967
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004968 LocationSummary* locations = load->GetLocations();
4969 Register out = locations->Out().AsRegister<Register>();
4970 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004971 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004972 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004973 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004974 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004975 __ testl(out, out);
4976 __ j(kEqual, slow_path->GetEntryLabel());
4977 __ Bind(slow_path->GetExitLabel());
4978}
4979
David Brazdilcb1c0552015-08-04 16:22:25 +01004980static Address GetExceptionTlsAddress() {
4981 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
4982}
4983
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004984void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4985 LocationSummary* locations =
4986 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4987 locations->SetOut(Location::RequiresRegister());
4988}
4989
4990void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004991 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
4992}
4993
4994void LocationsBuilderX86::VisitClearException(HClearException* clear) {
4995 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4996}
4997
4998void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4999 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005000}
5001
5002void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5003 LocationSummary* locations =
5004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5005 InvokeRuntimeCallingConvention calling_convention;
5006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5007}
5008
5009void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005010 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5011 instruction,
5012 instruction->GetDexPc(),
5013 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005014}
5015
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005016void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005017 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5018 switch (instruction->GetTypeCheckKind()) {
5019 case TypeCheckKind::kExactCheck:
5020 case TypeCheckKind::kAbstractClassCheck:
5021 case TypeCheckKind::kClassHierarchyCheck:
5022 case TypeCheckKind::kArrayObjectCheck:
5023 call_kind = LocationSummary::kNoCall;
5024 break;
5025 case TypeCheckKind::kInterfaceCheck:
5026 call_kind = LocationSummary::kCall;
5027 break;
5028 case TypeCheckKind::kArrayCheck:
5029 call_kind = LocationSummary::kCallOnSlowPath;
5030 break;
5031 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005033 if (call_kind != LocationSummary::kCall) {
5034 locations->SetInAt(0, Location::RequiresRegister());
5035 locations->SetInAt(1, Location::Any());
5036 // Note that TypeCheckSlowPathX86 uses this register too.
5037 locations->SetOut(Location::RequiresRegister());
5038 } else {
5039 InvokeRuntimeCallingConvention calling_convention;
5040 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5041 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5042 locations->SetOut(Location::RegisterLocation(EAX));
5043 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005044}
5045
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005046void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005047 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005048 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005049 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005050 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005051 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005052 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5053 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5054 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005055 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005056 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005057
5058 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005059 // Avoid null check if we know obj is not null.
5060 if (instruction->MustDoNullCheck()) {
5061 __ testl(obj, obj);
5062 __ j(kEqual, &zero);
5063 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005064
5065 // In case of an interface check, we put the object class into the object register.
5066 // This is safe, as the register is caller-save, and the object must be in another
5067 // register if it survives the runtime call.
5068 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
5069 ? obj
5070 : out;
5071 __ movl(target, Address(obj, class_offset));
5072 __ MaybeUnpoisonHeapReference(target);
5073
5074 switch (instruction->GetTypeCheckKind()) {
5075 case TypeCheckKind::kExactCheck: {
5076 if (cls.IsRegister()) {
5077 __ cmpl(out, cls.AsRegister<Register>());
5078 } else {
5079 DCHECK(cls.IsStackSlot()) << cls;
5080 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5081 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005082
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005083 // Classes must be equal for the instanceof to succeed.
5084 __ j(kNotEqual, &zero);
5085 __ movl(out, Immediate(1));
5086 __ jmp(&done);
5087 break;
5088 }
5089 case TypeCheckKind::kAbstractClassCheck: {
5090 // If the class is abstract, we eagerly fetch the super class of the
5091 // object to avoid doing a comparison we know will fail.
5092 NearLabel loop;
5093 __ Bind(&loop);
5094 __ movl(out, Address(out, super_offset));
5095 __ MaybeUnpoisonHeapReference(out);
5096 __ testl(out, out);
5097 // If `out` is null, we use it for the result, and jump to `done`.
5098 __ j(kEqual, &done);
5099 if (cls.IsRegister()) {
5100 __ cmpl(out, cls.AsRegister<Register>());
5101 } else {
5102 DCHECK(cls.IsStackSlot()) << cls;
5103 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5104 }
5105 __ j(kNotEqual, &loop);
5106 __ movl(out, Immediate(1));
5107 if (zero.IsLinked()) {
5108 __ jmp(&done);
5109 }
5110 break;
5111 }
5112 case TypeCheckKind::kClassHierarchyCheck: {
5113 // Walk over the class hierarchy to find a match.
5114 NearLabel loop, success;
5115 __ Bind(&loop);
5116 if (cls.IsRegister()) {
5117 __ cmpl(out, cls.AsRegister<Register>());
5118 } else {
5119 DCHECK(cls.IsStackSlot()) << cls;
5120 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5121 }
5122 __ j(kEqual, &success);
5123 __ movl(out, Address(out, super_offset));
5124 __ MaybeUnpoisonHeapReference(out);
5125 __ testl(out, out);
5126 __ j(kNotEqual, &loop);
5127 // If `out` is null, we use it for the result, and jump to `done`.
5128 __ jmp(&done);
5129 __ Bind(&success);
5130 __ movl(out, Immediate(1));
5131 if (zero.IsLinked()) {
5132 __ jmp(&done);
5133 }
5134 break;
5135 }
5136 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005137 // Do an exact check.
5138 NearLabel exact_check;
5139 if (cls.IsRegister()) {
5140 __ cmpl(out, cls.AsRegister<Register>());
5141 } else {
5142 DCHECK(cls.IsStackSlot()) << cls;
5143 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5144 }
5145 __ j(kEqual, &exact_check);
5146 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005147 __ movl(out, Address(out, component_offset));
5148 __ MaybeUnpoisonHeapReference(out);
5149 __ testl(out, out);
5150 // If `out` is null, we use it for the result, and jump to `done`.
5151 __ j(kEqual, &done);
5152 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5153 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005154 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005155 __ movl(out, Immediate(1));
5156 __ jmp(&done);
5157 break;
5158 }
5159 case TypeCheckKind::kArrayCheck: {
5160 if (cls.IsRegister()) {
5161 __ cmpl(out, cls.AsRegister<Register>());
5162 } else {
5163 DCHECK(cls.IsStackSlot()) << cls;
5164 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5165 }
5166 DCHECK(locations->OnlyCallsOnSlowPath());
5167 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5168 instruction, /* is_fatal */ false);
5169 codegen_->AddSlowPath(slow_path);
5170 __ j(kNotEqual, slow_path->GetEntryLabel());
5171 __ movl(out, Immediate(1));
5172 if (zero.IsLinked()) {
5173 __ jmp(&done);
5174 }
5175 break;
5176 }
5177
5178 case TypeCheckKind::kInterfaceCheck:
5179 default: {
5180 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5181 instruction,
5182 instruction->GetDexPc(),
5183 nullptr);
5184 if (zero.IsLinked()) {
5185 __ jmp(&done);
5186 }
5187 break;
5188 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005189 }
5190
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005191 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005192 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005193 __ xorl(out, out);
5194 }
5195
5196 if (done.IsLinked()) {
5197 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005198 }
5199
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005200 if (slow_path != nullptr) {
5201 __ Bind(slow_path->GetExitLabel());
5202 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005203}
5204
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005205void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005206 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5207 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5208
5209 switch (instruction->GetTypeCheckKind()) {
5210 case TypeCheckKind::kExactCheck:
5211 case TypeCheckKind::kAbstractClassCheck:
5212 case TypeCheckKind::kClassHierarchyCheck:
5213 case TypeCheckKind::kArrayObjectCheck:
5214 call_kind = throws_into_catch
5215 ? LocationSummary::kCallOnSlowPath
5216 : LocationSummary::kNoCall;
5217 break;
5218 case TypeCheckKind::kInterfaceCheck:
5219 call_kind = LocationSummary::kCall;
5220 break;
5221 case TypeCheckKind::kArrayCheck:
5222 call_kind = LocationSummary::kCallOnSlowPath;
5223 break;
5224 }
5225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005227 instruction, call_kind);
5228 if (call_kind != LocationSummary::kCall) {
5229 locations->SetInAt(0, Location::RequiresRegister());
5230 locations->SetInAt(1, Location::Any());
5231 // Note that TypeCheckSlowPathX86 uses this register too.
5232 locations->AddTemp(Location::RequiresRegister());
5233 } else {
5234 InvokeRuntimeCallingConvention calling_convention;
5235 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5236 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5237 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005238}
5239
5240void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5241 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005243 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005244 Register temp = locations->WillCall()
5245 ? kNoRegister
5246 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5249 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5250 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5251 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5252 SlowPathCode* slow_path = nullptr;
5253
5254 if (!locations->WillCall()) {
5255 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5256 instruction, !locations->CanCall());
5257 codegen_->AddSlowPath(slow_path);
5258 }
5259
5260 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005261 // Avoid null check if we know obj is not null.
5262 if (instruction->MustDoNullCheck()) {
5263 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005264 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005265 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005266
5267 if (locations->WillCall()) {
5268 __ movl(obj, Address(obj, class_offset));
5269 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005270 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005271 __ movl(temp, Address(obj, class_offset));
5272 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005273 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005274
5275 switch (instruction->GetTypeCheckKind()) {
5276 case TypeCheckKind::kExactCheck:
5277 case TypeCheckKind::kArrayCheck: {
5278 if (cls.IsRegister()) {
5279 __ cmpl(temp, cls.AsRegister<Register>());
5280 } else {
5281 DCHECK(cls.IsStackSlot()) << cls;
5282 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5283 }
5284 // Jump to slow path for throwing the exception or doing a
5285 // more involved array check.
5286 __ j(kNotEqual, slow_path->GetEntryLabel());
5287 break;
5288 }
5289 case TypeCheckKind::kAbstractClassCheck: {
5290 // If the class is abstract, we eagerly fetch the super class of the
5291 // object to avoid doing a comparison we know will fail.
5292 NearLabel loop, success;
5293 __ Bind(&loop);
5294 __ movl(temp, Address(temp, super_offset));
5295 __ MaybeUnpoisonHeapReference(temp);
5296 __ testl(temp, temp);
5297 // Jump to the slow path to throw the exception.
5298 __ j(kEqual, slow_path->GetEntryLabel());
5299 if (cls.IsRegister()) {
5300 __ cmpl(temp, cls.AsRegister<Register>());
5301 } else {
5302 DCHECK(cls.IsStackSlot()) << cls;
5303 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5304 }
5305 __ j(kNotEqual, &loop);
5306 break;
5307 }
5308 case TypeCheckKind::kClassHierarchyCheck: {
5309 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005310 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005311 __ Bind(&loop);
5312 if (cls.IsRegister()) {
5313 __ cmpl(temp, cls.AsRegister<Register>());
5314 } else {
5315 DCHECK(cls.IsStackSlot()) << cls;
5316 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5317 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005318 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005319 __ movl(temp, Address(temp, super_offset));
5320 __ MaybeUnpoisonHeapReference(temp);
5321 __ testl(temp, temp);
5322 __ j(kNotEqual, &loop);
5323 // Jump to the slow path to throw the exception.
5324 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005325 break;
5326 }
5327 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005328 // Do an exact check.
5329 if (cls.IsRegister()) {
5330 __ cmpl(temp, cls.AsRegister<Register>());
5331 } else {
5332 DCHECK(cls.IsStackSlot()) << cls;
5333 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5334 }
5335 __ j(kEqual, &done);
5336 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005337 __ movl(temp, Address(temp, component_offset));
5338 __ MaybeUnpoisonHeapReference(temp);
5339 __ testl(temp, temp);
5340 __ j(kEqual, slow_path->GetEntryLabel());
5341 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5342 __ j(kNotEqual, slow_path->GetEntryLabel());
5343 break;
5344 }
5345 case TypeCheckKind::kInterfaceCheck:
5346 default:
5347 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5348 instruction,
5349 instruction->GetDexPc(),
5350 nullptr);
5351 break;
5352 }
5353 __ Bind(&done);
5354
5355 if (slow_path != nullptr) {
5356 __ Bind(slow_path->GetExitLabel());
5357 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005358}
5359
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005360void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5361 LocationSummary* locations =
5362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5363 InvokeRuntimeCallingConvention calling_convention;
5364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5365}
5366
5367void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005368 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5369 : QUICK_ENTRY_POINT(pUnlockObject),
5370 instruction,
5371 instruction->GetDexPc(),
5372 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005373}
5374
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005375void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5376void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5377void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5378
5379void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5380 LocationSummary* locations =
5381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5382 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5383 || instruction->GetResultType() == Primitive::kPrimLong);
5384 locations->SetInAt(0, Location::RequiresRegister());
5385 locations->SetInAt(1, Location::Any());
5386 locations->SetOut(Location::SameAsFirstInput());
5387}
5388
5389void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5390 HandleBitwiseOperation(instruction);
5391}
5392
5393void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5394 HandleBitwiseOperation(instruction);
5395}
5396
5397void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5398 HandleBitwiseOperation(instruction);
5399}
5400
5401void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5402 LocationSummary* locations = instruction->GetLocations();
5403 Location first = locations->InAt(0);
5404 Location second = locations->InAt(1);
5405 DCHECK(first.Equals(locations->Out()));
5406
5407 if (instruction->GetResultType() == Primitive::kPrimInt) {
5408 if (second.IsRegister()) {
5409 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005410 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005411 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005412 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005413 } else {
5414 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005415 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005416 }
5417 } else if (second.IsConstant()) {
5418 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005419 __ andl(first.AsRegister<Register>(),
5420 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005421 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005422 __ orl(first.AsRegister<Register>(),
5423 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005424 } else {
5425 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005426 __ xorl(first.AsRegister<Register>(),
5427 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005428 }
5429 } else {
5430 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005431 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005433 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005434 } else {
5435 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005436 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005437 }
5438 }
5439 } else {
5440 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5441 if (second.IsRegisterPair()) {
5442 if (instruction->IsAnd()) {
5443 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5444 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5445 } else if (instruction->IsOr()) {
5446 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5447 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5448 } else {
5449 DCHECK(instruction->IsXor());
5450 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5451 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5452 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005453 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005454 if (instruction->IsAnd()) {
5455 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5456 __ andl(first.AsRegisterPairHigh<Register>(),
5457 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5458 } else if (instruction->IsOr()) {
5459 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5460 __ orl(first.AsRegisterPairHigh<Register>(),
5461 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5462 } else {
5463 DCHECK(instruction->IsXor());
5464 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5465 __ xorl(first.AsRegisterPairHigh<Register>(),
5466 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5467 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005468 } else {
5469 DCHECK(second.IsConstant()) << second;
5470 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005471 int32_t low_value = Low32Bits(value);
5472 int32_t high_value = High32Bits(value);
5473 Immediate low(low_value);
5474 Immediate high(high_value);
5475 Register first_low = first.AsRegisterPairLow<Register>();
5476 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005477 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005478 if (low_value == 0) {
5479 __ xorl(first_low, first_low);
5480 } else if (low_value != -1) {
5481 __ andl(first_low, low);
5482 }
5483 if (high_value == 0) {
5484 __ xorl(first_high, first_high);
5485 } else if (high_value != -1) {
5486 __ andl(first_high, high);
5487 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005488 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005489 if (low_value != 0) {
5490 __ orl(first_low, low);
5491 }
5492 if (high_value != 0) {
5493 __ orl(first_high, high);
5494 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005495 } else {
5496 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005497 if (low_value != 0) {
5498 __ xorl(first_low, low);
5499 }
5500 if (high_value != 0) {
5501 __ xorl(first_high, high);
5502 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005503 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005504 }
5505 }
5506}
5507
Calin Juravleb1498f62015-02-16 13:13:29 +00005508void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5509 // Nothing to do, this should be removed during prepare for register allocator.
5510 UNUSED(instruction);
5511 LOG(FATAL) << "Unreachable";
5512}
5513
5514void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5515 // Nothing to do, this should be removed during prepare for register allocator.
5516 UNUSED(instruction);
5517 LOG(FATAL) << "Unreachable";
5518}
5519
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005520void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5521 DCHECK(codegen_->IsBaseline());
5522 LocationSummary* locations =
5523 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5524 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5525}
5526
5527void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5528 DCHECK(codegen_->IsBaseline());
5529 // Will be generated at use site.
5530}
5531
Mark Mendellfe57faa2015-09-18 09:26:15 -04005532// Simple implementation of packed switch - generate cascaded compare/jumps.
5533void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5534 LocationSummary* locations =
5535 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5536 locations->SetInAt(0, Location::RequiresRegister());
5537}
5538
5539void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5540 int32_t lower_bound = switch_instr->GetStartValue();
5541 int32_t num_entries = switch_instr->GetNumEntries();
5542 LocationSummary* locations = switch_instr->GetLocations();
5543 Register value_reg = locations->InAt(0).AsRegister<Register>();
5544 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5545
5546 // Create a series of compare/jumps.
5547 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5548 for (int i = 0; i < num_entries; i++) {
5549 int32_t case_value = lower_bound + i;
5550 if (case_value == 0) {
5551 __ testl(value_reg, value_reg);
5552 } else {
5553 __ cmpl(value_reg, Immediate(case_value));
5554 }
5555 __ j(kEqual, codegen_->GetLabelOf(successors.at(i)));
5556 }
5557
5558 // And the default for any other value.
5559 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5560 __ jmp(codegen_->GetLabelOf(default_block));
5561 }
5562}
5563
Mark Mendell0616ae02015-04-17 12:49:27 -04005564void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5565 HX86ComputeBaseMethodAddress* insn) {
5566 LocationSummary* locations =
5567 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5568 locations->SetOut(Location::RequiresRegister());
5569}
5570
5571void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5572 HX86ComputeBaseMethodAddress* insn) {
5573 LocationSummary* locations = insn->GetLocations();
5574 Register reg = locations->Out().AsRegister<Register>();
5575
5576 // Generate call to next instruction.
5577 Label next_instruction;
5578 __ call(&next_instruction);
5579 __ Bind(&next_instruction);
5580
5581 // Remember this offset for later use with constant area.
5582 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5583
5584 // Grab the return address off the stack.
5585 __ popl(reg);
5586}
5587
5588void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5589 HX86LoadFromConstantTable* insn) {
5590 LocationSummary* locations =
5591 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5592
5593 locations->SetInAt(0, Location::RequiresRegister());
5594 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5595
5596 // If we don't need to be materialized, we only need the inputs to be set.
5597 if (!insn->NeedsMaterialization()) {
5598 return;
5599 }
5600
5601 switch (insn->GetType()) {
5602 case Primitive::kPrimFloat:
5603 case Primitive::kPrimDouble:
5604 locations->SetOut(Location::RequiresFpuRegister());
5605 break;
5606
5607 case Primitive::kPrimInt:
5608 locations->SetOut(Location::RequiresRegister());
5609 break;
5610
5611 default:
5612 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5613 }
5614}
5615
5616void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5617 if (!insn->NeedsMaterialization()) {
5618 return;
5619 }
5620
5621 LocationSummary* locations = insn->GetLocations();
5622 Location out = locations->Out();
5623 Register const_area = locations->InAt(0).AsRegister<Register>();
5624 HConstant *value = insn->GetConstant();
5625
5626 switch (insn->GetType()) {
5627 case Primitive::kPrimFloat:
5628 __ movss(out.AsFpuRegister<XmmRegister>(),
5629 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5630 break;
5631
5632 case Primitive::kPrimDouble:
5633 __ movsd(out.AsFpuRegister<XmmRegister>(),
5634 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5635 break;
5636
5637 case Primitive::kPrimInt:
5638 __ movl(out.AsRegister<Register>(),
5639 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5640 break;
5641
5642 default:
5643 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5644 }
5645}
5646
5647void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5648 // Generate the constant area if needed.
5649 X86Assembler* assembler = GetAssembler();
5650 if (!assembler->IsConstantAreaEmpty()) {
5651 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5652 // byte values.
5653 assembler->Align(4, 0);
5654 constant_area_start_ = assembler->CodeSize();
5655 assembler->AddConstantArea();
5656 }
5657
5658 // And finish up.
5659 CodeGenerator::Finalize(allocator);
5660}
5661
5662/**
5663 * Class to handle late fixup of offsets into constant area.
5664 */
5665class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5666 public:
5667 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5668 : codegen_(codegen), offset_into_constant_area_(offset) {}
5669
5670 private:
5671 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5672 // Patch the correct offset for the instruction. The place to patch is the
5673 // last 4 bytes of the instruction.
5674 // The value to patch is the distance from the offset in the constant area
5675 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5676 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5677 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5678
5679 // Patch in the right value.
5680 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5681 }
5682
5683 const CodeGeneratorX86& codegen_;
5684
5685 // Location in constant area that the fixup refers to.
5686 int offset_into_constant_area_;
5687};
5688
5689Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5690 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5691 return Address(reg, kDummy32BitOffset, fixup);
5692}
5693
5694Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5695 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5696 return Address(reg, kDummy32BitOffset, fixup);
5697}
5698
5699Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5700 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5701 return Address(reg, kDummy32BitOffset, fixup);
5702}
5703
5704Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5705 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5706 return Address(reg, kDummy32BitOffset, fixup);
5707}
5708
5709/**
5710 * Finds instructions that need the constant area base as an input.
5711 */
5712class ConstantHandlerVisitor : public HGraphVisitor {
5713 public:
5714 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5715
5716 private:
5717 void VisitAdd(HAdd* add) OVERRIDE {
5718 BinaryFP(add);
5719 }
5720
5721 void VisitSub(HSub* sub) OVERRIDE {
5722 BinaryFP(sub);
5723 }
5724
5725 void VisitMul(HMul* mul) OVERRIDE {
5726 BinaryFP(mul);
5727 }
5728
5729 void VisitDiv(HDiv* div) OVERRIDE {
5730 BinaryFP(div);
5731 }
5732
5733 void VisitReturn(HReturn* ret) OVERRIDE {
5734 HConstant* value = ret->InputAt(0)->AsConstant();
5735 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5736 ReplaceInput(ret, value, 0, true);
5737 }
5738 }
5739
5740 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5741 HandleInvoke(invoke);
5742 }
5743
5744 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5745 HandleInvoke(invoke);
5746 }
5747
5748 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5749 HandleInvoke(invoke);
5750 }
5751
5752 void BinaryFP(HBinaryOperation* bin) {
5753 HConstant* rhs = bin->InputAt(1)->AsConstant();
5754 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5755 ReplaceInput(bin, rhs, 1, false);
5756 }
5757 }
5758
5759 void InitializeConstantAreaPointer(HInstruction* user) {
5760 // Ensure we only initialize the pointer once.
5761 if (base_ != nullptr) {
5762 return;
5763 }
5764
5765 HGraph* graph = GetGraph();
5766 HBasicBlock* entry = graph->GetEntryBlock();
5767 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5768 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5769 entry->InsertInstructionBefore(base_, insert_pos);
5770 DCHECK(base_ != nullptr);
5771 }
5772
5773 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5774 InitializeConstantAreaPointer(insn);
5775 HGraph* graph = GetGraph();
5776 HBasicBlock* block = insn->GetBlock();
5777 HX86LoadFromConstantTable* load_constant =
5778 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5779 block->InsertInstructionBefore(load_constant, insn);
5780 insn->ReplaceInput(load_constant, input_index);
5781 }
5782
5783 void HandleInvoke(HInvoke* invoke) {
5784 // Ensure that we can load FP arguments from the constant area.
5785 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5786 HConstant* input = invoke->InputAt(i)->AsConstant();
5787 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5788 ReplaceInput(invoke, input, i, true);
5789 }
5790 }
5791 }
5792
5793 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5794 // input to the HX86LoadFromConstantTable instructions.
5795 HX86ComputeBaseMethodAddress* base_;
5796};
5797
5798void ConstantAreaFixups::Run() {
5799 ConstantHandlerVisitor visitor(graph_);
5800 visitor.VisitInsertionOrder();
5801}
5802
Andreas Gampe85b62f22015-09-09 13:15:38 -07005803// TODO: target as memory.
5804void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5805 if (!target.IsValid()) {
5806 DCHECK(type == Primitive::kPrimVoid);
5807 return;
5808 }
5809
5810 DCHECK_NE(type, Primitive::kPrimVoid);
5811
5812 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
5813 if (target.Equals(return_loc)) {
5814 return;
5815 }
5816
5817 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5818 // with the else branch.
5819 if (type == Primitive::kPrimLong) {
5820 HParallelMove parallel_move(GetGraph()->GetArena());
5821 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
5822 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
5823 GetMoveResolver()->EmitNativeCode(&parallel_move);
5824 } else {
5825 // Let the parallel move resolver take care of all of this.
5826 HParallelMove parallel_move(GetGraph()->GetArena());
5827 parallel_move.AddMove(return_loc, target, type, nullptr);
5828 GetMoveResolver()->EmitNativeCode(&parallel_move);
5829 }
5830}
5831
Roland Levillain4d027112015-07-01 15:41:14 +01005832#undef __
5833
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005834} // namespace x86
5835} // namespace art