blob: 2aea859b7d9b9e13b5ae9bff9a2b5d750e4a3108 [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);
1309 DCHECK(cond->IsCondition());
1310 if (cond->AsCondition()->NeedsMaterialization()) {
1311 locations->SetInAt(0, Location::Any());
1312 }
1313}
1314
1315void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001316 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001317 DeoptimizationSlowPathX86(deoptimize);
1318 codegen_->AddSlowPath(slow_path);
1319 Label* slow_path_entry = slow_path->GetEntryLabel();
1320 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1321}
1322
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001324 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001325}
1326
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001327void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1328 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001329}
1330
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001332 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333}
1334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001335void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001336 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001337}
1338
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 switch (store->InputAt(1)->GetType()) {
1343 case Primitive::kPrimBoolean:
1344 case Primitive::kPrimByte:
1345 case Primitive::kPrimChar:
1346 case Primitive::kPrimShort:
1347 case Primitive::kPrimInt:
1348 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001349 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001350 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1351 break;
1352
1353 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001354 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001355 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1356 break;
1357
1358 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001359 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001360 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001361}
1362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001363void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001364}
1365
Roland Levillain0d37cd02015-05-27 16:39:19 +01001366void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001367 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001368 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001369 // Handle the long/FP comparisons made in instruction simplification.
1370 switch (cond->InputAt(0)->GetType()) {
1371 case Primitive::kPrimLong: {
1372 locations->SetInAt(0, Location::RequiresRegister());
1373 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1374 if (cond->NeedsMaterialization()) {
1375 locations->SetOut(Location::RequiresRegister());
1376 }
1377 break;
1378 }
1379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble: {
1381 locations->SetInAt(0, Location::RequiresFpuRegister());
1382 locations->SetInAt(1, Location::RequiresFpuRegister());
1383 if (cond->NeedsMaterialization()) {
1384 locations->SetOut(Location::RequiresRegister());
1385 }
1386 break;
1387 }
1388 default:
1389 locations->SetInAt(0, Location::RequiresRegister());
1390 locations->SetInAt(1, Location::Any());
1391 if (cond->NeedsMaterialization()) {
1392 // We need a byte register.
1393 locations->SetOut(Location::RegisterLocation(ECX));
1394 }
1395 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001396 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397}
1398
Roland Levillain0d37cd02015-05-27 16:39:19 +01001399void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001400 if (!cond->NeedsMaterialization()) {
1401 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001402 }
Mark Mendellc4701932015-04-10 13:18:51 -04001403
1404 LocationSummary* locations = cond->GetLocations();
1405 Location lhs = locations->InAt(0);
1406 Location rhs = locations->InAt(1);
1407 Register reg = locations->Out().AsRegister<Register>();
1408 Label true_label, false_label;
1409
1410 switch (cond->InputAt(0)->GetType()) {
1411 default: {
1412 // Integer case.
1413
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001414 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001415 __ xorl(reg, reg);
1416
1417 if (rhs.IsRegister()) {
1418 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1419 } else if (rhs.IsConstant()) {
1420 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1421 if (constant == 0) {
1422 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1423 } else {
1424 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1425 }
1426 } else {
1427 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1428 }
Aart Bike9f37602015-10-09 11:15:55 -07001429 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001430 return;
1431 }
1432 case Primitive::kPrimLong:
1433 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1434 break;
1435 case Primitive::kPrimFloat:
1436 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1437 GenerateFPJumps(cond, &true_label, &false_label);
1438 break;
1439 case Primitive::kPrimDouble:
1440 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1441 GenerateFPJumps(cond, &true_label, &false_label);
1442 break;
1443 }
1444
1445 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001446 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001447
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001449 __ Bind(&false_label);
1450 __ xorl(reg, reg);
1451 __ jmp(&done_label);
1452
Roland Levillain4fa13f62015-07-06 18:11:54 +01001453 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001454 __ Bind(&true_label);
1455 __ movl(reg, Immediate(1));
1456 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001457}
1458
1459void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1460 VisitCondition(comp);
1461}
1462
1463void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1464 VisitCondition(comp);
1465}
1466
1467void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1468 VisitCondition(comp);
1469}
1470
1471void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1472 VisitCondition(comp);
1473}
1474
1475void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1476 VisitCondition(comp);
1477}
1478
1479void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1480 VisitCondition(comp);
1481}
1482
1483void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1484 VisitCondition(comp);
1485}
1486
1487void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1488 VisitCondition(comp);
1489}
1490
1491void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1492 VisitCondition(comp);
1493}
1494
1495void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1496 VisitCondition(comp);
1497}
1498
1499void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1500 VisitCondition(comp);
1501}
1502
1503void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1504 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001505}
1506
Aart Bike9f37602015-10-09 11:15:55 -07001507void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1508 VisitCondition(comp);
1509}
1510
1511void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1512 VisitCondition(comp);
1513}
1514
1515void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1516 VisitCondition(comp);
1517}
1518
1519void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1520 VisitCondition(comp);
1521}
1522
1523void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1524 VisitCondition(comp);
1525}
1526
1527void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1528 VisitCondition(comp);
1529}
1530
1531void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1532 VisitCondition(comp);
1533}
1534
1535void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1536 VisitCondition(comp);
1537}
1538
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001542 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001543}
1544
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001545void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001546 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001547}
1548
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001549void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1550 LocationSummary* locations =
1551 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1552 locations->SetOut(Location::ConstantLocation(constant));
1553}
1554
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001555void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001556 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001557}
1558
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001560 LocationSummary* locations =
1561 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001562 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563}
1564
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001565void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 // Will be generated at use site.
1567}
1568
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001569void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1570 LocationSummary* locations =
1571 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1572 locations->SetOut(Location::ConstantLocation(constant));
1573}
1574
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001575void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001576 // Will be generated at use site.
1577}
1578
1579void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1580 LocationSummary* locations =
1581 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1582 locations->SetOut(Location::ConstantLocation(constant));
1583}
1584
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001585void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001586 // Will be generated at use site.
1587}
1588
Calin Juravle27df7582015-04-17 19:12:31 +01001589void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1590 memory_barrier->SetLocations(nullptr);
1591}
1592
1593void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1594 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1595}
1596
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001597void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001598 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001599}
1600
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001601void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001602 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001603}
1604
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001605void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001606 LocationSummary* locations =
1607 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001608 switch (ret->InputAt(0)->GetType()) {
1609 case Primitive::kPrimBoolean:
1610 case Primitive::kPrimByte:
1611 case Primitive::kPrimChar:
1612 case Primitive::kPrimShort:
1613 case Primitive::kPrimInt:
1614 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001616 break;
1617
1618 case Primitive::kPrimLong:
1619 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001620 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001621 break;
1622
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001623 case Primitive::kPrimFloat:
1624 case Primitive::kPrimDouble:
1625 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001627 break;
1628
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001629 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001630 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001631 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001632}
1633
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001634void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635 if (kIsDebugBuild) {
1636 switch (ret->InputAt(0)->GetType()) {
1637 case Primitive::kPrimBoolean:
1638 case Primitive::kPrimByte:
1639 case Primitive::kPrimChar:
1640 case Primitive::kPrimShort:
1641 case Primitive::kPrimInt:
1642 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001643 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001644 break;
1645
1646 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001647 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1648 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001649 break;
1650
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001651 case Primitive::kPrimFloat:
1652 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 break;
1655
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001656 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001657 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001658 }
1659 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001660 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661}
1662
Calin Juravle175dc732015-08-25 15:42:32 +01001663void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1664 // The trampoline uses the same calling convention as dex calling conventions,
1665 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1666 // the method_idx.
1667 HandleInvoke(invoke);
1668}
1669
1670void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1671 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1672}
1673
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001674void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001675 // When we do not run baseline, explicit clinit checks triggered by static
1676 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1677 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001678
Mark Mendellfb8d2792015-03-31 22:16:59 -04001679 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001680 if (intrinsic.TryDispatch(invoke)) {
1681 return;
1682 }
1683
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001684 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001685
1686 if (codegen_->IsBaseline()) {
1687 // Baseline does not have enough registers if the current method also
1688 // needs a register. We therefore do not require a register for it, and let
1689 // the code generation of the invoke handle it.
1690 LocationSummary* locations = invoke->GetLocations();
1691 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1692 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1693 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1694 }
1695 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001696}
1697
Mark Mendell09ed1a32015-03-25 08:30:06 -04001698static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1699 if (invoke->GetLocations()->Intrinsified()) {
1700 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1701 intrinsic.Dispatch(invoke);
1702 return true;
1703 }
1704 return false;
1705}
1706
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001707void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001708 // When we do not run baseline, explicit clinit checks triggered by static
1709 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1710 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001711
Mark Mendell09ed1a32015-03-25 08:30:06 -04001712 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1713 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001714 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001715
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001716 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001717 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001718 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001719 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001720}
1721
1722void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1723 HandleInvoke(invoke);
1724}
1725
1726void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001727 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001728 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001729}
1730
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001731void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001732 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1733 return;
1734 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001735
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001736 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001737 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001738 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001739}
1740
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001741void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1742 HandleInvoke(invoke);
1743 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001744 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001745}
1746
1747void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1748 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001749 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001750 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1751 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001752 LocationSummary* locations = invoke->GetLocations();
1753 Location receiver = locations->InAt(0);
1754 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1755
1756 // Set the hidden argument.
1757 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001759
1760 // temp = object->GetClass();
1761 if (receiver.IsStackSlot()) {
1762 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1763 __ movl(temp, Address(temp, class_offset));
1764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001765 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001766 }
Roland Levillain4d027112015-07-01 15:41:14 +01001767 codegen_->MaybeRecordImplicitNullCheck(invoke);
1768 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001769 // temp = temp->GetImtEntryAt(method_offset);
1770 __ movl(temp, Address(temp, method_offset));
1771 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001773 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001774
1775 DCHECK(!codegen_->IsLeafMethod());
1776 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1777}
1778
Roland Levillain88cb1752014-10-20 16:36:47 +01001779void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1782 switch (neg->GetResultType()) {
1783 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001784 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001785 locations->SetInAt(0, Location::RequiresRegister());
1786 locations->SetOut(Location::SameAsFirstInput());
1787 break;
1788
Roland Levillain88cb1752014-10-20 16:36:47 +01001789 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001790 locations->SetInAt(0, Location::RequiresFpuRegister());
1791 locations->SetOut(Location::SameAsFirstInput());
1792 locations->AddTemp(Location::RequiresRegister());
1793 locations->AddTemp(Location::RequiresFpuRegister());
1794 break;
1795
Roland Levillain88cb1752014-10-20 16:36:47 +01001796 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001797 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001798 locations->SetOut(Location::SameAsFirstInput());
1799 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001800 break;
1801
1802 default:
1803 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1804 }
1805}
1806
1807void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1808 LocationSummary* locations = neg->GetLocations();
1809 Location out = locations->Out();
1810 Location in = locations->InAt(0);
1811 switch (neg->GetResultType()) {
1812 case Primitive::kPrimInt:
1813 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001814 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001815 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001816 break;
1817
1818 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001819 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001820 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001821 __ negl(out.AsRegisterPairLow<Register>());
1822 // Negation is similar to subtraction from zero. The least
1823 // significant byte triggers a borrow when it is different from
1824 // zero; to take it into account, add 1 to the most significant
1825 // byte if the carry flag (CF) is set to 1 after the first NEGL
1826 // operation.
1827 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1828 __ negl(out.AsRegisterPairHigh<Register>());
1829 break;
1830
Roland Levillain5368c212014-11-27 15:03:41 +00001831 case Primitive::kPrimFloat: {
1832 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001833 Register constant = locations->GetTemp(0).AsRegister<Register>();
1834 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001835 // Implement float negation with an exclusive or with value
1836 // 0x80000000 (mask for bit 31, representing the sign of a
1837 // single-precision floating-point number).
1838 __ movl(constant, Immediate(INT32_C(0x80000000)));
1839 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001840 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001841 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001842 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001843
Roland Levillain5368c212014-11-27 15:03:41 +00001844 case Primitive::kPrimDouble: {
1845 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001846 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001847 // Implement double negation with an exclusive or with value
1848 // 0x8000000000000000 (mask for bit 63, representing the sign of
1849 // a double-precision floating-point number).
1850 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001851 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001852 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001853 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001854
1855 default:
1856 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1857 }
1858}
1859
Roland Levillaindff1f282014-11-05 14:15:05 +00001860void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 Primitive::Type result_type = conversion->GetResultType();
1862 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001863 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001864
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001865 // The float-to-long and double-to-long type conversions rely on a
1866 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001867 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001868 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1869 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001870 ? LocationSummary::kCall
1871 : LocationSummary::kNoCall;
1872 LocationSummary* locations =
1873 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1874
David Brazdilb2bd1c52015-03-25 11:17:37 +00001875 // The Java language does not allow treating boolean as an integral type but
1876 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001877
Roland Levillaindff1f282014-11-05 14:15:05 +00001878 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001879 case Primitive::kPrimByte:
1880 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001881 case Primitive::kPrimBoolean:
1882 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001883 case Primitive::kPrimShort:
1884 case Primitive::kPrimInt:
1885 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001886 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001887 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1888 // Make the output overlap to please the register allocator. This greatly simplifies
1889 // the validation of the linear scan implementation
1890 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001891 break;
1892
1893 default:
1894 LOG(FATAL) << "Unexpected type conversion from " << input_type
1895 << " to " << result_type;
1896 }
1897 break;
1898
Roland Levillain01a8d712014-11-14 16:27:39 +00001899 case Primitive::kPrimShort:
1900 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001901 case Primitive::kPrimBoolean:
1902 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001903 case Primitive::kPrimByte:
1904 case Primitive::kPrimInt:
1905 case Primitive::kPrimChar:
1906 // Processing a Dex `int-to-short' instruction.
1907 locations->SetInAt(0, Location::Any());
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909 break;
1910
1911 default:
1912 LOG(FATAL) << "Unexpected type conversion from " << input_type
1913 << " to " << result_type;
1914 }
1915 break;
1916
Roland Levillain946e1432014-11-11 17:35:19 +00001917 case Primitive::kPrimInt:
1918 switch (input_type) {
1919 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001920 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001921 locations->SetInAt(0, Location::Any());
1922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1923 break;
1924
1925 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001926 // Processing a Dex `float-to-int' instruction.
1927 locations->SetInAt(0, Location::RequiresFpuRegister());
1928 locations->SetOut(Location::RequiresRegister());
1929 locations->AddTemp(Location::RequiresFpuRegister());
1930 break;
1931
Roland Levillain946e1432014-11-11 17:35:19 +00001932 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001933 // Processing a Dex `double-to-int' instruction.
1934 locations->SetInAt(0, Location::RequiresFpuRegister());
1935 locations->SetOut(Location::RequiresRegister());
1936 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001937 break;
1938
1939 default:
1940 LOG(FATAL) << "Unexpected type conversion from " << input_type
1941 << " to " << result_type;
1942 }
1943 break;
1944
Roland Levillaindff1f282014-11-05 14:15:05 +00001945 case Primitive::kPrimLong:
1946 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001947 case Primitive::kPrimBoolean:
1948 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001949 case Primitive::kPrimByte:
1950 case Primitive::kPrimShort:
1951 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001952 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001953 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001954 locations->SetInAt(0, Location::RegisterLocation(EAX));
1955 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1956 break;
1957
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001958 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001959 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001960 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001961 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001962 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1963 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1964
Vladimir Marko949c91f2015-01-27 10:48:44 +00001965 // The runtime helper puts the result in EAX, EDX.
1966 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001967 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001968 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001969
1970 default:
1971 LOG(FATAL) << "Unexpected type conversion from " << input_type
1972 << " to " << result_type;
1973 }
1974 break;
1975
Roland Levillain981e4542014-11-14 11:47:14 +00001976 case Primitive::kPrimChar:
1977 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001978 case Primitive::kPrimBoolean:
1979 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001980 case Primitive::kPrimByte:
1981 case Primitive::kPrimShort:
1982 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001983 // Processing a Dex `int-to-char' instruction.
1984 locations->SetInAt(0, Location::Any());
1985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1986 break;
1987
1988 default:
1989 LOG(FATAL) << "Unexpected type conversion from " << input_type
1990 << " to " << result_type;
1991 }
1992 break;
1993
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001995 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001996 case Primitive::kPrimBoolean:
1997 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001998 case Primitive::kPrimByte:
1999 case Primitive::kPrimShort:
2000 case Primitive::kPrimInt:
2001 case Primitive::kPrimChar:
2002 // Processing a Dex `int-to-float' instruction.
2003 locations->SetInAt(0, Location::RequiresRegister());
2004 locations->SetOut(Location::RequiresFpuRegister());
2005 break;
2006
2007 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002008 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002009 locations->SetInAt(0, Location::Any());
2010 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002011 break;
2012
Roland Levillaincff13742014-11-17 14:32:17 +00002013 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002014 // Processing a Dex `double-to-float' instruction.
2015 locations->SetInAt(0, Location::RequiresFpuRegister());
2016 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002017 break;
2018
2019 default:
2020 LOG(FATAL) << "Unexpected type conversion from " << input_type
2021 << " to " << result_type;
2022 };
2023 break;
2024
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002026 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002027 case Primitive::kPrimBoolean:
2028 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002029 case Primitive::kPrimByte:
2030 case Primitive::kPrimShort:
2031 case Primitive::kPrimInt:
2032 case Primitive::kPrimChar:
2033 // Processing a Dex `int-to-double' instruction.
2034 locations->SetInAt(0, Location::RequiresRegister());
2035 locations->SetOut(Location::RequiresFpuRegister());
2036 break;
2037
2038 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002039 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002040 locations->SetInAt(0, Location::Any());
2041 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002042 break;
2043
Roland Levillaincff13742014-11-17 14:32:17 +00002044 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002045 // Processing a Dex `float-to-double' instruction.
2046 locations->SetInAt(0, Location::RequiresFpuRegister());
2047 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002048 break;
2049
2050 default:
2051 LOG(FATAL) << "Unexpected type conversion from " << input_type
2052 << " to " << result_type;
2053 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected type conversion from " << input_type
2058 << " to " << result_type;
2059 }
2060}
2061
2062void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2063 LocationSummary* locations = conversion->GetLocations();
2064 Location out = locations->Out();
2065 Location in = locations->InAt(0);
2066 Primitive::Type result_type = conversion->GetResultType();
2067 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002068 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002069 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002070 case Primitive::kPrimByte:
2071 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002072 case Primitive::kPrimBoolean:
2073 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002074 case Primitive::kPrimShort:
2075 case Primitive::kPrimInt:
2076 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002077 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002078 if (in.IsRegister()) {
2079 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002080 } else {
2081 DCHECK(in.GetConstant()->IsIntConstant());
2082 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2083 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2084 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
Roland Levillain01a8d712014-11-14 16:27:39 +00002093 case Primitive::kPrimShort:
2094 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002095 case Primitive::kPrimBoolean:
2096 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002097 case Primitive::kPrimByte:
2098 case Primitive::kPrimInt:
2099 case Primitive::kPrimChar:
2100 // Processing a Dex `int-to-short' instruction.
2101 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002103 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002105 } else {
2106 DCHECK(in.GetConstant()->IsIntConstant());
2107 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002108 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002109 }
2110 break;
2111
2112 default:
2113 LOG(FATAL) << "Unexpected type conversion from " << input_type
2114 << " to " << result_type;
2115 }
2116 break;
2117
Roland Levillain946e1432014-11-11 17:35:19 +00002118 case Primitive::kPrimInt:
2119 switch (input_type) {
2120 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002121 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002122 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002124 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002125 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002126 } else {
2127 DCHECK(in.IsConstant());
2128 DCHECK(in.GetConstant()->IsLongConstant());
2129 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002131 }
2132 break;
2133
Roland Levillain3f8f9362014-12-02 17:45:01 +00002134 case Primitive::kPrimFloat: {
2135 // Processing a Dex `float-to-int' instruction.
2136 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2137 Register output = out.AsRegister<Register>();
2138 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002139 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002140
2141 __ movl(output, Immediate(kPrimIntMax));
2142 // temp = int-to-float(output)
2143 __ cvtsi2ss(temp, output);
2144 // if input >= temp goto done
2145 __ comiss(input, temp);
2146 __ j(kAboveEqual, &done);
2147 // if input == NaN goto nan
2148 __ j(kUnordered, &nan);
2149 // output = float-to-int-truncate(input)
2150 __ cvttss2si(output, input);
2151 __ jmp(&done);
2152 __ Bind(&nan);
2153 // output = 0
2154 __ xorl(output, output);
2155 __ Bind(&done);
2156 break;
2157 }
2158
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002159 case Primitive::kPrimDouble: {
2160 // Processing a Dex `double-to-int' instruction.
2161 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2162 Register output = out.AsRegister<Register>();
2163 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002164 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002165
2166 __ movl(output, Immediate(kPrimIntMax));
2167 // temp = int-to-double(output)
2168 __ cvtsi2sd(temp, output);
2169 // if input >= temp goto done
2170 __ comisd(input, temp);
2171 __ j(kAboveEqual, &done);
2172 // if input == NaN goto nan
2173 __ j(kUnordered, &nan);
2174 // output = double-to-int-truncate(input)
2175 __ cvttsd2si(output, input);
2176 __ jmp(&done);
2177 __ Bind(&nan);
2178 // output = 0
2179 __ xorl(output, output);
2180 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002181 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002182 }
Roland Levillain946e1432014-11-11 17:35:19 +00002183
2184 default:
2185 LOG(FATAL) << "Unexpected type conversion from " << input_type
2186 << " to " << result_type;
2187 }
2188 break;
2189
Roland Levillaindff1f282014-11-05 14:15:05 +00002190 case Primitive::kPrimLong:
2191 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002192 case Primitive::kPrimBoolean:
2193 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002194 case Primitive::kPrimByte:
2195 case Primitive::kPrimShort:
2196 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002197 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002198 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002199 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2200 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002201 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 __ cdq();
2203 break;
2204
2205 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002206 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002207 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2208 conversion,
2209 conversion->GetDexPc(),
2210 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002211 break;
2212
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002214 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002215 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2216 conversion,
2217 conversion->GetDexPc(),
2218 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002219 break;
2220
2221 default:
2222 LOG(FATAL) << "Unexpected type conversion from " << input_type
2223 << " to " << result_type;
2224 }
2225 break;
2226
Roland Levillain981e4542014-11-14 11:47:14 +00002227 case Primitive::kPrimChar:
2228 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002229 case Primitive::kPrimBoolean:
2230 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002231 case Primitive::kPrimByte:
2232 case Primitive::kPrimShort:
2233 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002234 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2235 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002237 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002239 } else {
2240 DCHECK(in.GetConstant()->IsIntConstant());
2241 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002243 }
2244 break;
2245
2246 default:
2247 LOG(FATAL) << "Unexpected type conversion from " << input_type
2248 << " to " << result_type;
2249 }
2250 break;
2251
Roland Levillaindff1f282014-11-05 14:15:05 +00002252 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002253 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002254 case Primitive::kPrimBoolean:
2255 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002256 case Primitive::kPrimByte:
2257 case Primitive::kPrimShort:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002260 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002262 break;
2263
Roland Levillain6d0e4832014-11-27 18:31:21 +00002264 case Primitive::kPrimLong: {
2265 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002266 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002267
Roland Levillain232ade02015-04-20 15:14:36 +01002268 // Create stack space for the call to
2269 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2270 // TODO: enhance register allocator to ask for stack temporaries.
2271 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2272 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2273 __ subl(ESP, Immediate(adjustment));
2274 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275
Roland Levillain232ade02015-04-20 15:14:36 +01002276 // Load the value to the FP stack, using temporaries if needed.
2277 PushOntoFPStack(in, 0, adjustment, false, true);
2278
2279 if (out.IsStackSlot()) {
2280 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2281 } else {
2282 __ fstps(Address(ESP, 0));
2283 Location stack_temp = Location::StackSlot(0);
2284 codegen_->Move32(out, stack_temp);
2285 }
2286
2287 // Remove the temporary stack space we allocated.
2288 if (adjustment != 0) {
2289 __ addl(ESP, Immediate(adjustment));
2290 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002291 break;
2292 }
2293
Roland Levillaincff13742014-11-17 14:32:17 +00002294 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002295 // Processing a Dex `double-to-float' instruction.
2296 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002297 break;
2298
2299 default:
2300 LOG(FATAL) << "Unexpected type conversion from " << input_type
2301 << " to " << result_type;
2302 };
2303 break;
2304
Roland Levillaindff1f282014-11-05 14:15:05 +00002305 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002306 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002307 case Primitive::kPrimBoolean:
2308 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002309 case Primitive::kPrimByte:
2310 case Primitive::kPrimShort:
2311 case Primitive::kPrimInt:
2312 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002313 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002314 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002315 break;
2316
Roland Levillain647b9ed2014-11-27 12:06:00 +00002317 case Primitive::kPrimLong: {
2318 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002319 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002320
Roland Levillain232ade02015-04-20 15:14:36 +01002321 // Create stack space for the call to
2322 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2323 // TODO: enhance register allocator to ask for stack temporaries.
2324 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2325 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2326 __ subl(ESP, Immediate(adjustment));
2327 }
2328
2329 // Load the value to the FP stack, using temporaries if needed.
2330 PushOntoFPStack(in, 0, adjustment, false, true);
2331
2332 if (out.IsDoubleStackSlot()) {
2333 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2334 } else {
2335 __ fstpl(Address(ESP, 0));
2336 Location stack_temp = Location::DoubleStackSlot(0);
2337 codegen_->Move64(out, stack_temp);
2338 }
2339
2340 // Remove the temporary stack space we allocated.
2341 if (adjustment != 0) {
2342 __ addl(ESP, Immediate(adjustment));
2343 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002344 break;
2345 }
2346
Roland Levillaincff13742014-11-17 14:32:17 +00002347 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002348 // Processing a Dex `float-to-double' instruction.
2349 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362}
2363
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002364void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002365 LocationSummary* locations =
2366 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002367 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002368 case Primitive::kPrimInt: {
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2372 break;
2373 }
2374
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002375 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002376 locations->SetInAt(0, Location::RequiresRegister());
2377 locations->SetInAt(1, Location::Any());
2378 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002379 break;
2380 }
2381
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002382 case Primitive::kPrimFloat:
2383 case Primitive::kPrimDouble: {
2384 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002385 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002386 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002387 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002388 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002389
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002390 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002391 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2392 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002393 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002394}
2395
2396void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2397 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002398 Location first = locations->InAt(0);
2399 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002400 Location out = locations->Out();
2401
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002402 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002403 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002404 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002405 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2406 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002407 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2408 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002409 } else {
2410 __ leal(out.AsRegister<Register>(), Address(
2411 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2412 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002413 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002414 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2415 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2416 __ addl(out.AsRegister<Register>(), Immediate(value));
2417 } else {
2418 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2419 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002420 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002421 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002422 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002423 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002424 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002425 }
2426
2427 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002428 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002429 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2430 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002431 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002432 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2433 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002434 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002435 } else {
2436 DCHECK(second.IsConstant()) << second;
2437 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2438 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2439 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002440 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002441 break;
2442 }
2443
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002444 case Primitive::kPrimFloat: {
2445 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002447 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2448 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2449 DCHECK(!const_area->NeedsMaterialization());
2450 __ addss(first.AsFpuRegister<XmmRegister>(),
2451 codegen_->LiteralFloatAddress(
2452 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2453 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2454 } else {
2455 DCHECK(second.IsStackSlot());
2456 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002457 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002458 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002459 }
2460
2461 case Primitive::kPrimDouble: {
2462 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002463 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002464 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2465 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2466 DCHECK(!const_area->NeedsMaterialization());
2467 __ addsd(first.AsFpuRegister<XmmRegister>(),
2468 codegen_->LiteralDoubleAddress(
2469 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2470 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2471 } else {
2472 DCHECK(second.IsDoubleStackSlot());
2473 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002474 }
2475 break;
2476 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002477
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002478 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002479 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002480 }
2481}
2482
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002483void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002484 LocationSummary* locations =
2485 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002486 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002487 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002488 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002489 locations->SetInAt(0, Location::RequiresRegister());
2490 locations->SetInAt(1, Location::Any());
2491 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002492 break;
2493 }
Calin Juravle11351682014-10-23 15:38:15 +01002494 case Primitive::kPrimFloat:
2495 case Primitive::kPrimDouble: {
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002497 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002498 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002499 break;
Calin Juravle11351682014-10-23 15:38:15 +01002500 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002501
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002502 default:
Calin Juravle11351682014-10-23 15:38:15 +01002503 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002504 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002505}
2506
2507void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2508 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002509 Location first = locations->InAt(0);
2510 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002511 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002512 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002513 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002514 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002516 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002517 __ subl(first.AsRegister<Register>(),
2518 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002519 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002520 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002521 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002522 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002523 }
2524
2525 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002526 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002527 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2528 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002529 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002530 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002531 __ sbbl(first.AsRegisterPairHigh<Register>(),
2532 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002533 } else {
2534 DCHECK(second.IsConstant()) << second;
2535 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2536 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2537 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002538 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002539 break;
2540 }
2541
Calin Juravle11351682014-10-23 15:38:15 +01002542 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002543 if (second.IsFpuRegister()) {
2544 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2545 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2546 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2547 DCHECK(!const_area->NeedsMaterialization());
2548 __ subss(first.AsFpuRegister<XmmRegister>(),
2549 codegen_->LiteralFloatAddress(
2550 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2551 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2552 } else {
2553 DCHECK(second.IsStackSlot());
2554 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2555 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002556 break;
Calin Juravle11351682014-10-23 15:38:15 +01002557 }
2558
2559 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002560 if (second.IsFpuRegister()) {
2561 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2562 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2563 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2564 DCHECK(!const_area->NeedsMaterialization());
2565 __ subsd(first.AsFpuRegister<XmmRegister>(),
2566 codegen_->LiteralDoubleAddress(
2567 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2568 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2569 } else {
2570 DCHECK(second.IsDoubleStackSlot());
2571 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2572 }
Calin Juravle11351682014-10-23 15:38:15 +01002573 break;
2574 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002575
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002576 default:
Calin Juravle11351682014-10-23 15:38:15 +01002577 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002578 }
2579}
2580
Calin Juravle34bacdf2014-10-07 20:23:36 +01002581void LocationsBuilderX86::VisitMul(HMul* mul) {
2582 LocationSummary* locations =
2583 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2584 switch (mul->GetResultType()) {
2585 case Primitive::kPrimInt:
2586 locations->SetInAt(0, Location::RequiresRegister());
2587 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002588 if (mul->InputAt(1)->IsIntConstant()) {
2589 // Can use 3 operand multiply.
2590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2591 } else {
2592 locations->SetOut(Location::SameAsFirstInput());
2593 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002594 break;
2595 case Primitive::kPrimLong: {
2596 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002597 locations->SetInAt(1, Location::Any());
2598 locations->SetOut(Location::SameAsFirstInput());
2599 // Needed for imul on 32bits with 64bits output.
2600 locations->AddTemp(Location::RegisterLocation(EAX));
2601 locations->AddTemp(Location::RegisterLocation(EDX));
2602 break;
2603 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002604 case Primitive::kPrimFloat:
2605 case Primitive::kPrimDouble: {
2606 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002607 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002608 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002609 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002610 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002611
2612 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002613 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002614 }
2615}
2616
2617void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2618 LocationSummary* locations = mul->GetLocations();
2619 Location first = locations->InAt(0);
2620 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002621 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002622
2623 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002624 case Primitive::kPrimInt:
2625 // The constant may have ended up in a register, so test explicitly to avoid
2626 // problems where the output may not be the same as the first operand.
2627 if (mul->InputAt(1)->IsIntConstant()) {
2628 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2629 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2630 } else if (second.IsRegister()) {
2631 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002632 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002633 } else {
2634 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002635 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002637 }
2638 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002639
2640 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641 Register in1_hi = first.AsRegisterPairHigh<Register>();
2642 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 Register eax = locations->GetTemp(0).AsRegister<Register>();
2644 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645
2646 DCHECK_EQ(EAX, eax);
2647 DCHECK_EQ(EDX, edx);
2648
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002649 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 // output: in1
2651 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2652 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2653 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002654 if (second.IsConstant()) {
2655 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002656
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002657 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2658 int32_t low_value = Low32Bits(value);
2659 int32_t high_value = High32Bits(value);
2660 Immediate low(low_value);
2661 Immediate high(high_value);
2662
2663 __ movl(eax, high);
2664 // eax <- in1.lo * in2.hi
2665 __ imull(eax, in1_lo);
2666 // in1.hi <- in1.hi * in2.lo
2667 __ imull(in1_hi, low);
2668 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2669 __ addl(in1_hi, eax);
2670 // move in2_lo to eax to prepare for double precision
2671 __ movl(eax, low);
2672 // edx:eax <- in1.lo * in2.lo
2673 __ mull(in1_lo);
2674 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2675 __ addl(in1_hi, edx);
2676 // in1.lo <- (in1.lo * in2.lo)[31:0];
2677 __ movl(in1_lo, eax);
2678 } else if (second.IsRegisterPair()) {
2679 Register in2_hi = second.AsRegisterPairHigh<Register>();
2680 Register in2_lo = second.AsRegisterPairLow<Register>();
2681
2682 __ movl(eax, in2_hi);
2683 // eax <- in1.lo * in2.hi
2684 __ imull(eax, in1_lo);
2685 // in1.hi <- in1.hi * in2.lo
2686 __ imull(in1_hi, in2_lo);
2687 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2688 __ addl(in1_hi, eax);
2689 // move in1_lo to eax to prepare for double precision
2690 __ movl(eax, in1_lo);
2691 // edx:eax <- in1.lo * in2.lo
2692 __ mull(in2_lo);
2693 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2694 __ addl(in1_hi, edx);
2695 // in1.lo <- (in1.lo * in2.lo)[31:0];
2696 __ movl(in1_lo, eax);
2697 } else {
2698 DCHECK(second.IsDoubleStackSlot()) << second;
2699 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2700 Address in2_lo(ESP, second.GetStackIndex());
2701
2702 __ movl(eax, in2_hi);
2703 // eax <- in1.lo * in2.hi
2704 __ imull(eax, in1_lo);
2705 // in1.hi <- in1.hi * in2.lo
2706 __ imull(in1_hi, in2_lo);
2707 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2708 __ addl(in1_hi, eax);
2709 // move in1_lo to eax to prepare for double precision
2710 __ movl(eax, in1_lo);
2711 // edx:eax <- in1.lo * in2.lo
2712 __ mull(in2_lo);
2713 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2714 __ addl(in1_hi, edx);
2715 // in1.lo <- (in1.lo * in2.lo)[31:0];
2716 __ movl(in1_lo, eax);
2717 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002718
2719 break;
2720 }
2721
Calin Juravleb5bfa962014-10-21 18:02:24 +01002722 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002723 DCHECK(first.Equals(locations->Out()));
2724 if (second.IsFpuRegister()) {
2725 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2726 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2727 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2728 DCHECK(!const_area->NeedsMaterialization());
2729 __ mulss(first.AsFpuRegister<XmmRegister>(),
2730 codegen_->LiteralFloatAddress(
2731 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2732 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2733 } else {
2734 DCHECK(second.IsStackSlot());
2735 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2736 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002737 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002738 }
2739
2740 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002741 DCHECK(first.Equals(locations->Out()));
2742 if (second.IsFpuRegister()) {
2743 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2744 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2745 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2746 DCHECK(!const_area->NeedsMaterialization());
2747 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2748 codegen_->LiteralDoubleAddress(
2749 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2750 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2751 } else {
2752 DCHECK(second.IsDoubleStackSlot());
2753 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2754 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002755 break;
2756 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002757
2758 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002759 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002760 }
2761}
2762
Roland Levillain232ade02015-04-20 15:14:36 +01002763void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2764 uint32_t temp_offset,
2765 uint32_t stack_adjustment,
2766 bool is_fp,
2767 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002768 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002769 DCHECK(!is_wide);
2770 if (is_fp) {
2771 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2772 } else {
2773 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2774 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002775 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002776 DCHECK(is_wide);
2777 if (is_fp) {
2778 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2779 } else {
2780 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2781 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002782 } else {
2783 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002784 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002785 Location stack_temp = Location::StackSlot(temp_offset);
2786 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002787 if (is_fp) {
2788 __ flds(Address(ESP, temp_offset));
2789 } else {
2790 __ filds(Address(ESP, temp_offset));
2791 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002792 } else {
2793 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2794 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002795 if (is_fp) {
2796 __ fldl(Address(ESP, temp_offset));
2797 } else {
2798 __ fildl(Address(ESP, temp_offset));
2799 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002800 }
2801 }
2802}
2803
2804void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2805 Primitive::Type type = rem->GetResultType();
2806 bool is_float = type == Primitive::kPrimFloat;
2807 size_t elem_size = Primitive::ComponentSize(type);
2808 LocationSummary* locations = rem->GetLocations();
2809 Location first = locations->InAt(0);
2810 Location second = locations->InAt(1);
2811 Location out = locations->Out();
2812
2813 // Create stack space for 2 elements.
2814 // TODO: enhance register allocator to ask for stack temporaries.
2815 __ subl(ESP, Immediate(2 * elem_size));
2816
2817 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002818 const bool is_wide = !is_float;
2819 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2820 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002821
2822 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002823 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002824 __ Bind(&retry);
2825 __ fprem();
2826
2827 // Move FP status to AX.
2828 __ fstsw();
2829
2830 // And see if the argument reduction is complete. This is signaled by the
2831 // C2 FPU flag bit set to 0.
2832 __ andl(EAX, Immediate(kC2ConditionMask));
2833 __ j(kNotEqual, &retry);
2834
2835 // We have settled on the final value. Retrieve it into an XMM register.
2836 // Store FP top of stack to real stack.
2837 if (is_float) {
2838 __ fsts(Address(ESP, 0));
2839 } else {
2840 __ fstl(Address(ESP, 0));
2841 }
2842
2843 // Pop the 2 items from the FP stack.
2844 __ fucompp();
2845
2846 // Load the value from the stack into an XMM register.
2847 DCHECK(out.IsFpuRegister()) << out;
2848 if (is_float) {
2849 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2850 } else {
2851 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2852 }
2853
2854 // And remove the temporary stack space we allocated.
2855 __ addl(ESP, Immediate(2 * elem_size));
2856}
2857
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002858
2859void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2860 DCHECK(instruction->IsDiv() || instruction->IsRem());
2861
2862 LocationSummary* locations = instruction->GetLocations();
2863 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002864 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002865
2866 Register out_register = locations->Out().AsRegister<Register>();
2867 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002868 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002869
2870 DCHECK(imm == 1 || imm == -1);
2871
2872 if (instruction->IsRem()) {
2873 __ xorl(out_register, out_register);
2874 } else {
2875 __ movl(out_register, input_register);
2876 if (imm == -1) {
2877 __ negl(out_register);
2878 }
2879 }
2880}
2881
2882
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002883void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002884 LocationSummary* locations = instruction->GetLocations();
2885
2886 Register out_register = locations->Out().AsRegister<Register>();
2887 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002888 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002889
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002890 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002891 Register num = locations->GetTemp(0).AsRegister<Register>();
2892
2893 __ leal(num, Address(input_register, std::abs(imm) - 1));
2894 __ testl(input_register, input_register);
2895 __ cmovl(kGreaterEqual, num, input_register);
2896 int shift = CTZ(imm);
2897 __ sarl(num, Immediate(shift));
2898
2899 if (imm < 0) {
2900 __ negl(num);
2901 }
2902
2903 __ movl(out_register, num);
2904}
2905
2906void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2907 DCHECK(instruction->IsDiv() || instruction->IsRem());
2908
2909 LocationSummary* locations = instruction->GetLocations();
2910 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2911
2912 Register eax = locations->InAt(0).AsRegister<Register>();
2913 Register out = locations->Out().AsRegister<Register>();
2914 Register num;
2915 Register edx;
2916
2917 if (instruction->IsDiv()) {
2918 edx = locations->GetTemp(0).AsRegister<Register>();
2919 num = locations->GetTemp(1).AsRegister<Register>();
2920 } else {
2921 edx = locations->Out().AsRegister<Register>();
2922 num = locations->GetTemp(0).AsRegister<Register>();
2923 }
2924
2925 DCHECK_EQ(EAX, eax);
2926 DCHECK_EQ(EDX, edx);
2927 if (instruction->IsDiv()) {
2928 DCHECK_EQ(EAX, out);
2929 } else {
2930 DCHECK_EQ(EDX, out);
2931 }
2932
2933 int64_t magic;
2934 int shift;
2935 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2936
Mark Mendell0c9497d2015-08-21 09:30:05 -04002937 NearLabel ndiv;
2938 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002939 // If numerator is 0, the result is 0, no computation needed.
2940 __ testl(eax, eax);
2941 __ j(kNotEqual, &ndiv);
2942
2943 __ xorl(out, out);
2944 __ jmp(&end);
2945
2946 __ Bind(&ndiv);
2947
2948 // Save the numerator.
2949 __ movl(num, eax);
2950
2951 // EAX = magic
2952 __ movl(eax, Immediate(magic));
2953
2954 // EDX:EAX = magic * numerator
2955 __ imull(num);
2956
2957 if (imm > 0 && magic < 0) {
2958 // EDX += num
2959 __ addl(edx, num);
2960 } else if (imm < 0 && magic > 0) {
2961 __ subl(edx, num);
2962 }
2963
2964 // Shift if needed.
2965 if (shift != 0) {
2966 __ sarl(edx, Immediate(shift));
2967 }
2968
2969 // EDX += 1 if EDX < 0
2970 __ movl(eax, edx);
2971 __ shrl(edx, Immediate(31));
2972 __ addl(edx, eax);
2973
2974 if (instruction->IsRem()) {
2975 __ movl(eax, num);
2976 __ imull(edx, Immediate(imm));
2977 __ subl(eax, edx);
2978 __ movl(edx, eax);
2979 } else {
2980 __ movl(eax, edx);
2981 }
2982 __ Bind(&end);
2983}
2984
Calin Juravlebacfec32014-11-14 15:54:36 +00002985void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2986 DCHECK(instruction->IsDiv() || instruction->IsRem());
2987
2988 LocationSummary* locations = instruction->GetLocations();
2989 Location out = locations->Out();
2990 Location first = locations->InAt(0);
2991 Location second = locations->InAt(1);
2992 bool is_div = instruction->IsDiv();
2993
2994 switch (instruction->GetResultType()) {
2995 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 DCHECK_EQ(EAX, first.AsRegister<Register>());
2997 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002998
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002999 if (instruction->InputAt(1)->IsIntConstant()) {
3000 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003001
3002 if (imm == 0) {
3003 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3004 } else if (imm == 1 || imm == -1) {
3005 DivRemOneOrMinusOne(instruction);
3006 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003007 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003008 } else {
3009 DCHECK(imm <= -2 || imm >= 2);
3010 GenerateDivRemWithAnyConstant(instruction);
3011 }
3012 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003013 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003014 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003015 is_div);
3016 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003017
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003018 Register second_reg = second.AsRegister<Register>();
3019 // 0x80000000/-1 triggers an arithmetic exception!
3020 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3021 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003022
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003023 __ cmpl(second_reg, Immediate(-1));
3024 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003025
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003026 // edx:eax <- sign-extended of eax
3027 __ cdq();
3028 // eax = quotient, edx = remainder
3029 __ idivl(second_reg);
3030 __ Bind(slow_path->GetExitLabel());
3031 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003032 break;
3033 }
3034
3035 case Primitive::kPrimLong: {
3036 InvokeRuntimeCallingConvention calling_convention;
3037 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3038 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3039 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3040 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3041 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3042 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3043
3044 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003045 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3046 instruction,
3047 instruction->GetDexPc(),
3048 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003049 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003050 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3051 instruction,
3052 instruction->GetDexPc(),
3053 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003054 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003055 break;
3056 }
3057
3058 default:
3059 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3060 }
3061}
3062
Calin Juravle7c4954d2014-10-28 16:57:40 +00003063void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003064 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003065 ? LocationSummary::kCall
3066 : LocationSummary::kNoCall;
3067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3068
Calin Juravle7c4954d2014-10-28 16:57:40 +00003069 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003070 case Primitive::kPrimInt: {
3071 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003072 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003073 locations->SetOut(Location::SameAsFirstInput());
3074 // Intel uses edx:eax as the dividend.
3075 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3077 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3078 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003079 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003080 locations->AddTemp(Location::RequiresRegister());
3081 }
Calin Juravled0d48522014-11-04 16:40:20 +00003082 break;
3083 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003084 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003085 InvokeRuntimeCallingConvention calling_convention;
3086 locations->SetInAt(0, Location::RegisterPairLocation(
3087 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3088 locations->SetInAt(1, Location::RegisterPairLocation(
3089 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3090 // Runtime helper puts the result in EAX, EDX.
3091 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003092 break;
3093 }
3094 case Primitive::kPrimFloat:
3095 case Primitive::kPrimDouble: {
3096 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003098 locations->SetOut(Location::SameAsFirstInput());
3099 break;
3100 }
3101
3102 default:
3103 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3104 }
3105}
3106
3107void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3108 LocationSummary* locations = div->GetLocations();
3109 Location first = locations->InAt(0);
3110 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003111
3112 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003113 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003114 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003115 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003116 break;
3117 }
3118
3119 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 if (second.IsFpuRegister()) {
3121 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3122 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3123 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3124 DCHECK(!const_area->NeedsMaterialization());
3125 __ divss(first.AsFpuRegister<XmmRegister>(),
3126 codegen_->LiteralFloatAddress(
3127 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3128 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3129 } else {
3130 DCHECK(second.IsStackSlot());
3131 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3132 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003133 break;
3134 }
3135
3136 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003137 if (second.IsFpuRegister()) {
3138 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3139 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3140 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3141 DCHECK(!const_area->NeedsMaterialization());
3142 __ divsd(first.AsFpuRegister<XmmRegister>(),
3143 codegen_->LiteralDoubleAddress(
3144 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3145 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3146 } else {
3147 DCHECK(second.IsDoubleStackSlot());
3148 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3149 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003150 break;
3151 }
3152
3153 default:
3154 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3155 }
3156}
3157
Calin Juravlebacfec32014-11-14 15:54:36 +00003158void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003159 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003160
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003161 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3162 ? LocationSummary::kCall
3163 : LocationSummary::kNoCall;
3164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003165
Calin Juravled2ec87d2014-12-08 14:24:46 +00003166 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003167 case Primitive::kPrimInt: {
3168 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003169 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003171 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3172 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3173 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003174 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003175 locations->AddTemp(Location::RequiresRegister());
3176 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003177 break;
3178 }
3179 case Primitive::kPrimLong: {
3180 InvokeRuntimeCallingConvention calling_convention;
3181 locations->SetInAt(0, Location::RegisterPairLocation(
3182 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3183 locations->SetInAt(1, Location::RegisterPairLocation(
3184 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3185 // Runtime helper puts the result in EAX, EDX.
3186 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3187 break;
3188 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003189 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003190 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003191 locations->SetInAt(0, Location::Any());
3192 locations->SetInAt(1, Location::Any());
3193 locations->SetOut(Location::RequiresFpuRegister());
3194 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003195 break;
3196 }
3197
3198 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003199 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003200 }
3201}
3202
3203void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3204 Primitive::Type type = rem->GetResultType();
3205 switch (type) {
3206 case Primitive::kPrimInt:
3207 case Primitive::kPrimLong: {
3208 GenerateDivRemIntegral(rem);
3209 break;
3210 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003211 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003212 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003213 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003214 break;
3215 }
3216 default:
3217 LOG(FATAL) << "Unexpected rem type " << type;
3218 }
3219}
3220
Calin Juravled0d48522014-11-04 16:40:20 +00003221void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003222 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3223 ? LocationSummary::kCallOnSlowPath
3224 : LocationSummary::kNoCall;
3225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003226 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003227 case Primitive::kPrimByte:
3228 case Primitive::kPrimChar:
3229 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003230 case Primitive::kPrimInt: {
3231 locations->SetInAt(0, Location::Any());
3232 break;
3233 }
3234 case Primitive::kPrimLong: {
3235 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3236 if (!instruction->IsConstant()) {
3237 locations->AddTemp(Location::RequiresRegister());
3238 }
3239 break;
3240 }
3241 default:
3242 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3243 }
Calin Juravled0d48522014-11-04 16:40:20 +00003244 if (instruction->HasUses()) {
3245 locations->SetOut(Location::SameAsFirstInput());
3246 }
3247}
3248
3249void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003250 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003251 codegen_->AddSlowPath(slow_path);
3252
3253 LocationSummary* locations = instruction->GetLocations();
3254 Location value = locations->InAt(0);
3255
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003256 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003257 case Primitive::kPrimByte:
3258 case Primitive::kPrimChar:
3259 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003260 case Primitive::kPrimInt: {
3261 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003263 __ j(kEqual, slow_path->GetEntryLabel());
3264 } else if (value.IsStackSlot()) {
3265 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3266 __ j(kEqual, slow_path->GetEntryLabel());
3267 } else {
3268 DCHECK(value.IsConstant()) << value;
3269 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3270 __ jmp(slow_path->GetEntryLabel());
3271 }
3272 }
3273 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003274 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003275 case Primitive::kPrimLong: {
3276 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003278 __ movl(temp, value.AsRegisterPairLow<Register>());
3279 __ orl(temp, value.AsRegisterPairHigh<Register>());
3280 __ j(kEqual, slow_path->GetEntryLabel());
3281 } else {
3282 DCHECK(value.IsConstant()) << value;
3283 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3284 __ jmp(slow_path->GetEntryLabel());
3285 }
3286 }
3287 break;
3288 }
3289 default:
3290 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003291 }
Calin Juravled0d48522014-11-04 16:40:20 +00003292}
3293
Calin Juravle9aec02f2014-11-18 23:06:35 +00003294void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3295 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3296
3297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3299
3300 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003301 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003302 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003303 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003304 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003305 // The shift count needs to be in CL or a constant.
3306 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003307 locations->SetOut(Location::SameAsFirstInput());
3308 break;
3309 }
3310 default:
3311 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3312 }
3313}
3314
3315void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3316 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3317
3318 LocationSummary* locations = op->GetLocations();
3319 Location first = locations->InAt(0);
3320 Location second = locations->InAt(1);
3321 DCHECK(first.Equals(locations->Out()));
3322
3323 switch (op->GetResultType()) {
3324 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003325 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003326 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003327 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003329 DCHECK_EQ(ECX, second_reg);
3330 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003331 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003332 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003333 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003334 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003335 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003336 }
3337 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003338 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3339 if (shift == 0) {
3340 return;
3341 }
3342 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003343 if (op->IsShl()) {
3344 __ shll(first_reg, imm);
3345 } else if (op->IsShr()) {
3346 __ sarl(first_reg, imm);
3347 } else {
3348 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003349 }
3350 }
3351 break;
3352 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003353 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003354 if (second.IsRegister()) {
3355 Register second_reg = second.AsRegister<Register>();
3356 DCHECK_EQ(ECX, second_reg);
3357 if (op->IsShl()) {
3358 GenerateShlLong(first, second_reg);
3359 } else if (op->IsShr()) {
3360 GenerateShrLong(first, second_reg);
3361 } else {
3362 GenerateUShrLong(first, second_reg);
3363 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003364 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003365 // Shift by a constant.
3366 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3367 // Nothing to do if the shift is 0, as the input is already the output.
3368 if (shift != 0) {
3369 if (op->IsShl()) {
3370 GenerateShlLong(first, shift);
3371 } else if (op->IsShr()) {
3372 GenerateShrLong(first, shift);
3373 } else {
3374 GenerateUShrLong(first, shift);
3375 }
3376 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003377 }
3378 break;
3379 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003380 default:
3381 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3382 }
3383}
3384
Mark P Mendell73945692015-04-29 14:56:17 +00003385void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3386 Register low = loc.AsRegisterPairLow<Register>();
3387 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003388 if (shift == 1) {
3389 // This is just an addition.
3390 __ addl(low, low);
3391 __ adcl(high, high);
3392 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003393 // Shift by 32 is easy. High gets low, and low gets 0.
3394 codegen_->EmitParallelMoves(
3395 loc.ToLow(),
3396 loc.ToHigh(),
3397 Primitive::kPrimInt,
3398 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3399 loc.ToLow(),
3400 Primitive::kPrimInt);
3401 } else if (shift > 32) {
3402 // Low part becomes 0. High part is low part << (shift-32).
3403 __ movl(high, low);
3404 __ shll(high, Immediate(shift - 32));
3405 __ xorl(low, low);
3406 } else {
3407 // Between 1 and 31.
3408 __ shld(high, low, Immediate(shift));
3409 __ shll(low, Immediate(shift));
3410 }
3411}
3412
Calin Juravle9aec02f2014-11-18 23:06:35 +00003413void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003414 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003415 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3416 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3417 __ testl(shifter, Immediate(32));
3418 __ j(kEqual, &done);
3419 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3420 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3421 __ Bind(&done);
3422}
3423
Mark P Mendell73945692015-04-29 14:56:17 +00003424void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3425 Register low = loc.AsRegisterPairLow<Register>();
3426 Register high = loc.AsRegisterPairHigh<Register>();
3427 if (shift == 32) {
3428 // Need to copy the sign.
3429 DCHECK_NE(low, high);
3430 __ movl(low, high);
3431 __ sarl(high, Immediate(31));
3432 } else if (shift > 32) {
3433 DCHECK_NE(low, high);
3434 // High part becomes sign. Low part is shifted by shift - 32.
3435 __ movl(low, high);
3436 __ sarl(high, Immediate(31));
3437 __ sarl(low, Immediate(shift - 32));
3438 } else {
3439 // Between 1 and 31.
3440 __ shrd(low, high, Immediate(shift));
3441 __ sarl(high, Immediate(shift));
3442 }
3443}
3444
Calin Juravle9aec02f2014-11-18 23:06:35 +00003445void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003446 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003447 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3448 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3449 __ testl(shifter, Immediate(32));
3450 __ j(kEqual, &done);
3451 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3452 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3453 __ Bind(&done);
3454}
3455
Mark P Mendell73945692015-04-29 14:56:17 +00003456void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3457 Register low = loc.AsRegisterPairLow<Register>();
3458 Register high = loc.AsRegisterPairHigh<Register>();
3459 if (shift == 32) {
3460 // Shift by 32 is easy. Low gets high, and high gets 0.
3461 codegen_->EmitParallelMoves(
3462 loc.ToHigh(),
3463 loc.ToLow(),
3464 Primitive::kPrimInt,
3465 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3466 loc.ToHigh(),
3467 Primitive::kPrimInt);
3468 } else if (shift > 32) {
3469 // Low part is high >> (shift - 32). High part becomes 0.
3470 __ movl(low, high);
3471 __ shrl(low, Immediate(shift - 32));
3472 __ xorl(high, high);
3473 } else {
3474 // Between 1 and 31.
3475 __ shrd(low, high, Immediate(shift));
3476 __ shrl(high, Immediate(shift));
3477 }
3478}
3479
Calin Juravle9aec02f2014-11-18 23:06:35 +00003480void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003481 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003482 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3483 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3484 __ testl(shifter, Immediate(32));
3485 __ j(kEqual, &done);
3486 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3487 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3488 __ Bind(&done);
3489}
3490
3491void LocationsBuilderX86::VisitShl(HShl* shl) {
3492 HandleShift(shl);
3493}
3494
3495void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3496 HandleShift(shl);
3497}
3498
3499void LocationsBuilderX86::VisitShr(HShr* shr) {
3500 HandleShift(shr);
3501}
3502
3503void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3504 HandleShift(shr);
3505}
3506
3507void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3508 HandleShift(ushr);
3509}
3510
3511void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3512 HandleShift(ushr);
3513}
3514
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003515void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003516 LocationSummary* locations =
3517 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003518 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003519 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003520 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003522}
3523
3524void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3525 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003526 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003527 // Note: if heap poisoning is enabled, the entry point takes cares
3528 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003529 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3530 instruction,
3531 instruction->GetDexPc(),
3532 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003533 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003534}
3535
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003536void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3539 locations->SetOut(Location::RegisterLocation(EAX));
3540 InvokeRuntimeCallingConvention calling_convention;
3541 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003542 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003543 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003544}
3545
3546void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3547 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003548 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3549
Roland Levillain4d027112015-07-01 15:41:14 +01003550 // Note: if heap poisoning is enabled, the entry point takes cares
3551 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003552 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3553 instruction,
3554 instruction->GetDexPc(),
3555 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003556 DCHECK(!codegen_->IsLeafMethod());
3557}
3558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003559void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003562 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3563 if (location.IsStackSlot()) {
3564 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3565 } else if (location.IsDoubleStackSlot()) {
3566 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003567 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003568 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003569}
3570
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003571void InstructionCodeGeneratorX86::VisitParameterValue(
3572 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3573}
3574
3575void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3576 LocationSummary* locations =
3577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3578 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3579}
3580
3581void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003582}
3583
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003584void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003585 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003586 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003587 locations->SetInAt(0, Location::RequiresRegister());
3588 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003589}
3590
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003591void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3592 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003593 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003594 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003595 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003596 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003597 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003598 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003599 break;
3600
3601 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003602 __ notl(out.AsRegisterPairLow<Register>());
3603 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003604 break;
3605
3606 default:
3607 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3608 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003609}
3610
David Brazdil66d126e2015-04-03 16:02:44 +01003611void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3612 LocationSummary* locations =
3613 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3614 locations->SetInAt(0, Location::RequiresRegister());
3615 locations->SetOut(Location::SameAsFirstInput());
3616}
3617
3618void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003619 LocationSummary* locations = bool_not->GetLocations();
3620 Location in = locations->InAt(0);
3621 Location out = locations->Out();
3622 DCHECK(in.Equals(out));
3623 __ xorl(out.AsRegister<Register>(), Immediate(1));
3624}
3625
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003626void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003627 LocationSummary* locations =
3628 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003629 switch (compare->InputAt(0)->GetType()) {
3630 case Primitive::kPrimLong: {
3631 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003632 locations->SetInAt(1, Location::Any());
3633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3634 break;
3635 }
3636 case Primitive::kPrimFloat:
3637 case Primitive::kPrimDouble: {
3638 locations->SetInAt(0, Location::RequiresFpuRegister());
3639 locations->SetInAt(1, Location::RequiresFpuRegister());
3640 locations->SetOut(Location::RequiresRegister());
3641 break;
3642 }
3643 default:
3644 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3645 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003646}
3647
3648void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003649 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003651 Location left = locations->InAt(0);
3652 Location right = locations->InAt(1);
3653
Mark Mendell0c9497d2015-08-21 09:30:05 -04003654 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003655 switch (compare->InputAt(0)->GetType()) {
3656 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003657 Register left_low = left.AsRegisterPairLow<Register>();
3658 Register left_high = left.AsRegisterPairHigh<Register>();
3659 int32_t val_low = 0;
3660 int32_t val_high = 0;
3661 bool right_is_const = false;
3662
3663 if (right.IsConstant()) {
3664 DCHECK(right.GetConstant()->IsLongConstant());
3665 right_is_const = true;
3666 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3667 val_low = Low32Bits(val);
3668 val_high = High32Bits(val);
3669 }
3670
Calin Juravleddb7df22014-11-25 20:56:51 +00003671 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003672 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003673 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003674 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003675 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003676 DCHECK(right_is_const) << right;
3677 if (val_high == 0) {
3678 __ testl(left_high, left_high);
3679 } else {
3680 __ cmpl(left_high, Immediate(val_high));
3681 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003682 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003683 __ j(kLess, &less); // Signed compare.
3684 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003685 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003686 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003687 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003688 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003689 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003690 DCHECK(right_is_const) << right;
3691 if (val_low == 0) {
3692 __ testl(left_low, left_low);
3693 } else {
3694 __ cmpl(left_low, Immediate(val_low));
3695 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003696 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003697 break;
3698 }
3699 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003701 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3702 break;
3703 }
3704 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003706 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003707 break;
3708 }
3709 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003710 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003711 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003712 __ movl(out, Immediate(0));
3713 __ j(kEqual, &done);
3714 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3715
3716 __ Bind(&greater);
3717 __ movl(out, Immediate(1));
3718 __ jmp(&done);
3719
3720 __ Bind(&less);
3721 __ movl(out, Immediate(-1));
3722
3723 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003724}
3725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003726void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003727 LocationSummary* locations =
3728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003729 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3730 locations->SetInAt(i, Location::Any());
3731 }
3732 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003733}
3734
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003735void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003736 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003737}
3738
Calin Juravle52c48962014-12-16 17:02:57 +00003739void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3740 /*
3741 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3742 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3743 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3744 */
3745 switch (kind) {
3746 case MemBarrierKind::kAnyAny: {
3747 __ mfence();
3748 break;
3749 }
3750 case MemBarrierKind::kAnyStore:
3751 case MemBarrierKind::kLoadAny:
3752 case MemBarrierKind::kStoreStore: {
3753 // nop
3754 break;
3755 }
3756 default:
3757 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003758 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003759}
3760
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003761
Vladimir Marko58155012015-08-19 12:49:41 +00003762void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3763 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3764 switch (invoke->GetMethodLoadKind()) {
3765 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3766 // temp = thread->string_init_entrypoint
3767 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3768 break;
3769 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3770 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3771 break;
3772 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3773 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3774 break;
3775 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3776 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3777 method_patches_.emplace_back(invoke->GetTargetMethod());
3778 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3779 break;
3780 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3781 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3782 FALLTHROUGH_INTENDED;
3783 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3784 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3785 Register method_reg;
3786 Register reg = temp.AsRegister<Register>();
3787 if (current_method.IsRegister()) {
3788 method_reg = current_method.AsRegister<Register>();
3789 } else {
3790 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3791 DCHECK(!current_method.IsValid());
3792 method_reg = reg;
3793 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3794 }
3795 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003796 __ movl(reg, Address(method_reg,
3797 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003798 // temp = temp[index_in_cache]
3799 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3800 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3801 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003802 }
Vladimir Marko58155012015-08-19 12:49:41 +00003803 }
3804
3805 switch (invoke->GetCodePtrLocation()) {
3806 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3807 __ call(GetFrameEntryLabel());
3808 break;
3809 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3810 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3811 Label* label = &relative_call_patches_.back().label;
3812 __ call(label); // Bind to the patch label, override at link time.
3813 __ Bind(label); // Bind the label at the end of the "call" insn.
3814 break;
3815 }
3816 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3817 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3818 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3819 // (Though the direct CALL ptr16:32 is available for consideration).
3820 FALLTHROUGH_INTENDED;
3821 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3822 // (callee_method + offset_of_quick_compiled_code)()
3823 __ call(Address(callee_method.AsRegister<Register>(),
3824 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3825 kX86WordSize).Int32Value()));
3826 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003827 }
3828
3829 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003830}
3831
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003832void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3833 Register temp = temp_in.AsRegister<Register>();
3834 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3835 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3836 LocationSummary* locations = invoke->GetLocations();
3837 Location receiver = locations->InAt(0);
3838 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3839 // temp = object->GetClass();
3840 DCHECK(receiver.IsRegister());
3841 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3842 MaybeRecordImplicitNullCheck(invoke);
3843 __ MaybeUnpoisonHeapReference(temp);
3844 // temp = temp->GetMethodAt(method_offset);
3845 __ movl(temp, Address(temp, method_offset));
3846 // call temp->GetEntryPoint();
3847 __ call(Address(
3848 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3849}
3850
Vladimir Marko58155012015-08-19 12:49:41 +00003851void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3852 DCHECK(linker_patches->empty());
3853 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3854 for (const MethodPatchInfo<Label>& info : method_patches_) {
3855 // The label points to the end of the "movl" insn but the literal offset for method
3856 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3857 uint32_t literal_offset = info.label.Position() - 4;
3858 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3859 info.target_method.dex_file,
3860 info.target_method.dex_method_index));
3861 }
3862 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3863 // The label points to the end of the "call" insn but the literal offset for method
3864 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3865 uint32_t literal_offset = info.label.Position() - 4;
3866 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3867 info.target_method.dex_file,
3868 info.target_method.dex_method_index));
3869 }
3870}
3871
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003872void CodeGeneratorX86::MarkGCCard(Register temp,
3873 Register card,
3874 Register object,
3875 Register value,
3876 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003877 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003878 if (value_can_be_null) {
3879 __ testl(value, value);
3880 __ j(kEqual, &is_null);
3881 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3883 __ movl(temp, object);
3884 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003885 __ movb(Address(temp, card, TIMES_1, 0),
3886 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003887 if (value_can_be_null) {
3888 __ Bind(&is_null);
3889 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003890}
3891
Calin Juravle52c48962014-12-16 17:02:57 +00003892void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3893 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003894 LocationSummary* locations =
3895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003896 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003897
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003898 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3899 locations->SetOut(Location::RequiresFpuRegister());
3900 } else {
3901 // The output overlaps in case of long: we don't want the low move to overwrite
3902 // the object's location.
3903 locations->SetOut(Location::RequiresRegister(),
3904 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3905 : Location::kNoOutputOverlap);
3906 }
Calin Juravle52c48962014-12-16 17:02:57 +00003907
3908 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3909 // Long values can be loaded atomically into an XMM using movsd.
3910 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3911 // and then copy the XMM into the output 32bits at a time).
3912 locations->AddTemp(Location::RequiresFpuRegister());
3913 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003914}
3915
Calin Juravle52c48962014-12-16 17:02:57 +00003916void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3917 const FieldInfo& field_info) {
3918 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003919
Calin Juravle52c48962014-12-16 17:02:57 +00003920 LocationSummary* locations = instruction->GetLocations();
3921 Register base = locations->InAt(0).AsRegister<Register>();
3922 Location out = locations->Out();
3923 bool is_volatile = field_info.IsVolatile();
3924 Primitive::Type field_type = field_info.GetFieldType();
3925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3926
3927 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003929 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003930 break;
3931 }
3932
3933 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003934 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003935 break;
3936 }
3937
3938 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003939 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003940 break;
3941 }
3942
3943 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003944 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945 break;
3946 }
3947
3948 case Primitive::kPrimInt:
3949 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003950 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003951 break;
3952 }
3953
3954 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003955 if (is_volatile) {
3956 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3957 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003959 __ movd(out.AsRegisterPairLow<Register>(), temp);
3960 __ psrlq(temp, Immediate(32));
3961 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3962 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003963 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003964 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003965 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003966 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3967 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003968 break;
3969 }
3970
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003971 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003972 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003973 break;
3974 }
3975
3976 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003977 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003978 break;
3979 }
3980
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003981 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003982 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003983 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984 }
Calin Juravle52c48962014-12-16 17:02:57 +00003985
Calin Juravle77520bc2015-01-12 18:45:46 +00003986 // Longs are handled in the switch.
3987 if (field_type != Primitive::kPrimLong) {
3988 codegen_->MaybeRecordImplicitNullCheck(instruction);
3989 }
3990
Calin Juravle52c48962014-12-16 17:02:57 +00003991 if (is_volatile) {
3992 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3993 }
Roland Levillain4d027112015-07-01 15:41:14 +01003994
3995 if (field_type == Primitive::kPrimNot) {
3996 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3997 }
Calin Juravle52c48962014-12-16 17:02:57 +00003998}
3999
4000void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4001 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4002
4003 LocationSummary* locations =
4004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4005 locations->SetInAt(0, Location::RequiresRegister());
4006 bool is_volatile = field_info.IsVolatile();
4007 Primitive::Type field_type = field_info.GetFieldType();
4008 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4009 || (field_type == Primitive::kPrimByte);
4010
4011 // The register allocator does not support multiple
4012 // inputs that die at entry with one in a specific register.
4013 if (is_byte_type) {
4014 // Ensure the value is in a byte register.
4015 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004016 } else if (Primitive::IsFloatingPointType(field_type)) {
4017 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004018 } else {
4019 locations->SetInAt(1, Location::RequiresRegister());
4020 }
Calin Juravle52c48962014-12-16 17:02:57 +00004021 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01004022 // Temporary registers for the write barrier.
4023 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00004024 // Ensure the card is in a byte register.
4025 locations->AddTemp(Location::RegisterLocation(ECX));
4026 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
4027 // 64bits value can be atomically written to an address with movsd and an XMM register.
4028 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4029 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4030 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4031 // isolated cases when we need this it isn't worth adding the extra complexity.
4032 locations->AddTemp(Location::RequiresFpuRegister());
4033 locations->AddTemp(Location::RequiresFpuRegister());
4034 }
4035}
4036
4037void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004038 const FieldInfo& field_info,
4039 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004040 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4041
4042 LocationSummary* locations = instruction->GetLocations();
4043 Register base = locations->InAt(0).AsRegister<Register>();
4044 Location value = locations->InAt(1);
4045 bool is_volatile = field_info.IsVolatile();
4046 Primitive::Type field_type = field_info.GetFieldType();
4047 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004048 bool needs_write_barrier =
4049 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004050
4051 if (is_volatile) {
4052 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4053 }
4054
4055 switch (field_type) {
4056 case Primitive::kPrimBoolean:
4057 case Primitive::kPrimByte: {
4058 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4059 break;
4060 }
4061
4062 case Primitive::kPrimShort:
4063 case Primitive::kPrimChar: {
4064 __ movw(Address(base, offset), value.AsRegister<Register>());
4065 break;
4066 }
4067
4068 case Primitive::kPrimInt:
4069 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004070 if (kPoisonHeapReferences && needs_write_barrier) {
4071 // Note that in the case where `value` is a null reference,
4072 // we do not enter this block, as the reference does not
4073 // need poisoning.
4074 DCHECK_EQ(field_type, Primitive::kPrimNot);
4075 Register temp = locations->GetTemp(0).AsRegister<Register>();
4076 __ movl(temp, value.AsRegister<Register>());
4077 __ PoisonHeapReference(temp);
4078 __ movl(Address(base, offset), temp);
4079 } else {
4080 __ movl(Address(base, offset), value.AsRegister<Register>());
4081 }
Calin Juravle52c48962014-12-16 17:02:57 +00004082 break;
4083 }
4084
4085 case Primitive::kPrimLong: {
4086 if (is_volatile) {
4087 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4088 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4089 __ movd(temp1, value.AsRegisterPairLow<Register>());
4090 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4091 __ punpckldq(temp1, temp2);
4092 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004094 } else {
4095 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004096 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004097 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4098 }
4099 break;
4100 }
4101
4102 case Primitive::kPrimFloat: {
4103 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4104 break;
4105 }
4106
4107 case Primitive::kPrimDouble: {
4108 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4109 break;
4110 }
4111
4112 case Primitive::kPrimVoid:
4113 LOG(FATAL) << "Unreachable type " << field_type;
4114 UNREACHABLE();
4115 }
4116
Calin Juravle77520bc2015-01-12 18:45:46 +00004117 // Longs are handled in the switch.
4118 if (field_type != Primitive::kPrimLong) {
4119 codegen_->MaybeRecordImplicitNullCheck(instruction);
4120 }
4121
Roland Levillain4d027112015-07-01 15:41:14 +01004122 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 Register temp = locations->GetTemp(0).AsRegister<Register>();
4124 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004125 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 }
4127
Calin Juravle52c48962014-12-16 17:02:57 +00004128 if (is_volatile) {
4129 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4130 }
4131}
4132
4133void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4134 HandleFieldGet(instruction, instruction->GetFieldInfo());
4135}
4136
4137void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4138 HandleFieldGet(instruction, instruction->GetFieldInfo());
4139}
4140
4141void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4142 HandleFieldSet(instruction, instruction->GetFieldInfo());
4143}
4144
4145void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004146 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004147}
4148
4149void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4150 HandleFieldSet(instruction, instruction->GetFieldInfo());
4151}
4152
4153void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004154 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004155}
4156
4157void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4158 HandleFieldGet(instruction, instruction->GetFieldInfo());
4159}
4160
4161void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4162 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004163}
4164
Calin Juravlee460d1d2015-09-29 04:52:17 +01004165void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4166 HUnresolvedInstanceFieldGet* instruction) {
4167 FieldAccessCallingConventionX86 calling_convention;
4168 codegen_->CreateUnresolvedFieldLocationSummary(
4169 instruction, instruction->GetFieldType(), calling_convention);
4170}
4171
4172void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4173 HUnresolvedInstanceFieldGet* instruction) {
4174 FieldAccessCallingConventionX86 calling_convention;
4175 codegen_->GenerateUnresolvedFieldAccess(instruction,
4176 instruction->GetFieldType(),
4177 instruction->GetFieldIndex(),
4178 instruction->GetDexPc(),
4179 calling_convention);
4180}
4181
4182void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4183 HUnresolvedInstanceFieldSet* instruction) {
4184 FieldAccessCallingConventionX86 calling_convention;
4185 codegen_->CreateUnresolvedFieldLocationSummary(
4186 instruction, instruction->GetFieldType(), calling_convention);
4187}
4188
4189void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4190 HUnresolvedInstanceFieldSet* instruction) {
4191 FieldAccessCallingConventionX86 calling_convention;
4192 codegen_->GenerateUnresolvedFieldAccess(instruction,
4193 instruction->GetFieldType(),
4194 instruction->GetFieldIndex(),
4195 instruction->GetDexPc(),
4196 calling_convention);
4197}
4198
4199void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4200 HUnresolvedStaticFieldGet* instruction) {
4201 FieldAccessCallingConventionX86 calling_convention;
4202 codegen_->CreateUnresolvedFieldLocationSummary(
4203 instruction, instruction->GetFieldType(), calling_convention);
4204}
4205
4206void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4207 HUnresolvedStaticFieldGet* instruction) {
4208 FieldAccessCallingConventionX86 calling_convention;
4209 codegen_->GenerateUnresolvedFieldAccess(instruction,
4210 instruction->GetFieldType(),
4211 instruction->GetFieldIndex(),
4212 instruction->GetDexPc(),
4213 calling_convention);
4214}
4215
4216void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4217 HUnresolvedStaticFieldSet* instruction) {
4218 FieldAccessCallingConventionX86 calling_convention;
4219 codegen_->CreateUnresolvedFieldLocationSummary(
4220 instruction, instruction->GetFieldType(), calling_convention);
4221}
4222
4223void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4224 HUnresolvedStaticFieldSet* instruction) {
4225 FieldAccessCallingConventionX86 calling_convention;
4226 codegen_->GenerateUnresolvedFieldAccess(instruction,
4227 instruction->GetFieldType(),
4228 instruction->GetFieldIndex(),
4229 instruction->GetDexPc(),
4230 calling_convention);
4231}
4232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004234 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4235 ? LocationSummary::kCallOnSlowPath
4236 : LocationSummary::kNoCall;
4237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4238 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004239 ? Location::RequiresRegister()
4240 : Location::Any();
4241 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004242 if (instruction->HasUses()) {
4243 locations->SetOut(Location::SameAsFirstInput());
4244 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245}
4246
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004247void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004248 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4249 return;
4250 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004251 LocationSummary* locations = instruction->GetLocations();
4252 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004253
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004254 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4255 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4256}
4257
4258void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004259 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260 codegen_->AddSlowPath(slow_path);
4261
4262 LocationSummary* locations = instruction->GetLocations();
4263 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264
4265 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004266 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004267 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004269 } else {
4270 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004271 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004272 __ jmp(slow_path->GetEntryLabel());
4273 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274 }
4275 __ j(kEqual, slow_path->GetEntryLabel());
4276}
4277
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004278void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004279 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004280 GenerateImplicitNullCheck(instruction);
4281 } else {
4282 GenerateExplicitNullCheck(instruction);
4283 }
4284}
4285
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004286void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004287 LocationSummary* locations =
4288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004289 locations->SetInAt(0, Location::RequiresRegister());
4290 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004291 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4292 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4293 } else {
4294 // The output overlaps in case of long: we don't want the low move to overwrite
4295 // the array's location.
4296 locations->SetOut(Location::RequiresRegister(),
4297 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4298 : Location::kNoOutputOverlap);
4299 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004300}
4301
4302void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4303 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004304 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 Location index = locations->InAt(1);
4306
Calin Juravle77520bc2015-01-12 18:45:46 +00004307 Primitive::Type type = instruction->GetType();
4308 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309 case Primitive::kPrimBoolean: {
4310 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004311 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004312 if (index.IsConstant()) {
4313 __ movzxb(out, Address(obj,
4314 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4315 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 }
4318 break;
4319 }
4320
4321 case Primitive::kPrimByte: {
4322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004323 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 if (index.IsConstant()) {
4325 __ movsxb(out, Address(obj,
4326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4327 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004328 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004329 }
4330 break;
4331 }
4332
4333 case Primitive::kPrimShort: {
4334 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004335 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004336 if (index.IsConstant()) {
4337 __ movsxw(out, Address(obj,
4338 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4339 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004340 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 }
4342 break;
4343 }
4344
4345 case Primitive::kPrimChar: {
4346 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004347 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348 if (index.IsConstant()) {
4349 __ movzxw(out, Address(obj,
4350 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4351 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 }
4354 break;
4355 }
4356
4357 case Primitive::kPrimInt:
4358 case Primitive::kPrimNot: {
4359 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 if (index.IsConstant()) {
4362 __ movl(out, Address(obj,
4363 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4364 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004365 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 }
4367 break;
4368 }
4369
4370 case Primitive::kPrimLong: {
4371 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004372 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004373 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004374 if (index.IsConstant()) {
4375 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004376 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004377 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004378 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004379 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004380 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004383 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004384 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385 }
4386 break;
4387 }
4388
Mark Mendell7c8d0092015-01-26 11:21:33 -05004389 case Primitive::kPrimFloat: {
4390 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4391 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4392 if (index.IsConstant()) {
4393 __ movss(out, Address(obj,
4394 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4395 } else {
4396 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4397 }
4398 break;
4399 }
4400
4401 case Primitive::kPrimDouble: {
4402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4403 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4404 if (index.IsConstant()) {
4405 __ movsd(out, Address(obj,
4406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4407 } else {
4408 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4409 }
4410 break;
4411 }
4412
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004414 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004415 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004417
4418 if (type != Primitive::kPrimLong) {
4419 codegen_->MaybeRecordImplicitNullCheck(instruction);
4420 }
Roland Levillain4d027112015-07-01 15:41:14 +01004421
4422 if (type == Primitive::kPrimNot) {
4423 Register out = locations->Out().AsRegister<Register>();
4424 __ MaybeUnpoisonHeapReference(out);
4425 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004426}
4427
4428void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004429 // This location builder might end up asking to up to four registers, which is
4430 // not currently possible for baseline. The situation in which we need four
4431 // registers cannot be met by baseline though, because it has not run any
4432 // optimization.
4433
Nicolas Geoffray39468442014-09-02 15:17:15 +01004434 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004435 bool needs_write_barrier =
4436 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4437
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004438 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004439
Nicolas Geoffray39468442014-09-02 15:17:15 +01004440 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4441 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004442 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004443
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004444 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4445 || (value_type == Primitive::kPrimByte);
4446 // We need the inputs to be different than the output in case of long operation.
4447 // In case of a byte operation, the register allocator does not support multiple
4448 // inputs that die at entry with one in a specific register.
4449 locations->SetInAt(0, Location::RequiresRegister());
4450 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4451 if (is_byte_type) {
4452 // Ensure the value is in a byte register.
4453 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4454 } else if (Primitive::IsFloatingPointType(value_type)) {
4455 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004457 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4458 }
4459 if (needs_write_barrier) {
4460 // Temporary registers for the write barrier.
4461 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4462 // Ensure the card is in a byte register.
4463 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004464 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004465}
4466
4467void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4468 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004469 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004470 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004471 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004472 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004473 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4474 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4475 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4476 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004477 bool needs_write_barrier =
4478 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479
4480 switch (value_type) {
4481 case Primitive::kPrimBoolean:
4482 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004483 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4484 Address address = index.IsConstant()
4485 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4486 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4487 if (value.IsRegister()) {
4488 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004489 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004490 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004491 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004492 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 break;
4494 }
4495
4496 case Primitive::kPrimShort:
4497 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004498 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4499 Address address = index.IsConstant()
4500 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4501 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4502 if (value.IsRegister()) {
4503 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004504 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004505 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004507 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508 break;
4509 }
4510
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004511 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004512 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4513 Address address = index.IsConstant()
4514 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4515 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4516 if (!value.IsRegister()) {
4517 // Just setting null.
4518 DCHECK(instruction->InputAt(2)->IsNullConstant());
4519 DCHECK(value.IsConstant()) << value;
4520 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004522 DCHECK(!needs_write_barrier);
4523 DCHECK(!may_need_runtime_call);
4524 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004526
4527 DCHECK(needs_write_barrier);
4528 Register register_value = value.AsRegister<Register>();
4529 NearLabel done, not_null, do_put;
4530 SlowPathCode* slow_path = nullptr;
4531 Register temp = locations->GetTemp(0).AsRegister<Register>();
4532 if (may_need_runtime_call) {
4533 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4534 codegen_->AddSlowPath(slow_path);
4535 if (instruction->GetValueCanBeNull()) {
4536 __ testl(register_value, register_value);
4537 __ j(kNotEqual, &not_null);
4538 __ movl(address, Immediate(0));
4539 codegen_->MaybeRecordImplicitNullCheck(instruction);
4540 __ jmp(&done);
4541 __ Bind(&not_null);
4542 }
4543
4544 __ movl(temp, Address(array, class_offset));
4545 codegen_->MaybeRecordImplicitNullCheck(instruction);
4546 __ MaybeUnpoisonHeapReference(temp);
4547 __ movl(temp, Address(temp, component_offset));
4548 // No need to poison/unpoison, we're comparing two poisoned references.
4549 __ cmpl(temp, Address(register_value, class_offset));
4550 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4551 __ j(kEqual, &do_put);
4552 __ MaybeUnpoisonHeapReference(temp);
4553 __ movl(temp, Address(temp, super_offset));
4554 // No need to unpoison, we're comparing against null..
4555 __ testl(temp, temp);
4556 __ j(kNotEqual, slow_path->GetEntryLabel());
4557 __ Bind(&do_put);
4558 } else {
4559 __ j(kNotEqual, slow_path->GetEntryLabel());
4560 }
4561 }
4562
4563 if (kPoisonHeapReferences) {
4564 __ movl(temp, register_value);
4565 __ PoisonHeapReference(temp);
4566 __ movl(address, temp);
4567 } else {
4568 __ movl(address, register_value);
4569 }
4570 if (!may_need_runtime_call) {
4571 codegen_->MaybeRecordImplicitNullCheck(instruction);
4572 }
4573
4574 Register card = locations->GetTemp(1).AsRegister<Register>();
4575 codegen_->MarkGCCard(
4576 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4577 __ Bind(&done);
4578
4579 if (slow_path != nullptr) {
4580 __ Bind(slow_path->GetExitLabel());
4581 }
4582
4583 break;
4584 }
4585 case Primitive::kPrimInt: {
4586 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4587 Address address = index.IsConstant()
4588 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4589 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4590 if (value.IsRegister()) {
4591 __ movl(address, value.AsRegister<Register>());
4592 } else {
4593 DCHECK(value.IsConstant()) << value;
4594 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4595 __ movl(address, Immediate(v));
4596 }
4597 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 break;
4599 }
4600
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 case Primitive::kPrimLong: {
4602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603 if (index.IsConstant()) {
4604 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004605 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004606 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004607 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004608 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004609 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004610 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004611 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004612 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004613 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004614 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004615 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004617 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004618 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004619 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004620 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004621 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004622 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004623 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004624 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004625 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004626 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004627 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004628 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004629 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004630 Immediate(High32Bits(val)));
4631 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 }
4633 break;
4634 }
4635
Mark Mendell7c8d0092015-01-26 11:21:33 -05004636 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4638 Address address = index.IsConstant()
4639 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4640 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004641 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004642 __ movss(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004643 break;
4644 }
4645
4646 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4648 Address address = index.IsConstant()
4649 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4650 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004651 DCHECK(value.IsFpuRegister());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 __ movsd(address, value.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004653 break;
4654 }
4655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 case Primitive::kPrimVoid:
4657 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004658 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 }
4660}
4661
4662void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004664 locations->SetInAt(0, Location::RequiresRegister());
4665 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666}
4667
4668void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4669 LocationSummary* locations = instruction->GetLocations();
4670 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004671 Register obj = locations->InAt(0).AsRegister<Register>();
4672 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004674 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004675}
4676
4677void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004678 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4679 ? LocationSummary::kCallOnSlowPath
4680 : LocationSummary::kNoCall;
4681 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004682 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004683 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004684 if (instruction->HasUses()) {
4685 locations->SetOut(Location::SameAsFirstInput());
4686 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687}
4688
4689void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4690 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004691 Location index_loc = locations->InAt(0);
4692 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004693 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004694 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695
Mark Mendell99dbd682015-04-22 16:18:52 -04004696 if (length_loc.IsConstant()) {
4697 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4698 if (index_loc.IsConstant()) {
4699 // BCE will remove the bounds check if we are guarenteed to pass.
4700 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4701 if (index < 0 || index >= length) {
4702 codegen_->AddSlowPath(slow_path);
4703 __ jmp(slow_path->GetEntryLabel());
4704 } else {
4705 // Some optimization after BCE may have generated this, and we should not
4706 // generate a bounds check if it is a valid range.
4707 }
4708 return;
4709 }
4710
4711 // We have to reverse the jump condition because the length is the constant.
4712 Register index_reg = index_loc.AsRegister<Register>();
4713 __ cmpl(index_reg, Immediate(length));
4714 codegen_->AddSlowPath(slow_path);
4715 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004716 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004717 Register length = length_loc.AsRegister<Register>();
4718 if (index_loc.IsConstant()) {
4719 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4720 __ cmpl(length, Immediate(value));
4721 } else {
4722 __ cmpl(length, index_loc.AsRegister<Register>());
4723 }
4724 codegen_->AddSlowPath(slow_path);
4725 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004726 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727}
4728
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004729void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4730 temp->SetLocations(nullptr);
4731}
4732
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004733void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734 // Nothing to do, this is driven by the code generator.
4735}
4736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004737void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004738 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004739}
4740
4741void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004742 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4743}
4744
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004745void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4747}
4748
4749void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004750 HBasicBlock* block = instruction->GetBlock();
4751 if (block->GetLoopInformation() != nullptr) {
4752 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4753 // The back edge will generate the suspend check.
4754 return;
4755 }
4756 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4757 // The goto will generate the suspend check.
4758 return;
4759 }
4760 GenerateSuspendCheck(instruction, nullptr);
4761}
4762
4763void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4764 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004765 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004766 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4767 if (slow_path == nullptr) {
4768 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4769 instruction->SetSlowPath(slow_path);
4770 codegen_->AddSlowPath(slow_path);
4771 if (successor != nullptr) {
4772 DCHECK(successor->IsLoopHeader());
4773 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4774 }
4775 } else {
4776 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4777 }
4778
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004779 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004780 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004781 if (successor == nullptr) {
4782 __ j(kNotEqual, slow_path->GetEntryLabel());
4783 __ Bind(slow_path->GetReturnLabel());
4784 } else {
4785 __ j(kEqual, codegen_->GetLabelOf(successor));
4786 __ jmp(slow_path->GetEntryLabel());
4787 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004788}
4789
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004790X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4791 return codegen_->GetAssembler();
4792}
4793
Mark Mendell7c8d0092015-01-26 11:21:33 -05004794void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004795 ScratchRegisterScope ensure_scratch(
4796 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4797 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4798 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4799 __ movl(temp_reg, Address(ESP, src + stack_offset));
4800 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004801}
4802
4803void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004804 ScratchRegisterScope ensure_scratch(
4805 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4806 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4807 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4808 __ movl(temp_reg, Address(ESP, src + stack_offset));
4809 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4810 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4811 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004812}
4813
4814void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004815 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004816 Location source = move->GetSource();
4817 Location destination = move->GetDestination();
4818
4819 if (source.IsRegister()) {
4820 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004821 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004822 } else {
4823 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004824 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004825 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004826 } else if (source.IsFpuRegister()) {
4827 if (destination.IsFpuRegister()) {
4828 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4829 } else if (destination.IsStackSlot()) {
4830 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4831 } else {
4832 DCHECK(destination.IsDoubleStackSlot());
4833 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4834 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004835 } else if (source.IsStackSlot()) {
4836 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004837 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004838 } else if (destination.IsFpuRegister()) {
4839 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004840 } else {
4841 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004842 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4843 }
4844 } else if (source.IsDoubleStackSlot()) {
4845 if (destination.IsFpuRegister()) {
4846 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4847 } else {
4848 DCHECK(destination.IsDoubleStackSlot()) << destination;
4849 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004850 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004851 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004852 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004853 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004854 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004855 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004856 if (value == 0) {
4857 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4858 } else {
4859 __ movl(destination.AsRegister<Register>(), Immediate(value));
4860 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004861 } else {
4862 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004863 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004864 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004865 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004866 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004867 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004868 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004869 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004870 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4871 if (value == 0) {
4872 // Easy handling of 0.0.
4873 __ xorps(dest, dest);
4874 } else {
4875 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004876 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4877 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4878 __ movl(temp, Immediate(value));
4879 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004880 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004881 } else {
4882 DCHECK(destination.IsStackSlot()) << destination;
4883 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4884 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004885 } else if (constant->IsLongConstant()) {
4886 int64_t value = constant->AsLongConstant()->GetValue();
4887 int32_t low_value = Low32Bits(value);
4888 int32_t high_value = High32Bits(value);
4889 Immediate low(low_value);
4890 Immediate high(high_value);
4891 if (destination.IsDoubleStackSlot()) {
4892 __ movl(Address(ESP, destination.GetStackIndex()), low);
4893 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4894 } else {
4895 __ movl(destination.AsRegisterPairLow<Register>(), low);
4896 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4897 }
4898 } else {
4899 DCHECK(constant->IsDoubleConstant());
4900 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004901 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004902 int32_t low_value = Low32Bits(value);
4903 int32_t high_value = High32Bits(value);
4904 Immediate low(low_value);
4905 Immediate high(high_value);
4906 if (destination.IsFpuRegister()) {
4907 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4908 if (value == 0) {
4909 // Easy handling of 0.0.
4910 __ xorpd(dest, dest);
4911 } else {
4912 __ pushl(high);
4913 __ pushl(low);
4914 __ movsd(dest, Address(ESP, 0));
4915 __ addl(ESP, Immediate(8));
4916 }
4917 } else {
4918 DCHECK(destination.IsDoubleStackSlot()) << destination;
4919 __ movl(Address(ESP, destination.GetStackIndex()), low);
4920 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4921 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004922 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004923 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004924 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004925 }
4926}
4927
Mark Mendella5c19ce2015-04-01 12:51:05 -04004928void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004929 Register suggested_scratch = reg == EAX ? EBX : EAX;
4930 ScratchRegisterScope ensure_scratch(
4931 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4932
4933 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4934 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4935 __ movl(Address(ESP, mem + stack_offset), reg);
4936 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004937}
4938
Mark Mendell7c8d0092015-01-26 11:21:33 -05004939void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004940 ScratchRegisterScope ensure_scratch(
4941 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4942
4943 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4944 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4945 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4946 __ movss(Address(ESP, mem + stack_offset), reg);
4947 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004948}
4949
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004950void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004951 ScratchRegisterScope ensure_scratch1(
4952 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004953
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004954 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4955 ScratchRegisterScope ensure_scratch2(
4956 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004957
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004958 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4959 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4960 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4961 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4962 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4963 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004964}
4965
4966void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004967 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004968 Location source = move->GetSource();
4969 Location destination = move->GetDestination();
4970
4971 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004972 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4973 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4974 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4975 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4976 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004977 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004979 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004980 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004981 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4982 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004983 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4984 // Use XOR Swap algorithm to avoid a temporary.
4985 DCHECK_NE(source.reg(), destination.reg());
4986 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4987 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4988 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4989 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4990 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4991 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4992 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004993 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4994 // Take advantage of the 16 bytes in the XMM register.
4995 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4996 Address stack(ESP, destination.GetStackIndex());
4997 // Load the double into the high doubleword.
4998 __ movhpd(reg, stack);
4999
5000 // Store the low double into the destination.
5001 __ movsd(stack, reg);
5002
5003 // Move the high double to the low double.
5004 __ psrldq(reg, Immediate(8));
5005 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5006 // Take advantage of the 16 bytes in the XMM register.
5007 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5008 Address stack(ESP, source.GetStackIndex());
5009 // Load the double into the high doubleword.
5010 __ movhpd(reg, stack);
5011
5012 // Store the low double into the destination.
5013 __ movsd(stack, reg);
5014
5015 // Move the high double to the low double.
5016 __ psrldq(reg, Immediate(8));
5017 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5018 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5019 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005020 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005021 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005022 }
5023}
5024
5025void ParallelMoveResolverX86::SpillScratch(int reg) {
5026 __ pushl(static_cast<Register>(reg));
5027}
5028
5029void ParallelMoveResolverX86::RestoreScratch(int reg) {
5030 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005031}
5032
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005033void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005034 InvokeRuntimeCallingConvention calling_convention;
5035 CodeGenerator::CreateLoadClassLocationSummary(
5036 cls,
5037 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5038 Location::RegisterLocation(EAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005039}
5040
5041void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005042 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005043 if (cls->NeedsAccessCheck()) {
5044 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5045 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5046 cls,
5047 cls->GetDexPc(),
5048 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005049 return;
5050 }
5051
5052 Register out = locations->Out().AsRegister<Register>();
5053 Register current_method = locations->InAt(0).AsRegister<Register>();
5054 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005055 DCHECK(!cls->CanCallRuntime());
5056 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07005057 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005058 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005059 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005060 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01005061 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005062 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005063 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005064
Andreas Gampe85b62f22015-09-09 13:15:38 -07005065 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005066 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5067 codegen_->AddSlowPath(slow_path);
5068 __ testl(out, out);
5069 __ j(kEqual, slow_path->GetEntryLabel());
5070 if (cls->MustGenerateClinitCheck()) {
5071 GenerateClassInitializationCheck(slow_path, out);
5072 } else {
5073 __ Bind(slow_path->GetExitLabel());
5074 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005075 }
5076}
5077
5078void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5079 LocationSummary* locations =
5080 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5081 locations->SetInAt(0, Location::RequiresRegister());
5082 if (check->HasUses()) {
5083 locations->SetOut(Location::SameAsFirstInput());
5084 }
5085}
5086
5087void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005088 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005089 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005090 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005091 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005092 GenerateClassInitializationCheck(slow_path,
5093 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005094}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005095
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005096void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005097 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005098 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5099 Immediate(mirror::Class::kStatusInitialized));
5100 __ j(kLess, slow_path->GetEntryLabel());
5101 __ Bind(slow_path->GetExitLabel());
5102 // No need for memory fence, thanks to the X86 memory model.
5103}
5104
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005105void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5106 LocationSummary* locations =
5107 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005108 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005109 locations->SetOut(Location::RequiresRegister());
5110}
5111
5112void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005113 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005114 codegen_->AddSlowPath(slow_path);
5115
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005116 LocationSummary* locations = load->GetLocations();
5117 Register out = locations->Out().AsRegister<Register>();
5118 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005119 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005120 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005121 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005122 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005123 __ testl(out, out);
5124 __ j(kEqual, slow_path->GetEntryLabel());
5125 __ Bind(slow_path->GetExitLabel());
5126}
5127
David Brazdilcb1c0552015-08-04 16:22:25 +01005128static Address GetExceptionTlsAddress() {
5129 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5130}
5131
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005132void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5133 LocationSummary* locations =
5134 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5135 locations->SetOut(Location::RequiresRegister());
5136}
5137
5138void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005139 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5140}
5141
5142void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5143 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5144}
5145
5146void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5147 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005148}
5149
5150void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5151 LocationSummary* locations =
5152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5153 InvokeRuntimeCallingConvention calling_convention;
5154 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5155}
5156
5157void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005158 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5159 instruction,
5160 instruction->GetDexPc(),
5161 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005162}
5163
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005164void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005165 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5166 switch (instruction->GetTypeCheckKind()) {
5167 case TypeCheckKind::kExactCheck:
5168 case TypeCheckKind::kAbstractClassCheck:
5169 case TypeCheckKind::kClassHierarchyCheck:
5170 case TypeCheckKind::kArrayObjectCheck:
5171 call_kind = LocationSummary::kNoCall;
5172 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005173 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005174 case TypeCheckKind::kInterfaceCheck:
5175 call_kind = LocationSummary::kCall;
5176 break;
5177 case TypeCheckKind::kArrayCheck:
5178 call_kind = LocationSummary::kCallOnSlowPath;
5179 break;
5180 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005181 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005182 if (call_kind != LocationSummary::kCall) {
5183 locations->SetInAt(0, Location::RequiresRegister());
5184 locations->SetInAt(1, Location::Any());
5185 // Note that TypeCheckSlowPathX86 uses this register too.
5186 locations->SetOut(Location::RequiresRegister());
5187 } else {
5188 InvokeRuntimeCallingConvention calling_convention;
5189 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5190 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5191 locations->SetOut(Location::RegisterLocation(EAX));
5192 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005193}
5194
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005195void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005196 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005197 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005198 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005199 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005200 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005201 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5202 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5203 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005204 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005205 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005206
5207 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005208 // Avoid null check if we know obj is not null.
5209 if (instruction->MustDoNullCheck()) {
5210 __ testl(obj, obj);
5211 __ j(kEqual, &zero);
5212 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005213
Calin Juravle98893e12015-10-02 21:05:03 +01005214 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005215 // This is safe, as the register is caller-save, and the object must be in another
5216 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01005217 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
5218 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005219 ? obj
5220 : out;
5221 __ movl(target, Address(obj, class_offset));
5222 __ MaybeUnpoisonHeapReference(target);
5223
5224 switch (instruction->GetTypeCheckKind()) {
5225 case TypeCheckKind::kExactCheck: {
5226 if (cls.IsRegister()) {
5227 __ cmpl(out, cls.AsRegister<Register>());
5228 } else {
5229 DCHECK(cls.IsStackSlot()) << cls;
5230 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5231 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005233 // Classes must be equal for the instanceof to succeed.
5234 __ j(kNotEqual, &zero);
5235 __ movl(out, Immediate(1));
5236 __ jmp(&done);
5237 break;
5238 }
5239 case TypeCheckKind::kAbstractClassCheck: {
5240 // If the class is abstract, we eagerly fetch the super class of the
5241 // object to avoid doing a comparison we know will fail.
5242 NearLabel loop;
5243 __ Bind(&loop);
5244 __ movl(out, Address(out, super_offset));
5245 __ MaybeUnpoisonHeapReference(out);
5246 __ testl(out, out);
5247 // If `out` is null, we use it for the result, and jump to `done`.
5248 __ j(kEqual, &done);
5249 if (cls.IsRegister()) {
5250 __ cmpl(out, cls.AsRegister<Register>());
5251 } else {
5252 DCHECK(cls.IsStackSlot()) << cls;
5253 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5254 }
5255 __ j(kNotEqual, &loop);
5256 __ movl(out, Immediate(1));
5257 if (zero.IsLinked()) {
5258 __ jmp(&done);
5259 }
5260 break;
5261 }
5262 case TypeCheckKind::kClassHierarchyCheck: {
5263 // Walk over the class hierarchy to find a match.
5264 NearLabel loop, success;
5265 __ Bind(&loop);
5266 if (cls.IsRegister()) {
5267 __ cmpl(out, cls.AsRegister<Register>());
5268 } else {
5269 DCHECK(cls.IsStackSlot()) << cls;
5270 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5271 }
5272 __ j(kEqual, &success);
5273 __ movl(out, Address(out, super_offset));
5274 __ MaybeUnpoisonHeapReference(out);
5275 __ testl(out, out);
5276 __ j(kNotEqual, &loop);
5277 // If `out` is null, we use it for the result, and jump to `done`.
5278 __ jmp(&done);
5279 __ Bind(&success);
5280 __ movl(out, Immediate(1));
5281 if (zero.IsLinked()) {
5282 __ jmp(&done);
5283 }
5284 break;
5285 }
5286 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005287 // Do an exact check.
5288 NearLabel exact_check;
5289 if (cls.IsRegister()) {
5290 __ cmpl(out, cls.AsRegister<Register>());
5291 } else {
5292 DCHECK(cls.IsStackSlot()) << cls;
5293 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5294 }
5295 __ j(kEqual, &exact_check);
5296 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 __ movl(out, Address(out, component_offset));
5298 __ MaybeUnpoisonHeapReference(out);
5299 __ testl(out, out);
5300 // If `out` is null, we use it for the result, and jump to `done`.
5301 __ j(kEqual, &done);
5302 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5303 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005304 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005305 __ movl(out, Immediate(1));
5306 __ jmp(&done);
5307 break;
5308 }
5309 case TypeCheckKind::kArrayCheck: {
5310 if (cls.IsRegister()) {
5311 __ cmpl(out, cls.AsRegister<Register>());
5312 } else {
5313 DCHECK(cls.IsStackSlot()) << cls;
5314 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5315 }
5316 DCHECK(locations->OnlyCallsOnSlowPath());
5317 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5318 instruction, /* is_fatal */ false);
5319 codegen_->AddSlowPath(slow_path);
5320 __ j(kNotEqual, slow_path->GetEntryLabel());
5321 __ movl(out, Immediate(1));
5322 if (zero.IsLinked()) {
5323 __ jmp(&done);
5324 }
5325 break;
5326 }
Calin Juravle98893e12015-10-02 21:05:03 +01005327 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005328 case TypeCheckKind::kInterfaceCheck:
5329 default: {
5330 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5331 instruction,
5332 instruction->GetDexPc(),
5333 nullptr);
5334 if (zero.IsLinked()) {
5335 __ jmp(&done);
5336 }
5337 break;
5338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005339 }
5340
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005341 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005342 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005343 __ xorl(out, out);
5344 }
5345
5346 if (done.IsLinked()) {
5347 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005348 }
5349
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005350 if (slow_path != nullptr) {
5351 __ Bind(slow_path->GetExitLabel());
5352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005353}
5354
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005355void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005356 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5357 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5358
5359 switch (instruction->GetTypeCheckKind()) {
5360 case TypeCheckKind::kExactCheck:
5361 case TypeCheckKind::kAbstractClassCheck:
5362 case TypeCheckKind::kClassHierarchyCheck:
5363 case TypeCheckKind::kArrayObjectCheck:
5364 call_kind = throws_into_catch
5365 ? LocationSummary::kCallOnSlowPath
5366 : LocationSummary::kNoCall;
5367 break;
5368 case TypeCheckKind::kInterfaceCheck:
Calin Juravle98893e12015-10-02 21:05:03 +01005369 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005370 call_kind = LocationSummary::kCall;
5371 break;
5372 case TypeCheckKind::kArrayCheck:
5373 call_kind = LocationSummary::kCallOnSlowPath;
5374 break;
5375 }
5376
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005378 instruction, call_kind);
5379 if (call_kind != LocationSummary::kCall) {
5380 locations->SetInAt(0, Location::RequiresRegister());
5381 locations->SetInAt(1, Location::Any());
5382 // Note that TypeCheckSlowPathX86 uses this register too.
5383 locations->AddTemp(Location::RequiresRegister());
5384 } else {
5385 InvokeRuntimeCallingConvention calling_convention;
5386 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5387 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5388 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005389}
5390
5391void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5392 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005393 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005394 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005395 Register temp = locations->WillCall()
5396 ? kNoRegister
5397 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005398
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5400 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5401 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5402 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5403 SlowPathCode* slow_path = nullptr;
5404
5405 if (!locations->WillCall()) {
5406 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5407 instruction, !locations->CanCall());
5408 codegen_->AddSlowPath(slow_path);
5409 }
5410
5411 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005412 // Avoid null check if we know obj is not null.
5413 if (instruction->MustDoNullCheck()) {
5414 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005416 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005417
5418 if (locations->WillCall()) {
5419 __ movl(obj, Address(obj, class_offset));
5420 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005421 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005422 __ movl(temp, Address(obj, class_offset));
5423 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005424 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005425
5426 switch (instruction->GetTypeCheckKind()) {
5427 case TypeCheckKind::kExactCheck:
5428 case TypeCheckKind::kArrayCheck: {
5429 if (cls.IsRegister()) {
5430 __ cmpl(temp, cls.AsRegister<Register>());
5431 } else {
5432 DCHECK(cls.IsStackSlot()) << cls;
5433 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5434 }
5435 // Jump to slow path for throwing the exception or doing a
5436 // more involved array check.
5437 __ j(kNotEqual, slow_path->GetEntryLabel());
5438 break;
5439 }
5440 case TypeCheckKind::kAbstractClassCheck: {
5441 // If the class is abstract, we eagerly fetch the super class of the
5442 // object to avoid doing a comparison we know will fail.
5443 NearLabel loop, success;
5444 __ Bind(&loop);
5445 __ movl(temp, Address(temp, super_offset));
5446 __ MaybeUnpoisonHeapReference(temp);
5447 __ testl(temp, temp);
5448 // Jump to the slow path to throw the exception.
5449 __ j(kEqual, slow_path->GetEntryLabel());
5450 if (cls.IsRegister()) {
5451 __ cmpl(temp, cls.AsRegister<Register>());
5452 } else {
5453 DCHECK(cls.IsStackSlot()) << cls;
5454 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5455 }
5456 __ j(kNotEqual, &loop);
5457 break;
5458 }
5459 case TypeCheckKind::kClassHierarchyCheck: {
5460 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005461 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005462 __ Bind(&loop);
5463 if (cls.IsRegister()) {
5464 __ cmpl(temp, cls.AsRegister<Register>());
5465 } else {
5466 DCHECK(cls.IsStackSlot()) << cls;
5467 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5468 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005469 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005470 __ movl(temp, Address(temp, super_offset));
5471 __ MaybeUnpoisonHeapReference(temp);
5472 __ testl(temp, temp);
5473 __ j(kNotEqual, &loop);
5474 // Jump to the slow path to throw the exception.
5475 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005476 break;
5477 }
5478 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005479 // Do an exact check.
5480 if (cls.IsRegister()) {
5481 __ cmpl(temp, cls.AsRegister<Register>());
5482 } else {
5483 DCHECK(cls.IsStackSlot()) << cls;
5484 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5485 }
5486 __ j(kEqual, &done);
5487 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 __ movl(temp, Address(temp, component_offset));
5489 __ MaybeUnpoisonHeapReference(temp);
5490 __ testl(temp, temp);
5491 __ j(kEqual, slow_path->GetEntryLabel());
5492 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5493 __ j(kNotEqual, slow_path->GetEntryLabel());
5494 break;
5495 }
Calin Juravle98893e12015-10-02 21:05:03 +01005496 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005497 case TypeCheckKind::kInterfaceCheck:
5498 default:
5499 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5500 instruction,
5501 instruction->GetDexPc(),
5502 nullptr);
5503 break;
5504 }
5505 __ Bind(&done);
5506
5507 if (slow_path != nullptr) {
5508 __ Bind(slow_path->GetExitLabel());
5509 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005510}
5511
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005512void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5513 LocationSummary* locations =
5514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5515 InvokeRuntimeCallingConvention calling_convention;
5516 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5517}
5518
5519void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005520 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5521 : QUICK_ENTRY_POINT(pUnlockObject),
5522 instruction,
5523 instruction->GetDexPc(),
5524 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005525}
5526
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005527void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5528void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5529void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5530
5531void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5532 LocationSummary* locations =
5533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5534 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5535 || instruction->GetResultType() == Primitive::kPrimLong);
5536 locations->SetInAt(0, Location::RequiresRegister());
5537 locations->SetInAt(1, Location::Any());
5538 locations->SetOut(Location::SameAsFirstInput());
5539}
5540
5541void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5542 HandleBitwiseOperation(instruction);
5543}
5544
5545void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5546 HandleBitwiseOperation(instruction);
5547}
5548
5549void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5550 HandleBitwiseOperation(instruction);
5551}
5552
5553void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5554 LocationSummary* locations = instruction->GetLocations();
5555 Location first = locations->InAt(0);
5556 Location second = locations->InAt(1);
5557 DCHECK(first.Equals(locations->Out()));
5558
5559 if (instruction->GetResultType() == Primitive::kPrimInt) {
5560 if (second.IsRegister()) {
5561 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005562 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005563 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005564 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005565 } else {
5566 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005567 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005568 }
5569 } else if (second.IsConstant()) {
5570 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005571 __ andl(first.AsRegister<Register>(),
5572 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005573 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005574 __ orl(first.AsRegister<Register>(),
5575 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005576 } else {
5577 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005578 __ xorl(first.AsRegister<Register>(),
5579 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005580 }
5581 } else {
5582 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005583 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005584 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005585 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005586 } else {
5587 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005588 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005589 }
5590 }
5591 } else {
5592 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5593 if (second.IsRegisterPair()) {
5594 if (instruction->IsAnd()) {
5595 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5596 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5597 } else if (instruction->IsOr()) {
5598 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5599 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5600 } else {
5601 DCHECK(instruction->IsXor());
5602 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5603 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5604 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005605 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005606 if (instruction->IsAnd()) {
5607 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5608 __ andl(first.AsRegisterPairHigh<Register>(),
5609 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5610 } else if (instruction->IsOr()) {
5611 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5612 __ orl(first.AsRegisterPairHigh<Register>(),
5613 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5614 } else {
5615 DCHECK(instruction->IsXor());
5616 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5617 __ xorl(first.AsRegisterPairHigh<Register>(),
5618 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5619 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005620 } else {
5621 DCHECK(second.IsConstant()) << second;
5622 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005623 int32_t low_value = Low32Bits(value);
5624 int32_t high_value = High32Bits(value);
5625 Immediate low(low_value);
5626 Immediate high(high_value);
5627 Register first_low = first.AsRegisterPairLow<Register>();
5628 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005629 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005630 if (low_value == 0) {
5631 __ xorl(first_low, first_low);
5632 } else if (low_value != -1) {
5633 __ andl(first_low, low);
5634 }
5635 if (high_value == 0) {
5636 __ xorl(first_high, first_high);
5637 } else if (high_value != -1) {
5638 __ andl(first_high, high);
5639 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005640 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005641 if (low_value != 0) {
5642 __ orl(first_low, low);
5643 }
5644 if (high_value != 0) {
5645 __ orl(first_high, high);
5646 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005647 } else {
5648 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005649 if (low_value != 0) {
5650 __ xorl(first_low, low);
5651 }
5652 if (high_value != 0) {
5653 __ xorl(first_high, high);
5654 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005655 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005656 }
5657 }
5658}
5659
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005660void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005661 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005662 LOG(FATAL) << "Unreachable";
5663}
5664
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005665void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005666 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005667 LOG(FATAL) << "Unreachable";
5668}
5669
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005670void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5671 DCHECK(codegen_->IsBaseline());
5672 LocationSummary* locations =
5673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5674 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5675}
5676
5677void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5678 DCHECK(codegen_->IsBaseline());
5679 // Will be generated at use site.
5680}
5681
Mark Mendellfe57faa2015-09-18 09:26:15 -04005682// Simple implementation of packed switch - generate cascaded compare/jumps.
5683void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5684 LocationSummary* locations =
5685 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5686 locations->SetInAt(0, Location::RequiresRegister());
5687}
5688
5689void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5690 int32_t lower_bound = switch_instr->GetStartValue();
5691 int32_t num_entries = switch_instr->GetNumEntries();
5692 LocationSummary* locations = switch_instr->GetLocations();
5693 Register value_reg = locations->InAt(0).AsRegister<Register>();
5694 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5695
5696 // Create a series of compare/jumps.
5697 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5698 for (int i = 0; i < num_entries; i++) {
5699 int32_t case_value = lower_bound + i;
5700 if (case_value == 0) {
5701 __ testl(value_reg, value_reg);
5702 } else {
5703 __ cmpl(value_reg, Immediate(case_value));
5704 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005705 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005706 }
5707
5708 // And the default for any other value.
5709 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5710 __ jmp(codegen_->GetLabelOf(default_block));
5711 }
5712}
5713
Mark Mendell805b3b52015-09-18 14:10:29 -04005714void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5715 LocationSummary* locations =
5716 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5717 locations->SetInAt(0, Location::RequiresRegister());
5718
5719 // Constant area pointer.
5720 locations->SetInAt(1, Location::RequiresRegister());
5721
5722 // And the temporary we need.
5723 locations->AddTemp(Location::RequiresRegister());
5724}
5725
5726void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5727 int32_t lower_bound = switch_instr->GetStartValue();
5728 int32_t num_entries = switch_instr->GetNumEntries();
5729 LocationSummary* locations = switch_instr->GetLocations();
5730 Register value_reg = locations->InAt(0).AsRegister<Register>();
5731 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5732
5733 // Optimizing has a jump area.
5734 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5735 Register constant_area = locations->InAt(1).AsRegister<Register>();
5736
5737 // Remove the bias, if needed.
5738 if (lower_bound != 0) {
5739 __ leal(temp_reg, Address(value_reg, -lower_bound));
5740 value_reg = temp_reg;
5741 }
5742
5743 // Is the value in range?
5744 DCHECK_GE(num_entries, 1);
5745 __ cmpl(value_reg, Immediate(num_entries - 1));
5746 __ j(kAbove, codegen_->GetLabelOf(default_block));
5747
5748 // We are in the range of the table.
5749 // Load (target-constant_area) from the jump table, indexing by the value.
5750 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
5751
5752 // Compute the actual target address by adding in constant_area.
5753 __ addl(temp_reg, constant_area);
5754
5755 // And jump.
5756 __ jmp(temp_reg);
5757}
5758
Mark Mendell0616ae02015-04-17 12:49:27 -04005759void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5760 HX86ComputeBaseMethodAddress* insn) {
5761 LocationSummary* locations =
5762 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5763 locations->SetOut(Location::RequiresRegister());
5764}
5765
5766void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5767 HX86ComputeBaseMethodAddress* insn) {
5768 LocationSummary* locations = insn->GetLocations();
5769 Register reg = locations->Out().AsRegister<Register>();
5770
5771 // Generate call to next instruction.
5772 Label next_instruction;
5773 __ call(&next_instruction);
5774 __ Bind(&next_instruction);
5775
5776 // Remember this offset for later use with constant area.
5777 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5778
5779 // Grab the return address off the stack.
5780 __ popl(reg);
5781}
5782
5783void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5784 HX86LoadFromConstantTable* insn) {
5785 LocationSummary* locations =
5786 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5787
5788 locations->SetInAt(0, Location::RequiresRegister());
5789 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5790
5791 // If we don't need to be materialized, we only need the inputs to be set.
5792 if (!insn->NeedsMaterialization()) {
5793 return;
5794 }
5795
5796 switch (insn->GetType()) {
5797 case Primitive::kPrimFloat:
5798 case Primitive::kPrimDouble:
5799 locations->SetOut(Location::RequiresFpuRegister());
5800 break;
5801
5802 case Primitive::kPrimInt:
5803 locations->SetOut(Location::RequiresRegister());
5804 break;
5805
5806 default:
5807 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5808 }
5809}
5810
5811void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5812 if (!insn->NeedsMaterialization()) {
5813 return;
5814 }
5815
5816 LocationSummary* locations = insn->GetLocations();
5817 Location out = locations->Out();
5818 Register const_area = locations->InAt(0).AsRegister<Register>();
5819 HConstant *value = insn->GetConstant();
5820
5821 switch (insn->GetType()) {
5822 case Primitive::kPrimFloat:
5823 __ movss(out.AsFpuRegister<XmmRegister>(),
5824 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5825 break;
5826
5827 case Primitive::kPrimDouble:
5828 __ movsd(out.AsFpuRegister<XmmRegister>(),
5829 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5830 break;
5831
5832 case Primitive::kPrimInt:
5833 __ movl(out.AsRegister<Register>(),
5834 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5835 break;
5836
5837 default:
5838 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5839 }
5840}
5841
Mark Mendell0616ae02015-04-17 12:49:27 -04005842/**
5843 * Class to handle late fixup of offsets into constant area.
5844 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005845class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04005846 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04005847 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
5848 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5849
5850 protected:
5851 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5852
5853 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005854
5855 private:
5856 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5857 // Patch the correct offset for the instruction. The place to patch is the
5858 // last 4 bytes of the instruction.
5859 // The value to patch is the distance from the offset in the constant area
5860 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04005861 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
5862 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04005863
5864 // Patch in the right value.
5865 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5866 }
5867
Mark Mendell0616ae02015-04-17 12:49:27 -04005868 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04005869 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005870};
5871
Mark Mendell805b3b52015-09-18 14:10:29 -04005872/**
5873 * Class to handle late fixup of offsets to a jump table that will be created in the
5874 * constant area.
5875 */
5876class JumpTableRIPFixup : public RIPFixup {
5877 public:
5878 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
5879 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
5880
5881 void CreateJumpTable() {
5882 X86Assembler* assembler = codegen_->GetAssembler();
5883
5884 // Ensure that the reference to the jump table has the correct offset.
5885 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
5886 SetOffset(offset_in_constant_table);
5887
5888 // The label values in the jump table are computed relative to the
5889 // instruction addressing the constant area.
5890 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
5891
5892 // Populate the jump table with the correct values for the jump table.
5893 int32_t num_entries = switch_instr_->GetNumEntries();
5894 HBasicBlock* block = switch_instr_->GetBlock();
5895 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
5896 // The value that we want is the target offset - the position of the table.
5897 for (int32_t i = 0; i < num_entries; i++) {
5898 HBasicBlock* b = successors[i];
5899 Label* l = codegen_->GetLabelOf(b);
5900 DCHECK(l->IsBound());
5901 int32_t offset_to_block = l->Position() - relative_offset;
5902 assembler->AppendInt32(offset_to_block);
5903 }
5904 }
5905
5906 private:
5907 const HX86PackedSwitch* switch_instr_;
5908};
5909
5910void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5911 // Generate the constant area if needed.
5912 X86Assembler* assembler = GetAssembler();
5913 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
5914 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5915 // byte values.
5916 assembler->Align(4, 0);
5917 constant_area_start_ = assembler->CodeSize();
5918
5919 // Populate any jump tables.
5920 for (auto jump_table : fixups_to_jump_tables_) {
5921 jump_table->CreateJumpTable();
5922 }
5923
5924 // And now add the constant area to the generated code.
5925 assembler->AddConstantArea();
5926 }
5927
5928 // And finish up.
5929 CodeGenerator::Finalize(allocator);
5930}
5931
Mark Mendell0616ae02015-04-17 12:49:27 -04005932Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5933 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5934 return Address(reg, kDummy32BitOffset, fixup);
5935}
5936
5937Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5938 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5939 return Address(reg, kDummy32BitOffset, fixup);
5940}
5941
5942Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5943 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5944 return Address(reg, kDummy32BitOffset, fixup);
5945}
5946
5947Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5948 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5949 return Address(reg, kDummy32BitOffset, fixup);
5950}
5951
Mark Mendell805b3b52015-09-18 14:10:29 -04005952Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
5953 Register reg,
5954 Register value) {
5955 // Create a fixup to be used to create and address the jump table.
5956 JumpTableRIPFixup* table_fixup =
5957 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
5958
5959 // We have to populate the jump tables.
5960 fixups_to_jump_tables_.push_back(table_fixup);
5961
5962 // We want a scaled address, as we are extracting the correct offset from the table.
5963 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
5964}
5965
Andreas Gampe85b62f22015-09-09 13:15:38 -07005966// TODO: target as memory.
5967void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
5968 if (!target.IsValid()) {
5969 DCHECK(type == Primitive::kPrimVoid);
5970 return;
5971 }
5972
5973 DCHECK_NE(type, Primitive::kPrimVoid);
5974
5975 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
5976 if (target.Equals(return_loc)) {
5977 return;
5978 }
5979
5980 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5981 // with the else branch.
5982 if (type == Primitive::kPrimLong) {
5983 HParallelMove parallel_move(GetGraph()->GetArena());
5984 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
5985 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
5986 GetMoveResolver()->EmitNativeCode(&parallel_move);
5987 } else {
5988 // Let the parallel move resolver take care of all of this.
5989 HParallelMove parallel_move(GetGraph()->GetArena());
5990 parallel_move.AddMove(return_loc, target, type, nullptr);
5991 GetMoveResolver()->EmitNativeCode(&parallel_move);
5992 }
5993}
5994
Roland Levillain4d027112015-07-01 15:41:14 +01005995#undef __
5996
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005997} // namespace x86
5998} // namespace art