blob: 0df7e3b30af992951c659472d6c2c97e4fcb79a0 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Mark Mendell0616ae02015-04-17 12:49:27 -040022#include "constant_area_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040026#include "intrinsics.h"
27#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace x86 {
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010041static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042
Mark Mendell5f874182015-03-04 15:42:45 -050043static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010050#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Andreas Gampe85b62f22015-09-09 13:15:38 -070052class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055
Alexandre Rames2ed20af2015-03-06 13:55:35 +000056 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010057 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000059 if (instruction_->CanThrowIntoCatchBlock()) {
60 // Live registers will be restored in the catch block if caught.
61 SaveLiveRegisters(codegen, instruction_->GetLocations());
62 }
Alexandre Rames8158f282015-08-07 10:26:17 +010063 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
64 instruction_,
65 instruction_->GetDexPc(),
66 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 }
68
Alexandre Rames8158f282015-08-07 10:26:17 +010069 bool IsFatal() const OVERRIDE { return true; }
70
Alexandre Rames9931f312015-06-19 14:47:01 +010071 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
72
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
76};
77
Andreas Gampe85b62f22015-09-09 13:15:38 -070078class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
80 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
81
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010083 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000085 if (instruction_->CanThrowIntoCatchBlock()) {
86 // Live registers will be restored in the catch block if caught.
87 SaveLiveRegisters(codegen, instruction_->GetLocations());
88 }
Alexandre Rames8158f282015-08-07 10:26:17 +010089 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
90 instruction_,
91 instruction_->GetDexPc(),
92 this);
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
100 HDivZeroCheck* const instruction_;
101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
102};
103
Andreas Gampe85b62f22015-09-09 13:15:38 -0700104class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100106 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
Alexandre Rames9931f312015-06-19 14:47:01 +0100118 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
119
Calin Juravled0d48522014-11-04 16:40:20 +0000120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Andreas Gampe85b62f22015-09-09 13:15:38 -0700126class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000134 // We're moving two locations to locations that could overlap, so we need a parallel
135 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000136 if (instruction_->CanThrowIntoCatchBlock()) {
137 // Live registers will be restored in the catch block if caught.
138 SaveLiveRegisters(codegen, instruction_->GetLocations());
139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000141 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100142 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100144 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
147 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Alexandre Rames9931f312015-06-19 14:47:01 +0100156 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160
161 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
162};
163
Andreas Gampe85b62f22015-09-09 13:15:38 -0700164class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000166 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000169 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100170 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100173 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
174 instruction_,
175 instruction_->GetDexPc(),
176 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 if (successor_ == nullptr) {
179 __ jmp(GetReturnLabel());
180 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100181 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 }
184
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 Label* GetReturnLabel() {
186 DCHECK(successor_ == nullptr);
187 return &return_label_;
188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100190 HBasicBlock* GetSuccessor() const {
191 return successor_;
192 }
193
Alexandre Rames9931f312015-06-19 14:47:01 +0100194 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
195
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 private:
197 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 Label return_label_;
200
201 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
202};
203
Andreas Gampe85b62f22015-09-09 13:15:38 -0700204class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205 public:
206 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
207
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209 LocationSummary* locations = instruction_->GetLocations();
210 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
211
212 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
213 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000214 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215
216 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800217 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
219 instruction_,
220 instruction_->GetDexPc(),
221 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000224
225 __ jmp(GetExitLabel());
226 }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 private:
231 HLoadString* const instruction_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
234};
235
Andreas Gampe85b62f22015-09-09 13:15:38 -0700236class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 public:
238 LoadClassSlowPathX86(HLoadClass* cls,
239 HInstruction* at,
240 uint32_t dex_pc,
241 bool do_clinit)
242 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
243 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
244 }
245
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 LocationSummary* locations = at_->GetLocations();
248 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
249 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251
252 InvokeRuntimeCallingConvention calling_convention;
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100254 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
255 : QUICK_ENTRY_POINT(pInitializeType),
256 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 __ jmp(GetExitLabel());
267 }
268
Alexandre Rames9931f312015-06-19 14:47:01 +0100269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
270
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
275 // The instruction where this slow path is happening.
276 // (Might be the load class or an initialization check).
277 HInstruction* const at_;
278
279 // The dex PC of `at_`.
280 const uint32_t dex_pc_;
281
282 // Whether to initialize the class.
283 const bool do_clinit_;
284
285 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
286};
287
Andreas Gampe85b62f22015-09-09 13:15:38 -0700288class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000290 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
291 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100295 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
296 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 DCHECK(instruction_->IsCheckCast()
298 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
301 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302
303 if (instruction_->IsCheckCast()) {
304 // The codegen for the instruction overwrites `temp`, so put it back in place.
305 Register obj = locations->InAt(0).AsRegister<Register>();
306 Register temp = locations->GetTemp(0).AsRegister<Register>();
307 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
308 __ movl(temp, Address(obj, class_offset));
309 __ MaybeUnpoisonHeapReference(temp);
310 }
311
312 if (!is_fatal_) {
313 SaveLiveRegisters(codegen, locations);
314 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
316 // We're moving two locations to locations that could overlap, so we need a parallel
317 // move resolver.
318 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000319 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
325 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100328 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
329 instruction_,
330 instruction_->GetDexPc(),
331 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 } else {
333 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 __ jmp(GetExitLabel());
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100352
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000354 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Andreas Gampe85b62f22015-09-09 13:15:38 -0700360class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 public:
362 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
363 : instruction_(instruction) {}
364
365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100366 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100367 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 __ Bind(GetEntryLabel());
369 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
371 instruction_,
372 instruction_->GetDexPc(),
373 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
379 HInstruction* const instruction_;
380 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
381};
382
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100383class ArraySetSlowPathX86 : public SlowPathCode {
384 public:
385 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
386
387 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
388 LocationSummary* locations = instruction_->GetLocations();
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 InvokeRuntimeCallingConvention calling_convention;
393 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
394 parallel_move.AddMove(
395 locations->InAt(0),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 nullptr);
399 parallel_move.AddMove(
400 locations->InAt(1),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
402 Primitive::kPrimInt,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(2),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
407 Primitive::kPrimNot,
408 nullptr);
409 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
410
411 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
412 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
423 HInstruction* const instruction_;
424
425 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
426};
427
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100429#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430
Aart Bike9f37602015-10-09 11:15:55 -0700431inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700432 switch (cond) {
433 case kCondEQ: return kEqual;
434 case kCondNE: return kNotEqual;
435 case kCondLT: return kLess;
436 case kCondLE: return kLessEqual;
437 case kCondGT: return kGreater;
438 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700439 case kCondB: return kBelow;
440 case kCondBE: return kBelowEqual;
441 case kCondA: return kAbove;
442 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700443 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100444 LOG(FATAL) << "Unreachable";
445 UNREACHABLE();
446}
447
Aart Bike9f37602015-10-09 11:15:55 -0700448// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100449inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
450 switch (cond) {
451 case kCondEQ: return kEqual;
452 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700453 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100454 case kCondLT: return kBelow;
455 case kCondLE: return kBelowEqual;
456 case kCondGT: return kAbove;
457 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700458 // Unsigned remain unchanged.
459 case kCondB: return kBelow;
460 case kCondBE: return kBelowEqual;
461 case kCondA: return kAbove;
462 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100463 }
464 LOG(FATAL) << "Unreachable";
465 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700466}
467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100468void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100469 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470}
471
472void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100473 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474}
475
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100476size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
477 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
478 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100479}
480
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100481size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
482 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
483 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100484}
485
Mark Mendell7c8d0092015-01-26 11:21:33 -0500486size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
487 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
488 return GetFloatingPointSpillSlotSize();
489}
490
491size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
492 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
493 return GetFloatingPointSpillSlotSize();
494}
495
Calin Juravle175dc732015-08-25 15:42:32 +0100496void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
497 HInstruction* instruction,
498 uint32_t dex_pc,
499 SlowPathCode* slow_path) {
500 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
501 instruction,
502 dex_pc,
503 slow_path);
504}
505
506void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100507 HInstruction* instruction,
508 uint32_t dex_pc,
509 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100510 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100511 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100512 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100513}
514
Mark Mendellfb8d2792015-03-31 22:16:59 -0400515CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
516 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100517 const CompilerOptions& compiler_options,
518 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500519 : CodeGenerator(graph,
520 kNumberOfCpuRegisters,
521 kNumberOfXmmRegisters,
522 kNumberOfRegisterPairs,
523 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
524 arraysize(kCoreCalleeSaves))
525 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100526 0,
527 compiler_options,
528 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100529 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100531 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400532 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000533 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100534 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400535 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
536 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000537 // Use a fake return address register to mimic Quick.
538 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100539}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100541Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 switch (type) {
543 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100544 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545 X86ManagedRegister pair =
546 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100547 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
548 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100549 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
550 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100551 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 }
554
555 case Primitive::kPrimByte:
556 case Primitive::kPrimBoolean:
557 case Primitive::kPrimChar:
558 case Primitive::kPrimShort:
559 case Primitive::kPrimInt:
560 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100561 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100562 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100563 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100564 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
565 X86ManagedRegister current =
566 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
567 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100568 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100569 }
570 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100572 }
573
574 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100576 return Location::FpuRegisterLocation(
577 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100579
580 case Primitive::kPrimVoid:
581 LOG(FATAL) << "Unreachable type " << type;
582 }
583
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100585}
586
Mark Mendell5f874182015-03-04 15:42:45 -0500587void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100589 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100590
591 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100592 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593
Mark Mendell5f874182015-03-04 15:42:45 -0500594 if (is_baseline) {
595 blocked_core_registers_[EBP] = true;
596 blocked_core_registers_[ESI] = true;
597 blocked_core_registers_[EDI] = true;
598 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100599
600 UpdateBlockedPairRegisters();
601}
602
603void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
604 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
605 X86ManagedRegister current =
606 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
607 if (blocked_core_registers_[current.AsRegisterPairLow()]
608 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
609 blocked_register_pairs_[i] = true;
610 }
611 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100612}
613
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100614InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
615 : HGraphVisitor(graph),
616 assembler_(codegen->GetAssembler()),
617 codegen_(codegen) {}
618
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100619static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100620 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100621}
622
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000623void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000625 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000626 bool skip_overflow_check =
627 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000628 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000629
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000630 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100631 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100632 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100633 }
634
Mark Mendell5f874182015-03-04 15:42:45 -0500635 if (HasEmptyFrame()) {
636 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000637 }
Mark Mendell5f874182015-03-04 15:42:45 -0500638
639 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
640 Register reg = kCoreCalleeSaves[i];
641 if (allocated_registers_.ContainsCoreRegister(reg)) {
642 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100643 __ cfi().AdjustCFAOffset(kX86WordSize);
644 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500645 }
646 }
647
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100648 int adjust = GetFrameSize() - FrameEntrySpillSize();
649 __ subl(ESP, Immediate(adjust));
650 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100651 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000652}
653
654void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100655 __ cfi().RememberState();
656 if (!HasEmptyFrame()) {
657 int adjust = GetFrameSize() - FrameEntrySpillSize();
658 __ addl(ESP, Immediate(adjust));
659 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500660
David Srbeckyc34dc932015-04-12 09:27:43 +0100661 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
662 Register reg = kCoreCalleeSaves[i];
663 if (allocated_registers_.ContainsCoreRegister(reg)) {
664 __ popl(reg);
665 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
666 __ cfi().Restore(DWARFReg(reg));
667 }
Mark Mendell5f874182015-03-04 15:42:45 -0500668 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000669 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100670 __ ret();
671 __ cfi().RestoreState();
672 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673}
674
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100675void CodeGeneratorX86::Bind(HBasicBlock* block) {
676 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000677}
678
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100679Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
680 switch (load->GetType()) {
681 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100683 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100684
685 case Primitive::kPrimInt:
686 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100687 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100689
690 case Primitive::kPrimBoolean:
691 case Primitive::kPrimByte:
692 case Primitive::kPrimChar:
693 case Primitive::kPrimShort:
694 case Primitive::kPrimVoid:
695 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700696 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100697 }
698
699 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700700 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100701}
702
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100703Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
704 switch (type) {
705 case Primitive::kPrimBoolean:
706 case Primitive::kPrimByte:
707 case Primitive::kPrimChar:
708 case Primitive::kPrimShort:
709 case Primitive::kPrimInt:
710 case Primitive::kPrimNot:
711 return Location::RegisterLocation(EAX);
712
713 case Primitive::kPrimLong:
714 return Location::RegisterPairLocation(EAX, EDX);
715
716 case Primitive::kPrimVoid:
717 return Location::NoLocation();
718
719 case Primitive::kPrimDouble:
720 case Primitive::kPrimFloat:
721 return Location::FpuRegisterLocation(XMM0);
722 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100723
724 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100725}
726
727Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
728 return Location::RegisterLocation(kMethodRegisterArgument);
729}
730
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100731Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100732 switch (type) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot: {
739 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000740 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100741 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100742 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100743 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000744 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100745 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100746 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100747
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000748 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100749 uint32_t index = gp_index_;
750 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000751 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100752 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100753 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
754 calling_convention.GetRegisterPairAt(index));
755 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100756 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000757 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
758 }
759 }
760
761 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100762 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000763 stack_index_++;
764 if (index < calling_convention.GetNumberOfFpuRegisters()) {
765 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
766 } else {
767 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
768 }
769 }
770
771 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100772 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000773 stack_index_ += 2;
774 if (index < calling_convention.GetNumberOfFpuRegisters()) {
775 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
776 } else {
777 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100778 }
779 }
780
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100781 case Primitive::kPrimVoid:
782 LOG(FATAL) << "Unexpected parameter type " << type;
783 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100784 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100785 return Location();
786}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788void CodeGeneratorX86::Move32(Location destination, Location source) {
789 if (source.Equals(destination)) {
790 return;
791 }
792 if (destination.IsRegister()) {
793 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000794 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000796 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 } else {
798 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 } else if (destination.IsFpuRegister()) {
802 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100804 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 } else {
807 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000808 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000811 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000813 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500816 } else if (source.IsConstant()) {
817 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000818 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500819 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 } else {
821 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100822 __ pushl(Address(ESP, source.GetStackIndex()));
823 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 }
825 }
826}
827
828void CodeGeneratorX86::Move64(Location destination, Location source) {
829 if (source.Equals(destination)) {
830 return;
831 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 if (destination.IsRegisterPair()) {
833 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000834 EmitParallelMoves(
835 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
836 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100837 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000838 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100839 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
840 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100841 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100842 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
843 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
844 __ psrlq(src_reg, Immediate(32));
845 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000847 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100849 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
850 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100851 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
852 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500854 if (source.IsFpuRegister()) {
855 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
856 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000857 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +0100858 } else if (source.IsRegisterPair()) {
859 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
860 // Create stack space for 2 elements.
861 __ subl(ESP, Immediate(2 * elem_size));
862 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
863 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
864 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
865 // And remove the temporary stack space we allocated.
866 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 } else {
868 LOG(FATAL) << "Unimplemented";
869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000871 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100872 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000873 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100874 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100876 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000879 } else if (source.IsConstant()) {
880 HConstant* constant = source.GetConstant();
881 int64_t value;
882 if (constant->IsLongConstant()) {
883 value = constant->AsLongConstant()->GetValue();
884 } else {
885 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000886 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000887 }
888 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
889 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000891 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000892 EmitParallelMoves(
893 Location::StackSlot(source.GetStackIndex()),
894 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100895 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000896 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100897 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
898 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899 }
900 }
901}
902
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100903void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000904 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100905 if (instruction->IsCurrentMethod()) {
906 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
907 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000908 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100909 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000910 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000911 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
912 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000913 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000914 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000915 } else if (location.IsStackSlot()) {
916 __ movl(Address(ESP, location.GetStackIndex()), imm);
917 } else {
918 DCHECK(location.IsConstant());
919 DCHECK_EQ(location.GetConstant(), const_to_move);
920 }
921 } else if (const_to_move->IsLongConstant()) {
922 int64_t value = const_to_move->AsLongConstant()->GetValue();
923 if (location.IsRegisterPair()) {
924 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
925 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
926 } else if (location.IsDoubleStackSlot()) {
927 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000928 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
929 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000930 } else {
931 DCHECK(location.IsConstant());
932 DCHECK_EQ(location.GetConstant(), instruction);
933 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000935 } else if (instruction->IsTemporary()) {
936 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000937 if (temp_location.IsStackSlot()) {
938 Move32(location, temp_location);
939 } else {
940 DCHECK(temp_location.IsDoubleStackSlot());
941 Move64(location, temp_location);
942 }
Roland Levillain476df552014-10-09 17:51:36 +0100943 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100944 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 switch (instruction->GetType()) {
946 case Primitive::kPrimBoolean:
947 case Primitive::kPrimByte:
948 case Primitive::kPrimChar:
949 case Primitive::kPrimShort:
950 case Primitive::kPrimInt:
951 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 case Primitive::kPrimFloat:
953 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954 break;
955
956 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100957 case Primitive::kPrimDouble:
958 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 break;
960
961 default:
962 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
963 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100965 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966 switch (instruction->GetType()) {
967 case Primitive::kPrimBoolean:
968 case Primitive::kPrimByte:
969 case Primitive::kPrimChar:
970 case Primitive::kPrimShort:
971 case Primitive::kPrimInt:
972 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000974 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 break;
976
977 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100978 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 break;
981
982 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100984 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000985 }
986}
987
Calin Juravle175dc732015-08-25 15:42:32 +0100988void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
989 DCHECK(location.IsRegister());
990 __ movl(location.AsRegister<Register>(), Immediate(value));
991}
992
Calin Juravlee460d1d2015-09-29 04:52:17 +0100993void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
994 if (Primitive::Is64BitType(dst_type)) {
995 Move64(dst, src);
996 } else {
997 Move32(dst, src);
998 }
999}
1000
1001void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1002 if (location.IsRegister()) {
1003 locations->AddTemp(location);
1004 } else if (location.IsRegisterPair()) {
1005 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1006 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1007 } else {
1008 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1009 }
1010}
1011
David Brazdilfc6a86a2015-06-26 10:33:45 +00001012void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001013 DCHECK(!successor->IsExitBlock());
1014
1015 HBasicBlock* block = got->GetBlock();
1016 HInstruction* previous = got->GetPrevious();
1017
1018 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001019 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001020 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1021 return;
1022 }
1023
1024 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1025 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1026 }
1027 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001029 }
1030}
1031
David Brazdilfc6a86a2015-06-26 10:33:45 +00001032void LocationsBuilderX86::VisitGoto(HGoto* got) {
1033 got->SetLocations(nullptr);
1034}
1035
1036void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1037 HandleGoto(got, got->GetSuccessor());
1038}
1039
1040void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1041 try_boundary->SetLocations(nullptr);
1042}
1043
1044void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1045 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1046 if (!successor->IsExitBlock()) {
1047 HandleGoto(try_boundary, successor);
1048 }
1049}
1050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053}
1054
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001055void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056}
1057
Mark Mendellc4701932015-04-10 13:18:51 -04001058void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1059 Label* true_label,
1060 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001061 if (cond->IsFPConditionTrueIfNaN()) {
1062 __ j(kUnordered, true_label);
1063 } else if (cond->IsFPConditionFalseIfNaN()) {
1064 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001065 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001066 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001067}
1068
1069void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1070 Label* true_label,
1071 Label* false_label) {
1072 LocationSummary* locations = cond->GetLocations();
1073 Location left = locations->InAt(0);
1074 Location right = locations->InAt(1);
1075 IfCondition if_cond = cond->GetCondition();
1076
Mark Mendellc4701932015-04-10 13:18:51 -04001077 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001078 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001079 IfCondition true_high_cond = if_cond;
1080 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001081 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001082
1083 // Set the conditions for the test, remembering that == needs to be
1084 // decided using the low words.
1085 switch (if_cond) {
1086 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001087 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001089 break;
1090 case kCondLT:
1091 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001092 break;
1093 case kCondLE:
1094 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001095 break;
1096 case kCondGT:
1097 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001098 break;
1099 case kCondGE:
1100 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001101 break;
Aart Bike9f37602015-10-09 11:15:55 -07001102 case kCondB:
1103 false_high_cond = kCondA;
1104 break;
1105 case kCondBE:
1106 true_high_cond = kCondB;
1107 break;
1108 case kCondA:
1109 false_high_cond = kCondB;
1110 break;
1111 case kCondAE:
1112 true_high_cond = kCondA;
1113 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001114 }
1115
1116 if (right.IsConstant()) {
1117 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001118 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001119 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001120
1121 if (val_high == 0) {
1122 __ testl(left_high, left_high);
1123 } else {
1124 __ cmpl(left_high, Immediate(val_high));
1125 }
1126 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001127 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001128 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001129 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001130 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001131 __ j(X86Condition(true_high_cond), true_label);
1132 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001133 }
1134 // Must be equal high, so compare the lows.
1135 if (val_low == 0) {
1136 __ testl(left_low, left_low);
1137 } else {
1138 __ cmpl(left_low, Immediate(val_low));
1139 }
1140 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001141 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001142 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001143
1144 __ cmpl(left_high, right_high);
1145 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001146 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001147 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001148 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001149 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001150 __ j(X86Condition(true_high_cond), true_label);
1151 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001152 }
1153 // Must be equal high, so compare the lows.
1154 __ cmpl(left_low, right_low);
1155 }
1156 // The last comparison might be unsigned.
1157 __ j(final_condition, true_label);
1158}
1159
1160void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1161 HCondition* condition,
1162 Label* true_target,
1163 Label* false_target,
1164 Label* always_true_target) {
1165 LocationSummary* locations = condition->GetLocations();
1166 Location left = locations->InAt(0);
1167 Location right = locations->InAt(1);
1168
1169 // We don't want true_target as a nullptr.
1170 if (true_target == nullptr) {
1171 true_target = always_true_target;
1172 }
1173 bool falls_through = (false_target == nullptr);
1174
1175 // FP compares don't like null false_targets.
1176 if (false_target == nullptr) {
1177 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1178 }
1179
1180 Primitive::Type type = condition->InputAt(0)->GetType();
1181 switch (type) {
1182 case Primitive::kPrimLong:
1183 GenerateLongComparesAndJumps(condition, true_target, false_target);
1184 break;
1185 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001186 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1187 GenerateFPJumps(condition, true_target, false_target);
1188 break;
1189 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001190 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1191 GenerateFPJumps(condition, true_target, false_target);
1192 break;
1193 default:
1194 LOG(FATAL) << "Unexpected compare type " << type;
1195 }
1196
1197 if (!falls_through) {
1198 __ jmp(false_target);
1199 }
1200}
1201
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001202void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1203 Label* true_target,
1204 Label* false_target,
1205 Label* always_true_target) {
1206 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001207 if (cond->IsIntConstant()) {
1208 // Constant condition, statically compared against 1.
1209 int32_t cond_value = cond->AsIntConstant()->GetValue();
1210 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001211 if (always_true_target != nullptr) {
1212 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001213 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001214 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001216 DCHECK_EQ(cond_value, 0);
1217 }
1218 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001220 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1221 // Moves do not affect the eflags register, so if the condition is
1222 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001223 // again. We can't use the eflags on long/FP conditions if they are
1224 // materialized due to the complex branching.
1225 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001226 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001227 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1229 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001230 if (!eflags_set) {
1231 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001232 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001233 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001234 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001235 } else {
1236 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1237 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001238 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001239 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001240 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001241 }
1242 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001243 // Condition has not been materialized, use its inputs as the
1244 // comparison and its condition as the branch condition.
1245
Mark Mendellc4701932015-04-10 13:18:51 -04001246 // Is this a long or FP comparison that has been folded into the HCondition?
1247 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1248 // Generate the comparison directly.
1249 GenerateCompareTestAndBranch(instruction->AsIf(),
1250 cond->AsCondition(),
1251 true_target,
1252 false_target,
1253 always_true_target);
1254 return;
1255 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001256
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001257 Location lhs = cond->GetLocations()->InAt(0);
1258 Location rhs = cond->GetLocations()->InAt(1);
1259 // LHS is guaranteed to be in a register (see
1260 // LocationsBuilderX86::VisitCondition).
1261 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001262 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001263 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001264 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001265 if (constant == 0) {
1266 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1267 } else {
1268 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1269 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001270 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001271 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001272 }
Aart Bike9f37602015-10-09 11:15:55 -07001273 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001274 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001275 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001276 if (false_target != nullptr) {
1277 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278 }
1279}
1280
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001281void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1282 LocationSummary* locations =
1283 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1284 HInstruction* cond = if_instr->InputAt(0);
1285 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1286 locations->SetInAt(0, Location::Any());
1287 }
1288}
1289
1290void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1291 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1292 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1293 Label* always_true_target = true_target;
1294 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1295 if_instr->IfTrueSuccessor())) {
1296 always_true_target = nullptr;
1297 }
1298 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1299 if_instr->IfFalseSuccessor())) {
1300 false_target = nullptr;
1301 }
1302 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1303}
1304
1305void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1306 LocationSummary* locations = new (GetGraph()->GetArena())
1307 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1308 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001309 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001310 locations->SetInAt(0, Location::Any());
1311 }
1312}
1313
1314void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001315 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001316 DeoptimizationSlowPathX86(deoptimize);
1317 codegen_->AddSlowPath(slow_path);
1318 Label* slow_path_entry = slow_path->GetEntryLabel();
1319 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1320}
1321
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001323 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001324}
1325
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001326void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1327 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001328}
1329
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001330void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001331 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332}
1333
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001334void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001335 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001336}
1337
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001338void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001339 LocationSummary* locations =
1340 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001341 switch (store->InputAt(1)->GetType()) {
1342 case Primitive::kPrimBoolean:
1343 case Primitive::kPrimByte:
1344 case Primitive::kPrimChar:
1345 case Primitive::kPrimShort:
1346 case Primitive::kPrimInt:
1347 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001349 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1350 break;
1351
1352 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001353 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1355 break;
1356
1357 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001359 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001360}
1361
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001362void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001363}
1364
Roland Levillain0d37cd02015-05-27 16:39:19 +01001365void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001366 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001367 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001368 // Handle the long/FP comparisons made in instruction simplification.
1369 switch (cond->InputAt(0)->GetType()) {
1370 case Primitive::kPrimLong: {
1371 locations->SetInAt(0, Location::RequiresRegister());
1372 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1373 if (cond->NeedsMaterialization()) {
1374 locations->SetOut(Location::RequiresRegister());
1375 }
1376 break;
1377 }
1378 case Primitive::kPrimFloat:
1379 case Primitive::kPrimDouble: {
1380 locations->SetInAt(0, Location::RequiresFpuRegister());
1381 locations->SetInAt(1, Location::RequiresFpuRegister());
1382 if (cond->NeedsMaterialization()) {
1383 locations->SetOut(Location::RequiresRegister());
1384 }
1385 break;
1386 }
1387 default:
1388 locations->SetInAt(0, Location::RequiresRegister());
1389 locations->SetInAt(1, Location::Any());
1390 if (cond->NeedsMaterialization()) {
1391 // We need a byte register.
1392 locations->SetOut(Location::RegisterLocation(ECX));
1393 }
1394 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001395 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396}
1397
Roland Levillain0d37cd02015-05-27 16:39:19 +01001398void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001399 if (!cond->NeedsMaterialization()) {
1400 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001401 }
Mark Mendellc4701932015-04-10 13:18:51 -04001402
1403 LocationSummary* locations = cond->GetLocations();
1404 Location lhs = locations->InAt(0);
1405 Location rhs = locations->InAt(1);
1406 Register reg = locations->Out().AsRegister<Register>();
1407 Label true_label, false_label;
1408
1409 switch (cond->InputAt(0)->GetType()) {
1410 default: {
1411 // Integer case.
1412
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001413 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001414 __ xorl(reg, reg);
1415
1416 if (rhs.IsRegister()) {
1417 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1418 } else if (rhs.IsConstant()) {
1419 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1420 if (constant == 0) {
1421 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1422 } else {
1423 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1424 }
1425 } else {
1426 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1427 }
Aart Bike9f37602015-10-09 11:15:55 -07001428 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001429 return;
1430 }
1431 case Primitive::kPrimLong:
1432 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1433 break;
1434 case Primitive::kPrimFloat:
1435 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1436 GenerateFPJumps(cond, &true_label, &false_label);
1437 break;
1438 case Primitive::kPrimDouble:
1439 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1440 GenerateFPJumps(cond, &true_label, &false_label);
1441 break;
1442 }
1443
1444 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001445 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001446
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001448 __ Bind(&false_label);
1449 __ xorl(reg, reg);
1450 __ jmp(&done_label);
1451
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001453 __ Bind(&true_label);
1454 __ movl(reg, Immediate(1));
1455 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001456}
1457
1458void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1459 VisitCondition(comp);
1460}
1461
1462void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1463 VisitCondition(comp);
1464}
1465
1466void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1467 VisitCondition(comp);
1468}
1469
1470void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1471 VisitCondition(comp);
1472}
1473
1474void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1475 VisitCondition(comp);
1476}
1477
1478void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1479 VisitCondition(comp);
1480}
1481
1482void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1483 VisitCondition(comp);
1484}
1485
1486void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1487 VisitCondition(comp);
1488}
1489
1490void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1491 VisitCondition(comp);
1492}
1493
1494void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1495 VisitCondition(comp);
1496}
1497
1498void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1499 VisitCondition(comp);
1500}
1501
1502void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1503 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504}
1505
Aart Bike9f37602015-10-09 11:15:55 -07001506void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1507 VisitCondition(comp);
1508}
1509
1510void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1511 VisitCondition(comp);
1512}
1513
1514void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1515 VisitCondition(comp);
1516}
1517
1518void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1519 VisitCondition(comp);
1520}
1521
1522void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1523 VisitCondition(comp);
1524}
1525
1526void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1527 VisitCondition(comp);
1528}
1529
1530void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1531 VisitCondition(comp);
1532}
1533
1534void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1535 VisitCondition(comp);
1536}
1537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001539 LocationSummary* locations =
1540 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001541 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542}
1543
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001544void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001545 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546}
1547
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001548void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1551 locations->SetOut(Location::ConstantLocation(constant));
1552}
1553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001554void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001555 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001556}
1557
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001561 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562}
1563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 // Will be generated at use site.
1566}
1567
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001568void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1571 locations->SetOut(Location::ConstantLocation(constant));
1572}
1573
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001574void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001575 // Will be generated at use site.
1576}
1577
1578void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1581 locations->SetOut(Location::ConstantLocation(constant));
1582}
1583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001584void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001585 // Will be generated at use site.
1586}
1587
Calin Juravle27df7582015-04-17 19:12:31 +01001588void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1589 memory_barrier->SetLocations(nullptr);
1590}
1591
1592void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1593 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1594}
1595
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001596void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001597 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001598}
1599
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001600void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001601 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001602}
1603
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 LocationSummary* locations =
1606 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001607 switch (ret->InputAt(0)->GetType()) {
1608 case Primitive::kPrimBoolean:
1609 case Primitive::kPrimByte:
1610 case Primitive::kPrimChar:
1611 case Primitive::kPrimShort:
1612 case Primitive::kPrimInt:
1613 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001614 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001615 break;
1616
1617 case Primitive::kPrimLong:
1618 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001619 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 break;
1621
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001622 case Primitive::kPrimFloat:
1623 case Primitive::kPrimDouble:
1624 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001625 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001626 break;
1627
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001628 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001629 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001630 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001631}
1632
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001633void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001634 if (kIsDebugBuild) {
1635 switch (ret->InputAt(0)->GetType()) {
1636 case Primitive::kPrimBoolean:
1637 case Primitive::kPrimByte:
1638 case Primitive::kPrimChar:
1639 case Primitive::kPrimShort:
1640 case Primitive::kPrimInt:
1641 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643 break;
1644
1645 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001646 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1647 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648 break;
1649
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 case Primitive::kPrimFloat:
1651 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001653 break;
1654
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001655 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001656 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001657 }
1658 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001659 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660}
1661
Calin Juravle175dc732015-08-25 15:42:32 +01001662void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1663 // The trampoline uses the same calling convention as dex calling conventions,
1664 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1665 // the method_idx.
1666 HandleInvoke(invoke);
1667}
1668
1669void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1670 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1671}
1672
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001673void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001674 // When we do not run baseline, explicit clinit checks triggered by static
1675 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1676 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001677
Mark Mendellfb8d2792015-03-31 22:16:59 -04001678 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001679 if (intrinsic.TryDispatch(invoke)) {
1680 return;
1681 }
1682
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001683 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001684
1685 if (codegen_->IsBaseline()) {
1686 // Baseline does not have enough registers if the current method also
1687 // needs a register. We therefore do not require a register for it, and let
1688 // the code generation of the invoke handle it.
1689 LocationSummary* locations = invoke->GetLocations();
1690 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1691 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1692 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1693 }
1694 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001695}
1696
Mark Mendell09ed1a32015-03-25 08:30:06 -04001697static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1698 if (invoke->GetLocations()->Intrinsified()) {
1699 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1700 intrinsic.Dispatch(invoke);
1701 return true;
1702 }
1703 return false;
1704}
1705
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001706void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001707 // When we do not run baseline, explicit clinit checks triggered by static
1708 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1709 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001710
Mark Mendell09ed1a32015-03-25 08:30:06 -04001711 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1712 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001713 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001714
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001715 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001716 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001717 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001718 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001719}
1720
1721void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1722 HandleInvoke(invoke);
1723}
1724
1725void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001726 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001727 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001728}
1729
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001730void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001731 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1732 return;
1733 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001734
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001735 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001736 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001737 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001738}
1739
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001740void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1741 HandleInvoke(invoke);
1742 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001743 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001744}
1745
1746void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1747 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1750 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001751 LocationSummary* locations = invoke->GetLocations();
1752 Location receiver = locations->InAt(0);
1753 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1754
1755 // Set the hidden argument.
1756 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001758
1759 // temp = object->GetClass();
1760 if (receiver.IsStackSlot()) {
1761 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1762 __ movl(temp, Address(temp, class_offset));
1763 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001765 }
Roland Levillain4d027112015-07-01 15:41:14 +01001766 codegen_->MaybeRecordImplicitNullCheck(invoke);
1767 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001768 // temp = temp->GetImtEntryAt(method_offset);
1769 __ movl(temp, Address(temp, method_offset));
1770 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001771 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001772 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001773
1774 DCHECK(!codegen_->IsLeafMethod());
1775 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1776}
1777
Roland Levillain88cb1752014-10-20 16:36:47 +01001778void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1781 switch (neg->GetResultType()) {
1782 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001783 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001784 locations->SetInAt(0, Location::RequiresRegister());
1785 locations->SetOut(Location::SameAsFirstInput());
1786 break;
1787
Roland Levillain88cb1752014-10-20 16:36:47 +01001788 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001789 locations->SetInAt(0, Location::RequiresFpuRegister());
1790 locations->SetOut(Location::SameAsFirstInput());
1791 locations->AddTemp(Location::RequiresRegister());
1792 locations->AddTemp(Location::RequiresFpuRegister());
1793 break;
1794
Roland Levillain88cb1752014-10-20 16:36:47 +01001795 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001796 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001797 locations->SetOut(Location::SameAsFirstInput());
1798 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001799 break;
1800
1801 default:
1802 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1803 }
1804}
1805
1806void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1807 LocationSummary* locations = neg->GetLocations();
1808 Location out = locations->Out();
1809 Location in = locations->InAt(0);
1810 switch (neg->GetResultType()) {
1811 case Primitive::kPrimInt:
1812 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001813 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001814 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001815 break;
1816
1817 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001818 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001819 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001820 __ negl(out.AsRegisterPairLow<Register>());
1821 // Negation is similar to subtraction from zero. The least
1822 // significant byte triggers a borrow when it is different from
1823 // zero; to take it into account, add 1 to the most significant
1824 // byte if the carry flag (CF) is set to 1 after the first NEGL
1825 // operation.
1826 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1827 __ negl(out.AsRegisterPairHigh<Register>());
1828 break;
1829
Roland Levillain5368c212014-11-27 15:03:41 +00001830 case Primitive::kPrimFloat: {
1831 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001832 Register constant = locations->GetTemp(0).AsRegister<Register>();
1833 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001834 // Implement float negation with an exclusive or with value
1835 // 0x80000000 (mask for bit 31, representing the sign of a
1836 // single-precision floating-point number).
1837 __ movl(constant, Immediate(INT32_C(0x80000000)));
1838 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001839 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001840 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001841 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001842
Roland Levillain5368c212014-11-27 15:03:41 +00001843 case Primitive::kPrimDouble: {
1844 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001845 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001846 // Implement double negation with an exclusive or with value
1847 // 0x8000000000000000 (mask for bit 63, representing the sign of
1848 // a double-precision floating-point number).
1849 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001850 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001851 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001852 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001853
1854 default:
1855 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1856 }
1857}
1858
Roland Levillaindff1f282014-11-05 14:15:05 +00001859void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001860 Primitive::Type result_type = conversion->GetResultType();
1861 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001862 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001863
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001864 // The float-to-long and double-to-long type conversions rely on a
1865 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001866 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001867 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1868 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001869 ? LocationSummary::kCall
1870 : LocationSummary::kNoCall;
1871 LocationSummary* locations =
1872 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1873
David Brazdilb2bd1c52015-03-25 11:17:37 +00001874 // The Java language does not allow treating boolean as an integral type but
1875 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001876
Roland Levillaindff1f282014-11-05 14:15:05 +00001877 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001878 case Primitive::kPrimByte:
1879 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001880 case Primitive::kPrimBoolean:
1881 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001882 case Primitive::kPrimShort:
1883 case Primitive::kPrimInt:
1884 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001885 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001886 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1887 // Make the output overlap to please the register allocator. This greatly simplifies
1888 // the validation of the linear scan implementation
1889 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001890 break;
1891
1892 default:
1893 LOG(FATAL) << "Unexpected type conversion from " << input_type
1894 << " to " << result_type;
1895 }
1896 break;
1897
Roland Levillain01a8d712014-11-14 16:27:39 +00001898 case Primitive::kPrimShort:
1899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001900 case Primitive::kPrimBoolean:
1901 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001902 case Primitive::kPrimByte:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimChar:
1905 // Processing a Dex `int-to-short' instruction.
1906 locations->SetInAt(0, Location::Any());
1907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
1914 break;
1915
Roland Levillain946e1432014-11-11 17:35:19 +00001916 case Primitive::kPrimInt:
1917 switch (input_type) {
1918 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001919 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001920 locations->SetInAt(0, Location::Any());
1921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1922 break;
1923
1924 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001925 // Processing a Dex `float-to-int' instruction.
1926 locations->SetInAt(0, Location::RequiresFpuRegister());
1927 locations->SetOut(Location::RequiresRegister());
1928 locations->AddTemp(Location::RequiresFpuRegister());
1929 break;
1930
Roland Levillain946e1432014-11-11 17:35:19 +00001931 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001932 // Processing a Dex `double-to-int' instruction.
1933 locations->SetInAt(0, Location::RequiresFpuRegister());
1934 locations->SetOut(Location::RequiresRegister());
1935 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001936 break;
1937
1938 default:
1939 LOG(FATAL) << "Unexpected type conversion from " << input_type
1940 << " to " << result_type;
1941 }
1942 break;
1943
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 case Primitive::kPrimLong:
1945 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001946 case Primitive::kPrimBoolean:
1947 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001948 case Primitive::kPrimByte:
1949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001951 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001952 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001953 locations->SetInAt(0, Location::RegisterLocation(EAX));
1954 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1955 break;
1956
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001957 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001958 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001959 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001960 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001961 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1962 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1963
Vladimir Marko949c91f2015-01-27 10:48:44 +00001964 // The runtime helper puts the result in EAX, EDX.
1965 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001966 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001967 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001968
1969 default:
1970 LOG(FATAL) << "Unexpected type conversion from " << input_type
1971 << " to " << result_type;
1972 }
1973 break;
1974
Roland Levillain981e4542014-11-14 11:47:14 +00001975 case Primitive::kPrimChar:
1976 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001977 case Primitive::kPrimBoolean:
1978 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001979 case Primitive::kPrimByte:
1980 case Primitive::kPrimShort:
1981 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001982 // Processing a Dex `int-to-char' instruction.
1983 locations->SetInAt(0, Location::Any());
1984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1985 break;
1986
1987 default:
1988 LOG(FATAL) << "Unexpected type conversion from " << input_type
1989 << " to " << result_type;
1990 }
1991 break;
1992
Roland Levillaindff1f282014-11-05 14:15:05 +00001993 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001994 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001995 case Primitive::kPrimBoolean:
1996 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001997 case Primitive::kPrimByte:
1998 case Primitive::kPrimShort:
1999 case Primitive::kPrimInt:
2000 case Primitive::kPrimChar:
2001 // Processing a Dex `int-to-float' instruction.
2002 locations->SetInAt(0, Location::RequiresRegister());
2003 locations->SetOut(Location::RequiresFpuRegister());
2004 break;
2005
2006 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002007 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002008 locations->SetInAt(0, Location::Any());
2009 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002010 break;
2011
Roland Levillaincff13742014-11-17 14:32:17 +00002012 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002013 // Processing a Dex `double-to-float' instruction.
2014 locations->SetInAt(0, Location::RequiresFpuRegister());
2015 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002016 break;
2017
2018 default:
2019 LOG(FATAL) << "Unexpected type conversion from " << input_type
2020 << " to " << result_type;
2021 };
2022 break;
2023
Roland Levillaindff1f282014-11-05 14:15:05 +00002024 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002025 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002026 case Primitive::kPrimBoolean:
2027 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002028 case Primitive::kPrimByte:
2029 case Primitive::kPrimShort:
2030 case Primitive::kPrimInt:
2031 case Primitive::kPrimChar:
2032 // Processing a Dex `int-to-double' instruction.
2033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetOut(Location::RequiresFpuRegister());
2035 break;
2036
2037 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002038 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002039 locations->SetInAt(0, Location::Any());
2040 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002041 break;
2042
Roland Levillaincff13742014-11-17 14:32:17 +00002043 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002044 // Processing a Dex `float-to-double' instruction.
2045 locations->SetInAt(0, Location::RequiresFpuRegister());
2046 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unexpected type conversion from " << input_type
2051 << " to " << result_type;
2052 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected type conversion from " << input_type
2057 << " to " << result_type;
2058 }
2059}
2060
2061void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2062 LocationSummary* locations = conversion->GetLocations();
2063 Location out = locations->Out();
2064 Location in = locations->InAt(0);
2065 Primitive::Type result_type = conversion->GetResultType();
2066 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002067 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002068 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002069 case Primitive::kPrimByte:
2070 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002071 case Primitive::kPrimBoolean:
2072 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002073 case Primitive::kPrimShort:
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002076 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002077 if (in.IsRegister()) {
2078 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002079 } else {
2080 DCHECK(in.GetConstant()->IsIntConstant());
2081 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2082 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2083 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002084 break;
2085
2086 default:
2087 LOG(FATAL) << "Unexpected type conversion from " << input_type
2088 << " to " << result_type;
2089 }
2090 break;
2091
Roland Levillain01a8d712014-11-14 16:27:39 +00002092 case Primitive::kPrimShort:
2093 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002094 case Primitive::kPrimBoolean:
2095 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002096 case Primitive::kPrimByte:
2097 case Primitive::kPrimInt:
2098 case Primitive::kPrimChar:
2099 // Processing a Dex `int-to-short' instruction.
2100 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002102 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002104 } else {
2105 DCHECK(in.GetConstant()->IsIntConstant());
2106 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002108 }
2109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unexpected type conversion from " << input_type
2113 << " to " << result_type;
2114 }
2115 break;
2116
Roland Levillain946e1432014-11-11 17:35:19 +00002117 case Primitive::kPrimInt:
2118 switch (input_type) {
2119 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002120 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002121 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002123 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002125 } else {
2126 DCHECK(in.IsConstant());
2127 DCHECK(in.GetConstant()->IsLongConstant());
2128 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002130 }
2131 break;
2132
Roland Levillain3f8f9362014-12-02 17:45:01 +00002133 case Primitive::kPrimFloat: {
2134 // Processing a Dex `float-to-int' instruction.
2135 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2136 Register output = out.AsRegister<Register>();
2137 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002138 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002139
2140 __ movl(output, Immediate(kPrimIntMax));
2141 // temp = int-to-float(output)
2142 __ cvtsi2ss(temp, output);
2143 // if input >= temp goto done
2144 __ comiss(input, temp);
2145 __ j(kAboveEqual, &done);
2146 // if input == NaN goto nan
2147 __ j(kUnordered, &nan);
2148 // output = float-to-int-truncate(input)
2149 __ cvttss2si(output, input);
2150 __ jmp(&done);
2151 __ Bind(&nan);
2152 // output = 0
2153 __ xorl(output, output);
2154 __ Bind(&done);
2155 break;
2156 }
2157
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002158 case Primitive::kPrimDouble: {
2159 // Processing a Dex `double-to-int' instruction.
2160 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2161 Register output = out.AsRegister<Register>();
2162 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002163 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002164
2165 __ movl(output, Immediate(kPrimIntMax));
2166 // temp = int-to-double(output)
2167 __ cvtsi2sd(temp, output);
2168 // if input >= temp goto done
2169 __ comisd(input, temp);
2170 __ j(kAboveEqual, &done);
2171 // if input == NaN goto nan
2172 __ j(kUnordered, &nan);
2173 // output = double-to-int-truncate(input)
2174 __ cvttsd2si(output, input);
2175 __ jmp(&done);
2176 __ Bind(&nan);
2177 // output = 0
2178 __ xorl(output, output);
2179 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002180 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 }
Roland Levillain946e1432014-11-11 17:35:19 +00002182
2183 default:
2184 LOG(FATAL) << "Unexpected type conversion from " << input_type
2185 << " to " << result_type;
2186 }
2187 break;
2188
Roland Levillaindff1f282014-11-05 14:15:05 +00002189 case Primitive::kPrimLong:
2190 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002191 case Primitive::kPrimBoolean:
2192 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002193 case Primitive::kPrimByte:
2194 case Primitive::kPrimShort:
2195 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002196 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002197 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2199 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002200 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 __ cdq();
2202 break;
2203
2204 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002205 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002206 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2207 conversion,
2208 conversion->GetDexPc(),
2209 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002210 break;
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002213 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002214 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2215 conversion,
2216 conversion->GetDexPc(),
2217 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain981e4542014-11-14 11:47:14 +00002226 case Primitive::kPrimChar:
2227 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002228 case Primitive::kPrimBoolean:
2229 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002230 case Primitive::kPrimByte:
2231 case Primitive::kPrimShort:
2232 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002233 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2234 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002235 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002236 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002237 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002238 } else {
2239 DCHECK(in.GetConstant()->IsIntConstant());
2240 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002242 }
2243 break;
2244
2245 default:
2246 LOG(FATAL) << "Unexpected type conversion from " << input_type
2247 << " to " << result_type;
2248 }
2249 break;
2250
Roland Levillaindff1f282014-11-05 14:15:05 +00002251 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002252 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002253 case Primitive::kPrimBoolean:
2254 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002255 case Primitive::kPrimByte:
2256 case Primitive::kPrimShort:
2257 case Primitive::kPrimInt:
2258 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002259 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002260 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002261 break;
2262
Roland Levillain6d0e4832014-11-27 18:31:21 +00002263 case Primitive::kPrimLong: {
2264 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002265 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002266
Roland Levillain232ade02015-04-20 15:14:36 +01002267 // Create stack space for the call to
2268 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2269 // TODO: enhance register allocator to ask for stack temporaries.
2270 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2271 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2272 __ subl(ESP, Immediate(adjustment));
2273 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002274
Roland Levillain232ade02015-04-20 15:14:36 +01002275 // Load the value to the FP stack, using temporaries if needed.
2276 PushOntoFPStack(in, 0, adjustment, false, true);
2277
2278 if (out.IsStackSlot()) {
2279 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2280 } else {
2281 __ fstps(Address(ESP, 0));
2282 Location stack_temp = Location::StackSlot(0);
2283 codegen_->Move32(out, stack_temp);
2284 }
2285
2286 // Remove the temporary stack space we allocated.
2287 if (adjustment != 0) {
2288 __ addl(ESP, Immediate(adjustment));
2289 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002290 break;
2291 }
2292
Roland Levillaincff13742014-11-17 14:32:17 +00002293 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002294 // Processing a Dex `double-to-float' instruction.
2295 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002296 break;
2297
2298 default:
2299 LOG(FATAL) << "Unexpected type conversion from " << input_type
2300 << " to " << result_type;
2301 };
2302 break;
2303
Roland Levillaindff1f282014-11-05 14:15:05 +00002304 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002305 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
2311 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002312 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002314 break;
2315
Roland Levillain647b9ed2014-11-27 12:06:00 +00002316 case Primitive::kPrimLong: {
2317 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002318 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002319
Roland Levillain232ade02015-04-20 15:14:36 +01002320 // Create stack space for the call to
2321 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2322 // TODO: enhance register allocator to ask for stack temporaries.
2323 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2324 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2325 __ subl(ESP, Immediate(adjustment));
2326 }
2327
2328 // Load the value to the FP stack, using temporaries if needed.
2329 PushOntoFPStack(in, 0, adjustment, false, true);
2330
2331 if (out.IsDoubleStackSlot()) {
2332 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2333 } else {
2334 __ fstpl(Address(ESP, 0));
2335 Location stack_temp = Location::DoubleStackSlot(0);
2336 codegen_->Move64(out, stack_temp);
2337 }
2338
2339 // Remove the temporary stack space we allocated.
2340 if (adjustment != 0) {
2341 __ addl(ESP, Immediate(adjustment));
2342 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002343 break;
2344 }
2345
Roland Levillaincff13742014-11-17 14:32:17 +00002346 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002347 // Processing a Dex `float-to-double' instruction.
2348 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected type conversion from " << input_type
2353 << " to " << result_type;
2354 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361}
2362
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002363void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002364 LocationSummary* locations =
2365 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002366 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002367 case Primitive::kPrimInt: {
2368 locations->SetInAt(0, Location::RequiresRegister());
2369 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2371 break;
2372 }
2373
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetInAt(1, Location::Any());
2377 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378 break;
2379 }
2380
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002381 case Primitive::kPrimFloat:
2382 case Primitive::kPrimDouble: {
2383 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002384 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002385 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002389 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2391 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002392 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002393}
2394
2395void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2396 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002397 Location first = locations->InAt(0);
2398 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002399 Location out = locations->Out();
2400
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002401 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002402 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002403 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002404 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2405 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002406 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2407 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002408 } else {
2409 __ leal(out.AsRegister<Register>(), Address(
2410 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2411 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002413 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2414 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2415 __ addl(out.AsRegister<Register>(), Immediate(value));
2416 } else {
2417 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2418 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002419 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002420 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002421 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002422 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002423 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002424 }
2425
2426 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002427 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002428 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2429 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002430 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002431 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2432 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002433 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002434 } else {
2435 DCHECK(second.IsConstant()) << second;
2436 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2437 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2438 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002439 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002440 break;
2441 }
2442
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002443 case Primitive::kPrimFloat: {
2444 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002446 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2447 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2448 DCHECK(!const_area->NeedsMaterialization());
2449 __ addss(first.AsFpuRegister<XmmRegister>(),
2450 codegen_->LiteralFloatAddress(
2451 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2452 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2453 } else {
2454 DCHECK(second.IsStackSlot());
2455 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002456 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002457 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002458 }
2459
2460 case Primitive::kPrimDouble: {
2461 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002463 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2464 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2465 DCHECK(!const_area->NeedsMaterialization());
2466 __ addsd(first.AsFpuRegister<XmmRegister>(),
2467 codegen_->LiteralDoubleAddress(
2468 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2469 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2470 } else {
2471 DCHECK(second.IsDoubleStackSlot());
2472 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002473 }
2474 break;
2475 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002476
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002477 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002478 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002479 }
2480}
2481
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002482void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002485 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002486 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002487 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002488 locations->SetInAt(0, Location::RequiresRegister());
2489 locations->SetInAt(1, Location::Any());
2490 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002491 break;
2492 }
Calin Juravle11351682014-10-23 15:38:15 +01002493 case Primitive::kPrimFloat:
2494 case Primitive::kPrimDouble: {
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002496 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002497 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002498 break;
Calin Juravle11351682014-10-23 15:38:15 +01002499 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002500
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002501 default:
Calin Juravle11351682014-10-23 15:38:15 +01002502 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002503 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002504}
2505
2506void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2507 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002508 Location first = locations->InAt(0);
2509 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002510 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002511 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002512 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002515 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002516 __ subl(first.AsRegister<Register>(),
2517 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002518 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002519 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002520 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002521 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002522 }
2523
2524 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002525 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002526 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2527 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002528 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002529 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 __ sbbl(first.AsRegisterPairHigh<Register>(),
2531 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002532 } else {
2533 DCHECK(second.IsConstant()) << second;
2534 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2535 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2536 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002537 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002538 break;
2539 }
2540
Calin Juravle11351682014-10-23 15:38:15 +01002541 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002542 if (second.IsFpuRegister()) {
2543 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2544 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2545 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2546 DCHECK(!const_area->NeedsMaterialization());
2547 __ subss(first.AsFpuRegister<XmmRegister>(),
2548 codegen_->LiteralFloatAddress(
2549 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2550 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2551 } else {
2552 DCHECK(second.IsStackSlot());
2553 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2554 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002555 break;
Calin Juravle11351682014-10-23 15:38:15 +01002556 }
2557
2558 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002559 if (second.IsFpuRegister()) {
2560 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2561 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2562 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2563 DCHECK(!const_area->NeedsMaterialization());
2564 __ subsd(first.AsFpuRegister<XmmRegister>(),
2565 codegen_->LiteralDoubleAddress(
2566 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2567 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2568 } else {
2569 DCHECK(second.IsDoubleStackSlot());
2570 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2571 }
Calin Juravle11351682014-10-23 15:38:15 +01002572 break;
2573 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002574
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002575 default:
Calin Juravle11351682014-10-23 15:38:15 +01002576 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002577 }
2578}
2579
Calin Juravle34bacdf2014-10-07 20:23:36 +01002580void LocationsBuilderX86::VisitMul(HMul* mul) {
2581 LocationSummary* locations =
2582 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2583 switch (mul->GetResultType()) {
2584 case Primitive::kPrimInt:
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002587 if (mul->InputAt(1)->IsIntConstant()) {
2588 // Can use 3 operand multiply.
2589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2590 } else {
2591 locations->SetOut(Location::SameAsFirstInput());
2592 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002593 break;
2594 case Primitive::kPrimLong: {
2595 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002596 locations->SetInAt(1, Location::Any());
2597 locations->SetOut(Location::SameAsFirstInput());
2598 // Needed for imul on 32bits with 64bits output.
2599 locations->AddTemp(Location::RegisterLocation(EAX));
2600 locations->AddTemp(Location::RegisterLocation(EDX));
2601 break;
2602 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002603 case Primitive::kPrimFloat:
2604 case Primitive::kPrimDouble: {
2605 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002606 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002607 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002608 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002609 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002610
2611 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002612 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002613 }
2614}
2615
2616void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2617 LocationSummary* locations = mul->GetLocations();
2618 Location first = locations->InAt(0);
2619 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002620 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002621
2622 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002623 case Primitive::kPrimInt:
2624 // The constant may have ended up in a register, so test explicitly to avoid
2625 // problems where the output may not be the same as the first operand.
2626 if (mul->InputAt(1)->IsIntConstant()) {
2627 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2628 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2629 } else if (second.IsRegister()) {
2630 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002632 } else {
2633 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002634 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002636 }
2637 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638
2639 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002640 Register in1_hi = first.AsRegisterPairHigh<Register>();
2641 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 Register eax = locations->GetTemp(0).AsRegister<Register>();
2643 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002644
2645 DCHECK_EQ(EAX, eax);
2646 DCHECK_EQ(EDX, edx);
2647
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002648 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002649 // output: in1
2650 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2651 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2652 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002653 if (second.IsConstant()) {
2654 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002655
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002656 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2657 int32_t low_value = Low32Bits(value);
2658 int32_t high_value = High32Bits(value);
2659 Immediate low(low_value);
2660 Immediate high(high_value);
2661
2662 __ movl(eax, high);
2663 // eax <- in1.lo * in2.hi
2664 __ imull(eax, in1_lo);
2665 // in1.hi <- in1.hi * in2.lo
2666 __ imull(in1_hi, low);
2667 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2668 __ addl(in1_hi, eax);
2669 // move in2_lo to eax to prepare for double precision
2670 __ movl(eax, low);
2671 // edx:eax <- in1.lo * in2.lo
2672 __ mull(in1_lo);
2673 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2674 __ addl(in1_hi, edx);
2675 // in1.lo <- (in1.lo * in2.lo)[31:0];
2676 __ movl(in1_lo, eax);
2677 } else if (second.IsRegisterPair()) {
2678 Register in2_hi = second.AsRegisterPairHigh<Register>();
2679 Register in2_lo = second.AsRegisterPairLow<Register>();
2680
2681 __ movl(eax, in2_hi);
2682 // eax <- in1.lo * in2.hi
2683 __ imull(eax, in1_lo);
2684 // in1.hi <- in1.hi * in2.lo
2685 __ imull(in1_hi, in2_lo);
2686 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2687 __ addl(in1_hi, eax);
2688 // move in1_lo to eax to prepare for double precision
2689 __ movl(eax, in1_lo);
2690 // edx:eax <- in1.lo * in2.lo
2691 __ mull(in2_lo);
2692 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2693 __ addl(in1_hi, edx);
2694 // in1.lo <- (in1.lo * in2.lo)[31:0];
2695 __ movl(in1_lo, eax);
2696 } else {
2697 DCHECK(second.IsDoubleStackSlot()) << second;
2698 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2699 Address in2_lo(ESP, second.GetStackIndex());
2700
2701 __ movl(eax, in2_hi);
2702 // eax <- in1.lo * in2.hi
2703 __ imull(eax, in1_lo);
2704 // in1.hi <- in1.hi * in2.lo
2705 __ imull(in1_hi, in2_lo);
2706 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2707 __ addl(in1_hi, eax);
2708 // move in1_lo to eax to prepare for double precision
2709 __ movl(eax, in1_lo);
2710 // edx:eax <- in1.lo * in2.lo
2711 __ mull(in2_lo);
2712 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2713 __ addl(in1_hi, edx);
2714 // in1.lo <- (in1.lo * in2.lo)[31:0];
2715 __ movl(in1_lo, eax);
2716 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717
2718 break;
2719 }
2720
Calin Juravleb5bfa962014-10-21 18:02:24 +01002721 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002722 DCHECK(first.Equals(locations->Out()));
2723 if (second.IsFpuRegister()) {
2724 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2725 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2726 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2727 DCHECK(!const_area->NeedsMaterialization());
2728 __ mulss(first.AsFpuRegister<XmmRegister>(),
2729 codegen_->LiteralFloatAddress(
2730 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2731 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2732 } else {
2733 DCHECK(second.IsStackSlot());
2734 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2735 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002736 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002737 }
2738
2739 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002740 DCHECK(first.Equals(locations->Out()));
2741 if (second.IsFpuRegister()) {
2742 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2743 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2744 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2745 DCHECK(!const_area->NeedsMaterialization());
2746 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2747 codegen_->LiteralDoubleAddress(
2748 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2749 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2750 } else {
2751 DCHECK(second.IsDoubleStackSlot());
2752 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2753 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002754 break;
2755 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002756
2757 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002758 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002759 }
2760}
2761
Roland Levillain232ade02015-04-20 15:14:36 +01002762void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2763 uint32_t temp_offset,
2764 uint32_t stack_adjustment,
2765 bool is_fp,
2766 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002767 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002768 DCHECK(!is_wide);
2769 if (is_fp) {
2770 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2771 } else {
2772 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2773 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002774 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002775 DCHECK(is_wide);
2776 if (is_fp) {
2777 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2778 } else {
2779 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2780 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002781 } else {
2782 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002783 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002784 Location stack_temp = Location::StackSlot(temp_offset);
2785 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002786 if (is_fp) {
2787 __ flds(Address(ESP, temp_offset));
2788 } else {
2789 __ filds(Address(ESP, temp_offset));
2790 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002791 } else {
2792 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2793 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002794 if (is_fp) {
2795 __ fldl(Address(ESP, temp_offset));
2796 } else {
2797 __ fildl(Address(ESP, temp_offset));
2798 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002799 }
2800 }
2801}
2802
2803void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2804 Primitive::Type type = rem->GetResultType();
2805 bool is_float = type == Primitive::kPrimFloat;
2806 size_t elem_size = Primitive::ComponentSize(type);
2807 LocationSummary* locations = rem->GetLocations();
2808 Location first = locations->InAt(0);
2809 Location second = locations->InAt(1);
2810 Location out = locations->Out();
2811
2812 // Create stack space for 2 elements.
2813 // TODO: enhance register allocator to ask for stack temporaries.
2814 __ subl(ESP, Immediate(2 * elem_size));
2815
2816 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002817 const bool is_wide = !is_float;
2818 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2819 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002820
2821 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002822 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002823 __ Bind(&retry);
2824 __ fprem();
2825
2826 // Move FP status to AX.
2827 __ fstsw();
2828
2829 // And see if the argument reduction is complete. This is signaled by the
2830 // C2 FPU flag bit set to 0.
2831 __ andl(EAX, Immediate(kC2ConditionMask));
2832 __ j(kNotEqual, &retry);
2833
2834 // We have settled on the final value. Retrieve it into an XMM register.
2835 // Store FP top of stack to real stack.
2836 if (is_float) {
2837 __ fsts(Address(ESP, 0));
2838 } else {
2839 __ fstl(Address(ESP, 0));
2840 }
2841
2842 // Pop the 2 items from the FP stack.
2843 __ fucompp();
2844
2845 // Load the value from the stack into an XMM register.
2846 DCHECK(out.IsFpuRegister()) << out;
2847 if (is_float) {
2848 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2849 } else {
2850 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2851 }
2852
2853 // And remove the temporary stack space we allocated.
2854 __ addl(ESP, Immediate(2 * elem_size));
2855}
2856
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002857
2858void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2859 DCHECK(instruction->IsDiv() || instruction->IsRem());
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002863 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002864
2865 Register out_register = locations->Out().AsRegister<Register>();
2866 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002867 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002868
2869 DCHECK(imm == 1 || imm == -1);
2870
2871 if (instruction->IsRem()) {
2872 __ xorl(out_register, out_register);
2873 } else {
2874 __ movl(out_register, input_register);
2875 if (imm == -1) {
2876 __ negl(out_register);
2877 }
2878 }
2879}
2880
2881
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002882void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002883 LocationSummary* locations = instruction->GetLocations();
2884
2885 Register out_register = locations->Out().AsRegister<Register>();
2886 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002887 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002888
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002889 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002890 Register num = locations->GetTemp(0).AsRegister<Register>();
2891
2892 __ leal(num, Address(input_register, std::abs(imm) - 1));
2893 __ testl(input_register, input_register);
2894 __ cmovl(kGreaterEqual, num, input_register);
2895 int shift = CTZ(imm);
2896 __ sarl(num, Immediate(shift));
2897
2898 if (imm < 0) {
2899 __ negl(num);
2900 }
2901
2902 __ movl(out_register, num);
2903}
2904
2905void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2906 DCHECK(instruction->IsDiv() || instruction->IsRem());
2907
2908 LocationSummary* locations = instruction->GetLocations();
2909 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2910
2911 Register eax = locations->InAt(0).AsRegister<Register>();
2912 Register out = locations->Out().AsRegister<Register>();
2913 Register num;
2914 Register edx;
2915
2916 if (instruction->IsDiv()) {
2917 edx = locations->GetTemp(0).AsRegister<Register>();
2918 num = locations->GetTemp(1).AsRegister<Register>();
2919 } else {
2920 edx = locations->Out().AsRegister<Register>();
2921 num = locations->GetTemp(0).AsRegister<Register>();
2922 }
2923
2924 DCHECK_EQ(EAX, eax);
2925 DCHECK_EQ(EDX, edx);
2926 if (instruction->IsDiv()) {
2927 DCHECK_EQ(EAX, out);
2928 } else {
2929 DCHECK_EQ(EDX, out);
2930 }
2931
2932 int64_t magic;
2933 int shift;
2934 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2935
Mark Mendell0c9497d2015-08-21 09:30:05 -04002936 NearLabel ndiv;
2937 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002938 // If numerator is 0, the result is 0, no computation needed.
2939 __ testl(eax, eax);
2940 __ j(kNotEqual, &ndiv);
2941
2942 __ xorl(out, out);
2943 __ jmp(&end);
2944
2945 __ Bind(&ndiv);
2946
2947 // Save the numerator.
2948 __ movl(num, eax);
2949
2950 // EAX = magic
2951 __ movl(eax, Immediate(magic));
2952
2953 // EDX:EAX = magic * numerator
2954 __ imull(num);
2955
2956 if (imm > 0 && magic < 0) {
2957 // EDX += num
2958 __ addl(edx, num);
2959 } else if (imm < 0 && magic > 0) {
2960 __ subl(edx, num);
2961 }
2962
2963 // Shift if needed.
2964 if (shift != 0) {
2965 __ sarl(edx, Immediate(shift));
2966 }
2967
2968 // EDX += 1 if EDX < 0
2969 __ movl(eax, edx);
2970 __ shrl(edx, Immediate(31));
2971 __ addl(edx, eax);
2972
2973 if (instruction->IsRem()) {
2974 __ movl(eax, num);
2975 __ imull(edx, Immediate(imm));
2976 __ subl(eax, edx);
2977 __ movl(edx, eax);
2978 } else {
2979 __ movl(eax, edx);
2980 }
2981 __ Bind(&end);
2982}
2983
Calin Juravlebacfec32014-11-14 15:54:36 +00002984void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2985 DCHECK(instruction->IsDiv() || instruction->IsRem());
2986
2987 LocationSummary* locations = instruction->GetLocations();
2988 Location out = locations->Out();
2989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
2991 bool is_div = instruction->IsDiv();
2992
2993 switch (instruction->GetResultType()) {
2994 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 DCHECK_EQ(EAX, first.AsRegister<Register>());
2996 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002997
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002998 if (instruction->InputAt(1)->IsIntConstant()) {
2999 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003000
3001 if (imm == 0) {
3002 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3003 } else if (imm == 1 || imm == -1) {
3004 DivRemOneOrMinusOne(instruction);
3005 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003006 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003007 } else {
3008 DCHECK(imm <= -2 || imm >= 2);
3009 GenerateDivRemWithAnyConstant(instruction);
3010 }
3011 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003012 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003013 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003014 is_div);
3015 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003016
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003017 Register second_reg = second.AsRegister<Register>();
3018 // 0x80000000/-1 triggers an arithmetic exception!
3019 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3020 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003021
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003022 __ cmpl(second_reg, Immediate(-1));
3023 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003024
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003025 // edx:eax <- sign-extended of eax
3026 __ cdq();
3027 // eax = quotient, edx = remainder
3028 __ idivl(second_reg);
3029 __ Bind(slow_path->GetExitLabel());
3030 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 break;
3032 }
3033
3034 case Primitive::kPrimLong: {
3035 InvokeRuntimeCallingConvention calling_convention;
3036 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3037 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3038 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3039 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3040 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3041 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3042
3043 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003044 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3045 instruction,
3046 instruction->GetDexPc(),
3047 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003048 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003049 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3050 instruction,
3051 instruction->GetDexPc(),
3052 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003053 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003054 break;
3055 }
3056
3057 default:
3058 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3059 }
3060}
3061
Calin Juravle7c4954d2014-10-28 16:57:40 +00003062void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003063 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003064 ? LocationSummary::kCall
3065 : LocationSummary::kNoCall;
3066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3067
Calin Juravle7c4954d2014-10-28 16:57:40 +00003068 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003069 case Primitive::kPrimInt: {
3070 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003071 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003072 locations->SetOut(Location::SameAsFirstInput());
3073 // Intel uses edx:eax as the dividend.
3074 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003075 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3076 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3077 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003078 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003079 locations->AddTemp(Location::RequiresRegister());
3080 }
Calin Juravled0d48522014-11-04 16:40:20 +00003081 break;
3082 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003083 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003084 InvokeRuntimeCallingConvention calling_convention;
3085 locations->SetInAt(0, Location::RegisterPairLocation(
3086 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3087 locations->SetInAt(1, Location::RegisterPairLocation(
3088 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3089 // Runtime helper puts the result in EAX, EDX.
3090 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003091 break;
3092 }
3093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble: {
3095 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003096 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003097 locations->SetOut(Location::SameAsFirstInput());
3098 break;
3099 }
3100
3101 default:
3102 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3103 }
3104}
3105
3106void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3107 LocationSummary* locations = div->GetLocations();
3108 Location first = locations->InAt(0);
3109 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003110
3111 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003112 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003113 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003114 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003115 break;
3116 }
3117
3118 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003119 if (second.IsFpuRegister()) {
3120 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3121 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3122 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3123 DCHECK(!const_area->NeedsMaterialization());
3124 __ divss(first.AsFpuRegister<XmmRegister>(),
3125 codegen_->LiteralFloatAddress(
3126 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3127 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3128 } else {
3129 DCHECK(second.IsStackSlot());
3130 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3131 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003132 break;
3133 }
3134
3135 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003136 if (second.IsFpuRegister()) {
3137 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3138 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3139 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3140 DCHECK(!const_area->NeedsMaterialization());
3141 __ divsd(first.AsFpuRegister<XmmRegister>(),
3142 codegen_->LiteralDoubleAddress(
3143 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3144 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3145 } else {
3146 DCHECK(second.IsDoubleStackSlot());
3147 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3148 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003149 break;
3150 }
3151
3152 default:
3153 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3154 }
3155}
3156
Calin Juravlebacfec32014-11-14 15:54:36 +00003157void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003158 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003159
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003160 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3161 ? LocationSummary::kCall
3162 : LocationSummary::kNoCall;
3163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003164
Calin Juravled2ec87d2014-12-08 14:24:46 +00003165 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003166 case Primitive::kPrimInt: {
3167 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003168 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003169 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003170 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3171 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3172 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003173 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003174 locations->AddTemp(Location::RequiresRegister());
3175 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003176 break;
3177 }
3178 case Primitive::kPrimLong: {
3179 InvokeRuntimeCallingConvention calling_convention;
3180 locations->SetInAt(0, Location::RegisterPairLocation(
3181 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3182 locations->SetInAt(1, Location::RegisterPairLocation(
3183 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3184 // Runtime helper puts the result in EAX, EDX.
3185 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3186 break;
3187 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003189 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003190 locations->SetInAt(0, Location::Any());
3191 locations->SetInAt(1, Location::Any());
3192 locations->SetOut(Location::RequiresFpuRegister());
3193 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003194 break;
3195 }
3196
3197 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003198 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003199 }
3200}
3201
3202void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3203 Primitive::Type type = rem->GetResultType();
3204 switch (type) {
3205 case Primitive::kPrimInt:
3206 case Primitive::kPrimLong: {
3207 GenerateDivRemIntegral(rem);
3208 break;
3209 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003210 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003211 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003213 break;
3214 }
3215 default:
3216 LOG(FATAL) << "Unexpected rem type " << type;
3217 }
3218}
3219
Calin Juravled0d48522014-11-04 16:40:20 +00003220void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003221 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3222 ? LocationSummary::kCallOnSlowPath
3223 : LocationSummary::kNoCall;
3224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003225 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003226 case Primitive::kPrimByte:
3227 case Primitive::kPrimChar:
3228 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003229 case Primitive::kPrimInt: {
3230 locations->SetInAt(0, Location::Any());
3231 break;
3232 }
3233 case Primitive::kPrimLong: {
3234 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3235 if (!instruction->IsConstant()) {
3236 locations->AddTemp(Location::RequiresRegister());
3237 }
3238 break;
3239 }
3240 default:
3241 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3242 }
Calin Juravled0d48522014-11-04 16:40:20 +00003243 if (instruction->HasUses()) {
3244 locations->SetOut(Location::SameAsFirstInput());
3245 }
3246}
3247
3248void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003249 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003250 codegen_->AddSlowPath(slow_path);
3251
3252 LocationSummary* locations = instruction->GetLocations();
3253 Location value = locations->InAt(0);
3254
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003255 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003256 case Primitive::kPrimByte:
3257 case Primitive::kPrimChar:
3258 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003259 case Primitive::kPrimInt: {
3260 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003262 __ j(kEqual, slow_path->GetEntryLabel());
3263 } else if (value.IsStackSlot()) {
3264 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3265 __ j(kEqual, slow_path->GetEntryLabel());
3266 } else {
3267 DCHECK(value.IsConstant()) << value;
3268 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3269 __ jmp(slow_path->GetEntryLabel());
3270 }
3271 }
3272 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003273 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003274 case Primitive::kPrimLong: {
3275 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003277 __ movl(temp, value.AsRegisterPairLow<Register>());
3278 __ orl(temp, value.AsRegisterPairHigh<Register>());
3279 __ j(kEqual, slow_path->GetEntryLabel());
3280 } else {
3281 DCHECK(value.IsConstant()) << value;
3282 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3283 __ jmp(slow_path->GetEntryLabel());
3284 }
3285 }
3286 break;
3287 }
3288 default:
3289 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003290 }
Calin Juravled0d48522014-11-04 16:40:20 +00003291}
3292
Calin Juravle9aec02f2014-11-18 23:06:35 +00003293void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3294 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3295
3296 LocationSummary* locations =
3297 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3298
3299 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003300 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003301 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003302 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003303 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003304 // The shift count needs to be in CL or a constant.
3305 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003306 locations->SetOut(Location::SameAsFirstInput());
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3311 }
3312}
3313
3314void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3315 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3316
3317 LocationSummary* locations = op->GetLocations();
3318 Location first = locations->InAt(0);
3319 Location second = locations->InAt(1);
3320 DCHECK(first.Equals(locations->Out()));
3321
3322 switch (op->GetResultType()) {
3323 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003324 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003325 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003328 DCHECK_EQ(ECX, second_reg);
3329 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003330 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003331 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003332 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003333 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003334 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335 }
3336 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003337 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3338 if (shift == 0) {
3339 return;
3340 }
3341 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003342 if (op->IsShl()) {
3343 __ shll(first_reg, imm);
3344 } else if (op->IsShr()) {
3345 __ sarl(first_reg, imm);
3346 } else {
3347 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003348 }
3349 }
3350 break;
3351 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003352 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003353 if (second.IsRegister()) {
3354 Register second_reg = second.AsRegister<Register>();
3355 DCHECK_EQ(ECX, second_reg);
3356 if (op->IsShl()) {
3357 GenerateShlLong(first, second_reg);
3358 } else if (op->IsShr()) {
3359 GenerateShrLong(first, second_reg);
3360 } else {
3361 GenerateUShrLong(first, second_reg);
3362 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003363 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003364 // Shift by a constant.
3365 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3366 // Nothing to do if the shift is 0, as the input is already the output.
3367 if (shift != 0) {
3368 if (op->IsShl()) {
3369 GenerateShlLong(first, shift);
3370 } else if (op->IsShr()) {
3371 GenerateShrLong(first, shift);
3372 } else {
3373 GenerateUShrLong(first, shift);
3374 }
3375 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003376 }
3377 break;
3378 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003379 default:
3380 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3381 }
3382}
3383
Mark P Mendell73945692015-04-29 14:56:17 +00003384void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3385 Register low = loc.AsRegisterPairLow<Register>();
3386 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003387 if (shift == 1) {
3388 // This is just an addition.
3389 __ addl(low, low);
3390 __ adcl(high, high);
3391 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003392 // Shift by 32 is easy. High gets low, and low gets 0.
3393 codegen_->EmitParallelMoves(
3394 loc.ToLow(),
3395 loc.ToHigh(),
3396 Primitive::kPrimInt,
3397 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3398 loc.ToLow(),
3399 Primitive::kPrimInt);
3400 } else if (shift > 32) {
3401 // Low part becomes 0. High part is low part << (shift-32).
3402 __ movl(high, low);
3403 __ shll(high, Immediate(shift - 32));
3404 __ xorl(low, low);
3405 } else {
3406 // Between 1 and 31.
3407 __ shld(high, low, Immediate(shift));
3408 __ shll(low, Immediate(shift));
3409 }
3410}
3411
Calin Juravle9aec02f2014-11-18 23:06:35 +00003412void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003413 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003414 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3415 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3416 __ testl(shifter, Immediate(32));
3417 __ j(kEqual, &done);
3418 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3419 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3420 __ Bind(&done);
3421}
3422
Mark P Mendell73945692015-04-29 14:56:17 +00003423void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3424 Register low = loc.AsRegisterPairLow<Register>();
3425 Register high = loc.AsRegisterPairHigh<Register>();
3426 if (shift == 32) {
3427 // Need to copy the sign.
3428 DCHECK_NE(low, high);
3429 __ movl(low, high);
3430 __ sarl(high, Immediate(31));
3431 } else if (shift > 32) {
3432 DCHECK_NE(low, high);
3433 // High part becomes sign. Low part is shifted by shift - 32.
3434 __ movl(low, high);
3435 __ sarl(high, Immediate(31));
3436 __ sarl(low, Immediate(shift - 32));
3437 } else {
3438 // Between 1 and 31.
3439 __ shrd(low, high, Immediate(shift));
3440 __ sarl(high, Immediate(shift));
3441 }
3442}
3443
Calin Juravle9aec02f2014-11-18 23:06:35 +00003444void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003445 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003446 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3447 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3448 __ testl(shifter, Immediate(32));
3449 __ j(kEqual, &done);
3450 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3451 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3452 __ Bind(&done);
3453}
3454
Mark P Mendell73945692015-04-29 14:56:17 +00003455void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3456 Register low = loc.AsRegisterPairLow<Register>();
3457 Register high = loc.AsRegisterPairHigh<Register>();
3458 if (shift == 32) {
3459 // Shift by 32 is easy. Low gets high, and high gets 0.
3460 codegen_->EmitParallelMoves(
3461 loc.ToHigh(),
3462 loc.ToLow(),
3463 Primitive::kPrimInt,
3464 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3465 loc.ToHigh(),
3466 Primitive::kPrimInt);
3467 } else if (shift > 32) {
3468 // Low part is high >> (shift - 32). High part becomes 0.
3469 __ movl(low, high);
3470 __ shrl(low, Immediate(shift - 32));
3471 __ xorl(high, high);
3472 } else {
3473 // Between 1 and 31.
3474 __ shrd(low, high, Immediate(shift));
3475 __ shrl(high, Immediate(shift));
3476 }
3477}
3478
Calin Juravle9aec02f2014-11-18 23:06:35 +00003479void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003480 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003481 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3482 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3483 __ testl(shifter, Immediate(32));
3484 __ j(kEqual, &done);
3485 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3486 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3487 __ Bind(&done);
3488}
3489
3490void LocationsBuilderX86::VisitShl(HShl* shl) {
3491 HandleShift(shl);
3492}
3493
3494void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3495 HandleShift(shl);
3496}
3497
3498void LocationsBuilderX86::VisitShr(HShr* shr) {
3499 HandleShift(shr);
3500}
3501
3502void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3503 HandleShift(shr);
3504}
3505
3506void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3507 HandleShift(ushr);
3508}
3509
3510void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3511 HandleShift(ushr);
3512}
3513
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003514void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003517 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003518 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003519 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003520 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003521}
3522
3523void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3524 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003525 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003526 // Note: if heap poisoning is enabled, the entry point takes cares
3527 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003528 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3529 instruction,
3530 instruction->GetDexPc(),
3531 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003532 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003533}
3534
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003535void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3536 LocationSummary* locations =
3537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3538 locations->SetOut(Location::RegisterLocation(EAX));
3539 InvokeRuntimeCallingConvention calling_convention;
3540 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003541 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003542 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003543}
3544
3545void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3546 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003547 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3548
Roland Levillain4d027112015-07-01 15:41:14 +01003549 // Note: if heap poisoning is enabled, the entry point takes cares
3550 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003551 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3552 instruction,
3553 instruction->GetDexPc(),
3554 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003555 DCHECK(!codegen_->IsLeafMethod());
3556}
3557
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003558void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003561 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3562 if (location.IsStackSlot()) {
3563 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3564 } else if (location.IsDoubleStackSlot()) {
3565 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003566 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003567 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003568}
3569
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003570void InstructionCodeGeneratorX86::VisitParameterValue(
3571 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3572}
3573
3574void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3577 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3578}
3579
3580void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003581}
3582
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003583void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003584 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003585 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003588}
3589
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003590void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3591 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003592 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003593 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003594 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003595 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003596 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003598 break;
3599
3600 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003601 __ notl(out.AsRegisterPairLow<Register>());
3602 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003603 break;
3604
3605 default:
3606 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3607 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003608}
3609
David Brazdil66d126e2015-04-03 16:02:44 +01003610void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3611 LocationSummary* locations =
3612 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3613 locations->SetInAt(0, Location::RequiresRegister());
3614 locations->SetOut(Location::SameAsFirstInput());
3615}
3616
3617void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003618 LocationSummary* locations = bool_not->GetLocations();
3619 Location in = locations->InAt(0);
3620 Location out = locations->Out();
3621 DCHECK(in.Equals(out));
3622 __ xorl(out.AsRegister<Register>(), Immediate(1));
3623}
3624
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003625void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003628 switch (compare->InputAt(0)->GetType()) {
3629 case Primitive::kPrimLong: {
3630 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003631 locations->SetInAt(1, Location::Any());
3632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3633 break;
3634 }
3635 case Primitive::kPrimFloat:
3636 case Primitive::kPrimDouble: {
3637 locations->SetInAt(0, Location::RequiresFpuRegister());
3638 locations->SetInAt(1, Location::RequiresFpuRegister());
3639 locations->SetOut(Location::RequiresRegister());
3640 break;
3641 }
3642 default:
3643 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3644 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003645}
3646
3647void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003648 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003650 Location left = locations->InAt(0);
3651 Location right = locations->InAt(1);
3652
Mark Mendell0c9497d2015-08-21 09:30:05 -04003653 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003654 switch (compare->InputAt(0)->GetType()) {
3655 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003656 Register left_low = left.AsRegisterPairLow<Register>();
3657 Register left_high = left.AsRegisterPairHigh<Register>();
3658 int32_t val_low = 0;
3659 int32_t val_high = 0;
3660 bool right_is_const = false;
3661
3662 if (right.IsConstant()) {
3663 DCHECK(right.GetConstant()->IsLongConstant());
3664 right_is_const = true;
3665 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3666 val_low = Low32Bits(val);
3667 val_high = High32Bits(val);
3668 }
3669
Calin Juravleddb7df22014-11-25 20:56:51 +00003670 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003671 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003672 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003673 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003674 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003675 DCHECK(right_is_const) << right;
3676 if (val_high == 0) {
3677 __ testl(left_high, left_high);
3678 } else {
3679 __ cmpl(left_high, Immediate(val_high));
3680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003681 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003682 __ j(kLess, &less); // Signed compare.
3683 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003684 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003685 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003686 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003687 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003688 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003689 DCHECK(right_is_const) << right;
3690 if (val_low == 0) {
3691 __ testl(left_low, left_low);
3692 } else {
3693 __ cmpl(left_low, Immediate(val_low));
3694 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003696 break;
3697 }
3698 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003700 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3701 break;
3702 }
3703 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003704 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003705 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 break;
3707 }
3708 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003709 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003710 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 __ movl(out, Immediate(0));
3712 __ j(kEqual, &done);
3713 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3714
3715 __ Bind(&greater);
3716 __ movl(out, Immediate(1));
3717 __ jmp(&done);
3718
3719 __ Bind(&less);
3720 __ movl(out, Immediate(-1));
3721
3722 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003723}
3724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003725void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003726 LocationSummary* locations =
3727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003728 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3729 locations->SetInAt(i, Location::Any());
3730 }
3731 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003732}
3733
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003734void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003735 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003736}
3737
Calin Juravle52c48962014-12-16 17:02:57 +00003738void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3739 /*
3740 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3741 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3742 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3743 */
3744 switch (kind) {
3745 case MemBarrierKind::kAnyAny: {
3746 __ mfence();
3747 break;
3748 }
3749 case MemBarrierKind::kAnyStore:
3750 case MemBarrierKind::kLoadAny:
3751 case MemBarrierKind::kStoreStore: {
3752 // nop
3753 break;
3754 }
3755 default:
3756 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003757 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003758}
3759
Vladimir Markodc151b22015-10-15 18:02:30 +01003760HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
3761 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3762 MethodReference target_method ATTRIBUTE_UNUSED) {
3763 if (desired_dispatch_info.method_load_kind ==
3764 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) {
3765 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3766 return HInvokeStaticOrDirect::DispatchInfo {
3767 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3768 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3769 0u,
3770 0u
3771 };
3772 }
3773 switch (desired_dispatch_info.code_ptr_location) {
3774 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3775 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3776 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3777 // (Though the direct CALL ptr16:32 is available for consideration).
3778 return HInvokeStaticOrDirect::DispatchInfo {
3779 desired_dispatch_info.method_load_kind,
3780 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3781 desired_dispatch_info.method_load_data,
3782 0u
3783 };
3784 default:
3785 return desired_dispatch_info;
3786 }
3787}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003788
Vladimir Marko58155012015-08-19 12:49:41 +00003789void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3790 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3791 switch (invoke->GetMethodLoadKind()) {
3792 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3793 // temp = thread->string_init_entrypoint
3794 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3795 break;
3796 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3797 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3798 break;
3799 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3800 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3801 break;
3802 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3803 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3804 method_patches_.emplace_back(invoke->GetTargetMethod());
3805 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3806 break;
3807 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003808 // TODO: Implement this type.
3809 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3810 LOG(FATAL) << "Unsupported";
3811 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003812 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3813 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3814 Register method_reg;
3815 Register reg = temp.AsRegister<Register>();
3816 if (current_method.IsRegister()) {
3817 method_reg = current_method.AsRegister<Register>();
3818 } else {
3819 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3820 DCHECK(!current_method.IsValid());
3821 method_reg = reg;
3822 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3823 }
3824 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003825 __ movl(reg, Address(method_reg,
3826 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003827 // temp = temp[index_in_cache]
3828 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3829 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3830 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003831 }
Vladimir Marko58155012015-08-19 12:49:41 +00003832 }
3833
3834 switch (invoke->GetCodePtrLocation()) {
3835 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3836 __ call(GetFrameEntryLabel());
3837 break;
3838 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3839 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3840 Label* label = &relative_call_patches_.back().label;
3841 __ call(label); // Bind to the patch label, override at link time.
3842 __ Bind(label); // Bind the label at the end of the "call" insn.
3843 break;
3844 }
3845 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3846 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01003847 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3848 LOG(FATAL) << "Unsupported";
3849 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003850 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3851 // (callee_method + offset_of_quick_compiled_code)()
3852 __ call(Address(callee_method.AsRegister<Register>(),
3853 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3854 kX86WordSize).Int32Value()));
3855 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003856 }
3857
3858 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003859}
3860
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003861void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3862 Register temp = temp_in.AsRegister<Register>();
3863 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3864 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3865 LocationSummary* locations = invoke->GetLocations();
3866 Location receiver = locations->InAt(0);
3867 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3868 // temp = object->GetClass();
3869 DCHECK(receiver.IsRegister());
3870 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3871 MaybeRecordImplicitNullCheck(invoke);
3872 __ MaybeUnpoisonHeapReference(temp);
3873 // temp = temp->GetMethodAt(method_offset);
3874 __ movl(temp, Address(temp, method_offset));
3875 // call temp->GetEntryPoint();
3876 __ call(Address(
3877 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3878}
3879
Vladimir Marko58155012015-08-19 12:49:41 +00003880void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3881 DCHECK(linker_patches->empty());
3882 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3883 for (const MethodPatchInfo<Label>& info : method_patches_) {
3884 // The label points to the end of the "movl" insn but the literal offset for method
3885 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3886 uint32_t literal_offset = info.label.Position() - 4;
3887 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3888 info.target_method.dex_file,
3889 info.target_method.dex_method_index));
3890 }
3891 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3892 // The label points to the end of the "call" insn but the literal offset for method
3893 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3894 uint32_t literal_offset = info.label.Position() - 4;
3895 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3896 info.target_method.dex_file,
3897 info.target_method.dex_method_index));
3898 }
3899}
3900
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003901void CodeGeneratorX86::MarkGCCard(Register temp,
3902 Register card,
3903 Register object,
3904 Register value,
3905 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003906 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003907 if (value_can_be_null) {
3908 __ testl(value, value);
3909 __ j(kEqual, &is_null);
3910 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3912 __ movl(temp, object);
3913 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003914 __ movb(Address(temp, card, TIMES_1, 0),
3915 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003916 if (value_can_be_null) {
3917 __ Bind(&is_null);
3918 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003919}
3920
Calin Juravle52c48962014-12-16 17:02:57 +00003921void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3922 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003923 LocationSummary* locations =
3924 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003925 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003926
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003927 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3928 locations->SetOut(Location::RequiresFpuRegister());
3929 } else {
3930 // The output overlaps in case of long: we don't want the low move to overwrite
3931 // the object's location.
3932 locations->SetOut(Location::RequiresRegister(),
3933 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3934 : Location::kNoOutputOverlap);
3935 }
Calin Juravle52c48962014-12-16 17:02:57 +00003936
3937 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3938 // Long values can be loaded atomically into an XMM using movsd.
3939 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3940 // and then copy the XMM into the output 32bits at a time).
3941 locations->AddTemp(Location::RequiresFpuRegister());
3942 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003943}
3944
Calin Juravle52c48962014-12-16 17:02:57 +00003945void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3946 const FieldInfo& field_info) {
3947 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003948
Calin Juravle52c48962014-12-16 17:02:57 +00003949 LocationSummary* locations = instruction->GetLocations();
3950 Register base = locations->InAt(0).AsRegister<Register>();
3951 Location out = locations->Out();
3952 bool is_volatile = field_info.IsVolatile();
3953 Primitive::Type field_type = field_info.GetFieldType();
3954 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3955
3956 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003957 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003958 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003959 break;
3960 }
3961
3962 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003963 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003964 break;
3965 }
3966
3967 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003968 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003969 break;
3970 }
3971
3972 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003973 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974 break;
3975 }
3976
3977 case Primitive::kPrimInt:
3978 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003979 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003980 break;
3981 }
3982
3983 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003984 if (is_volatile) {
3985 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3986 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003988 __ movd(out.AsRegisterPairLow<Register>(), temp);
3989 __ psrlq(temp, Immediate(32));
3990 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3991 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003992 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003993 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003994 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003995 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003997 break;
3998 }
3999
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004000 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004001 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004002 break;
4003 }
4004
4005 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004006 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004007 break;
4008 }
4009
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004010 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004011 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004012 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004013 }
Calin Juravle52c48962014-12-16 17:02:57 +00004014
Calin Juravle77520bc2015-01-12 18:45:46 +00004015 // Longs are handled in the switch.
4016 if (field_type != Primitive::kPrimLong) {
4017 codegen_->MaybeRecordImplicitNullCheck(instruction);
4018 }
4019
Calin Juravle52c48962014-12-16 17:02:57 +00004020 if (is_volatile) {
4021 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4022 }
Roland Levillain4d027112015-07-01 15:41:14 +01004023
4024 if (field_type == Primitive::kPrimNot) {
4025 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
4026 }
Calin Juravle52c48962014-12-16 17:02:57 +00004027}
4028
4029void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4030 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4031
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4034 locations->SetInAt(0, Location::RequiresRegister());
4035 bool is_volatile = field_info.IsVolatile();
4036 Primitive::Type field_type = field_info.GetFieldType();
4037 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4038 || (field_type == Primitive::kPrimByte);
4039
4040 // The register allocator does not support multiple
4041 // inputs that die at entry with one in a specific register.
4042 if (is_byte_type) {
4043 // Ensure the value is in a byte register.
4044 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004045 } else if (Primitive::IsFloatingPointType(field_type)) {
4046 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004047 } else {
4048 locations->SetInAt(1, Location::RequiresRegister());
4049 }
Calin Juravle52c48962014-12-16 17:02:57 +00004050 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01004051 // Temporary registers for the write barrier.
4052 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00004053 // Ensure the card is in a byte register.
4054 locations->AddTemp(Location::RegisterLocation(ECX));
4055 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
4056 // 64bits value can be atomically written to an address with movsd and an XMM register.
4057 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4058 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4059 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4060 // isolated cases when we need this it isn't worth adding the extra complexity.
4061 locations->AddTemp(Location::RequiresFpuRegister());
4062 locations->AddTemp(Location::RequiresFpuRegister());
4063 }
4064}
4065
4066void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004067 const FieldInfo& field_info,
4068 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004069 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4070
4071 LocationSummary* locations = instruction->GetLocations();
4072 Register base = locations->InAt(0).AsRegister<Register>();
4073 Location value = locations->InAt(1);
4074 bool is_volatile = field_info.IsVolatile();
4075 Primitive::Type field_type = field_info.GetFieldType();
4076 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004077 bool needs_write_barrier =
4078 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004079
4080 if (is_volatile) {
4081 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4082 }
4083
4084 switch (field_type) {
4085 case Primitive::kPrimBoolean:
4086 case Primitive::kPrimByte: {
4087 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4088 break;
4089 }
4090
4091 case Primitive::kPrimShort:
4092 case Primitive::kPrimChar: {
4093 __ movw(Address(base, offset), value.AsRegister<Register>());
4094 break;
4095 }
4096
4097 case Primitive::kPrimInt:
4098 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004099 if (kPoisonHeapReferences && needs_write_barrier) {
4100 // Note that in the case where `value` is a null reference,
4101 // we do not enter this block, as the reference does not
4102 // need poisoning.
4103 DCHECK_EQ(field_type, Primitive::kPrimNot);
4104 Register temp = locations->GetTemp(0).AsRegister<Register>();
4105 __ movl(temp, value.AsRegister<Register>());
4106 __ PoisonHeapReference(temp);
4107 __ movl(Address(base, offset), temp);
4108 } else {
4109 __ movl(Address(base, offset), value.AsRegister<Register>());
4110 }
Calin Juravle52c48962014-12-16 17:02:57 +00004111 break;
4112 }
4113
4114 case Primitive::kPrimLong: {
4115 if (is_volatile) {
4116 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4117 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4118 __ movd(temp1, value.AsRegisterPairLow<Register>());
4119 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4120 __ punpckldq(temp1, temp2);
4121 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004122 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004123 } else {
4124 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004125 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004126 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4127 }
4128 break;
4129 }
4130
4131 case Primitive::kPrimFloat: {
4132 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4133 break;
4134 }
4135
4136 case Primitive::kPrimDouble: {
4137 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4138 break;
4139 }
4140
4141 case Primitive::kPrimVoid:
4142 LOG(FATAL) << "Unreachable type " << field_type;
4143 UNREACHABLE();
4144 }
4145
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 // Longs are handled in the switch.
4147 if (field_type != Primitive::kPrimLong) {
4148 codegen_->MaybeRecordImplicitNullCheck(instruction);
4149 }
4150
Roland Levillain4d027112015-07-01 15:41:14 +01004151 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 Register temp = locations->GetTemp(0).AsRegister<Register>();
4153 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004154 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 }
4156
Calin Juravle52c48962014-12-16 17:02:57 +00004157 if (is_volatile) {
4158 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4159 }
4160}
4161
4162void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4163 HandleFieldGet(instruction, instruction->GetFieldInfo());
4164}
4165
4166void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4167 HandleFieldGet(instruction, instruction->GetFieldInfo());
4168}
4169
4170void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4171 HandleFieldSet(instruction, instruction->GetFieldInfo());
4172}
4173
4174void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004175 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004176}
4177
4178void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4179 HandleFieldSet(instruction, instruction->GetFieldInfo());
4180}
4181
4182void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004183 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004184}
4185
4186void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4187 HandleFieldGet(instruction, instruction->GetFieldInfo());
4188}
4189
4190void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4191 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192}
4193
Calin Juravlee460d1d2015-09-29 04:52:17 +01004194void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4195 HUnresolvedInstanceFieldGet* instruction) {
4196 FieldAccessCallingConventionX86 calling_convention;
4197 codegen_->CreateUnresolvedFieldLocationSummary(
4198 instruction, instruction->GetFieldType(), calling_convention);
4199}
4200
4201void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4202 HUnresolvedInstanceFieldGet* instruction) {
4203 FieldAccessCallingConventionX86 calling_convention;
4204 codegen_->GenerateUnresolvedFieldAccess(instruction,
4205 instruction->GetFieldType(),
4206 instruction->GetFieldIndex(),
4207 instruction->GetDexPc(),
4208 calling_convention);
4209}
4210
4211void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4212 HUnresolvedInstanceFieldSet* instruction) {
4213 FieldAccessCallingConventionX86 calling_convention;
4214 codegen_->CreateUnresolvedFieldLocationSummary(
4215 instruction, instruction->GetFieldType(), calling_convention);
4216}
4217
4218void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4219 HUnresolvedInstanceFieldSet* instruction) {
4220 FieldAccessCallingConventionX86 calling_convention;
4221 codegen_->GenerateUnresolvedFieldAccess(instruction,
4222 instruction->GetFieldType(),
4223 instruction->GetFieldIndex(),
4224 instruction->GetDexPc(),
4225 calling_convention);
4226}
4227
4228void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4229 HUnresolvedStaticFieldGet* instruction) {
4230 FieldAccessCallingConventionX86 calling_convention;
4231 codegen_->CreateUnresolvedFieldLocationSummary(
4232 instruction, instruction->GetFieldType(), calling_convention);
4233}
4234
4235void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4236 HUnresolvedStaticFieldGet* instruction) {
4237 FieldAccessCallingConventionX86 calling_convention;
4238 codegen_->GenerateUnresolvedFieldAccess(instruction,
4239 instruction->GetFieldType(),
4240 instruction->GetFieldIndex(),
4241 instruction->GetDexPc(),
4242 calling_convention);
4243}
4244
4245void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4246 HUnresolvedStaticFieldSet* instruction) {
4247 FieldAccessCallingConventionX86 calling_convention;
4248 codegen_->CreateUnresolvedFieldLocationSummary(
4249 instruction, instruction->GetFieldType(), calling_convention);
4250}
4251
4252void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4253 HUnresolvedStaticFieldSet* instruction) {
4254 FieldAccessCallingConventionX86 calling_convention;
4255 codegen_->GenerateUnresolvedFieldAccess(instruction,
4256 instruction->GetFieldType(),
4257 instruction->GetFieldIndex(),
4258 instruction->GetDexPc(),
4259 calling_convention);
4260}
4261
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004263 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4264 ? LocationSummary::kCallOnSlowPath
4265 : LocationSummary::kNoCall;
4266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4267 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004268 ? Location::RequiresRegister()
4269 : Location::Any();
4270 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004271 if (instruction->HasUses()) {
4272 locations->SetOut(Location::SameAsFirstInput());
4273 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274}
4275
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004276void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004277 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4278 return;
4279 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004280 LocationSummary* locations = instruction->GetLocations();
4281 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004282
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004283 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4284 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4285}
4286
4287void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004288 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289 codegen_->AddSlowPath(slow_path);
4290
4291 LocationSummary* locations = instruction->GetLocations();
4292 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293
4294 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004295 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004296 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004298 } else {
4299 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004300 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004301 __ jmp(slow_path->GetEntryLabel());
4302 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 }
4304 __ j(kEqual, slow_path->GetEntryLabel());
4305}
4306
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004307void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004308 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004309 GenerateImplicitNullCheck(instruction);
4310 } else {
4311 GenerateExplicitNullCheck(instruction);
4312 }
4313}
4314
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004315void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004316 LocationSummary* locations =
4317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004318 locations->SetInAt(0, Location::RequiresRegister());
4319 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004320 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4322 } else {
4323 // The output overlaps in case of long: we don't want the low move to overwrite
4324 // the array's location.
4325 locations->SetOut(Location::RequiresRegister(),
4326 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4327 : Location::kNoOutputOverlap);
4328 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004329}
4330
4331void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4332 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004333 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004334 Location index = locations->InAt(1);
4335
Calin Juravle77520bc2015-01-12 18:45:46 +00004336 Primitive::Type type = instruction->GetType();
4337 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004338 case Primitive::kPrimBoolean: {
4339 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004340 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 if (index.IsConstant()) {
4342 __ movzxb(out, Address(obj,
4343 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4344 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004345 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 }
4347 break;
4348 }
4349
4350 case Primitive::kPrimByte: {
4351 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 if (index.IsConstant()) {
4354 __ movsxb(out, Address(obj,
4355 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4356 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004357 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 }
4359 break;
4360 }
4361
4362 case Primitive::kPrimShort: {
4363 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004364 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004365 if (index.IsConstant()) {
4366 __ movsxw(out, Address(obj,
4367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4368 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004369 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 }
4371 break;
4372 }
4373
4374 case Primitive::kPrimChar: {
4375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004376 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004377 if (index.IsConstant()) {
4378 __ movzxw(out, Address(obj,
4379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4380 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 }
4383 break;
4384 }
4385
4386 case Primitive::kPrimInt:
4387 case Primitive::kPrimNot: {
4388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004389 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390 if (index.IsConstant()) {
4391 __ movl(out, Address(obj,
4392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4393 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004395 }
4396 break;
4397 }
4398
4399 case Primitive::kPrimLong: {
4400 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004401 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004402 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 if (index.IsConstant()) {
4404 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004405 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004406 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004407 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004409 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004411 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004412 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004414 }
4415 break;
4416 }
4417
Mark Mendell7c8d0092015-01-26 11:21:33 -05004418 case Primitive::kPrimFloat: {
4419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4420 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4421 if (index.IsConstant()) {
4422 __ movss(out, Address(obj,
4423 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4424 } else {
4425 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4426 }
4427 break;
4428 }
4429
4430 case Primitive::kPrimDouble: {
4431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4432 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4433 if (index.IsConstant()) {
4434 __ movsd(out, Address(obj,
4435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4436 } else {
4437 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4438 }
4439 break;
4440 }
4441
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004443 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004444 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004445 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004446
4447 if (type != Primitive::kPrimLong) {
4448 codegen_->MaybeRecordImplicitNullCheck(instruction);
4449 }
Roland Levillain4d027112015-07-01 15:41:14 +01004450
4451 if (type == Primitive::kPrimNot) {
4452 Register out = locations->Out().AsRegister<Register>();
4453 __ MaybeUnpoisonHeapReference(out);
4454 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455}
4456
4457void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004458 // This location builder might end up asking to up to four registers, which is
4459 // not currently possible for baseline. The situation in which we need four
4460 // registers cannot be met by baseline though, because it has not run any
4461 // optimization.
4462
Nicolas Geoffray39468442014-09-02 15:17:15 +01004463 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004464 bool needs_write_barrier =
4465 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4466
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004467 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004468
Nicolas Geoffray39468442014-09-02 15:17:15 +01004469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4470 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004471 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004472
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004473 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4474 || (value_type == Primitive::kPrimByte);
4475 // We need the inputs to be different than the output in case of long operation.
4476 // In case of a byte operation, the register allocator does not support multiple
4477 // inputs that die at entry with one in a specific register.
4478 locations->SetInAt(0, Location::RequiresRegister());
4479 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4480 if (is_byte_type) {
4481 // Ensure the value is in a byte register.
4482 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4483 } else if (Primitive::IsFloatingPointType(value_type)) {
4484 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004485 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004486 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4487 }
4488 if (needs_write_barrier) {
4489 // Temporary registers for the write barrier.
4490 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4491 // Ensure the card is in a byte register.
4492 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004494}
4495
4496void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4497 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004498 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004500 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004501 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004502 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4503 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4504 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4505 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004506 bool needs_write_barrier =
4507 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508
4509 switch (value_type) {
4510 case Primitive::kPrimBoolean:
4511 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004512 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4513 Address address = index.IsConstant()
4514 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4515 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4516 if (value.IsRegister()) {
4517 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004518 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004519 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522 break;
4523 }
4524
4525 case Primitive::kPrimShort:
4526 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004527 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4528 Address address = index.IsConstant()
4529 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4530 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4531 if (value.IsRegister()) {
4532 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004534 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537 break;
4538 }
4539
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004540 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004541 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4542 Address address = index.IsConstant()
4543 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4544 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4545 if (!value.IsRegister()) {
4546 // Just setting null.
4547 DCHECK(instruction->InputAt(2)->IsNullConstant());
4548 DCHECK(value.IsConstant()) << value;
4549 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004550 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004551 DCHECK(!needs_write_barrier);
4552 DCHECK(!may_need_runtime_call);
4553 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004555
4556 DCHECK(needs_write_barrier);
4557 Register register_value = value.AsRegister<Register>();
4558 NearLabel done, not_null, do_put;
4559 SlowPathCode* slow_path = nullptr;
4560 Register temp = locations->GetTemp(0).AsRegister<Register>();
4561 if (may_need_runtime_call) {
4562 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4563 codegen_->AddSlowPath(slow_path);
4564 if (instruction->GetValueCanBeNull()) {
4565 __ testl(register_value, register_value);
4566 __ j(kNotEqual, &not_null);
4567 __ movl(address, Immediate(0));
4568 codegen_->MaybeRecordImplicitNullCheck(instruction);
4569 __ jmp(&done);
4570 __ Bind(&not_null);
4571 }
4572
4573 __ movl(temp, Address(array, class_offset));
4574 codegen_->MaybeRecordImplicitNullCheck(instruction);
4575 __ MaybeUnpoisonHeapReference(temp);
4576 __ movl(temp, Address(temp, component_offset));
4577 // No need to poison/unpoison, we're comparing two poisoned references.
4578 __ cmpl(temp, Address(register_value, class_offset));
4579 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4580 __ j(kEqual, &do_put);
4581 __ MaybeUnpoisonHeapReference(temp);
4582 __ movl(temp, Address(temp, super_offset));
4583 // No need to unpoison, we're comparing against null..
4584 __ testl(temp, temp);
4585 __ j(kNotEqual, slow_path->GetEntryLabel());
4586 __ Bind(&do_put);
4587 } else {
4588 __ j(kNotEqual, slow_path->GetEntryLabel());
4589 }
4590 }
4591
4592 if (kPoisonHeapReferences) {
4593 __ movl(temp, register_value);
4594 __ PoisonHeapReference(temp);
4595 __ movl(address, temp);
4596 } else {
4597 __ movl(address, register_value);
4598 }
4599 if (!may_need_runtime_call) {
4600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 }
4602
4603 Register card = locations->GetTemp(1).AsRegister<Register>();
4604 codegen_->MarkGCCard(
4605 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4606 __ Bind(&done);
4607
4608 if (slow_path != nullptr) {
4609 __ Bind(slow_path->GetExitLabel());
4610 }
4611
4612 break;
4613 }
4614 case Primitive::kPrimInt: {
4615 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4616 Address address = index.IsConstant()
4617 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4618 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4619 if (value.IsRegister()) {
4620 __ movl(address, value.AsRegister<Register>());
4621 } else {
4622 DCHECK(value.IsConstant()) << value;
4623 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4624 __ movl(address, Immediate(v));
4625 }
4626 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 break;
4628 }
4629
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 case Primitive::kPrimLong: {
4631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 if (index.IsConstant()) {
4633 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004634 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004635 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004638 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004639 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004640 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004641 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004643 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004644 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004646 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004648 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004649 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004650 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004651 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004652 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004653 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004654 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004655 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004656 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004657 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004658 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004659 Immediate(High32Bits(val)));
4660 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004661 }
4662 break;
4663 }
4664
Mark Mendell7c8d0092015-01-26 11:21:33 -05004665 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004666 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4667 Address address = index.IsConstant()
4668 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4669 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004670 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 __ movss(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004672 break;
4673 }
4674
4675 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004676 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4677 Address address = index.IsConstant()
4678 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4679 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004680 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004681 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004682 break;
4683 }
4684
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685 case Primitive::kPrimVoid:
4686 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004687 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 }
4689}
4690
4691void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004693 locations->SetInAt(0, Location::RequiresRegister());
4694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695}
4696
4697void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4698 LocationSummary* locations = instruction->GetLocations();
4699 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004700 Register obj = locations->InAt(0).AsRegister<Register>();
4701 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004703 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704}
4705
4706void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004707 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4708 ? LocationSummary::kCallOnSlowPath
4709 : LocationSummary::kNoCall;
4710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004711 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004712 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004713 if (instruction->HasUses()) {
4714 locations->SetOut(Location::SameAsFirstInput());
4715 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716}
4717
4718void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4719 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004720 Location index_loc = locations->InAt(0);
4721 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004722 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004723 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004724
Mark Mendell99dbd682015-04-22 16:18:52 -04004725 if (length_loc.IsConstant()) {
4726 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4727 if (index_loc.IsConstant()) {
4728 // BCE will remove the bounds check if we are guarenteed to pass.
4729 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4730 if (index < 0 || index >= length) {
4731 codegen_->AddSlowPath(slow_path);
4732 __ jmp(slow_path->GetEntryLabel());
4733 } else {
4734 // Some optimization after BCE may have generated this, and we should not
4735 // generate a bounds check if it is a valid range.
4736 }
4737 return;
4738 }
4739
4740 // We have to reverse the jump condition because the length is the constant.
4741 Register index_reg = index_loc.AsRegister<Register>();
4742 __ cmpl(index_reg, Immediate(length));
4743 codegen_->AddSlowPath(slow_path);
4744 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004745 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004746 Register length = length_loc.AsRegister<Register>();
4747 if (index_loc.IsConstant()) {
4748 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4749 __ cmpl(length, Immediate(value));
4750 } else {
4751 __ cmpl(length, index_loc.AsRegister<Register>());
4752 }
4753 codegen_->AddSlowPath(slow_path);
4754 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004755 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004756}
4757
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004758void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4759 temp->SetLocations(nullptr);
4760}
4761
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004762void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763 // Nothing to do, this is driven by the code generator.
4764}
4765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004766void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004767 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004768}
4769
4770void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004771 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4772}
4773
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004774void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4775 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4776}
4777
4778void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004779 HBasicBlock* block = instruction->GetBlock();
4780 if (block->GetLoopInformation() != nullptr) {
4781 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4782 // The back edge will generate the suspend check.
4783 return;
4784 }
4785 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4786 // The goto will generate the suspend check.
4787 return;
4788 }
4789 GenerateSuspendCheck(instruction, nullptr);
4790}
4791
4792void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4793 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004794 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004795 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4796 if (slow_path == nullptr) {
4797 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4798 instruction->SetSlowPath(slow_path);
4799 codegen_->AddSlowPath(slow_path);
4800 if (successor != nullptr) {
4801 DCHECK(successor->IsLoopHeader());
4802 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4803 }
4804 } else {
4805 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4806 }
4807
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004808 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004809 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004810 if (successor == nullptr) {
4811 __ j(kNotEqual, slow_path->GetEntryLabel());
4812 __ Bind(slow_path->GetReturnLabel());
4813 } else {
4814 __ j(kEqual, codegen_->GetLabelOf(successor));
4815 __ jmp(slow_path->GetEntryLabel());
4816 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004817}
4818
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004819X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4820 return codegen_->GetAssembler();
4821}
4822
Mark Mendell7c8d0092015-01-26 11:21:33 -05004823void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004824 ScratchRegisterScope ensure_scratch(
4825 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4826 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4827 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4828 __ movl(temp_reg, Address(ESP, src + stack_offset));
4829 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004830}
4831
4832void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004833 ScratchRegisterScope ensure_scratch(
4834 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4835 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4836 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4837 __ movl(temp_reg, Address(ESP, src + stack_offset));
4838 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4839 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4840 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004841}
4842
4843void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004844 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004845 Location source = move->GetSource();
4846 Location destination = move->GetDestination();
4847
4848 if (source.IsRegister()) {
4849 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004850 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004851 } else {
4852 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004853 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004854 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004855 } else if (source.IsFpuRegister()) {
4856 if (destination.IsFpuRegister()) {
4857 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4858 } else if (destination.IsStackSlot()) {
4859 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4860 } else {
4861 DCHECK(destination.IsDoubleStackSlot());
4862 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4863 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004864 } else if (source.IsStackSlot()) {
4865 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004866 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004867 } else if (destination.IsFpuRegister()) {
4868 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004869 } else {
4870 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004871 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4872 }
4873 } else if (source.IsDoubleStackSlot()) {
4874 if (destination.IsFpuRegister()) {
4875 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4876 } else {
4877 DCHECK(destination.IsDoubleStackSlot()) << destination;
4878 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004879 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004880 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004881 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004882 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004883 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004884 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004885 if (value == 0) {
4886 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4887 } else {
4888 __ movl(destination.AsRegister<Register>(), Immediate(value));
4889 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004890 } else {
4891 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004892 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004893 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004894 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004895 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004896 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004897 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004898 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004899 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4900 if (value == 0) {
4901 // Easy handling of 0.0.
4902 __ xorps(dest, dest);
4903 } else {
4904 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004905 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4906 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4907 __ movl(temp, Immediate(value));
4908 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004909 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004910 } else {
4911 DCHECK(destination.IsStackSlot()) << destination;
4912 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4913 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004914 } else if (constant->IsLongConstant()) {
4915 int64_t value = constant->AsLongConstant()->GetValue();
4916 int32_t low_value = Low32Bits(value);
4917 int32_t high_value = High32Bits(value);
4918 Immediate low(low_value);
4919 Immediate high(high_value);
4920 if (destination.IsDoubleStackSlot()) {
4921 __ movl(Address(ESP, destination.GetStackIndex()), low);
4922 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4923 } else {
4924 __ movl(destination.AsRegisterPairLow<Register>(), low);
4925 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4926 }
4927 } else {
4928 DCHECK(constant->IsDoubleConstant());
4929 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004930 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004931 int32_t low_value = Low32Bits(value);
4932 int32_t high_value = High32Bits(value);
4933 Immediate low(low_value);
4934 Immediate high(high_value);
4935 if (destination.IsFpuRegister()) {
4936 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4937 if (value == 0) {
4938 // Easy handling of 0.0.
4939 __ xorpd(dest, dest);
4940 } else {
4941 __ pushl(high);
4942 __ pushl(low);
4943 __ movsd(dest, Address(ESP, 0));
4944 __ addl(ESP, Immediate(8));
4945 }
4946 } else {
4947 DCHECK(destination.IsDoubleStackSlot()) << destination;
4948 __ movl(Address(ESP, destination.GetStackIndex()), low);
4949 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4950 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004951 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004952 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004953 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004954 }
4955}
4956
Mark Mendella5c19ce2015-04-01 12:51:05 -04004957void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004958 Register suggested_scratch = reg == EAX ? EBX : EAX;
4959 ScratchRegisterScope ensure_scratch(
4960 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4961
4962 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4963 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4964 __ movl(Address(ESP, mem + stack_offset), reg);
4965 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004966}
4967
Mark Mendell7c8d0092015-01-26 11:21:33 -05004968void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004969 ScratchRegisterScope ensure_scratch(
4970 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4971
4972 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4973 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4974 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4975 __ movss(Address(ESP, mem + stack_offset), reg);
4976 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004977}
4978
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004979void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004980 ScratchRegisterScope ensure_scratch1(
4981 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004982
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004983 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4984 ScratchRegisterScope ensure_scratch2(
4985 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004986
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004987 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4988 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4989 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4990 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4991 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4992 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004993}
4994
4995void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004996 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004997 Location source = move->GetSource();
4998 Location destination = move->GetDestination();
4999
5000 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005001 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5002 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5003 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5004 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5005 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005006 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005007 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005008 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005009 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005010 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5011 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005012 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5013 // Use XOR Swap algorithm to avoid a temporary.
5014 DCHECK_NE(source.reg(), destination.reg());
5015 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5016 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5017 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5018 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5019 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5020 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5021 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005022 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5023 // Take advantage of the 16 bytes in the XMM register.
5024 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5025 Address stack(ESP, destination.GetStackIndex());
5026 // Load the double into the high doubleword.
5027 __ movhpd(reg, stack);
5028
5029 // Store the low double into the destination.
5030 __ movsd(stack, reg);
5031
5032 // Move the high double to the low double.
5033 __ psrldq(reg, Immediate(8));
5034 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5035 // Take advantage of the 16 bytes in the XMM register.
5036 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5037 Address stack(ESP, source.GetStackIndex());
5038 // Load the double into the high doubleword.
5039 __ movhpd(reg, stack);
5040
5041 // Store the low double into the destination.
5042 __ movsd(stack, reg);
5043
5044 // Move the high double to the low double.
5045 __ psrldq(reg, Immediate(8));
5046 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5047 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5048 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005049 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005050 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005051 }
5052}
5053
5054void ParallelMoveResolverX86::SpillScratch(int reg) {
5055 __ pushl(static_cast<Register>(reg));
5056}
5057
5058void ParallelMoveResolverX86::RestoreScratch(int reg) {
5059 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005060}
5061
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005062void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005063 InvokeRuntimeCallingConvention calling_convention;
5064 CodeGenerator::CreateLoadClassLocationSummary(
5065 cls,
5066 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5067 Location::RegisterLocation(EAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068}
5069
5070void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005071 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005072 if (cls->NeedsAccessCheck()) {
5073 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5074 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5075 cls,
5076 cls->GetDexPc(),
5077 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005078 return;
5079 }
5080
5081 Register out = locations->Out().AsRegister<Register>();
5082 Register current_method = locations->InAt(0).AsRegister<Register>();
5083 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005084 DCHECK(!cls->CanCallRuntime());
5085 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07005086 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005087 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005088 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005089 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01005090 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005091 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005092 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005093
Andreas Gampe85b62f22015-09-09 13:15:38 -07005094 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005095 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5096 codegen_->AddSlowPath(slow_path);
5097 __ testl(out, out);
5098 __ j(kEqual, slow_path->GetEntryLabel());
5099 if (cls->MustGenerateClinitCheck()) {
5100 GenerateClassInitializationCheck(slow_path, out);
5101 } else {
5102 __ Bind(slow_path->GetExitLabel());
5103 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005104 }
5105}
5106
5107void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5108 LocationSummary* locations =
5109 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5110 locations->SetInAt(0, Location::RequiresRegister());
5111 if (check->HasUses()) {
5112 locations->SetOut(Location::SameAsFirstInput());
5113 }
5114}
5115
5116void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005117 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005118 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005119 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005120 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005121 GenerateClassInitializationCheck(slow_path,
5122 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005123}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005124
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005125void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005126 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005127 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5128 Immediate(mirror::Class::kStatusInitialized));
5129 __ j(kLess, slow_path->GetEntryLabel());
5130 __ Bind(slow_path->GetExitLabel());
5131 // No need for memory fence, thanks to the X86 memory model.
5132}
5133
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005134void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5135 LocationSummary* locations =
5136 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005137 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005138 locations->SetOut(Location::RequiresRegister());
5139}
5140
5141void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005142 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005143 codegen_->AddSlowPath(slow_path);
5144
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005145 LocationSummary* locations = load->GetLocations();
5146 Register out = locations->Out().AsRegister<Register>();
5147 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005148 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005149 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005150 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005151 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005152 __ testl(out, out);
5153 __ j(kEqual, slow_path->GetEntryLabel());
5154 __ Bind(slow_path->GetExitLabel());
5155}
5156
David Brazdilcb1c0552015-08-04 16:22:25 +01005157static Address GetExceptionTlsAddress() {
5158 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5159}
5160
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005161void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5162 LocationSummary* locations =
5163 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5164 locations->SetOut(Location::RequiresRegister());
5165}
5166
5167void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005168 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5169}
5170
5171void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5172 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5173}
5174
5175void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5176 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005177}
5178
5179void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5180 LocationSummary* locations =
5181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5182 InvokeRuntimeCallingConvention calling_convention;
5183 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5184}
5185
5186void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005187 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5188 instruction,
5189 instruction->GetDexPc(),
5190 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005191}
5192
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005193void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005194 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5195 switch (instruction->GetTypeCheckKind()) {
5196 case TypeCheckKind::kExactCheck:
5197 case TypeCheckKind::kAbstractClassCheck:
5198 case TypeCheckKind::kClassHierarchyCheck:
5199 case TypeCheckKind::kArrayObjectCheck:
5200 call_kind = LocationSummary::kNoCall;
5201 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005202 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005203 case TypeCheckKind::kInterfaceCheck:
5204 call_kind = LocationSummary::kCall;
5205 break;
5206 case TypeCheckKind::kArrayCheck:
5207 call_kind = LocationSummary::kCallOnSlowPath;
5208 break;
5209 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005211 if (call_kind != LocationSummary::kCall) {
5212 locations->SetInAt(0, Location::RequiresRegister());
5213 locations->SetInAt(1, Location::Any());
5214 // Note that TypeCheckSlowPathX86 uses this register too.
5215 locations->SetOut(Location::RequiresRegister());
5216 } else {
5217 InvokeRuntimeCallingConvention calling_convention;
5218 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5219 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5220 locations->SetOut(Location::RegisterLocation(EAX));
5221 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005222}
5223
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005224void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005225 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005226 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005227 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005228 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005229 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005230 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5231 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5232 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005233 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005234 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005235
5236 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005237 // Avoid null check if we know obj is not null.
5238 if (instruction->MustDoNullCheck()) {
5239 __ testl(obj, obj);
5240 __ j(kEqual, &zero);
5241 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005242
Calin Juravle98893e12015-10-02 21:05:03 +01005243 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005244 // This is safe, as the register is caller-save, and the object must be in another
5245 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01005246 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
5247 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005248 ? obj
5249 : out;
5250 __ movl(target, Address(obj, class_offset));
5251 __ MaybeUnpoisonHeapReference(target);
5252
5253 switch (instruction->GetTypeCheckKind()) {
5254 case TypeCheckKind::kExactCheck: {
5255 if (cls.IsRegister()) {
5256 __ cmpl(out, cls.AsRegister<Register>());
5257 } else {
5258 DCHECK(cls.IsStackSlot()) << cls;
5259 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5260 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005262 // Classes must be equal for the instanceof to succeed.
5263 __ j(kNotEqual, &zero);
5264 __ movl(out, Immediate(1));
5265 __ jmp(&done);
5266 break;
5267 }
5268 case TypeCheckKind::kAbstractClassCheck: {
5269 // If the class is abstract, we eagerly fetch the super class of the
5270 // object to avoid doing a comparison we know will fail.
5271 NearLabel loop;
5272 __ Bind(&loop);
5273 __ movl(out, Address(out, super_offset));
5274 __ MaybeUnpoisonHeapReference(out);
5275 __ testl(out, out);
5276 // If `out` is null, we use it for the result, and jump to `done`.
5277 __ j(kEqual, &done);
5278 if (cls.IsRegister()) {
5279 __ cmpl(out, cls.AsRegister<Register>());
5280 } else {
5281 DCHECK(cls.IsStackSlot()) << cls;
5282 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5283 }
5284 __ j(kNotEqual, &loop);
5285 __ movl(out, Immediate(1));
5286 if (zero.IsLinked()) {
5287 __ jmp(&done);
5288 }
5289 break;
5290 }
5291 case TypeCheckKind::kClassHierarchyCheck: {
5292 // Walk over the class hierarchy to find a match.
5293 NearLabel loop, success;
5294 __ Bind(&loop);
5295 if (cls.IsRegister()) {
5296 __ cmpl(out, cls.AsRegister<Register>());
5297 } else {
5298 DCHECK(cls.IsStackSlot()) << cls;
5299 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5300 }
5301 __ j(kEqual, &success);
5302 __ movl(out, Address(out, super_offset));
5303 __ MaybeUnpoisonHeapReference(out);
5304 __ testl(out, out);
5305 __ j(kNotEqual, &loop);
5306 // If `out` is null, we use it for the result, and jump to `done`.
5307 __ jmp(&done);
5308 __ Bind(&success);
5309 __ movl(out, Immediate(1));
5310 if (zero.IsLinked()) {
5311 __ jmp(&done);
5312 }
5313 break;
5314 }
5315 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005316 // Do an exact check.
5317 NearLabel exact_check;
5318 if (cls.IsRegister()) {
5319 __ cmpl(out, cls.AsRegister<Register>());
5320 } else {
5321 DCHECK(cls.IsStackSlot()) << cls;
5322 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5323 }
5324 __ j(kEqual, &exact_check);
5325 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005326 __ movl(out, Address(out, component_offset));
5327 __ MaybeUnpoisonHeapReference(out);
5328 __ testl(out, out);
5329 // If `out` is null, we use it for the result, and jump to `done`.
5330 __ j(kEqual, &done);
5331 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5332 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005333 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005334 __ movl(out, Immediate(1));
5335 __ jmp(&done);
5336 break;
5337 }
5338 case TypeCheckKind::kArrayCheck: {
5339 if (cls.IsRegister()) {
5340 __ cmpl(out, cls.AsRegister<Register>());
5341 } else {
5342 DCHECK(cls.IsStackSlot()) << cls;
5343 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5344 }
5345 DCHECK(locations->OnlyCallsOnSlowPath());
5346 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5347 instruction, /* is_fatal */ false);
5348 codegen_->AddSlowPath(slow_path);
5349 __ j(kNotEqual, slow_path->GetEntryLabel());
5350 __ movl(out, Immediate(1));
5351 if (zero.IsLinked()) {
5352 __ jmp(&done);
5353 }
5354 break;
5355 }
Calin Juravle98893e12015-10-02 21:05:03 +01005356 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005357 case TypeCheckKind::kInterfaceCheck:
5358 default: {
5359 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5360 instruction,
5361 instruction->GetDexPc(),
5362 nullptr);
5363 if (zero.IsLinked()) {
5364 __ jmp(&done);
5365 }
5366 break;
5367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005368 }
5369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005370 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005371 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005372 __ xorl(out, out);
5373 }
5374
5375 if (done.IsLinked()) {
5376 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005377 }
5378
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005379 if (slow_path != nullptr) {
5380 __ Bind(slow_path->GetExitLabel());
5381 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005382}
5383
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005384void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005385 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5386 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5387
5388 switch (instruction->GetTypeCheckKind()) {
5389 case TypeCheckKind::kExactCheck:
5390 case TypeCheckKind::kAbstractClassCheck:
5391 case TypeCheckKind::kClassHierarchyCheck:
5392 case TypeCheckKind::kArrayObjectCheck:
5393 call_kind = throws_into_catch
5394 ? LocationSummary::kCallOnSlowPath
5395 : LocationSummary::kNoCall;
5396 break;
5397 case TypeCheckKind::kInterfaceCheck:
Calin Juravle98893e12015-10-02 21:05:03 +01005398 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 call_kind = LocationSummary::kCall;
5400 break;
5401 case TypeCheckKind::kArrayCheck:
5402 call_kind = LocationSummary::kCallOnSlowPath;
5403 break;
5404 }
5405
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005407 instruction, call_kind);
5408 if (call_kind != LocationSummary::kCall) {
5409 locations->SetInAt(0, Location::RequiresRegister());
5410 locations->SetInAt(1, Location::Any());
5411 // Note that TypeCheckSlowPathX86 uses this register too.
5412 locations->AddTemp(Location::RequiresRegister());
5413 } else {
5414 InvokeRuntimeCallingConvention calling_convention;
5415 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5416 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5417 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005418}
5419
5420void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5421 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005422 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005423 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424 Register temp = locations->WillCall()
5425 ? kNoRegister
5426 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005427
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5429 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5430 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5431 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5432 SlowPathCode* slow_path = nullptr;
5433
5434 if (!locations->WillCall()) {
5435 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5436 instruction, !locations->CanCall());
5437 codegen_->AddSlowPath(slow_path);
5438 }
5439
5440 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005441 // Avoid null check if we know obj is not null.
5442 if (instruction->MustDoNullCheck()) {
5443 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005445 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005446
5447 if (locations->WillCall()) {
5448 __ movl(obj, Address(obj, class_offset));
5449 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005450 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 __ movl(temp, Address(obj, class_offset));
5452 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005453 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454
5455 switch (instruction->GetTypeCheckKind()) {
5456 case TypeCheckKind::kExactCheck:
5457 case TypeCheckKind::kArrayCheck: {
5458 if (cls.IsRegister()) {
5459 __ cmpl(temp, cls.AsRegister<Register>());
5460 } else {
5461 DCHECK(cls.IsStackSlot()) << cls;
5462 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5463 }
5464 // Jump to slow path for throwing the exception or doing a
5465 // more involved array check.
5466 __ j(kNotEqual, slow_path->GetEntryLabel());
5467 break;
5468 }
5469 case TypeCheckKind::kAbstractClassCheck: {
5470 // If the class is abstract, we eagerly fetch the super class of the
5471 // object to avoid doing a comparison we know will fail.
5472 NearLabel loop, success;
5473 __ Bind(&loop);
5474 __ movl(temp, Address(temp, super_offset));
5475 __ MaybeUnpoisonHeapReference(temp);
5476 __ testl(temp, temp);
5477 // Jump to the slow path to throw the exception.
5478 __ j(kEqual, slow_path->GetEntryLabel());
5479 if (cls.IsRegister()) {
5480 __ cmpl(temp, cls.AsRegister<Register>());
5481 } else {
5482 DCHECK(cls.IsStackSlot()) << cls;
5483 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5484 }
5485 __ j(kNotEqual, &loop);
5486 break;
5487 }
5488 case TypeCheckKind::kClassHierarchyCheck: {
5489 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005490 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 __ Bind(&loop);
5492 if (cls.IsRegister()) {
5493 __ cmpl(temp, cls.AsRegister<Register>());
5494 } else {
5495 DCHECK(cls.IsStackSlot()) << cls;
5496 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5497 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005498 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 __ movl(temp, Address(temp, super_offset));
5500 __ MaybeUnpoisonHeapReference(temp);
5501 __ testl(temp, temp);
5502 __ j(kNotEqual, &loop);
5503 // Jump to the slow path to throw the exception.
5504 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 break;
5506 }
5507 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005508 // Do an exact check.
5509 if (cls.IsRegister()) {
5510 __ cmpl(temp, cls.AsRegister<Register>());
5511 } else {
5512 DCHECK(cls.IsStackSlot()) << cls;
5513 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5514 }
5515 __ j(kEqual, &done);
5516 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005517 __ movl(temp, Address(temp, component_offset));
5518 __ MaybeUnpoisonHeapReference(temp);
5519 __ testl(temp, temp);
5520 __ j(kEqual, slow_path->GetEntryLabel());
5521 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5522 __ j(kNotEqual, slow_path->GetEntryLabel());
5523 break;
5524 }
Calin Juravle98893e12015-10-02 21:05:03 +01005525 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005526 case TypeCheckKind::kInterfaceCheck:
5527 default:
5528 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5529 instruction,
5530 instruction->GetDexPc(),
5531 nullptr);
5532 break;
5533 }
5534 __ Bind(&done);
5535
5536 if (slow_path != nullptr) {
5537 __ Bind(slow_path->GetExitLabel());
5538 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005539}
5540
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005541void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5542 LocationSummary* locations =
5543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5544 InvokeRuntimeCallingConvention calling_convention;
5545 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5546}
5547
5548void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005549 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5550 : QUICK_ENTRY_POINT(pUnlockObject),
5551 instruction,
5552 instruction->GetDexPc(),
5553 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005554}
5555
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005556void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5557void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5558void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5559
5560void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5561 LocationSummary* locations =
5562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5563 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5564 || instruction->GetResultType() == Primitive::kPrimLong);
5565 locations->SetInAt(0, Location::RequiresRegister());
5566 locations->SetInAt(1, Location::Any());
5567 locations->SetOut(Location::SameAsFirstInput());
5568}
5569
5570void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5571 HandleBitwiseOperation(instruction);
5572}
5573
5574void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5575 HandleBitwiseOperation(instruction);
5576}
5577
5578void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5579 HandleBitwiseOperation(instruction);
5580}
5581
5582void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5583 LocationSummary* locations = instruction->GetLocations();
5584 Location first = locations->InAt(0);
5585 Location second = locations->InAt(1);
5586 DCHECK(first.Equals(locations->Out()));
5587
5588 if (instruction->GetResultType() == Primitive::kPrimInt) {
5589 if (second.IsRegister()) {
5590 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005591 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005592 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005593 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005594 } else {
5595 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005596 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005597 }
5598 } else if (second.IsConstant()) {
5599 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005600 __ andl(first.AsRegister<Register>(),
5601 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005602 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005603 __ orl(first.AsRegister<Register>(),
5604 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005605 } else {
5606 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005607 __ xorl(first.AsRegister<Register>(),
5608 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005609 }
5610 } else {
5611 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005613 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005615 } else {
5616 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005617 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005618 }
5619 }
5620 } else {
5621 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5622 if (second.IsRegisterPair()) {
5623 if (instruction->IsAnd()) {
5624 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5625 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5626 } else if (instruction->IsOr()) {
5627 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5628 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5629 } else {
5630 DCHECK(instruction->IsXor());
5631 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5632 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5633 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005634 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005635 if (instruction->IsAnd()) {
5636 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5637 __ andl(first.AsRegisterPairHigh<Register>(),
5638 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5639 } else if (instruction->IsOr()) {
5640 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5641 __ orl(first.AsRegisterPairHigh<Register>(),
5642 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5643 } else {
5644 DCHECK(instruction->IsXor());
5645 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5646 __ xorl(first.AsRegisterPairHigh<Register>(),
5647 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5648 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005649 } else {
5650 DCHECK(second.IsConstant()) << second;
5651 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005652 int32_t low_value = Low32Bits(value);
5653 int32_t high_value = High32Bits(value);
5654 Immediate low(low_value);
5655 Immediate high(high_value);
5656 Register first_low = first.AsRegisterPairLow<Register>();
5657 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005658 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005659 if (low_value == 0) {
5660 __ xorl(first_low, first_low);
5661 } else if (low_value != -1) {
5662 __ andl(first_low, low);
5663 }
5664 if (high_value == 0) {
5665 __ xorl(first_high, first_high);
5666 } else if (high_value != -1) {
5667 __ andl(first_high, high);
5668 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005669 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005670 if (low_value != 0) {
5671 __ orl(first_low, low);
5672 }
5673 if (high_value != 0) {
5674 __ orl(first_high, high);
5675 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005676 } else {
5677 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005678 if (low_value != 0) {
5679 __ xorl(first_low, low);
5680 }
5681 if (high_value != 0) {
5682 __ xorl(first_high, high);
5683 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005684 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005685 }
5686 }
5687}
5688
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005689void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005690 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005691 LOG(FATAL) << "Unreachable";
5692}
5693
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005694void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005695 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005696 LOG(FATAL) << "Unreachable";
5697}
5698
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005699void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5700 DCHECK(codegen_->IsBaseline());
5701 LocationSummary* locations =
5702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5703 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5704}
5705
5706void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5707 DCHECK(codegen_->IsBaseline());
5708 // Will be generated at use site.
5709}
5710
Mark Mendellfe57faa2015-09-18 09:26:15 -04005711// Simple implementation of packed switch - generate cascaded compare/jumps.
5712void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5713 LocationSummary* locations =
5714 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5715 locations->SetInAt(0, Location::RequiresRegister());
5716}
5717
5718void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5719 int32_t lower_bound = switch_instr->GetStartValue();
5720 int32_t num_entries = switch_instr->GetNumEntries();
5721 LocationSummary* locations = switch_instr->GetLocations();
5722 Register value_reg = locations->InAt(0).AsRegister<Register>();
5723 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5724
5725 // Create a series of compare/jumps.
5726 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5727 for (int i = 0; i < num_entries; i++) {
5728 int32_t case_value = lower_bound + i;
5729 if (case_value == 0) {
5730 __ testl(value_reg, value_reg);
5731 } else {
5732 __ cmpl(value_reg, Immediate(case_value));
5733 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005734 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005735 }
5736
5737 // And the default for any other value.
5738 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5739 __ jmp(codegen_->GetLabelOf(default_block));
5740 }
5741}
5742
Mark Mendell805b3b52015-09-18 14:10:29 -04005743void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5744 LocationSummary* locations =
5745 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5746 locations->SetInAt(0, Location::RequiresRegister());
5747
5748 // Constant area pointer.
5749 locations->SetInAt(1, Location::RequiresRegister());
5750
5751 // And the temporary we need.
5752 locations->AddTemp(Location::RequiresRegister());
5753}
5754
5755void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5756 int32_t lower_bound = switch_instr->GetStartValue();
5757 int32_t num_entries = switch_instr->GetNumEntries();
5758 LocationSummary* locations = switch_instr->GetLocations();
5759 Register value_reg = locations->InAt(0).AsRegister<Register>();
5760 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5761
5762 // Optimizing has a jump area.
5763 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5764 Register constant_area = locations->InAt(1).AsRegister<Register>();
5765
5766 // Remove the bias, if needed.
5767 if (lower_bound != 0) {
5768 __ leal(temp_reg, Address(value_reg, -lower_bound));
5769 value_reg = temp_reg;
5770 }
5771
5772 // Is the value in range?
5773 DCHECK_GE(num_entries, 1);
5774 __ cmpl(value_reg, Immediate(num_entries - 1));
5775 __ j(kAbove, codegen_->GetLabelOf(default_block));
5776
5777 // We are in the range of the table.
5778 // Load (target-constant_area) from the jump table, indexing by the value.
5779 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
5780
5781 // Compute the actual target address by adding in constant_area.
5782 __ addl(temp_reg, constant_area);
5783
5784 // And jump.
5785 __ jmp(temp_reg);
5786}
5787
Mark Mendell0616ae02015-04-17 12:49:27 -04005788void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5789 HX86ComputeBaseMethodAddress* insn) {
5790 LocationSummary* locations =
5791 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5792 locations->SetOut(Location::RequiresRegister());
5793}
5794
5795void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5796 HX86ComputeBaseMethodAddress* insn) {
5797 LocationSummary* locations = insn->GetLocations();
5798 Register reg = locations->Out().AsRegister<Register>();
5799
5800 // Generate call to next instruction.
5801 Label next_instruction;
5802 __ call(&next_instruction);
5803 __ Bind(&next_instruction);
5804
5805 // Remember this offset for later use with constant area.
5806 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5807
5808 // Grab the return address off the stack.
5809 __ popl(reg);
5810}
5811
5812void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5813 HX86LoadFromConstantTable* insn) {
5814 LocationSummary* locations =
5815 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5816
5817 locations->SetInAt(0, Location::RequiresRegister());
5818 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5819
5820 // If we don't need to be materialized, we only need the inputs to be set.
5821 if (!insn->NeedsMaterialization()) {
5822 return;
5823 }
5824
5825 switch (insn->GetType()) {
5826 case Primitive::kPrimFloat:
5827 case Primitive::kPrimDouble:
5828 locations->SetOut(Location::RequiresFpuRegister());
5829 break;
5830
5831 case Primitive::kPrimInt:
5832 locations->SetOut(Location::RequiresRegister());
5833 break;
5834
5835 default:
5836 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5837 }
5838}
5839
5840void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5841 if (!insn->NeedsMaterialization()) {
5842 return;
5843 }
5844
5845 LocationSummary* locations = insn->GetLocations();
5846 Location out = locations->Out();
5847 Register const_area = locations->InAt(0).AsRegister<Register>();
5848 HConstant *value = insn->GetConstant();
5849
5850 switch (insn->GetType()) {
5851 case Primitive::kPrimFloat:
5852 __ movss(out.AsFpuRegister<XmmRegister>(),
5853 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5854 break;
5855
5856 case Primitive::kPrimDouble:
5857 __ movsd(out.AsFpuRegister<XmmRegister>(),
5858 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5859 break;
5860
5861 case Primitive::kPrimInt:
5862 __ movl(out.AsRegister<Register>(),
5863 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5864 break;
5865
5866 default:
5867 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5868 }
5869}
5870
Mark Mendell0616ae02015-04-17 12:49:27 -04005871/**
5872 * Class to handle late fixup of offsets into constant area.
5873 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005874class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04005875 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04005876 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
5877 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5878
5879 protected:
5880 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5881
5882 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005883
5884 private:
5885 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5886 // Patch the correct offset for the instruction. The place to patch is the
5887 // last 4 bytes of the instruction.
5888 // The value to patch is the distance from the offset in the constant area
5889 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04005890 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5891 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04005892
5893 // Patch in the right value.
5894 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5895 }
5896
Mark Mendell0616ae02015-04-17 12:49:27 -04005897 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04005898 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005899};
5900
Mark Mendell805b3b52015-09-18 14:10:29 -04005901/**
5902 * Class to handle late fixup of offsets to a jump table that will be created in the
5903 * constant area.
5904 */
5905class JumpTableRIPFixup : public RIPFixup {
5906 public:
5907 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
5908 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
5909
5910 void CreateJumpTable() {
5911 X86Assembler* assembler = codegen_->GetAssembler();
5912
5913 // Ensure that the reference to the jump table has the correct offset.
5914 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5915 SetOffset(offset_in_constant_table);
5916
5917 // The label values in the jump table are computed relative to the
5918 // instruction addressing the constant area.
5919 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
5920
5921 // Populate the jump table with the correct values for the jump table.
5922 int32_t num_entries = switch_instr_->GetNumEntries();
5923 HBasicBlock* block = switch_instr_->GetBlock();
5924 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5925 // The value that we want is the target offset - the position of the table.
5926 for (int32_t i = 0; i < num_entries; i++) {
5927 HBasicBlock* b = successors[i];
5928 Label* l = codegen_->GetLabelOf(b);
5929 DCHECK(l->IsBound());
5930 int32_t offset_to_block = l->Position() - relative_offset;
5931 assembler->AppendInt32(offset_to_block);
5932 }
5933 }
5934
5935 private:
5936 const HX86PackedSwitch* switch_instr_;
5937};
5938
5939void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5940 // Generate the constant area if needed.
5941 X86Assembler* assembler = GetAssembler();
5942 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5943 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5944 // byte values.
5945 assembler->Align(4, 0);
5946 constant_area_start_ = assembler->CodeSize();
5947
5948 // Populate any jump tables.
5949 for (auto jump_table : fixups_to_jump_tables_) {
5950 jump_table->CreateJumpTable();
5951 }
5952
5953 // And now add the constant area to the generated code.
5954 assembler->AddConstantArea();
5955 }
5956
5957 // And finish up.
5958 CodeGenerator::Finalize(allocator);
5959}
5960
Mark Mendell0616ae02015-04-17 12:49:27 -04005961Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5962 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5963 return Address(reg, kDummy32BitOffset, fixup);
5964}
5965
5966Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5967 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5968 return Address(reg, kDummy32BitOffset, fixup);
5969}
5970
5971Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5972 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5973 return Address(reg, kDummy32BitOffset, fixup);
5974}
5975
5976Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5977 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5978 return Address(reg, kDummy32BitOffset, fixup);
5979}
5980
Mark Mendell805b3b52015-09-18 14:10:29 -04005981Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
5982 Register reg,
5983 Register value) {
5984 // Create a fixup to be used to create and address the jump table.
5985 JumpTableRIPFixup* table_fixup =
5986 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5987
5988 // We have to populate the jump tables.
5989 fixups_to_jump_tables_.push_back(table_fixup);
5990
5991 // We want a scaled address, as we are extracting the correct offset from the table.
5992 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
5993}
5994
Andreas Gampe85b62f22015-09-09 13:15:38 -07005995// TODO: target as memory.
5996void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5997 if (!target.IsValid()) {
5998 DCHECK(type == Primitive::kPrimVoid);
5999 return;
6000 }
6001
6002 DCHECK_NE(type, Primitive::kPrimVoid);
6003
6004 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6005 if (target.Equals(return_loc)) {
6006 return;
6007 }
6008
6009 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6010 // with the else branch.
6011 if (type == Primitive::kPrimLong) {
6012 HParallelMove parallel_move(GetGraph()->GetArena());
6013 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6014 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6015 GetMoveResolver()->EmitNativeCode(&parallel_move);
6016 } else {
6017 // Let the parallel move resolver take care of all of this.
6018 HParallelMove parallel_move(GetGraph()->GetArena());
6019 parallel_move.AddMove(return_loc, target, type, nullptr);
6020 GetMoveResolver()->EmitNativeCode(&parallel_move);
6021 }
6022}
6023
Roland Levillain4d027112015-07-01 15:41:14 +01006024#undef __
6025
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006026} // namespace x86
6027} // namespace art